11
22const sheets_lib = require ( "./dudk/sheets.js" ) ;
33const session_lib = require ( "./session.js" ) ;
4+ const backend_lib = require ( "./dudk/backend.js" ) ;
45
56const IMPORTER_SESSION_KEY = "importer.session" ;
67const IMPORTER_ERROR_KEY = "importer.error" ;
@@ -48,10 +49,39 @@ const importerErrorMappingData = (error, key) => {
4849 return ""
4950 }
5051
52+ // ABS FIXME: Do we need to remove a field- or mapping- prefix from k before parseInt?
5153 const m = new Map ( Object . entries ( error . data ) . map ( ( [ k , v ] ) => [ parseInt ( k ) , v ] ) )
5254 return m . get ( key ) || ""
5355}
5456
57+ //--------------------------------------------------------------------
58+ // As long as column->field mappings have been set up, this function will return
59+ // a list of possible formats for a specified column (by index). The return
60+ // value will be false if the column is mapped to a field whose type does not
61+ // require formats, otherwise it will be a list of objects with 'name' (internal
62+ // code), 'displayName' (human-facing name) and 'description' (longer description) fields.
63+ // --------------------------------------------------------------------
64+ const importerPossibleColumnFormats = ( data , index ) => {
65+ const session_data = data [ IMPORTER_SESSION_KEY ] ;
66+ const session = new session_lib . Session ( session_data ) ;
67+ const supportedTypes = backend_lib . SessionGetSupportedTypes ( session . backendSid ) ;
68+ const mappings = backend_lib . SessionGetMappingRules ( session . backendSid ) ;
69+ const columnField = mappings [ index ] ;
70+ const columnDef = session . fields . find ( ( field ) => field . name == columnField ) ;
71+ const columnTypeName = columnDef . type ;
72+ const columnType = supportedTypes . get ( columnTypeName ) ;
73+ if ( columnType . formats ) {
74+ const options = Array . from ( columnType . formats . entries ( ) ) . map ( ( fmtEntry ) => ( {
75+ name : fmtEntry [ 0 ] ,
76+ displayName : fmtEntry [ 1 ] . displayName ,
77+ description : fmtEntry [ 1 ] . description
78+ } ) ) ;
79+ return options ;
80+ } else {
81+ return false ;
82+ }
83+ }
84+
5585//--------------------------------------------------------------------
5686// Allows a template to obtain `count` rows from the start of the data
5787// range.
@@ -156,7 +186,7 @@ const importerMappedData = (data) => {
156186 const session_data = data [ IMPORTER_SESSION_KEY ] ;
157187 const session = new session_lib . Session ( session_data )
158188
159- const mapResults = sheets_lib . MapData ( session . backendSid , session . sheet , session . mapping , session . fields ) ;
189+ const mapResults = sheets_lib . MapData ( session . backendSid , session . sheet , session . mapping , session . formats , session . fields ) ;
160190 const headers = session . fields ;
161191
162192 return {
@@ -186,7 +216,7 @@ const data_sum = (data, column) => {
186216 const session_data = data [ IMPORTER_SESSION_KEY ] ;
187217 const session = new session_lib . Session ( session_data )
188218
189- const mapResults = sheets_lib . MapData ( session . backendSid , session . sheet , session . mapping , session . fields ) ;
219+ const mapResults = sheets_lib . MapData ( session . backendSid , session . sheet , session . mapping , session . formats , session . fields ) ;
190220 const headers = session . fields ;
191221
192222 const idx = headers . findIndex ( ( x ) => x . name == column )
@@ -206,7 +236,7 @@ const data_avg = (data, column) => {
206236 const session_data = data [ IMPORTER_SESSION_KEY ] ;
207237 const session = new session_lib . Session ( session_data )
208238
209- const mapResults = sheets_lib . MapData ( session . backendSid , session . sheet , session . mapping , session . fields ) ;
239+ const mapResults = sheets_lib . MapData ( session . backendSid , session . sheet , session . mapping , session . formats , session . fields ) ;
210240 const headers = session . fields ;
211241
212242 const idx = headers . findIndex ( ( x ) => x . name == column )
@@ -247,6 +277,7 @@ const parseNumberFromString = (s) => {
247277module . exports = {
248278 importerError,
249279 importerErrorMappingData,
280+ importerPossibleColumnFormats,
250281 importSheetPreview,
251282 importerGetRows,
252283 importerGetHeaders,
@@ -257,3 +288,9 @@ module.exports = {
257288 data_sum,
258289 data_avg
259290}
291+
292+ /*
293+ Local Variables:
294+ js-indent-level: 4
295+ End:
296+ */
0 commit comments