diff --git a/packages/nc-gui/components/dlg/QuickImport.vue b/packages/nc-gui/components/dlg/QuickImport.vue index 00840a7e4a..85753d9865 100644 --- a/packages/nc-gui/components/dlg/QuickImport.vue +++ b/packages/nc-gui/components/dlg/QuickImport.vue @@ -454,7 +454,7 @@ const beforeUpload = (file: UploadFile) => { - + Auto-Select Field Types diff --git a/packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts b/packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts index 438c304fb0..6e49843bf8 100644 --- a/packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts +++ b/packages/nc-gui/utils/parsers/ExcelTemplateAdapter.ts @@ -114,86 +114,89 @@ export default class ExcelTemplateAdapter extends TemplateGenerator { column_name: cn, ref_column_name: cn, meta: {}, + uidt: UITypes.SingleLineText, } - const cellId = this.xlsx.utils.encode_cell({ - c: range.s.c + col, - r: columnNameRowExist, - }) - const cellProps = ws[cellId] || {} - column.uidt = excelTypeToUidt[cellProps.t] || UITypes.SingleLineText - - if (column.uidt === UITypes.SingleLineText) { - // check for long text - if (isMultiLineTextType(rows)) { - column.uidt = UITypes.LongText - } + if (this.config.autoSelectFieldTypes) { + const cellId = this.xlsx.utils.encode_cell({ + c: range.s.c + col, + r: columnNameRowExist, + }) + const cellProps = ws[cellId] || {} + column.uidt = excelTypeToUidt[cellProps.t] || UITypes.SingleLineText + + if (column.uidt === UITypes.SingleLineText) { + // check for long text + if (isMultiLineTextType(rows)) { + column.uidt = UITypes.LongText + } - if (isEmailType(rows)) { - column.uidt = UITypes.Email - } + if (isEmailType(rows)) { + column.uidt = UITypes.Email + } - if (isUrlType(rows)) { - column.uidt = UITypes.URL - } else { - const vals = rows - .slice(columnNameRowExist ? 1 : 0) - .map((r: any) => r[col]) - .filter((v: any) => v !== null && v !== undefined && v.toString().trim() !== '') - - const checkboxType = isCheckboxType(vals) - if (checkboxType.length === 1) { - column.uidt = UITypes.Checkbox + if (isUrlType(rows)) { + column.uidt = UITypes.URL } else { - // Single Select / Multi Select - Object.assign(column, extractMultiOrSingleSelectProps(vals)) + const vals = rows + .slice(columnNameRowExist ? 1 : 0) + .map((r: any) => r[col]) + .filter((v: any) => v !== null && v !== undefined && v.toString().trim() !== '') + + const checkboxType = isCheckboxType(vals) + if (checkboxType.length === 1) { + column.uidt = UITypes.Checkbox + } else { + // Single Select / Multi Select + Object.assign(column, extractMultiOrSingleSelectProps(vals)) + } } - } - } else if (column.uidt === UITypes.Number) { - if ( - rows.slice(1, this.config.maxRowsToParse).some((v: any) => { - return v && v[col] && parseInt(v[col]) !== +v[col] - }) - ) { - column.uidt = UITypes.Decimal - } - if ( - rows.slice(1, this.config.maxRowsToParse).every((v: any, i: any) => { - const cellId = this.xlsx.utils.encode_cell({ - c: range.s.c + col, - r: i + columnNameRowExist, + } else if (column.uidt === UITypes.Number) { + if ( + rows.slice(1, this.config.maxRowsToParse).some((v: any) => { + return v && v[col] && parseInt(v[col]) !== +v[col] }) + ) { + column.uidt = UITypes.Decimal + } + if ( + rows.slice(1, this.config.maxRowsToParse).every((v: any, i: any) => { + const cellId = this.xlsx.utils.encode_cell({ + c: range.s.c + col, + r: i + columnNameRowExist, + }) - const cellObj = ws[cellId] + const cellObj = ws[cellId] - return !cellObj || (cellObj.w && cellObj.w.startsWith('$')) - }) - ) { - column.uidt = UITypes.Currency - } - } else if (column.uidt === UITypes.DateTime) { - // TODO(import): centralise - // hold the possible date format found in the date - const dateFormat: Record = {} - if ( - rows.slice(1, this.config.maxRowsToParse).every((v: any, i: any) => { - const cellId = this.xlsx.utils.encode_cell({ - c: range.s.c + col, - r: i + columnNameRowExist, + return !cellObj || (cellObj.w && cellObj.w.startsWith('$')) }) - - const cellObj = ws[cellId] - 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' + ) { + column.uidt = UITypes.Currency + } + } else if (column.uidt === UITypes.DateTime) { + // TODO(import): centralise + // hold the possible date format found in the date + const dateFormat: Record = {} + if ( + rows.slice(1, this.config.maxRowsToParse).every((v: any, i: any) => { + const cellId = this.xlsx.utils.encode_cell({ + c: range.s.c + col, + r: i + columnNameRowExist, + }) + + const cellObj = ws[cellId] + 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) diff --git a/packages/nc-gui/utils/parsers/JSONTemplateAdapter.ts b/packages/nc-gui/utils/parsers/JSONTemplateAdapter.ts index 7167eb993c..a3ea20a1e0 100644 --- a/packages/nc-gui/utils/parsers/JSONTemplateAdapter.ts +++ b/packages/nc-gui/utils/parsers/JSONTemplateAdapter.ts @@ -89,17 +89,17 @@ export default class JSONTemplateAdapter extends TemplateGenerator { } } else { const cn = path.join('_').replace(/\W/g, '_').trim() - const column: Record = { column_name: cn, ref_column_name: cn, + uidt: UITypes.SingleLineText, path, } - - column.uidt = jsonTypeToUidt[typeof firstRowVal] || UITypes.SingleLineText - - const colData = jsonData.map((r: any) => extractNestedData(r, path)) - Object.assign(column, getColumnUIDTAndMetas(colData, column.uidt)) + if (this.config.autoSelectFieldTypes) { + column.uidt = jsonTypeToUidt[typeof firstRowVal] || UITypes.SingleLineText + const colData = jsonData.map((r: any) => extractNestedData(r, path)) + Object.assign(column, getColumnUIDTAndMetas(colData, column.uidt)) + } columns.push(column) }