diff --git a/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/data/rich-text.json b/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/data/rich-text.json new file mode 100644 index 0000000000..4d2b8793f9 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/data/rich-text.json @@ -0,0 +1,3 @@ +{ + "longText": "

" +} diff --git a/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts b/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts index dea15554cc..be6d05fea3 100644 --- a/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts +++ b/packages/plugins/@nocobase/plugin-action-export/src/server/__tests__/export-to-xlsx.test.ts @@ -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; + }); }); diff --git a/packages/plugins/@nocobase/plugin-action-export/src/server/actions/export-xlsx.ts b/packages/plugins/@nocobase/plugin-action-export/src/server/actions/export-xlsx.ts index 0325e82155..8a14bb79c3 100644 --- a/packages/plugins/@nocobase/plugin-action-export/src/server/actions/export-xlsx.ts +++ b/packages/plugins/@nocobase/plugin-action-export/src/server/actions/export-xlsx.ts @@ -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', diff --git a/packages/plugins/@nocobase/plugin-action-export/src/server/services/xlsx-exporter.ts b/packages/plugins/@nocobase/plugin-action-export/src/server/services/xlsx-exporter.ts index 900837d3fc..3027a7689a 100644 --- a/packages/plugins/@nocobase/plugin-action-export/src/server/services/xlsx-exporter.ts +++ b/packages/plugins/@nocobase/plugin-action-export/src/server/services/xlsx-exporter.ts @@ -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 & { columns: Array; }; +const XLSX_LIMIT_CHAER = 32767; + export class XlsxExporter extends BaseExporter> }> { /** * You can adjust the maximum number of exported rows based on business needs and system @@ -88,6 +99,17 @@ export class XlsxExporter extends BaseExporter 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) {