Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(abc:st): fix export mismatch #1740

Merged
merged 1 commit into from Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 13 additions & 11 deletions packages/abc/st/st-export.ts
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
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