diff --git a/packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts b/packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts index 71a6168d5c..6e1b9ea4c9 100644 --- a/packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts +++ b/packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts @@ -1,4 +1,3 @@ -import { SSF, read, utils } from 'xlsx' import { UITypes } from 'nocodb-sdk' import TemplateGenerator from './TemplateGenerator' import { getCheckboxValue, isCheckboxType } from './parserHelpers' @@ -28,6 +27,8 @@ export default class ExcelTemplateAdapter extends TemplateGenerator { wb: any + xlsx: typeof import('xlsx') + constructor(name = '', data = {}, parserConfig = {}) { super() this.config = { @@ -43,21 +44,25 @@ export default class ExcelTemplateAdapter extends TemplateGenerator { title: this.name, tables: [], } + + this.xlsx = {} as any } async init() { + this.xlsx = await import('xlsx') + const options = { cellText: true, cellDates: true, } if (this.name.slice(-3) === 'csv') { - this.wb = read(new TextDecoder().decode(new Uint8Array(this.excelData)), { + this.wb = this.xlsx.read(new TextDecoder().decode(new Uint8Array(this.excelData)), { type: 'string', ...options, }) } else { - this.wb = read(new Uint8Array(this.excelData), { + this.wb = this.xlsx.read(new Uint8Array(this.excelData), { type: 'array', ...options, }) @@ -80,8 +85,8 @@ export default class ExcelTemplateAdapter extends TemplateGenerator { const table = { table_name: tn, ref_table_name: tn, columns: [] as any[] } this.data[tn] = [] const ws: any = this.wb.Sheets[sheet] - const range = utils.decode_range(ws['!ref']) - let rows: any = utils.sheet_to_json(ws, { header: 1, blankrows: false, defval: null }) + 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 }) if (this.name.slice(-3) !== 'csv') { // fix precision bug & timezone offset issues introduced by xlsx @@ -92,7 +97,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator { const day_ms = 24 * 60 * 60 * 1000 // handle date1904 property const fixImportedDate = (date: Date) => { - const parsed = SSF.parse_date_code((date.getTime() - dnthresh) / day_ms, { + 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) @@ -126,7 +131,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator { table.columns.push(column) // const cellId = `${col.toString(26).split('').map(s => (parseInt(s, 26) + 10).toString(36).toUpperCase())}2`; - const cellId = utils.encode_cell({ + const cellId = this.xlsx.utils.encode_cell({ c: range.s.c + col, r: columnNameRowExist, }) @@ -188,7 +193,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator { } if ( rows.slice(1, this.config.maxRowsToParse).every((v: any, i: any) => { - const cellId = utils.encode_cell({ + const cellId = this.xlsx.utils.encode_cell({ c: range.s.c + col, r: i + columnNameRowExist, }) @@ -203,7 +208,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator { } else if (column.uidt === UITypes.DateTime) { if ( rows.slice(1, this.config.maxRowsToParse).every((v: any, i: any) => { - const cellId = utils.encode_cell({ + const cellId = this.xlsx.utils.encode_cell({ c: range.s.c + col, r: i + columnNameRowExist, }) @@ -224,7 +229,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator { if (table.columns[i].uidt === UITypes.Checkbox) { rowData[table.columns[i].column_name] = getCheckboxValue(row[i]) } else if (table.columns[i].uidt === UITypes.Currency) { - const cellId = utils.encode_cell({ + const cellId = this.xlsx.utils.encode_cell({ c: range.s.c + i, r: rowIndex + columnNameRowExist, })