Browse Source

fix: handle empty column names/special char in excel import

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/914/head
Pranav C 3 years ago
parent
commit
0759d3614d
  1. 17
      packages/nc-gui/components/import/templateParsers/ExcelTemplateAdapter.js

17
packages/nc-gui/components/import/templateParsers/ExcelTemplateAdapter.js

@ -33,7 +33,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
parse() { parse() {
const tableNamePrefixRef = {} const tableNamePrefixRef = {}
for (let i = 0; i < this.wb.SheetNames.length; i++) { for (let i = 0; i < this.wb.SheetNames.length; i++) {
const columnNamePrefixRef = {} const columnNamePrefixRef = { id: 0 }
const sheet = this.wb.SheetNames[i] const sheet = this.wb.SheetNames[i]
let tn = sheet let tn = sheet
if (tn in tableNamePrefixRef) { if (tn in tableNamePrefixRef) {
@ -51,18 +51,18 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
// const colLen = Math.max() // const colLen = Math.max()
for (let col = 0; col < rows[0].length; col++) { for (let col = 0; col < rows[0].length; col++) {
let cn = (rows[0][col] || let cn = ((rows[0] && rows[0][col] && rows[0][col].toString().trim()) ||
`field${col + 1}`).replace(/\./, '_').trim() `field${col + 1}`).replace(/\W/g, '_').trim()
if (cn in columnNamePrefixRef) { if (cn in columnNamePrefixRef) {
cn = `${cn}${++columnNamePrefixRef[cn]}` cn = `${cn}${++columnNamePrefixRef[cn]}`
} else { } else {
columnNamePrefixRef[cn] = 0 columnNamePrefixRef[cn] = 0
} }
const column = { const column = {
cn cn
} }
// 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,
@ -86,22 +86,22 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
if (checkboxType.length === 1) { if (checkboxType.length === 1) {
column.uidt = UITypes.Checkbox column.uidt = UITypes.Checkbox
} else { } else {
vals = vals.filter(v => v !== null && v !== undefined) vals = vals.filter(v => v !== null && v !== undefined).map(v => v.toString().trim())
// check column is multi or single select by comparing unique values // check column is multi or single select by comparing unique values
// todo: // todo:
if (vals.some(v => v && v.toString().includes(','))) { if (vals.some(v => v && v.toString().includes(','))) {
const flattenedVals = vals.flatMap(v => v ? v.toString().split(',') : []) const flattenedVals = vals.flatMap(v => v ? v.toString().trim().split(/\s*,\s*/) : [])
const uniqueVals = new Set(flattenedVals) const uniqueVals = new Set(flattenedVals)
if (flattenedVals.length > uniqueVals.size && uniqueVals.size <= Math.ceil(flattenedVals.length / 2)) { if (flattenedVals.length > uniqueVals.size && uniqueVals.size <= Math.ceil(flattenedVals.length / 2)) {
column.uidt = UITypes.MultiSelect column.uidt = UITypes.MultiSelect
column.dtxp = [...uniqueVals].join(',') column.dtxp = `'${[...uniqueVals].join("','")}'`
} }
} else { } else {
const uniqueVals = new Set(vals) const uniqueVals = new Set(vals)
if (vals.length > uniqueVals.size && uniqueVals.size <= Math.ceil(vals.length / 2)) { if (vals.length > uniqueVals.size && uniqueVals.size <= Math.ceil(vals.length / 2)) {
column.uidt = UITypes.SingleSelect column.uidt = UITypes.SingleSelect
column.dtxp = [...uniqueVals].join(',') column.dtxp = `'${[...uniqueVals].join("','")}'`
} }
} }
} }
@ -154,7 +154,6 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
} }
this.data[sheet].push(rowData) this.data[sheet].push(rowData)
} }
this.project.tables.push(table) this.project.tables.push(table)
} }
} }

Loading…
Cancel
Save