fix: the error of exporting long texts (#6713)

* fix: the error of exporting long texts
This commit is contained in:
ajie 2025-05-08 11:32:42 +08:00 committed by GitHub
parent d5bd381339
commit 856a9445d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 1 deletions

File diff suppressed because one or more lines are too long

View File

@ -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;
});
});

View File

@ -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',

View File

@ -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) {