优化风格
This commit is contained in:
15
src/utils.ts
15
src/utils.ts
@@ -22,6 +22,11 @@ const VARIABLE_MAPPING_KEY = 'label_var_column_mapping_v2';
|
||||
/** 变量映射:使用模板中配置的默认值(非数据列) */
|
||||
export const VARIABLE_MAPPING_USE_DEFAULT = '__use_default__';
|
||||
|
||||
/** 变量映射:使用当前数据行序号(1-based,与 {#INDEX} 一致) */
|
||||
export const VARIABLE_MAPPING_USE_ROW_INDEX = '__use_row_index__';
|
||||
|
||||
export const VARIABLE_MAPPING_ROW_INDEX_LABEL = '数据序号';
|
||||
|
||||
export function toDataSourceMeta(data: ImportData): DataSourceMeta {
|
||||
return {
|
||||
fileName: data.fileName,
|
||||
@@ -111,19 +116,25 @@ export function isVariableMappingConfigured(
|
||||
const mapped = mapping[varName];
|
||||
if (!mapped) return false;
|
||||
if (mapped === VARIABLE_MAPPING_USE_DEFAULT) return true;
|
||||
if (mapped === VARIABLE_MAPPING_USE_ROW_INDEX) return true;
|
||||
return columns.includes(mapped);
|
||||
}
|
||||
|
||||
/** 按映射将数据行中的列值填入模板变量键;「使用默认值」时写入 variableDefaults */
|
||||
/** 按映射将数据行中的列值填入模板变量键;「使用默认值」/「数据序号」时写入对应值 */
|
||||
export function applyRowVariableMapping(
|
||||
row: RecordRow,
|
||||
mapping: Record<string, string>,
|
||||
variables: string[],
|
||||
variableDefaults?: Record<string, string> | null
|
||||
variableDefaults?: Record<string, string> | null,
|
||||
rowIndex: number = 0
|
||||
): RecordRow {
|
||||
const result = { ...row };
|
||||
for (const varName of variables) {
|
||||
const mapped = mapping[varName];
|
||||
if (mapped === VARIABLE_MAPPING_USE_ROW_INDEX) {
|
||||
result[varName] = String(rowIndex + 1);
|
||||
continue;
|
||||
}
|
||||
if (mapped === VARIABLE_MAPPING_USE_DEFAULT) {
|
||||
const def = variableDefaults?.[varName];
|
||||
if (def !== undefined && String(def).trim() !== '') {
|
||||
|
||||
Reference in New Issue
Block a user