numberFormat property

String? get numberFormat

Represent the NumberFormat of the Range

Implementation

String? get numberFormat {
  String? format;
  if (isSingleRange) {
    if (!workbook.isSaving) {
      format = (cellStyle as CellStyle).numberFormat;
    } else if (_cellStyle != null) {
      format = (_cellStyle! as CellStyle).numberFormat;
    }
  }

  if (format == null) {
    final List<Range> rangeColection = cells;
    final int iCount = rangeColection.length;

    if (iCount <= 1) {
      return null;
    }

    Range range = rangeColection[0];
    String? strResult;
    strResult = range.numberFormat;
    for (int i = 1; i < iCount; i++) {
      range = rangeColection[i];
      if (strResult != range.numberFormat) {
        return null;
      }
    }

    format = strResult;
  }
  if (format != null && format != '') {
    if (format.contains(r'\\')) {
      format = _checkAndGetDateUncustomizedString(format);
    }

    format = _checkForAccountingString(format);
    format.replaceAllMapped(FormatParser.numberFormatRegex, (Match match) {
      return '';
    });
  }
  return format;
}
set numberFormat (String? value)

Sets number format of cell which is similar to Style.numberFormat property.

Workbook workbook = new Workbook();
Worksheet sheet = workbook.worksheets[0];
Range range = sheet.getRangeByName('A1');
range.value = '1/1/2015';
range.numberFormat = 'dd-MMM-yyyy';
String displayText = range.displayText;
List<int> bytes = workbook.saveAsStream();
File('NumberFormat.xlsx').writeAsBytes(bytes);
workbook.dispose();

Implementation

set numberFormat(String? value) {
  if (isSingleRange) {
    _cellStyle ??= CellStyle(workbook);
    (_cellStyle! as CellStyle).numberFormat = value;
    _isDefaultFormat = false;
    _setRange();
  } else {
    // ignore: prefer_final_locals
    for (int iRow = row, iLastRow = lastRow; iRow <= iLastRow; iRow++) {
      // ignore: prefer_final_locals
      for (int iCol = column, iLastCol = lastColumn;
          iCol <= iLastCol;
          iCol++) {
        worksheet.getRangeByIndex(iRow, iCol).numberFormat = value;
      }
    }
  }
}