Skip to content

Commit 91cbf84

Browse files
committed
feat: add optimization parameter to exporting excel
1 parent 2e9093e commit 91cbf84

3 files changed

Lines changed: 42 additions & 12 deletions

File tree

docs/assets/guide/en/export/excel.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,12 @@ const excelOption = {
137137
};
138138
await downloadExcel(await exportVTableToExcel(tableInstance, excelOption));
139139
```
140+
141+
### equestIdleCallback
142+
143+
`@visactor/vtable-export` uses the `exceljs` library as a tool for exporting Excel files. If you need tosolve the impact on page performance during the export process, you can set the `optimization` parameter to enable `requestIdleCallback`.
144+
145+
```js
146+
const excelOption = {};
147+
await downloadExcel(await exportVTableToExcel(tableInstance, excelOption, true));
148+
```

docs/assets/guide/zh/export/excel.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,12 @@ const excelOption = {
137137
};
138138
await downloadExcel(await exportVTableToExcel(tableInstance, excelOption));
139139
```
140+
141+
### equestIdleCallback
142+
143+
`@visactor/vtable-export`使用`exceljs`库作为导出 Excel 文件的工具,如果需要解决导出对页面性能影响,可以设置`requestIdleCallback`的启用参数`optimization`
144+
145+
```js
146+
const excelOption = {};
147+
await downloadExcel(await exportVTableToExcel(tableInstance, excelOption, true));
148+
```

packages/vtable-export/src/excel/index.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ export type ExportVTableToExcelOptions = {
3333
skipImageExportCellType?: SkipImageExportCellType[];
3434
};
3535

36-
export async function exportVTableToExcel(tableInstance: IVTable, options?: ExportVTableToExcelOptions) {
36+
function requestIdleCallbackPromise(options?: IdleRequestOptions) {
37+
return new Promise<IdleDeadline>((resolve) => {
38+
requestIdleCallback((deadline) => {
39+
resolve(deadline);
40+
}, options);
41+
});
42+
}
43+
44+
export async function exportVTableToExcel(tableInstance: IVTable, options?: ExportVTableToExcelOptions, optimization = false) {
3745
const workbook = new ExcelJS.Workbook();
3846
const worksheet = workbook.addWorksheet('sheet1');
3947
worksheet.properties.defaultRowHeight = 40;
@@ -49,9 +57,9 @@ export async function exportVTableToExcel(tableInstance: IVTable, options?: Expo
4957
const SLICE_SIZE = 100;
5058
let currentRow = minRow;
5159

52-
function processSlice(deadline: IdleDeadline) {
60+
function processSlice(deadline?: IdleDeadline) {
5361
return new Promise<void>(async resolve => {
54-
while (currentRow <= maxRow && deadline.timeRemaining() > 0) {
62+
while (currentRow <= maxRow && (!optimization || (deadline?.timeRemaining() > 0))) {
5563
const endRow = Math.min(currentRow + SLICE_SIZE - 1, maxRow);
5664
for (let col = minCol; col <= maxCol; col++) {
5765
const colWidth = tableInstance.getColWidth(col);
@@ -84,19 +92,23 @@ export async function exportVTableToExcel(tableInstance: IVTable, options?: Expo
8492
if (currentRow > maxRow) {
8593
resolve();
8694
} else {
87-
requestIdleCallback(async nextDeadline => {
88-
await processSlice(nextDeadline);
89-
resolve();
90-
});
95+
let nextDeadline: IdleDeadline | undefined;
96+
if (optimization) {
97+
nextDeadline = await requestIdleCallbackPromise()
98+
}
99+
await processSlice(nextDeadline);
100+
resolve();
91101
}
92102
});
93103
}
94104

95-
await new Promise<void>(resolve => {
96-
requestIdleCallback(async deadline => {
97-
await processSlice(deadline);
98-
resolve();
99-
});
105+
await new Promise<void>(async resolve => {
106+
let deadline: IdleDeadline | undefined;
107+
if (optimization) {
108+
deadline = await requestIdleCallbackPromise()
109+
}
110+
await processSlice(deadline);
111+
resolve();
100112
});
101113

102114
worksheet.columns = columns;

0 commit comments

Comments
 (0)