(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
This commit is contained in:
George Gevoian 2022-02-11 15:30:05 -08:00
parent f877f3859d
commit 6abe7d5827
2 changed files with 15 additions and 4 deletions

View File

@ -13,7 +13,7 @@ import {BulkColValues, CellValue, fromTableDataAction, TableRecordValue, UserAct
import * as gutil from 'app/common/gutil'; import * as gutil from 'app/common/gutil';
import {DocStateComparison} from 'app/common/UserAPI'; import {DocStateComparison} from 'app/common/UserAPI';
import {ParseFileResult, ParseOptions} from 'app/plugin/FileParserAPI'; 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 {ActiveDoc} from 'app/server/lib/ActiveDoc';
import {DocSession, OptDocSession} from 'app/server/lib/DocSession'; import {DocSession, OptDocSession} from 'app/server/lib/DocSession';
import * as log from 'app/server/lib/log'; import * as log from 'app/server/lib/log';
@ -294,8 +294,9 @@ export class ActiveDocImport {
const origTableName = table.table_name ? table.table_name : ''; const origTableName = table.table_name ? table.table_name : '';
const transformRule = transformRuleMap && transformRuleMap.hasOwnProperty(origTableName) ? const transformRule = transformRuleMap && transformRuleMap.hasOwnProperty(origTableName) ?
transformRuleMap[origTableName] : null; transformRuleMap[origTableName] : null;
const columnMetadata = addLabelsIfPossible(table.column_metadata);
const result: ApplyUAResult = await this._activeDoc.applyUserActions(docSession, const result: ApplyUAResult = await this._activeDoc.applyUserActions(docSession,
[["AddTable", hiddenTableName, table.column_metadata]]); [["AddTable", hiddenTableName, columnMetadata]]);
const retValue: AddTableRetValue = result.retValues[0]; const retValue: AddTableRetValue = result.retValues[0];
const hiddenTableId = retValue.table_id; // The sanitized version of the table name. const hiddenTableId = retValue.table_id; // The sanitized version of the table name.
const hiddenTableColIds = retValue.columns; // The sanitized names of the columns. 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));
}

View File

@ -156,8 +156,9 @@ class ImportActions(object):
isCopyFormula = (formula.startswith("$") and formula[1:] in src_cols) isCopyFormula = (formula.startswith("$") and formula[1:] in src_cols)
if gen_all or not isCopyFormula: if gen_all or not isCopyFormula:
#if colId specified, use that. Else label is fine # If colId specified, use that. Otherwise, use the (sanitized) label.
new_col_id = _import_transform_col_prefix + (c.colId or c.label) col_id = c.colId or identifiers.pick_col_ident(c.label)
new_col_id = _import_transform_col_prefix + col_id
new_col_spec = { new_col_spec = {
"label": c.label, "label": c.label,
"type": c.type, "type": c.type,