From 5f53609e9e5a115079b3065a4d87814ffb4bdd11 Mon Sep 17 00:00:00 2001 From: LY Date: Sat, 5 Aug 2023 12:55:35 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81options=E5=A2=9E=E5=8A=A0minRowLength?= =?UTF-8?q?=20(=E6=9C=89minColLength=20=E4=BD=86=E6=B2=A1=E6=9C=89minRowLe?= =?UTF-8?q?ngth=20=E8=AF=B4=E4=B8=8D=E8=BF=87=E5=8E=BB=E5=90=A7)=202?= =?UTF-8?q?=E3=80=81options=E5=A2=9E=E5=8A=A0filterRows=E3=80=81filterCols?= =?UTF-8?q?=E3=80=81=E5=87=BD=E6=95=B0,=E8=BF=87=E6=BB=A4=E5=87=BA?= =?UTF-8?q?=E6=83=B3=E8=A6=81=E6=98=BE=E7=A4=BA=E7=9A=84=E8=A1=8C=E5=92=8C?= =?UTF-8?q?=E5=88=97=203=E3=80=81options=E7=9A=84widthOffset=E3=80=81heigh?= =?UTF-8?q?tOffset=20=E4=B9=9F=E5=8F=AF=E4=BB=A5=E6=98=AF=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=88=E7=89=B9=E5=AE=9A=E7=9A=84=E8=A1=8C=E5=88=97?= =?UTF-8?q?=E7=9A=84=E5=AE=BD=E9=AB=98=E5=81=8F=E7=A7=BB=E5=80=BC=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E4=B8=8D=E5=90=8C=EF=BC=89=204=E3=80=81options?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0getCellText,(=E5=86=85=E7=BD=AE=E7=9A=84getCe?= =?UTF-8?q?llText=E5=87=BD=E6=95=B0=E8=80=83=E8=99=91=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8D=E8=B6=B3=E5=BA=94=E5=AF=B9=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=83=85=E5=86=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/packages/vue-excel/src/excel.js | 73 ++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/core/packages/vue-excel/src/excel.js b/core/packages/vue-excel/src/excel.js index 554e99b..f1192bc 100644 --- a/core/packages/vue-excel/src/excel.js +++ b/core/packages/vue-excel/src/excel.js @@ -131,16 +131,24 @@ export function readExcelData(buffer){ function transferColumns(excelSheet, spreadSheet, options){ - for(let i = 0;i < (excelSheet.columns || []).length; i++){ + // for(let i = 0;i < (excelSheet.columns || []).length; i++){ + // spreadSheet.cols[i.toString()] = {}; + // if(excelSheet.columns[i].width) { + // spreadSheet.cols[i.toString()].width = excelSheet.columns[i].width * 6 + (options.widthOffset || 0); + // } else { + // spreadSheet.cols[i.toString()].width = defaultColWidth + (options.widthOffset || 0); + // } + // } + + // spreadSheet.cols.len = Math.max(Object.keys(spreadSheet.cols).length, options.minColLength || 0); + + const colsNewT = filter_with(excelSheet.columns || [],options.filterCols); + const lenNewT = Object.keys(colsNewT).length; + for (let i = 0; i < lenNewT; i++){ spreadSheet.cols[i.toString()] = {}; - if(excelSheet.columns[i].width) { - spreadSheet.cols[i.toString()].width = excelSheet.columns[i].width * 6 + (options.widthOffset || 0); - } else { - spreadSheet.cols[i.toString()].width = defaultColWidth + (options.widthOffset || 0); - } + spreadSheet.cols[i.toString()].width = getWidth_withOffset(colsNewT[i],defaultColWidth,options); } - - spreadSheet.cols.len = Math.max(Object.keys(spreadSheet.cols).length, options.minColLength || 0); + spreadSheet.cols.len = Math.max(lenNewT,options.minColLength || 0); } function getCellText(cell){ @@ -293,6 +301,32 @@ function getStyle(cell){ return cell.style; } +function getHeight_withOffset(rowRaw,heightDef,options,){ + const srcT = rowRaw.height ? rowRaw.height : heightDef; + let offsetT = 0; + if(typeof options.heightOffset == 'function') + offsetT = options.heightOffset(rowRaw); + else if(options.heightOffset) + offsetT = options.heightOffset; + return srcT + offsetT; +} + +function getWidth_withOffset(colRaw,widthDef,options,){ + const srcT = colRaw.width ? colRaw.width*6 : widthDef; + let offsetT = 0; + if(typeof options.widthOffset == 'function') + offsetT = options.widthOffset(colRaw); + else if(options.widthOffset) + offsetT = options.widthOffset; + return srcT + offsetT; +} + +function filter_with(arg,funcT,){ + if(funcT) + return funcT(arg); + return arg; +} + export function transferExcelToSpreadSheet(workbook, options){ let workbookData = []; // console.log(workbook, 'workbook') @@ -318,14 +352,17 @@ export function transferExcelToSpreadSheet(workbook, options){ transferColumns(sheet,sheetData, options); // 遍历行 - (sheet._rows || []).forEach((row,spreadSheetRowIndex) =>{ + //(sheet._rows || []).forEach((row,spreadSheetRowIndex) =>{ + const rowsNewT = filter_with(sheet._rows || [], options.filterRows); + (rowsNewT).forEach((row,spreadSheetRowIndex) =>{ sheetData.rows[spreadSheetRowIndex] = { cells: {} }; - if(row.height){ - sheetData.rows[spreadSheetRowIndex].height = row.height + (options.heightOffset || 0); - }else{ - sheetData.rows[spreadSheetRowIndex].height = defaultRowHeight + (options.heightOffset || 0); - } + // if(row.height){ + // sheetData.rows[spreadSheetRowIndex].height = row.height + (options.heightOffset || 0); + // }else{ + // sheetData.rows[spreadSheetRowIndex].height = defaultRowHeight + (options.heightOffset || 0); + // } + sheetData.rows[spreadSheetRowIndex].height = getHeight_withOffset(row,defaultRowHeight,options) //includeEmpty = false 不包含空白单元格 (row._cells || []).forEach((cell, spreadSheetColIndex) =>{ sheetData.rows[spreadSheetRowIndex].cells[spreadSheetColIndex] = {}; @@ -337,7 +374,10 @@ export function transferExcelToSpreadSheet(workbook, options){ if(mergeAddress){ sheetData.rows[spreadSheetRowIndex].cells[spreadSheetColIndex].merge = [mergeAddress.YRange, mergeAddress.XRange]; } - sheetData.rows[spreadSheetRowIndex].cells[spreadSheetColIndex].text = getCellText(cell); + //sheetData.rows[spreadSheetRowIndex].cells[spreadSheetColIndex].text = getCellText(cell); + const cellText = options.getCellText ? options.getCellText(cell): getCellText(cell); + sheetData.rows[spreadSheetRowIndex].cells[spreadSheetColIndex].text = cellText; + sheetData.styles.push(getStyle(cell)); sheetData.rows[spreadSheetRowIndex].cells[spreadSheetColIndex].style = sheetData.styles.length - 1; }); @@ -345,7 +385,8 @@ export function transferExcelToSpreadSheet(workbook, options){ if(sheetData._media){ sheetData.media = sheetData._media; } - sheetData.rows.len = Math.max(Object.keys(sheetData.rows).length, 100); + //sheetData.rows.len = Math.max(Object.keys(sheetData.rows).length, 100); + sheetData.rows.len = Math.max(Object.keys(rowsNewT).length, options.minRowLength || 0); workbookData.push(sheetData); }); // console.log(workbookData, 'workbookData')