Browse Source

feat(nc-gui): optionally import data

pull/4135/head
Wing-Kam Wong 2 years ago
parent
commit
4ad73666f8
  1. 8
      packages/nc-gui/components/dlg/QuickImport.vue
  2. 9
      packages/nc-gui/utils/parsers/CSVTemplateAdapter.ts
  3. 19
      packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts
  4. 5
      packages/nc-gui/utils/parsers/JSONTemplateAdapter.ts

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

@ -467,16 +467,16 @@ const beforeUpload = (file: UploadFile) => {
</a-form-item>
<!-- Flatten nested -->
<div v-if="isImportTypeJson" class="mt-3">
<a-form-item v-if="isImportTypeJson" class="!my-2">
<a-checkbox v-model:checked="importState.parserConfig.normalizeNested">
<span class="caption">{{ $t('labels.flattenNested') }}</span>
</a-checkbox>
</div>
</a-form-item>
<!-- Import Data -->
<div v-if="isImportTypeJson" class="mt-4">
<a-form-item class="!my-2">
<a-checkbox v-model:checked="importState.parserConfig.importData">{{ $t('labels.importData') }}</a-checkbox>
</div>
</a-form-item>
</div>
</div>
</div>

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

@ -27,12 +27,7 @@ export default class CSVTemplateAdapter {
columnValues: Record<number, []>
constructor(files: UploadFile[], parserConfig = {}) {
this.config = {
maxRowsToParse: 500,
autoSelectFieldTypes: true,
firstRowAsHeaders: true,
...parserConfig,
}
this.config = parserConfig
this.files = files
this.project = {
tables: [],
@ -258,6 +253,7 @@ export default class CSVTemplateAdapter {
let steppers = 0
const tn = file.name.replace(/[` ~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '_').trim()
this.data[tn] = []
if (this.config.importData) {
parse(file.originFileObj as File, {
worker: true,
step(row) {
@ -291,6 +287,7 @@ export default class CSVTemplateAdapter {
},
})
}
}
return this.data
}

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

@ -18,9 +18,7 @@ const excelTypeToUidt: Record<string, UITypes> = {
}
export default class ExcelTemplateAdapter extends TemplateGenerator {
config: {
maxRowsToParse: number
} & Record<string, any>
config: Record<string, any>
excelData: any
@ -36,17 +34,11 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
constructor(data = {}, parserConfig = {}) {
super()
this.config = {
maxRowsToParse: 500,
...parserConfig,
}
this.config = parserConfig
this.excelData = data
this.project = {
tables: [],
}
this.xlsx = {} as any
}
@ -79,7 +71,6 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
tableNamePrefixRef[tn] = 0
const table = { table_name: tn, ref_table_name: tn, columns: [] as any[] }
this.data[tn] = []
const ws: any = this.wb.Sheets[sheet]
const range = this.xlsx.utils.decode_range(ws['!ref'])
let rows: any = this.xlsx.utils.sheet_to_json(ws, { header: 1, blankrows: false, defval: null })
@ -207,7 +198,10 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
}
table.columns.push(column)
}
this.project.tables.push(table)
this.data[tn] = []
if (this.config.importData) {
let rowIndex = 0
for (const row of rows.slice(1)) {
const rowData: Record<string, any> = {}
@ -239,7 +233,8 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
this.data[tn].push(rowData)
rowIndex++
}
this.project.tables.push(table)
}
resolve(true)
}),
)

5
packages/nc-gui/utils/parsers/JSONTemplateAdapter.ts

@ -24,10 +24,7 @@ export default class JSONTemplateAdapter extends TemplateGenerator {
columns: object
constructor(data: object, parserConfig = {}) {
super()
this.config = {
maxRowsToParse: 500,
...parserConfig,
}
this.config = parserConfig
this._jsonData = data
this.project = {
tables: [],

Loading…
Cancel
Save