Browse Source

feat(nc-gui): add callback to excel parsing + refactor

pull/4135/head
Wing-Kam Wong 2 years ago
parent
commit
a2e515de5d
  1. 80
      packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts

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

@ -3,12 +3,12 @@ import TemplateGenerator from './TemplateGenerator'
import { import {
extractMultiOrSingleSelectProps, extractMultiOrSingleSelectProps,
getCheckboxValue, getCheckboxValue,
getDateFormat,
isCheckboxType, isCheckboxType,
isEmailType, isEmailType,
isMultiLineTextType, isMultiLineTextType,
isUrlType, isUrlType,
} from './parserHelpers' } from '#imports'
import { getDateFormat } from '~/utils'
const excelTypeToUidt: Record<string, UITypes> = { const excelTypeToUidt: Record<string, UITypes> = {
d: UITypes.DateTime, d: UITypes.DateTime,
@ -58,30 +58,19 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
cellDates: true, cellDates: true,
} }
// TODO(import): remove later
// if (this.name.slice(-3) === 'csv') {
// this.wb = this.xlsx.read(new TextDecoder().decode(new Uint8Array(this.excelData)), {
// type: 'string',
// ...options,
// })
// } else {
// this.wb = this.xlsx.read(new Uint8Array(this.excelData), {
// type: 'array',
// ...options,
// })
// }
this.wb = this.xlsx.read(new Uint8Array(this.excelData), { this.wb = this.xlsx.read(new Uint8Array(this.excelData), {
type: 'array', type: 'array',
...options, ...options,
}) })
} }
parse() { parse(callback: Function) {
const tableNamePrefixRef: Record<string, any> = {} const tableNamePrefixRef: Record<string, any> = {}
this.wb.SheetNames.reduce((acc: any, sheet: any) => {
for (let i = 0; i < this.wb.SheetNames.length; i++) { return acc.then(
() =>
new Promise((resolve) => {
const columnNamePrefixRef: Record<string, any> = { id: 0 } const columnNamePrefixRef: Record<string, any> = { id: 0 }
const sheet: any = this.wb.SheetNames[i]
let tn: string = (sheet || 'table').replace(/[` ~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '_').trim() let tn: string = (sheet || 'table').replace(/[` ~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '_').trim()
while (tn in tableNamePrefixRef) { while (tn in tableNamePrefixRef) {
@ -93,35 +82,35 @@ 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'])
const rows: any = this.xlsx.utils.sheet_to_json(ws, { header: 1, blankrows: false, defval: null }) let rows: any = this.xlsx.utils.sheet_to_json(ws, { header: 1, blankrows: false, defval: null })
// TODO(import): 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')
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, '_')
.trim() .trim()
@ -251,7 +240,10 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
rowIndex++ rowIndex++
} }
this.project.tables.push(table) this.project.tables.push(table)
} resolve(true)
}),
)
}, Promise.resolve()).then(callback)
} }
getTemplate() { getTemplate() {

Loading…
Cancel
Save