mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 13:39:24 +08:00
fix: fixed an error importing xlsx time field
This commit is contained in:
parent
0f21f483f7
commit
25889515fb
@ -15,3 +15,4 @@ export * from './datetime-interface';
|
|||||||
export * from './datetime-no-tz-interface';
|
export * from './datetime-no-tz-interface';
|
||||||
export * from './boolean-interface';
|
export * from './boolean-interface';
|
||||||
export * from './date-interface';
|
export * from './date-interface';
|
||||||
|
export * from './time-interface';
|
||||||
|
26
packages/core/database/src/interfaces/time-interface.ts
Normal file
26
packages/core/database/src/interfaces/time-interface.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -16,6 +16,7 @@ import {
|
|||||||
MultipleSelectInterface,
|
MultipleSelectInterface,
|
||||||
PercentInterface,
|
PercentInterface,
|
||||||
SelectInterface,
|
SelectInterface,
|
||||||
|
TimeInterface,
|
||||||
} from './index';
|
} from './index';
|
||||||
import { ManyToOneInterface } from './many-to-one-interface';
|
import { ManyToOneInterface } from './many-to-one-interface';
|
||||||
import { ManyToManyInterface } from './many-to-many-interface';
|
import { ManyToManyInterface } from './many-to-many-interface';
|
||||||
@ -50,6 +51,7 @@ const interfaces = {
|
|||||||
o2m: OneToManyInterface,
|
o2m: OneToManyInterface,
|
||||||
m2o: ManyToOneInterface,
|
m2o: ManyToOneInterface,
|
||||||
m2m: ManyToManyInterface,
|
m2m: ManyToManyInterface,
|
||||||
|
time: TimeInterface,
|
||||||
};
|
};
|
||||||
|
|
||||||
export function registerInterfaces(db: Database) {
|
export function registerInterfaces(db: Database) {
|
||||||
|
@ -2155,4 +2155,53 @@ describe('xlsx importer', () => {
|
|||||||
|
|
||||||
expect(await Post.repository.count()).toBe(1);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -36,6 +36,7 @@ async function importXlsxAction(ctx: Context, next: Next) {
|
|||||||
const workbook = XLSX.read(ctx.file.buffer, {
|
const workbook = XLSX.read(ctx.file.buffer, {
|
||||||
type: 'buffer',
|
type: 'buffer',
|
||||||
sheetRows: readLimit,
|
sheetRows: readLimit,
|
||||||
|
cellDates: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
const repository = ctx.getCurrentRepository() as Repository;
|
const repository = ctx.getCurrentRepository() as Repository;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user