/// <summary> /// Creates a non shared formula from the shared formula counterpart /// </summary> /// <param name="si">Shared Group Index</param> /// <param name="fpb"></param> /// <returns>non shared formula created for the given shared formula and this cell</returns> private String ConvertSharedFormula(int si, XSSFEvaluationWorkbook fpb) { XSSFSheet sheet = (XSSFSheet)Sheet; CT_CellFormula f = sheet.GetSharedFormula(si); if (f == null) { throw new InvalidOperationException( "Master cell of a shared formula with sid=" + si + " was not found"); } String sharedFormula = f.Value; //Range of cells which the shared formula applies to String sharedFormulaRange = f.@ref; CellRangeAddress ref1 = CellRangeAddress.ValueOf(sharedFormulaRange); int sheetIndex = sheet.Workbook.GetSheetIndex(sheet); SharedFormula sf = new SharedFormula(SpreadsheetVersion.EXCEL2007); Ptg[] ptgs = FormulaParser.Parse(sharedFormula, fpb, FormulaType.Cell, sheetIndex, RowIndex); Ptg[] fmla = sf.ConvertSharedFormulas(ptgs, RowIndex - ref1.FirstRow, ColumnIndex - ref1.FirstColumn); return(FormulaRenderer.ToFormulaString(fpb, fmla)); }
private string ConvertSharedFormula(int si) { XSSFSheet sheet = (XSSFSheet)this.Sheet; CT_CellFormula sharedFormula = sheet.GetSharedFormula(si); if (sharedFormula == null) { throw new InvalidOperationException("Master cell of a shared formula with sid=" + (object)si + " was not found"); } string formula = sharedFormula.Value; CellRangeAddress cellRangeAddress = CellRangeAddress.ValueOf(sharedFormula.@ref); int sheetIndex = sheet.Workbook.GetSheetIndex((ISheet)sheet); XSSFEvaluationWorkbook evaluationWorkbook = XSSFEvaluationWorkbook.Create(sheet.Workbook); Ptg[] ptgs = new SharedFormula(SpreadsheetVersion.EXCEL2007).ConvertSharedFormulas(FormulaParser.Parse(formula, (IFormulaParsingWorkbook)evaluationWorkbook, FormulaType.CELL, sheetIndex), this.RowIndex - cellRangeAddress.FirstRow, this.ColumnIndex - cellRangeAddress.FirstColumn); return(FormulaRenderer.ToFormulaString((IFormulaRenderingWorkbook)evaluationWorkbook, ptgs)); }
/// <summary> /// Update the formulas in specified row using the formula shifting policy specified by shifter /// </summary> /// <param name="row">the row to update the formulas on</param> /// <param name="Shifter">the formula shifting policy</param> public override void UpdateRowFormulas(IRow row, FormulaShifter Shifter) { XSSFSheet sheet = (XSSFSheet)row.Sheet; foreach (ICell c in row) { XSSFCell cell = (XSSFCell)c; CT_Cell ctCell = cell.GetCTCell(); if (ctCell.IsSetF()) { CT_CellFormula f = ctCell.f; String formula = f.Value; if (formula.Length > 0) { String ShiftedFormula = ShiftFormula(row, formula, Shifter); if (ShiftedFormula != null) { f.Value = (ShiftedFormula); if (f.t == ST_CellFormulaType.shared) { int si = (int)f.si; CT_CellFormula sf = sheet.GetSharedFormula(si); sf.Value = (ShiftedFormula); } } } if (f.isSetRef()) { //Range of cells which the formula applies to. String ref1 = f.@ref; String ShiftedRef = ShiftFormula(row, ref1, Shifter); if (ShiftedRef != null) { f.@ref = ShiftedRef; } } } } }