mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-09 23:49:27 +08:00
Merge branch 'main' into next
This commit is contained in:
commit
8105e047ed
File diff suppressed because one or more lines are too long
@ -1499,4 +1499,37 @@ describe('export to xlsx', () => {
|
||||
expect(sheetData[1]).toEqual(['user0', 0]); // first user
|
||||
expect(sheetData[10]).toEqual(['user9', 9]); // last user
|
||||
});
|
||||
|
||||
it('should import rich text field successfully when long text', async () => {
|
||||
const Test = app.db.collection({
|
||||
name: 'tests',
|
||||
fields: [
|
||||
{
|
||||
interface: 'richText',
|
||||
type: 'text',
|
||||
name: 'richText',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await app.db.sync();
|
||||
const data = require('./data/rich-text.json');
|
||||
const longText = data.longText;
|
||||
await Test.repository.create({
|
||||
values: {
|
||||
richText: longText /* .slice(0, 10000) */,
|
||||
},
|
||||
});
|
||||
const exporter = new XlsxExporter({
|
||||
collectionManager: app.mainDataSource.collectionManager,
|
||||
collection: Test,
|
||||
chunkSize: 10,
|
||||
limit: 10,
|
||||
columns: [{ dataIndex: ['richText'], defaultTitle: 'richText' }],
|
||||
});
|
||||
|
||||
const wb = await exporter.run();
|
||||
const buffer = XlsxExporter.xlsxSafeWrite(wb, { type: 'buffer', bookType: 'xlsx' });
|
||||
expect(buffer).exist;
|
||||
});
|
||||
});
|
||||
|
@ -45,7 +45,7 @@ async function exportXlsxAction(ctx: Context, next: Next) {
|
||||
|
||||
const wb = await xlsxExporter.run(ctx);
|
||||
|
||||
ctx.body = XLSX.write(wb, { type: 'buffer', bookType: 'xlsx' });
|
||||
ctx.body = XlsxExporter.xlsxSafeWrite(wb, { type: 'buffer', bookType: 'xlsx' });
|
||||
|
||||
ctx.set({
|
||||
'Content-Type': 'application/octet-stream',
|
||||
|
@ -1,3 +1,12 @@
|
||||
/**
|
||||
* 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 XLSX from 'xlsx';
|
||||
import { BaseExporter, ExportOptions } from './base-exporter';
|
||||
import { NumberField } from '@nocobase/database';
|
||||
@ -12,6 +21,8 @@ type XlsxExportOptions = Omit<ExportOptions, 'fields'> & {
|
||||
columns: Array<ExportColumn>;
|
||||
};
|
||||
|
||||
const XLSX_LIMIT_CHAER = 32767;
|
||||
|
||||
export class XlsxExporter extends BaseExporter<XlsxExportOptions & { fields: Array<Array<string>> }> {
|
||||
/**
|
||||
* You can adjust the maximum number of exported rows based on business needs and system
|
||||
@ -88,6 +99,17 @@ export class XlsxExporter extends BaseExporter<XlsxExportOptions & { fields: Arr
|
||||
return col.title || fieldInstance?.options.title || col.defaultTitle;
|
||||
});
|
||||
}
|
||||
|
||||
static xlsxSafeWrite(wb: XLSX.WorkBook, opts: XLSX.WritingOptions, path?: string) {
|
||||
const data = wb.Sheets.Data as object;
|
||||
for (const key of Object.keys(data)) {
|
||||
const v = data[key]?.v;
|
||||
if (v?.length > XLSX_LIMIT_CHAER) {
|
||||
data[key].v = v.slice(0, XLSX_LIMIT_CHAER);
|
||||
}
|
||||
}
|
||||
return path ? XLSX.writeFileXLSX(wb, path) : XLSX.write(wb, opts);
|
||||
}
|
||||
}
|
||||
|
||||
function isNumeric(n) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user