Browse Source

Merge pull request #4043 from nocodb/fix/import-dt-format

fix(nc-gui): handle date_format in import
pull/4064/head
աɨռɢӄաօռɢ 2 years ago committed by GitHub
parent
commit
3bebfee92e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      packages/nc-gui/components/template/Editor.vue
  2. 20
      packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts

6
packages/nc-gui/components/template/Editor.vue

@ -232,7 +232,10 @@ function remapColNames(batchData: any[], columns: ColumnType[]) {
let d = data[col.ref_column_name || col.column_name]
if (col.uidt === UITypes.Date && d) {
let dateFormat
if (col.key in dateFormatMap) {
if (col?.meta?.date_format) {
dateFormat = col.meta.date_format
dateFormatMap[col.key] = dateFormat
} else if (col.key in dateFormatMap) {
dateFormat = dateFormatMap[col.key]
} else {
dateFormat = getDateFormat(d)
@ -240,6 +243,7 @@ function remapColNames(batchData: any[], columns: ColumnType[]) {
}
d = dayjs(d).utc().format(dateFormat)
} else if (col.uidt === UITypes.DateTime && d) {
// TODO: handle more formats for DateTime
d = dayjs(data[col.ref_column_name || col.column_name])
.utc()
.format('YYYY-MM-DD HH:mm')

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

@ -1,6 +1,7 @@
import { UITypes } from 'nocodb-sdk'
import TemplateGenerator from './TemplateGenerator'
import { getCheckboxValue, isCheckboxType } from './parserHelpers'
import { getDateFormat } from '~/utils'
const excelTypeToUidt: Record<string, UITypes> = {
d: UITypes.DateTime,
@ -129,6 +130,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
const column: Record<string, any> = {
column_name: cn,
ref_column_name: cn,
meta: {},
}
// const cellId = `${col.toString(26).split('').map(s => (parseInt(s, 26) + 10).toString(36).toUpperCase())}2`;
@ -226,6 +228,8 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
column.uidt = UITypes.Currency
}
} else if (column.uidt === UITypes.DateTime) {
// hold the possible date format found in the date
const dateFormat: Record<string, number> = {}
if (
rows.slice(1, this.config.maxRowsToParse).every((v: any, i: any) => {
const cellId = this.xlsx.utils.encode_cell({
@ -234,10 +238,17 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
})
const cellObj = ws[cellId]
return !cellObj || (cellObj.w && cellObj.w.split(' ').length === 1)
const isDate = !cellObj || (cellObj.w && cellObj.w.split(' ').length === 1)
if (isDate && cellObj) {
dateFormat[getDateFormat(cellObj.w)] = (dateFormat[getDateFormat(cellObj.w)] || 0) + 1
}
return isDate
})
) {
column.uidt = UITypes.Date
// take the date format with the max occurrence
column.meta.date_format =
Object.keys(dateFormat).reduce((x, y) => (dateFormat[x] > dateFormat[y] ? x : y)) || 'YYYY/MM/DD'
}
}
table.columns.push(column)
@ -259,6 +270,13 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
rowData[table.columns[i].column_name] = (cellObj && cellObj.w && cellObj.w.replace(/[^\d.]+/g, '')) || row[i]
} else if (table.columns[i].uidt === UITypes.SingleSelect || table.columns[i].uidt === UITypes.MultiSelect) {
rowData[table.columns[i].column_name] = (row[i] || '').toString().trim() || null
} else if (table.columns[i].uidt === UITypes.Date) {
const cellId = this.xlsx.utils.encode_cell({
c: range.s.c + i,
r: rowIndex + columnNameRowExist,
})
const cellObj = ws[cellId]
rowData[table.columns[i].column_name] = (cellObj && cellObj.w) || row[i]
} else {
// toto: do parsing if necessary based on type
rowData[table.columns[i].column_name] = row[i]

Loading…
Cancel
Save