-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathtable-export.ts
More file actions
102 lines (98 loc) · 3.43 KB
/
table-export.ts
File metadata and controls
102 lines (98 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import type { pluginsDefinition, ListTable } from '@visactor/vtable';
import { TABLE_EVENT_TYPE } from '@visactor/vtable';
import type { ExportVTableToCsvOptions, ExportVTableToExcelOptions } from './table-export/index';
import {
exportVTableToCsv,
exportVTableToExcel,
downloadCsv,
downloadExcel,
exportMultipleVTablesToExcel
} from './table-export/index';
// // 扩展ListTable接口以包含导出方法
// declare module '@visactor/vtable' {
// interface ListTableAll {
// exportToCsv: (options?: any) => void;
// exportToExcel: (options?: any) => void;
// }
// }
export type TableExportPluginOptions = {
exportExcelOptions?: ExportVTableToExcelOptions;
exportCsvOptions?: ExportVTableToCsvOptions;
exportOnIdle?: boolean; //是否空闲时导出下载,默认false
};
/**
* 表格导出插件
* 提供CSV和Excel格式的表格数据导出功能
*/
export class TableExportPlugin implements pluginsDefinition.IVTablePlugin {
id = 'table-export-plugin';
name = 'TableExport';
runTime: any[] = [TABLE_EVENT_TYPE.INITIALIZED];
private table: ListTable;
private pluginOptions: TableExportPluginOptions;
constructor(pluginOptions: TableExportPluginOptions) {
this.pluginOptions = Object.assign(
{
exportOnIdle: false,
exportExcelOptions: { downloadFile: true, fileName: 'export' },
exportCsvOptions: { downloadFile: true, fileName: 'export' }
},
pluginOptions
);
}
/**
* 插件初始化方法
* @param tableInstance - 表格实例
*/
run(...args: any[]) {
// const eventArgs = args[0];
const runTime = args[1];
if (runTime === TABLE_EVENT_TYPE.INITIALIZED) {
const eventArgs = args[0];
this.table = args[2];
// 挂载导出方法到表格实例
(this.table as any).exportToCsv = () => {
const options = this.pluginOptions.exportCsvOptions;
if (options.downloadFile) {
return downloadCsv(exportVTableToCsv(this.table, options), options.fileName || 'export');
}
return exportVTableToCsv(this.table, options);
};
(this.table as any).exportToExcel = async () => {
const options = this.pluginOptions.exportExcelOptions;
if (options.downloadFile) {
return await downloadExcel(
(await exportVTableToExcel(this.table, options, this.pluginOptions.exportOnIdle)) as ArrayBuffer,
options.fileName || 'export'
);
}
return exportVTableToExcel(this.table, options, this.pluginOptions.exportOnIdle);
};
if ((this.table as any).__vtableSheet) {
// 给VTableSheet实例添加导出所有sheet到Excel的方法
if (!((this.table as any).__vtableSheet as any)._exportMutipleTablesToExcel) {
((this.table as any).__vtableSheet as any)._exportMutipleTablesToExcel = async (
tables: Array<{ table: any; name?: string }>
) => {
const buffer = (await exportMultipleVTablesToExcel(
tables,
this.pluginOptions.exportExcelOptions
)) as ArrayBuffer;
await downloadExcel(buffer, this.pluginOptions.exportExcelOptions.fileName || 'vtable-sheet-export');
};
}
}
}
}
/**
* 插件销毁方法
* 清理挂载到表格实例的方法
*/
release() {
if (this.table) {
delete (this.table as any).exportToCsv;
delete (this.table as any).exportToExcel;
this.table = null;
}
}
}