页面树结构

版本比较

标识

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。

...

代码块
linenumberstrue
collapsetrue
function main(spreadsheetReport) {
    var options = Packages.com.aspose.cells.AutoFitterOptions();
    //options.setAutoFitMergedCells(true);
    //spreadsheetReport.workbook.worksheets.get(0).autoFitRows(options);
    var cells = spreadsheetReport.workbook.worksheets.get(0).getCells();
    //一行字体高度
    var perRowHeight = 13.5;,
    //需要调整的单元格,每个单元格中一行多少字
   // var cell_B4perRowCharCount = 17;
   
    var cell_C4perRowCharCount = 17;
    
   // var cell_D4perRowCharCount = 17;
    if (spreadsheetReport.outputType == "PDF" || spreadsheetReport.outputType == "PRINT") {
        logger.debug("pdf");
       //fixExpandCells(cells, "B4", cell_B4perRowCharCount, perRowHeight);
       
       //需要调整的单元格
        fixExpandCells(cells, "C4", cell_C4perRowCharCount, perRowHeight);
        
      //  fixExpandCells(cells, "D4", cell_D4perRowCharCount, perRowHeight);
    }
}
function fixExpandCells(cells, pos, perRowCharCount, perRowHeight) {
    var expandedCells = spreadsheetReport.sheets[0].getExpandedPositions(pos);
    for (var i = 0; i < expandedCells.length; i++) {
        var eCell = expandedCells[i];
        var fixCell = cells.get(eCell.row, eCell.column);
        fixCellHeight(cells, fixCell, perRowCharCount, perRowHeight);
    }
}
function fixCellHeight(cells, cell, perRowCharCount, perRowHeight) {
    var fontSize = cell.getDisplayStyle().getFont().getSize();
    var mergedCount = 1;
    if (cell.getMergedRange() != null) {
        cell.getMergedRange().getColumnCount();
    }
    var cellColumn = cell.getColumn();
    var totalWidth = 0;
    for (var i = 0; i < mergedCount; i++) {
        var colWidth = cells.getColumnWidthPixel(i + cellColumn);
        //logger.debug(i + " is " + colWidth);
        totalWidth += colWidth;
    }
    var value = cell.getValue();
    if (value != null) {
        var values = value.split("\\n");
        var height = 0;
        for (var i = 0; i < values.length; i++) {
            var cellRowCount = (values[i].length() / perRowCharCount);
            var calHeight = Math.ceil(cellRowCount) * perRowHeight;
            height += calHeight;
        }
        var oldHeight = cells.getRowHeight(cell.getRow());
        if (height > oldHeight) {
            if (height < 409) {
                cells.setRowHeight(cell.getRow(), height);
            } else {
                cells.setRowHeight(cell.getRow(), 409);
            }
        }
    }
}

...