2
2
3
3
import openpyxl
4
4
5
+ from .util import json_append_deep_value , json_get_deep_value , json_set_deep_value
6
+
5
7
6
8
def _is_content_a_guide_field (content ):
7
9
return content and content .startswith ("SPREADSHEETFORM:" )
@@ -73,31 +75,29 @@ def get_data_from_form(guide_filename, in_filename):
73
75
74
76
# Step 2: Process single configs (easy ones)
75
77
for single_config in single_configs .values ():
76
- # TODO this only does top level paths - we should add so it can do paths with several levels
77
- data [single_config ["path" ]] = in_workbook [worksheet .title ][
78
- single_config ["coordinate" ]
79
- ].value
78
+ json_set_deep_value (
79
+ data ,
80
+ single_config ["path" ],
81
+ in_workbook [worksheet .title ][single_config ["coordinate" ]].value ,
82
+ )
80
83
81
84
# Step 3: Process Down Configs
82
85
for down_config in down_configs .values ():
83
86
start_row = down_config [0 ]["row" ]
84
87
max_row = in_workbook [worksheet .title ].max_row + 1
85
- # TODO this only does top level paths - we should add so it can do paths with several levels
86
- data [down_config [0 ]["list_path" ]] = []
88
+ json_set_deep_value (data , down_config [0 ]["list_path" ], [])
87
89
for row in range (start_row , max_row + 1 ):
88
90
item = {}
89
91
found_anything = False
90
92
for this_down_config in down_config :
91
- # TODO this only does top level paths - we should add so it can do paths with several levels
92
93
cell = in_workbook [worksheet .title ][
93
94
this_down_config ["column_letter" ] + str (row )
94
95
]
95
- item [ this_down_config ["item_path" ]] = cell .value
96
- if item [ this_down_config ["item_path" ]] :
96
+ json_set_deep_value ( item , this_down_config ["item_path" ], cell .value )
97
+ if json_get_deep_value ( item , this_down_config ["item_path" ]) :
97
98
found_anything = True
98
99
if found_anything :
99
- # TODO this only does top level paths - we should add so it can do paths with several levels
100
- data [down_config [0 ]["list_path" ]].append (item )
100
+ json_append_deep_value (data , down_config [0 ]["list_path" ], item )
101
101
102
102
return data
103
103
@@ -113,27 +113,28 @@ def put_data_in_form(guide_filename, data, out_filename):
113
113
114
114
# Step 2: Process single configs (easy ones)
115
115
for single_config in single_configs .values ():
116
- # TODO this only does top level paths - we should add so it can do paths with several levels
117
- worksheet [single_config ["coordinate" ]] = data .get (single_config ["path" ])
116
+ worksheet [single_config ["coordinate" ]] = json_get_deep_value (
117
+ data , single_config ["path" ]
118
+ )
118
119
119
120
# Step 3: Process Down Configs
120
121
for down_config in down_configs .values ():
121
- datas_to_insert = data . get ( down_config [0 ]["list_path" ], [ ])
122
+ datas_to_insert = json_get_deep_value ( data , down_config [0 ]["list_path" ])
122
123
if isinstance (datas_to_insert , list ):
123
124
if len (datas_to_insert ) == 0 :
124
125
# we still want to remove the special values from the output spreadsheet
125
126
for this_down_config in down_config :
126
- # TODO this only does top level paths - we should add so it can do paths with several levels
127
127
worksheet [this_down_config ["coordinate" ]] = ""
128
128
else :
129
129
extra_row = 0
130
130
for data_to_insert in datas_to_insert :
131
131
for this_down_config in down_config :
132
- # TODO this only does top level paths - we should add so it can do paths with several levels
133
132
worksheet [
134
133
this_down_config ["column_letter" ]
135
134
+ str (this_down_config ["row" ] + extra_row )
136
- ] = data_to_insert .get (this_down_config ["item_path" ])
135
+ ] = json_get_deep_value (
136
+ data_to_insert , this_down_config ["item_path" ]
137
+ )
137
138
extra_row += 1
138
139
139
140
workbook .save (out_filename )
0 commit comments