Browse Source

refactor(nc-gui): remove file name to adapter

pull/4135/head
Wing-Kam Wong 2 years ago
parent
commit
1b2a508fbb
  1. 17
      packages/nc-gui/utils/parsers/CSVTemplateAdapter.ts
  2. 86
      packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts
  3. 7
      packages/nc-gui/utils/parsers/JSONTemplateAdapter.ts

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

@ -1,5 +1,5 @@
import { parse } from 'papaparse' import { parse } from 'papaparse'
import { UploadFile } from 'ant-design-vue' import type { UploadFile } from 'ant-design-vue'
import { UITypes } from 'nocodb-sdk' import { UITypes } from 'nocodb-sdk'
import { import {
extractMultiOrSingleSelectProps, extractMultiOrSingleSelectProps,
@ -12,26 +12,23 @@ import {
export default class CSVTemplateAdapter { export default class CSVTemplateAdapter {
config: Record<string, any> config: Record<string, any>
fileName: string
files: UploadFile[] files: UploadFile[]
detectedColumnTypes: Record<number, Record<string, number>> detectedColumnTypes: Record<number, Record<string, number>>
distinctValues: Record<number, Set<string>> distinctValues: Record<number, Set<string>>
headers: Record<number, string[]> headers: Record<number, string[]>
project: { project: {
title: string
tables: Record<string, any>[] tables: Record<string, any>[]
} }
columnValues: Record<number, []> columnValues: Record<number, []>
constructor(fileName: string, files: UploadFile[], parserConfig = {}) { constructor(files: UploadFile[], parserConfig = {}) {
this.config = { this.config = {
maxRowsToParse: 500, maxRowsToParse: 500,
...parserConfig, ...parserConfig,
} }
this.fileName = fileName // TODO: check usage
this.files = files this.files = files
this.project = { this.project = {
title: this.fileName,
tables: [], tables: [],
} }
this.detectedColumnTypes = {} this.detectedColumnTypes = {}
@ -167,10 +164,10 @@ export default class CSVTemplateAdapter {
let steppers = 0 let steppers = 0
parse(file.originFileObj as File, { parse(file.originFileObj as File, {
worker: true, worker: true,
step: function (row) { step(row) {
steppers += 1 steppers += 1
if (row) { if (row) {
if (steppers == 1) { if (steppers === 1) {
that.initTemplate(tableIdx, file.name!, row.data as []) that.initTemplate(tableIdx, file.name!, row.data as [])
} else { } else {
that.detectColumnType(row.data as []) that.detectColumnType(row.data as [])
@ -181,9 +178,9 @@ export default class CSVTemplateAdapter {
} }
} }
}, },
complete: function () { complete() {
console.log('complete') console.log('complete')
console.log('steppers: ' + steppers) console.log(`steppers: ${steppers}`)
that.updateTemplate(tableIdx) that.updateTemplate(tableIdx)
console.log(that.project.tables) console.log(that.project.tables)
// TODO: enable import button // TODO: enable import button

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

@ -22,13 +22,10 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
maxRowsToParse: number maxRowsToParse: number
} & Record<string, any> } & Record<string, any>
name: string
excelData: any excelData: any
project: { project: {
title: string tables: Record<string, any>[]
tables: any[]
} }
data: Record<string, any> = {} data: Record<string, any> = {}
@ -37,19 +34,16 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
xlsx: typeof import('xlsx') xlsx: typeof import('xlsx')
constructor(name = '', data = {}, parserConfig = {}) { constructor(data = {}, parserConfig = {}) {
super() super()
this.config = { this.config = {
maxRowsToParse: 500, maxRowsToParse: 500,
...parserConfig, ...parserConfig,
} }
this.name = name
this.excelData = data this.excelData = data
this.project = { this.project = {
title: this.name,
tables: [], tables: [],
} }
@ -64,17 +58,22 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
cellDates: true, cellDates: true,
} }
if (this.name.slice(-3) === 'csv') { // TODO: remove later
this.wb = this.xlsx.read(new TextDecoder().decode(new Uint8Array(this.excelData)), { // if (this.name.slice(-3) === 'csv') {
type: 'string', // this.wb = this.xlsx.read(new TextDecoder().decode(new Uint8Array(this.excelData)), {
...options, // type: 'string',
}) // ...options,
} else { // })
this.wb = this.xlsx.read(new Uint8Array(this.excelData), { // } else {
type: 'array', // this.wb = this.xlsx.read(new Uint8Array(this.excelData), {
...options, // type: 'array',
}) // ...options,
} // })
// }
this.wb = this.xlsx.read(new Uint8Array(this.excelData), {
type: 'array',
...options,
})
} }
parse() { parse() {
@ -94,33 +93,33 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
this.data[tn] = [] this.data[tn] = []
const ws: any = this.wb.Sheets[sheet] const ws: any = this.wb.Sheets[sheet]
const range = this.xlsx.utils.decode_range(ws['!ref']) 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 }) const rows: any = this.xlsx.utils.sheet_to_json(ws, { header: 1, blankrows: false, defval: null })
if (this.name.slice(-3) !== 'csv') { // TODO: remove later
// fix precision bug & timezone offset issues introduced by xlsx // if (this.name.slice(-3) !== 'csv') {
const basedate = new Date(1899, 11, 30, 0, 0, 0) // // fix precision bug & timezone offset issues introduced by xlsx
// number of milliseconds since base date // const basedate = new Date(1899, 11, 30, 0, 0, 0)
const dnthresh = basedate.getTime() + (new Date().getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000 // // number of milliseconds since base date
// number of milliseconds in a day // const dnthresh = basedate.getTime() + (new Date().getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000
const day_ms = 24 * 60 * 60 * 1000 // // number of milliseconds in a day
// handle date1904 property // const day_ms = 24 * 60 * 60 * 1000
const fixImportedDate = (date: Date) => { // // handle date1904 property
const parsed = this.xlsx.SSF.parse_date_code((date.getTime() - dnthresh) / day_ms, { // const fixImportedDate = (date: Date) => {
date1904: this.wb.Workbook.WBProps.date1904, // const parsed = this.xlsx.SSF.parse_date_code((date.getTime() - dnthresh) / day_ms, {
}) // date1904: this.wb.Workbook.WBProps.date1904,
return new Date(parsed.y, parsed.m, parsed.d, parsed.H, parsed.M, parsed.S) // })
} // return new Date(parsed.y, parsed.m, parsed.d, parsed.H, parsed.M, parsed.S)
// fix imported date // }
rows = rows.map((r: any) => // // fix imported date
r.map((v: any) => { // rows = rows.map((r: any) =>
return v instanceof Date ? fixImportedDate(v) : v // r.map((v: any) => {
}), // return v instanceof Date ? fixImportedDate(v) : v
) // }),
} // )
// }
const columnNameRowExist = +rows[0].every((v: any) => v === null || typeof v === 'string') const columnNameRowExist = +rows[0].every((v: any) => v === null || typeof v === 'string')
// const colLen = Math.max()
for (let col = 0; col < rows[0].length; col++) { for (let col = 0; col < rows[0].length; col++) {
let cn: string = ((columnNameRowExist && rows[0] && rows[0][col] && rows[0][col].toString().trim()) || `field_${col + 1}`) let cn: string = ((columnNameRowExist && rows[0] && rows[0][col] && rows[0][col].toString().trim()) || `field_${col + 1}`)
.replace(/[` ~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '_') .replace(/[` ~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '_')
@ -193,6 +192,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
column.uidt = UITypes.Currency column.uidt = UITypes.Currency
} }
} else if (column.uidt === UITypes.DateTime) { } else if (column.uidt === UITypes.DateTime) {
// TODO: centralise
// hold the possible date format found in the date // hold the possible date format found in the date
const dateFormat: Record<string, number> = {} const dateFormat: Record<string, number> = {}
if ( if (

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

@ -14,25 +14,22 @@ const extractNestedData: any = (obj: any, path: any) => path.reduce((val: any, k
export default class JSONTemplateAdapter extends TemplateGenerator { export default class JSONTemplateAdapter extends TemplateGenerator {
config: Record<string, any> config: Record<string, any>
name: string
data: Record<string, any> data: Record<string, any>
_jsonData: string | Record<string, any> _jsonData: string | Record<string, any>
jsonData: Record<string, any> jsonData: Record<string, any>
project: { project: {
title: string
tables: Record<string, any>[] tables: Record<string, any>[]
} }
columns: object columns: object
constructor(name = 'test', data: object, parserConfig = {}) { constructor(data: object, parserConfig = {}) {
super() super()
this.config = { this.config = {
maxRowsToParse: 500, maxRowsToParse: 500,
...parserConfig, ...parserConfig,
} }
this.name = name
this._jsonData = data this._jsonData = data
this.project = { this.project = {
title: this.name,
tables: [], tables: [],
} }
this.jsonData = [] this.jsonData = []

Loading…
Cancel
Save