|
|
|
@ -3,12 +3,12 @@ import TemplateGenerator from './TemplateGenerator'
|
|
|
|
|
import { |
|
|
|
|
extractMultiOrSingleSelectProps, |
|
|
|
|
getCheckboxValue, |
|
|
|
|
getDateFormat, |
|
|
|
|
isCheckboxType, |
|
|
|
|
isEmailType, |
|
|
|
|
isMultiLineTextType, |
|
|
|
|
isUrlType, |
|
|
|
|
} from './parserHelpers' |
|
|
|
|
import { getDateFormat } from '~/utils' |
|
|
|
|
} from '#imports' |
|
|
|
|
|
|
|
|
|
const excelTypeToUidt: Record<string, UITypes> = { |
|
|
|
|
d: UITypes.DateTime, |
|
|
|
@ -58,30 +58,19 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
|
|
|
|
|
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), { |
|
|
|
|
type: 'array', |
|
|
|
|
...options, |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
parse() { |
|
|
|
|
parse(callback: Function) { |
|
|
|
|
const tableNamePrefixRef: Record<string, any> = {} |
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.wb.SheetNames.length; i++) { |
|
|
|
|
this.wb.SheetNames.reduce((acc: any, sheet: any) => { |
|
|
|
|
return acc.then( |
|
|
|
|
() => |
|
|
|
|
new Promise((resolve) => { |
|
|
|
|
const columnNamePrefixRef: Record<string, any> = { id: 0 } |
|
|
|
|
const sheet: any = this.wb.SheetNames[i] |
|
|
|
|
let tn: string = (sheet || 'table').replace(/[` ~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '_').trim() |
|
|
|
|
|
|
|
|
|
while (tn in tableNamePrefixRef) { |
|
|
|
@ -93,35 +82,35 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
|
|
|
|
|
this.data[tn] = [] |
|
|
|
|
const ws: any = this.wb.Sheets[sheet] |
|
|
|
|
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 }) |
|
|
|
|
|
|
|
|
|
// TODO(import): remove later
|
|
|
|
|
// if (this.name.slice(-3) !== 'csv') {
|
|
|
|
|
// // fix precision bug & timezone offset issues introduced by xlsx
|
|
|
|
|
// const basedate = new Date(1899, 11, 30, 0, 0, 0)
|
|
|
|
|
// // number of milliseconds since base date
|
|
|
|
|
// const dnthresh = basedate.getTime() + (new Date().getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000
|
|
|
|
|
// // number of milliseconds in a day
|
|
|
|
|
// const day_ms = 24 * 60 * 60 * 1000
|
|
|
|
|
// // handle date1904 property
|
|
|
|
|
// const fixImportedDate = (date: Date) => {
|
|
|
|
|
// 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)
|
|
|
|
|
// }
|
|
|
|
|
// // fix imported date
|
|
|
|
|
// rows = rows.map((r: any) =>
|
|
|
|
|
// r.map((v: any) => {
|
|
|
|
|
// return v instanceof Date ? fixImportedDate(v) : v
|
|
|
|
|
// }),
|
|
|
|
|
// )
|
|
|
|
|
// }
|
|
|
|
|
let rows: any = this.xlsx.utils.sheet_to_json(ws, { header: 1, blankrows: false, defval: null }) |
|
|
|
|
|
|
|
|
|
// fix precision bug & timezone offset issues introduced by xlsx
|
|
|
|
|
const basedate = new Date(1899, 11, 30, 0, 0, 0) |
|
|
|
|
// number of milliseconds since base date
|
|
|
|
|
const dnthresh = basedate.getTime() + (new Date().getTimezoneOffset() - basedate.getTimezoneOffset()) * 60000 |
|
|
|
|
// number of milliseconds in a day
|
|
|
|
|
const day_ms = 24 * 60 * 60 * 1000 |
|
|
|
|
// handle date1904 property
|
|
|
|
|
const fixImportedDate = (date: Date) => { |
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
// fix imported date
|
|
|
|
|
rows = rows.map((r: any) => |
|
|
|
|
r.map((v: any) => { |
|
|
|
|
return v instanceof Date ? fixImportedDate(v) : v |
|
|
|
|
}), |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
const columnNameRowExist = +rows[0].every((v: any) => v === null || typeof v === 'string') |
|
|
|
|
|
|
|
|
|
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, '_') |
|
|
|
|
.trim() |
|
|
|
|
|
|
|
|
@ -251,7 +240,10 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
|
|
|
|
|
rowIndex++ |
|
|
|
|
} |
|
|
|
|
this.project.tables.push(table) |
|
|
|
|
} |
|
|
|
|
resolve(true) |
|
|
|
|
}), |
|
|
|
|
) |
|
|
|
|
}, Promise.resolve()).then(callback) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getTemplate() { |
|
|
|
|