From 6abe7d5827f41dfa8eb3679ac77904791c53d7f1 Mon Sep 17 00:00:00 2001 From: George Gevoian Date: Fri, 11 Feb 2022 15:30:05 -0800 Subject: [PATCH] (core) Use original column headers during imports Summary: When possible, the original column headers from imported files will now be used as the labels for Grist columns. This includes values that were previously invalid Grist column identifiers, such as those containing Unicode. Test Plan: Updated server and browser tests. Reviewers: jarek Reviewed By: jarek Differential Revision: https://phab.getgrist.com/D3261 --- app/server/lib/ActiveDocImport.ts | 14 ++++++++++++-- sandbox/grist/import_actions.py | 5 +++-- 2 files changed, 15 insertions(+), 4 deletions(-) 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,