setFormula method

void setFormula(
  1. String? formula
)

Set formula value to the range.

Workbook workbook = new Workbook();
Worksheet sheet = workbook.worksheets[0];
Range range = sheet.getRangeByName('C1');
range.setFormula('=A1+B1');
List<int> bytes = workbook.saveAsStream();
File('Formula.xlsx').writeAsBytes(bytes);
workbook.dispose();

Implementation

void setFormula(String? formula) {
  if (formula != null) {
    if (isSingleRange) {
      if (formula[0] != '=') {
        formula = '=$formula';
      }
      _formula = formula;
      type = CellType.formula;
      _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).formula = formula;
        }
      }
    }
  }
}