Browse Source

fix(nc-gui): import to existing table issue

pull/4135/head
Wing-Kam Wong 2 years ago
parent
commit
c513997014
  1. 5
      packages/nc-gui/components/dlg/QuickImport.vue
  2. 80
      packages/nc-gui/utils/parsers/CSVTemplateAdapter.ts

5
packages/nc-gui/components/dlg/QuickImport.vue

@ -169,9 +169,7 @@ async function handleImport() {
message.error(t('msg.error.templateGeneratorNotFound'))
return
}
importLoading.value = true
importData.value = templateGenerator.getData()
await templateEditorRef.value.importTemplate()
} catch (e: any) {
return message.error(await extractSdkResponseErrorMsg(e))
@ -200,7 +198,8 @@ async function parseAndExtractStreamData(val: UploadFile[]) {
templateData.value = templateGenerator!.getTemplate()
// TODO(import): remove
// templateData.value.tables[0].table_name = populateUniqueTableName()
// if (importOnly) importColumns.value = templateGenerator.getColumns()
if (importOnly) importColumns.value = templateGenerator!.getColumns()
importData.value = templateGenerator!.getData()
templateEditorModal.value = true
isParsingData.value = false
preImportLoading.value = false

80
packages/nc-gui/utils/parsers/CSVTemplateAdapter.ts

@ -204,6 +204,9 @@ export default class CSVTemplateAdapter {
for (const [tableIdx, file] of this.files.entries()) {
let steppers = 0
const tn = file.name.replace(/[` ~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '_').trim()
this.data[tn] = []
// parse column meta
parse(file.originFileObj as File, {
worker: true,
skipEmptyLines: 'greedy',
@ -238,53 +241,54 @@ export default class CSVTemplateAdapter {
console.log('complete')
console.log(`steppers: ${steppers}`)
that.updateTemplate(tableIdx)
callback()
// parse table data
if (that.config.importData) {
steppers = 0
parse(file.originFileObj as File, {
worker: true,
skipEmptyLines: 'greedy',
step(row) {
steppers += 1
if (row && steppers >= +that.config.firstRowAsHeaders + 1) {
const rowData: Record<string, any> = {}
for (let columnIdx = 0; columnIdx < that.headers[tableIdx].length; columnIdx++) {
const column = that.project.tables[tableIdx].columns[columnIdx]
const data = (row.data as [])[columnIdx] === '' ? null : (row.data as [])[columnIdx]
if (column.uidt === UITypes.Checkbox) {
rowData[column.column_name] = getCheckboxValue(data)
rowData[column.column_name] = data
} else if (column.uidt === UITypes.SingleSelect || column.uidt === UITypes.MultiSelect) {
rowData[column.column_name] = (data || '').toString().trim() || null
} else {
// TODO(import): do parsing if necessary based on type
rowData[column.column_name] = data
}
}
that.data[tn].push(rowData)
}
},
complete() {
console.log('getData(): complete')
console.log(`getData(): steppers: ${steppers}`)
callback()
},
// TODO(import): add error
})
} else {
callback()
}
},
})
}
}
getColumns() {
// return this.columns
console.log(this.project.tables.map((t: Record<string, any>) => t.columns))
return this.project.tables.map((t: Record<string, any>) => t.columns)
}
getData() {
const that = this
for (const [tableIdx, file] of this.files.entries()) {
let steppers = 0
const tn = file.name.replace(/[` ~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '_').trim()
this.data[tn] = []
if (this.config.importData) {
parse(file.originFileObj as File, {
worker: true,
skipEmptyLines: 'greedy',
step(row) {
steppers += 1
if (row && steppers >= +that.config.firstRowAsHeaders + 1) {
const rowData: Record<string, any> = {}
for (let columnIdx = 0; columnIdx < that.headers[tableIdx].length; columnIdx++) {
const column = that.project.tables[tableIdx].columns[columnIdx]
const data = (row.data as [])[columnIdx] === '' ? null : (row.data as [])[columnIdx]
if (column.uidt === UITypes.Checkbox) {
rowData[column.column_name] = getCheckboxValue(data)
rowData[column.column_name] = data
} else if (column.uidt === UITypes.SingleSelect || column.uidt === UITypes.MultiSelect) {
rowData[column.column_name] = (data || '').toString().trim() || null
} else {
// TODO(import): do parsing if necessary based on type
rowData[column.column_name] = data
}
}
that.data[tn].push(rowData)
}
},
complete() {
console.log('getData(): complete')
console.log(`getData(): steppers: ${steppers}`)
},
})
}
}
return this.data
}

Loading…
Cancel
Save