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

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

@ -1,6 +1,7 @@
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'
import { getDateFormat } from '~/utils'
const excelTypeToUidt: Record<string, UITypes> = { const excelTypeToUidt: Record<string, UITypes> = {
d: UITypes.DateTime, d: UITypes.DateTime,
@ -129,6 +130,7 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
const column: Record<string, any> = { const column: Record<string, any> = {
column_name: cn, column_name: cn,
ref_column_name: cn, ref_column_name: cn,
meta: {},
} }
// 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`;
@ -226,6 +228,8 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
column.uidt = UITypes.Currency column.uidt = UITypes.Currency
} }
} else if (column.uidt === UITypes.DateTime) { } else if (column.uidt === UITypes.DateTime) {
// hold the possible date format found in the date
const dateFormat: Record<string, number> = {}
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 = this.xlsx.utils.encode_cell({ const cellId = this.xlsx.utils.encode_cell({
@ -234,10 +238,17 @@ export default class ExcelTemplateAdapter extends TemplateGenerator {
}) })
const cellObj = ws[cellId] 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 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) 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] 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) { } 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 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 { } else {
// toto: do parsing if necessary based on type // toto: do parsing if necessary based on type
rowData[table.columns[i].column_name] = row[i] rowData[table.columns[i].column_name] = row[i]

Loading…
Cancel
Save