diff --git a/packages/plugins/export/src/server/__tests__/utils/utils.test.ts b/packages/plugins/export/src/server/__tests__/utils/utils.test.ts new file mode 100644 index 000000000..651067977 --- /dev/null +++ b/packages/plugins/export/src/server/__tests__/utils/utils.test.ts @@ -0,0 +1,45 @@ +import { columns2Appends } from '../../utils/columns2Appends'; + +describe('utils', () => { + let columns = null; + beforeEach(async () => {}); + afterEach(async () => {}); + + it('first columns2Appends', async () => { + columns = [ + { dataIndex: ['f_kp6gk63udss'], defaultTitle: '商品名称' }, + { + dataIndex: ['f_brjkofr2mbt'], + enum: [ + { value: 'lzjqrrw2vdl', label: '节' }, + { value: 'i0qarqlm87m', label: '胡' }, + { value: '1fpb8x0swq1', label: '一一' }, + ], + defaultTitle: '工在在', + }, + { dataIndex: ['f_qhvvfuignh2', 'createdBy', 'id'], defaultTitle: 'ID' }, + { dataIndex: ['f_wu28mus1c65', 'roles', 'title'], defaultTitle: '角色名称' }, + ]; + const appends = columns2Appends(columns); + expect(appends).toMatchObject(['f_qhvvfuignh2.createdBy', 'f_wu28mus1c65.roles']); + }); + + it('second columns2Appends', async () => { + columns = [ + { dataIndex: ['f_kp6gk63udss'], defaultTitle: '商品名称' }, + { + dataIndex: ['f_brjkofr2mbt'], + enum: [ + { value: 'lzjqrrw2vdl', label: '节' }, + { value: 'i0qarqlm87m', label: '胡' }, + { value: '1fpb8x0swq1', label: '一一' }, + ], + defaultTitle: '工在在', + }, + { dataIndex: ['f_qhvvfuignh2', 'createdBy', 'id'], defaultTitle: 'ID' }, + { dataIndex: ['f_qhvvfuignh2', 'createdBy', 'nickname'], defaultTitle: '角色名称' }, + ]; + const appends = columns2Appends(columns); + expect(appends).toMatchObject(['f_qhvvfuignh2.createdBy']); + }); +}); diff --git a/packages/plugins/export/src/server/actions/export-xlsx.ts b/packages/plugins/export/src/server/actions/export-xlsx.ts index 08c209da0..e8f16a89f 100644 --- a/packages/plugins/export/src/server/actions/export-xlsx.ts +++ b/packages/plugins/export/src/server/actions/export-xlsx.ts @@ -2,13 +2,15 @@ import { Context, Next } from '@nocobase/actions'; import { Repository } from '@nocobase/database'; import xlsx from 'node-xlsx'; import render from '../renders'; +import { columns2Appends } from '../utils'; export async function exportXlsx(ctx: Context, next: Next) { - let { title, columns, filter, fields, except, appends } = ctx.action.params; + let { title, columns, filter, fields, except } = ctx.action.params; const { resourceName, resourceOf } = ctx.action; if (typeof columns === 'string') { columns = JSON.parse(columns); } + const appends = columns2Appends(columns); columns = columns?.filter((col) => col?.dataIndex?.length > 0); const repository = ctx.db.getRepository(resourceName, resourceOf) as Repository; const collection = repository.collection; diff --git a/packages/plugins/export/src/server/renders/renders.ts b/packages/plugins/export/src/server/renders/renders.ts index 9a16edffa..1e8e7d3f2 100644 --- a/packages/plugins/export/src/server/renders/renders.ts +++ b/packages/plugins/export/src/server/renders/renders.ts @@ -2,9 +2,30 @@ import moment from 'moment'; export async function _(field, row, ctx, column?: any) { if (column?.dataIndex.length > 1) { - return column.dataIndex.reduce((result, col) => { - return result?.[col]; + const result = column.dataIndex.reduce((result, col) => { + if (Array.isArray(result)) { + const subResults = []; + for (const r of result) { + subResults.push(r?.[col]); + } + return subResults; + } else { + if (Array.isArray(result?.[col])) { + const subResults = []; + for (const r of result?.[col]) { + subResults.push(r); + } + return subResults; + } else { + return result?.[col]; + } + } }, row); + if (Array.isArray(result)) { + return result.join(','); + } else { + return result; + } } else { return row.get(field.name); } diff --git a/packages/plugins/export/src/server/utils/columns2Appends.ts b/packages/plugins/export/src/server/utils/columns2Appends.ts new file mode 100644 index 000000000..5b3bf753d --- /dev/null +++ b/packages/plugins/export/src/server/utils/columns2Appends.ts @@ -0,0 +1,13 @@ +export function columns2Appends(columns) { + const appends = new Set([]); + for (const column of columns) { + if (column.dataIndex.length > 1) { + const appendColumns = []; + for (let i = 0, iLen = column.dataIndex.length - 1; i < iLen; i++) { + appendColumns.push(column.dataIndex[i]); + } + appends.add(appendColumns.join('.')); + } + } + return Array.from(appends); +} diff --git a/packages/plugins/export/src/server/utils/index.ts b/packages/plugins/export/src/server/utils/index.ts new file mode 100644 index 000000000..9d9a02a02 --- /dev/null +++ b/packages/plugins/export/src/server/utils/index.ts @@ -0,0 +1 @@ +export * from './columns2Appends';