fix: fixed an error importing xlsx time field

This commit is contained in:
aaaaaajie 2025-04-15 20:26:31 +08:00
parent 0f21f483f7
commit 25889515fb
5 changed files with 79 additions and 0 deletions

View File

@ -15,3 +15,4 @@ export * from './datetime-interface';
export * from './datetime-no-tz-interface';
export * from './boolean-interface';
export * from './date-interface';
export * from './time-interface';

View File

@ -0,0 +1,26 @@
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
import dayjs from 'dayjs';
import { BaseInterface } from './base-interface';
export class TimeInterface extends BaseInterface {
toValue(value: any, ctx?: any) {
if (this.validate(value)) {
const result = dayjs.utc(value).format('HH:mm:ss');
return result;
}
return value;
}
validate(value) {
const result = dayjs(value).isValid();
return result;
}
}

View File

@ -16,6 +16,7 @@ import {
MultipleSelectInterface,
PercentInterface,
SelectInterface,
TimeInterface,
} from './index';
import { ManyToOneInterface } from './many-to-one-interface';
import { ManyToManyInterface } from './many-to-many-interface';
@ -50,6 +51,7 @@ const interfaces = {
o2m: OneToManyInterface,
m2o: ManyToOneInterface,
m2m: ManyToManyInterface,
time: TimeInterface,
};
export function registerInterfaces(db: Database) {

View File

@ -2155,4 +2155,53 @@ describe('xlsx importer', () => {
expect(await Post.repository.count()).toBe(1);
});
it('should import time field successfully', async () => {
const TimeCollection = app.db.collection({
name: 'time_tests',
fields: [
{
type: 'time',
name: 'brithtime',
},
],
});
await app.db.sync();
const templateCreator = new TemplateCreator({
collection: TimeCollection,
explain: 'test',
columns: [
{
dataIndex: ['birthtime'],
defaultTitle: '出生时间',
},
],
});
const template = (await templateCreator.run({ returnXLSXWorkbook: true })) as XLSX.WorkBook;
const worksheet = template.Sheets[template.SheetNames[0]];
XLSX.utils.sheet_add_aoa(worksheet, [['12:12:12']], {
origin: 'A3',
});
const importer = new XlsxImporter({
collectionManager: app.mainDataSource.collectionManager,
collection: TimeCollection,
explain: 'test',
columns: [
{
dataIndex: ['brithtime'],
defaultTitle: '出生时间',
},
],
workbook: template,
});
await importer.run();
const count = await TimeCollection.repository.count();
expect(count).toBe(1);
});
});

View File

@ -36,6 +36,7 @@ async function importXlsxAction(ctx: Context, next: Next) {
const workbook = XLSX.read(ctx.file.buffer, {
type: 'buffer',
sheetRows: readLimit,
cellDates: true,
});
const repository = ctx.getCurrentRepository() as Repository;