From 259b0b707afb9879d75323d0510f1cc992572167 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Wed, 9 Oct 2024 09:20:00 +0800 Subject: [PATCH] fix: import with big date (#5356) --- .../server/__tests__/xlsx-importer.test.ts | 63 +++++++++++++++++++ .../src/server/services/xlsx-importer.ts | 2 +- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts b/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts index d86c5aadc9..130a5f8ee1 100644 --- a/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts +++ b/packages/plugins/@nocobase/plugin-action-import/src/server/__tests__/xlsx-importer.test.ts @@ -12,6 +12,7 @@ import { TemplateCreator } from '../services/template-creator'; import { XlsxImporter } from '../services/xlsx-importer'; import XLSX from 'xlsx'; import * as process from 'node:process'; +import moment from 'moment'; describe('xlsx importer', () => { let app: MockServer; @@ -25,6 +26,68 @@ describe('xlsx importer', () => { await app.destroy(); }); + describe('import with date field', () => { + let User; + + beforeEach(async () => { + User = app.db.collection({ + name: 'users', + fields: [ + { + type: 'string', + name: 'name', + }, + { + type: 'date', + name: 'date', + interface: 'datetime', + }, + ], + }); + + await app.db.sync(); + }); + + it('should import with date', async () => { + const columns = [ + { + dataIndex: ['name'], + defaultTitle: '姓名', + }, + { + dataIndex: ['date'], + defaultTitle: '日期', + }, + ]; + + const templateCreator = new TemplateCreator({ + collection: User, + columns, + }); + + const template = await templateCreator.run(); + + const worksheet = template.Sheets[template.SheetNames[0]]; + + XLSX.utils.sheet_add_aoa(worksheet, [['test', 77383]], { origin: 'A2' }); + + const importer = new XlsxImporter({ + collectionManager: app.mainDataSource.collectionManager, + collection: User, + columns, + workbook: template, + }); + + await importer.run(); + + expect(await User.repository.count()).toBe(1); + + const user = await User.repository.findOne(); + + expect(moment(user.get('date')).format('YYYY-MM-DD')).toBe('2111-11-12'); + }); + }); + describe('import with select fields', () => { let User; beforeEach(async () => { diff --git a/packages/plugins/@nocobase/plugin-action-import/src/server/services/xlsx-importer.ts b/packages/plugins/@nocobase/plugin-action-import/src/server/services/xlsx-importer.ts index bbeb042f9c..be60ea8df9 100644 --- a/packages/plugins/@nocobase/plugin-action-import/src/server/services/xlsx-importer.ts +++ b/packages/plugins/@nocobase/plugin-action-import/src/server/services/xlsx-importer.ts @@ -223,7 +223,7 @@ export class XlsxImporter extends EventEmitter { getData() { const firstSheet = this.firstSheet(); - const rows = XLSX.utils.sheet_to_json(firstSheet, { header: 1, defval: null, raw: false }); + const rows = XLSX.utils.sheet_to_json(firstSheet, { header: 1, defval: null }); if (this.options.explain) { rows.shift();