autoFitToRow method

void autoFitToRow(
  1. int rowIndex,
  2. int firstColumn,
  3. int lastColumn
)

Autofits row by checking only the cells in the row that are specified by column range.

Implementation

void autoFitToRow(int rowIndex, int firstColumn, int lastColumn) {
  if (firstColumn == 0 || lastColumn == 0 || firstColumn > lastColumn) {
    return;
  }

  final SizeF maxSize = SizeF(0, 0);
  SizeF curSize;
  bool hasRotation = false;
  bool isMergedAndWrapped = false;
  for (int j = firstColumn; j <= lastColumn; j++) {
    if (rows[rowIndex] == null || rows[rowIndex]!.ranges[j] == null) {
      continue;
    }
    final Range range = getRangeByIndex(rowIndex, j);
    if (range.rowSpan > 1) {
      continue;
    }
    final List<dynamic> result =
        _measureCell(range, true, false, isMergedAndWrapped);
    curSize = result[0] as SizeF;
    isMergedAndWrapped = result[1] as bool;
    if (maxSize.sHeight < curSize.sHeight &&
        !(range.number != null &&
            book.standardFontSize == range.cellStyle.fontSize)) {
      maxSize.sHeight = curSize.sHeight;
    }
    if (range.cellStyle.rotation > 0 &&
        maxSize.sHeight < curSize.sWidth &&
        !range.isMerged &&
        !range.cellStyle.wrapText) {
      maxSize.sHeight = curSize.sWidth;
      hasRotation = true;
    }
  }

  if (maxSize.sHeight == 0) {
    maxSize.sHeight =
        book.measureString(_defaultStandardChar, book.fonts[0]).sHeight;
  }

  double newHeight;
  if (!hasRotation) {
    newHeight = book.convertFromPixel(maxSize.sHeight, 6);
  } else {
    newHeight = book.convertFromPixel(maxSize.sHeight + _standardWidth, 6);
  }

  if (newHeight > defaultMaxHeight) {
    newHeight = defaultMaxHeight;
  }

  final Range range = getRangeByIndex(rowIndex, firstColumn);
  if (newHeight > standardHeight) {
    range.setRowHeight(newHeight, isMergedAndWrapped);
  } else {
    range.setRowHeight(standardHeight, isMergedAndWrapped);
  }
}