public void SetCellFormula(String formula) { if (IsPartOfArrayFormulaGroup) { NotifyArrayFormulaChanging(); } int row = _record.Row; int col = _record.Column; short styleIndex = _record.XFIndex; if (string.IsNullOrEmpty(formula)) { NotifyFormulaChanging(); SetCellType(CellType.Blank, false, row, col, styleIndex); return; } int sheetIndex = book.GetSheetIndex(_sheet); Ptg[] ptgs = HSSFFormulaParser.Parse(formula, book, FormulaType.Cell, sheetIndex); SetCellType(CellType.Formula, false, row, col, styleIndex); FormulaRecordAggregate agg = (FormulaRecordAggregate)_record; FormulaRecord frec = agg.FormulaRecord; frec.Options = ((short)2); frec.Value = (0); //only set to default if there is no extended format index already set if (agg.XFIndex == (short)0) { agg.XFIndex = ((short)0x0f); } agg.SetParsedExpression(ptgs); }
public void TestArrayFormulas() { int rownum = 4; int colnum = 4; FormulaRecord fr = new FormulaRecord(); fr.Row = (rownum); fr.Column = ((short)colnum); FormulaRecordAggregate agg = new FormulaRecordAggregate(fr, null, SharedValueManager.CreateEmpty()); Ptg[] ptgsForCell = { new ExpPtg(rownum, colnum) }; agg.SetParsedExpression(ptgsForCell); String formula = "SUM(A1:A3*B1:B3)"; Ptg[] ptgs = HSSFFormulaParser.Parse(formula, null, FormulaType.Array, 0); agg.SetArrayFormula(new CellRangeAddress(rownum, rownum, colnum, colnum), ptgs); Assert.IsTrue(agg.IsPartOfArrayFormula); Assert.AreEqual("E5", agg.GetArrayFormulaRange().FormatAsString()); Ptg[] ptg = agg.FormulaTokens; String fmlaSer = FormulaRenderer.ToFormulaString(null, ptg); Assert.AreEqual(formula, fmlaSer); agg.RemoveArrayFormula(rownum, colnum); Assert.IsFalse(agg.IsPartOfArrayFormula); }
internal void SetCellArrayFormula(CellRangeAddress range) { int row = _record.Row; int col = _record.Column; short styleIndex = _record.XFIndex; SetCellType(CellType.Formula, false, row, col, styleIndex); // Billet for formula in rec Ptg[] ptgsForCell = { new ExpPtg(range.FirstRow, range.FirstColumn) }; FormulaRecordAggregate agg = (FormulaRecordAggregate)_record; agg.SetParsedExpression(ptgsForCell); }