Browse Source

revert(nc-gui): use import on init for xlsx

pull/3801/head
braks 2 years ago
parent
commit
baa955ad7e
  1. 25
      packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts

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

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

Loading…
Cancel
Save