/** * Helper for {@link #updateColumnWidths(Row)}. * * @param cell the cell to compute the best fit width on * @param pair the column width pair to update * @since 3.14beta1 */ private void UpdateColumnWidth(ICell cell, ColumnWidthPair pair) { double unmergedWidth = SheetUtil.GetCellWidth(cell, defaultCharWidth, dataFormatter, false); double mergedWidth = SheetUtil.GetCellWidth(cell, defaultCharWidth, dataFormatter, true); pair.SetMaxColumnWidths(unmergedWidth, mergedWidth); }
/** * Calculate the best fit width for each tracked column in row * * @param row the row to get the cells * @since 3.14beta1 */ public void UpdateColumnWidths(IRow row) { // track new columns ImplicitlyTrackColumnsInRow(row); // update the widths // for-loop over the shorter of the number of cells in the row and the number of tracked columns // these two for-loops should do the same thing if (maxColumnWidths.Count < row.PhysicalNumberOfCells) { // loop over the tracked columns, because there are fewer tracked columns than cells in this row foreach (var e in maxColumnWidths) { int column = e.Key; ICell cell = row.GetCell(column); //is MissingCellPolicy=Row.RETURN_NULL_AND_BLANK needed? // FIXME: if cell belongs to a merged region, some of the merged region may have fallen outside of the random access window // In this case, getting the column width may result in an error. Need to gracefully handle this. // FIXME: Most cells are not merged, so calling getCellWidth twice re-computes the same value twice. // Need to rewrite this to avoid unnecessary computation if this proves to be a performance bottleneck. if (cell != null) { ColumnWidthPair pair = e.Value; UpdateColumnWidth(cell, pair); } } } else { // loop over the cells in this row, because there are fewer cells in this row than tracked columns foreach (var cell in row) { int column = cell.ColumnIndex; // FIXME: if cell belongs to a merged region, some of the merged region may have fallen outside of the random access window // In this case, getting the column width may result in an error. Need to gracefully handle this. // FIXME: Most cells are not merged, so calling getCellWidth twice re-computes the same value twice. // Need to rewrite this to avoid unnecessary computation if this proves to be a performance bottleneck. if (maxColumnWidths.ContainsKey(column)) { ColumnWidthPair pair = maxColumnWidths[column]; UpdateColumnWidth(cell, pair); } } } }