diff --git a/app/server/lib/ActiveDocImport.ts b/app/server/lib/ActiveDocImport.ts index 5a2efc43..7ad5ea8e 100644 --- a/app/server/lib/ActiveDocImport.ts +++ b/app/server/lib/ActiveDocImport.ts @@ -13,7 +13,7 @@ import {BulkColValues, CellValue, fromTableDataAction, TableRecordValue, UserAct import * as gutil from 'app/common/gutil'; import {DocStateComparison} from 'app/common/UserAPI'; import {ParseFileResult, ParseOptions} from 'app/plugin/FileParserAPI'; -import {GristTable} from 'app/plugin/GristTable'; +import {GristColumn, GristTable} from 'app/plugin/GristTable'; import {ActiveDoc} from 'app/server/lib/ActiveDoc'; import {DocSession, OptDocSession} from 'app/server/lib/DocSession'; import * as log from 'app/server/lib/log'; @@ -294,8 +294,9 @@ export class ActiveDocImport { const origTableName = table.table_name ? table.table_name : ''; const transformRule = transformRuleMap && transformRuleMap.hasOwnProperty(origTableName) ? transformRuleMap[origTableName] : null; + const columnMetadata = addLabelsIfPossible(table.column_metadata); const result: ApplyUAResult = await this._activeDoc.applyUserActions(docSession, - [["AddTable", hiddenTableName, table.column_metadata]]); + [["AddTable", hiddenTableName, columnMetadata]]); const retValue: AddTableRetValue = result.retValues[0]; const hiddenTableId = retValue.table_id; // The sanitized version of the table name. const hiddenTableColIds = retValue.columns; // The sanitized names of the columns. @@ -727,3 +728,12 @@ function getMergeFunction({type}: MergeStrategy): MergeFunction { } } } + +/** + * If `columns` is populated with non-blank column ids, adds labels to all + * columns using the values set for the column ids. Otherwise, returns + * a copy of columns with no modifications made. + */ +function addLabelsIfPossible(columns: GristColumn[]) { + return columns.map(c => (c.id ? {...c, label: c.id} : c)); +} diff --git a/sandbox/grist/import_actions.py b/sandbox/grist/import_actions.py index 4f14e8ac..b65a1d37 100644 --- a/sandbox/grist/import_actions.py +++ b/sandbox/grist/import_actions.py @@ -156,8 +156,9 @@ class ImportActions(object): isCopyFormula = (formula.startswith("$") and formula[1:] in src_cols) if gen_all or not isCopyFormula: - #if colId specified, use that. Else label is fine - new_col_id = _import_transform_col_prefix + (c.colId or c.label) + # If colId specified, use that. Otherwise, use the (sanitized) label. + col_id = c.colId or identifiers.pick_col_ident(c.label) + new_col_id = _import_transform_col_prefix + col_id new_col_spec = { "label": c.label, "type": c.type,