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

options #108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 57 additions & 16 deletions core/packages/vue-excel/src/excel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -342,6 +350,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');
Expand All @@ -367,14 +401,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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filterRows的作用是什么,过滤之后后续数据如何处理

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

比如①一个excel文件有100行和100列,但只想预览 第10-20行,第30-40列的内容。
②一个excel文件有内容的行只有10行10列,但exceljs在读取这个文件时,却返回了几万列。
filterRows和filterCols用于从exceljs读取后的行列中取出只想预览的那些行列。

(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] = {};
Expand All @@ -386,15 +423,19 @@ 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;
});
});
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')
Expand Down