setText method

void setText(
  1. String? text
)

Set text value to the range.

Workbook workbook = new Workbook();
Worksheet sheet = workbook.worksheets[0];
Range range = sheet.getRangeByName('A1');
range.setText('Hello');
List<int> bytes = workbook.saveAsStream();
File('Text.xlsx').writeAsBytes(bytes);
workbook.dispose();

Implementation

void setText(String? text) {
  if (text != null) {
    int sharedStringIndex = 0;
    if (workbook._sharedStrings.containsKey(text)) {
      sharedStringIndex = workbook._sharedStrings[text]!;
    } else {
      sharedStringIndex = workbook._sharedStrings.length;
      workbook._sharedStrings[text] = sharedStringIndex;
      workbook._sharedStringCount++;
    }
    if (isSingleRange) {
      _text = text;
      type = CellType.text;
      _saveType = 's';
      _textIndex = sharedStringIndex;
      _setRange();
      if (text.contains('\n')) {
        worksheet.getRangeByIndex(row, column).cellStyle.wrapText = true;
      }
    } 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).text = text;
          if (text.contains('\n')) {
            worksheet.getRangeByIndex(iRow, iCol).cellStyle.wrapText = true;
          }
        }
      }
    }
  }
}