本帖最后由 Ellia.Duan 于 2024-9-29 15:12 编辑
需要实现批量将xlsx转换为sjs的功能,常规思路是遍历数组,使用import导入,然后再回调中使用save拿到blob数据。现在的问题是操作1000份文件时,执行到300左右的文件save的回调基本就不触发了。无论是使用一个实例还是每个文件一个实例都会卡在300左右的节点
- const input = document.createElement('input')
- input.type = 'file'
- input.multiple = true
- input.accept = '.xlsx'
- input.style.display = 'none'
- document.body.appendChild(input)
- input.click()
- input.onchange = async function(e) {
- // 处理后的buferr数据
- let bufferData: IExcelData[] = []
- let num: number = 0
- // @ts-ignore
- let files: any[] = Array.from(e?.target?.files)
- files = files.map((item, index) => {
- return {
- // @ts-ignore
- spread: new GC.Spread.Sheets.Workbook(String(index)),
- file: item,
- bufferData: null
- }
- })
- async function dealFileData(file: any, spread: any) {
- return new Promise((resolve, reject) => {
- console.log('开始加载');
- spread.suspendPaint();
- spread.import(
- file,
- function () {
- console.log('加载成功');
- spread.export(async function(blob: any){
- console.log('保存成功');
- spread.clearSheets()
- const dataArray: any = await turnBlobToBuffer(blob)
- resolve({
- name: file.name,
- size: file.size,
- dataArray: dataArray
- })
- })
- },
- function () {},
- // @ts-ignore
- { fileType: GC.Spread.Sheets.excel}
- )
- })
- }
-
- for(let i = 0; i < files.length; i++) {
- const res: any = await dealFileData(files[i].file, files[i].spread)
- files[i].spread.destroy()
- files[i] = {
- spread: null,
- file: null,
- bufferData: res
- }
- num = num+1
- console.log('完成进度:' + num + '/' + files.length);
- setInfoStr('完成进度:' + num + '/' + files.length);
- }
- }
复制代码
|
|