From fb5af460b3508c75fe8ac64cfaa872c4272e1ee0 Mon Sep 17 00:00:00 2001 From: cipchk Date: Mon, 25 Dec 2023 17:08:58 +0800 Subject: [PATCH] fix(abc:st): fix export mismatch - close https://github.com/ng-alain/ng-alain/issues/2461 --- packages/abc/st/st-export.ts | 24 +++++++++++++----------- packages/abc/st/test/st-export.spec.ts | 4 ++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/abc/st/st-export.ts b/packages/abc/st/st-export.ts index d70a8a2a1d..ec3a35a4be 100644 --- a/packages/abc/st/st-export.ts +++ b/packages/abc/st/st-export.ts @@ -50,26 +50,28 @@ export class STExport { const sheets: { [sheet: string]: { [key: string]: NzSafeAny } } = {}; const sheet: { [key: string]: NzSafeAny } = (sheets[opt.sheetname || 'Sheet1'] = {}); const dataLen = opt.data!.length; + const columns = opt.columens! as _STColumn[]; let validColCount = 0; - const columns = (opt.columens! as _STColumn[]).filter( - col => !(col.exported === false || !col.index || !(!col.buttons || col.buttons.length === 0)) - ); - if (columns.findIndex(w => w._width != null) !== -1) { - // wpx: width in screen pixels https://github.com/SheetJS/sheetjs#column-properties - sheet['!cols'] = columns.map(col => ({ wpx: col._width })); - } - for (let colIdx = 0; colIdx < columns.length; colIdx++) { - const col = columns[colIdx]; + let wpx = false; + const invalidFn = (col: _STColumn): boolean => + col.exported === false || !col.index || !(!col.buttons || col.buttons.length === 0); + for (const [idx, col] of columns.entries()) { + if (invalidFn(col)) continue; + if (!wpx && col._width != null) wpx = true; ++validColCount; - const columnName = this.xlsxSrv.numberToSchema(colIdx + 1); + const columnName = this.xlsxSrv.numberToSchema(validColCount); sheet[`${columnName}1`] = { t: 's', v: typeof col.title === 'object' ? col.title.text : col.title }; for (let dataIdx = 0; dataIdx < dataLen; dataIdx++) { - sheet[`${columnName}${dataIdx + 2}`] = this._stGet(opt.data![dataIdx], col, dataIdx, colIdx); + sheet[`${columnName}${dataIdx + 2}`] = this._stGet(opt.data![dataIdx], col, dataIdx, idx); } } + if (wpx) { + // wpx: width in screen pixels https://github.com/SheetJS/sheetjs#column-properties + sheet['!cols'] = columns.filter(col => !invalidFn(col)).map(col => ({ wpx: col._width })); + } if (validColCount > 0 && dataLen > 0) { sheet['!ref'] = `A1:${this.xlsxSrv.numberToSchema(validColCount)}${dataLen + 1}`; diff --git a/packages/abc/st/test/st-export.spec.ts b/packages/abc/st/test/st-export.spec.ts index 6dc36ec489..7f7a2425ae 100644 --- a/packages/abc/st/test/st-export.spec.ts +++ b/packages/abc/st/test/st-export.spec.ts @@ -101,6 +101,10 @@ describe('abc: table: export', () => { expect(sheet).not.toBeNull(); const cc = columns.filter(w => w.exported !== false && w.index && (!w.buttons || w.buttons.length === 0)); expect(sheet['!ref']).toBe(`A1:${String.fromCharCode(65 + cc.length - 1)}${data.length + 1}`); + expect(sheet['A1'].v).toBe('id'); + expect(sheet['A2'].v).toBe(1); + expect(sheet['B1'].v).toBe('name'); + expect(sheet['B2'].v).toBe('n1'); }); it('should auto specify sheet name [Sheet1]', async () => {