Browse Source

feat(nc-gui): enrich .dt & save dtxp for singleSelect / multiSelect

pull/3811/head
Wing-Kam Wong 2 years ago
parent
commit
02fc7da224
  1. 22
      packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts

22
packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts

@ -103,13 +103,11 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
} }
columnNamePrefixRef[cn] = 0 columnNamePrefixRef[cn] = 0
const column: Record<string, any> = { let column: Record<string, any> = {
column_name: cn, column_name: cn,
ref_column_name: cn, ref_column_name: cn,
} }
table.columns.push(column)
// const cellId = `${col.toString(26).split('').map(s => (parseInt(s, 26) + 10).toString(36).toUpperCase())}2`; // const cellId = `${col.toString(26).split('').map(s => (parseInt(s, 26) + 10).toString(36).toUpperCase())}2`;
const cellId = XLSX.utils.encode_cell({ const cellId = XLSX.utils.encode_cell({
c: range.s.c + col, c: range.s.c + col,
@ -118,6 +116,11 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
const cellProps = ws[cellId] || {} const cellProps = ws[cellId] || {}
column.uidt = excelTypeToUidt[cellProps.t] || UITypes.SingleLineText column.uidt = excelTypeToUidt[cellProps.t] || UITypes.SingleLineText
const { sqlUi } = useProject()
// enrich data type for Template Editor to process
column = { ...column, ...sqlUi?.value?.getDataTypeForUiType({ uidt: column.uidt }) }
// todo: optimize // todo: optimize
if (column.uidt === UITypes.SingleLineText) { if (column.uidt === UITypes.SingleLineText) {
// check for long text // check for long text
@ -148,18 +151,26 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
const uniqueVals = (flattenedVals = flattenedVals.filter( const uniqueVals = (flattenedVals = flattenedVals.filter(
(v: any, i: any, arr: any) => i === arr.findIndex((v1: any) => v.toLowerCase() === v1.toLowerCase()), (v: any, i: any, arr: any) => i === arr.findIndex((v1: any) => v.toLowerCase() === v1.toLowerCase()),
)) ))
// assume the column type is multiple select if there are repeated values
if (flattenedVals.length > uniqueVals.length && uniqueVals.length <= Math.ceil(flattenedVals.length / 2)) { if (flattenedVals.length > uniqueVals.length && uniqueVals.length <= Math.ceil(flattenedVals.length / 2)) {
column.uidt = UITypes.MultiSelect column.uidt = UITypes.MultiSelect
column.dtxp = `'${uniqueVals.join("','")}'`
} }
// set dtxp here so that users can have the options even they switch the type from other types to MultiSelect
// once it's set, dtxp needs to be reset if the final column type is not MultiSelect
column.dtxp = `'${uniqueVals.join("','")}'`
} else { } else {
// assume the column type is single select if there are repeated values
// once it's set, dtxp needs to be reset if the final column type is not Single Select
const uniqueVals = vals const uniqueVals = vals
.map((v: any) => v.toString().trim()) .map((v: any) => v.toString().trim())
.filter((v: any, i: any, arr: any) => i === arr.findIndex((v1: any) => v.toLowerCase() === v1.toLowerCase())) .filter((v: any, i: any, arr: any) => i === arr.findIndex((v1: any) => v.toLowerCase() === v1.toLowerCase()))
if (vals.length > uniqueVals.length && uniqueVals.length <= Math.ceil(vals.length / 2)) { if (vals.length > uniqueVals.length && uniqueVals.length <= Math.ceil(vals.length / 2)) {
column.uidt = UITypes.SingleSelect column.uidt = UITypes.SingleSelect
column.dtxp = `'${uniqueVals.join("','")}'`
} }
// set dtxp here so that users can have the options even they switch the type from other types to SingleSelect
// once it's set, dtxp needs to be reset if the final column type is not MultiSelect
column.dtxp = `'${uniqueVals.join("','")}'`
} }
} }
} }
@ -200,6 +211,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
column.uidt = UITypes.Date column.uidt = UITypes.Date
} }
} }
table.columns.push(column)
} }
let rowIndex = 0 let rowIndex = 0

Loading…
Cancel
Save