Skip to content

Commit

Permalink
fix(abc:st): fix export mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Dec 25, 2023
1 parent 83f01ae commit fb5af46
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
24 changes: 13 additions & 11 deletions packages/abc/st/st-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
4 changes: 4 additions & 0 deletions packages/abc/st/test/st-export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit fb5af46

Please sign in to comment.