/** * * @param sheetIden may be <code>null</code> * @param part1 * @param part2 may be <code>null</code> */ private ParseNode CreateAreaRefParseNode(SheetIdentifier sheetIden, SimpleRangePart part1, SimpleRangePart part2) { int extIx; if (sheetIden == null) { extIx = Int32.MinValue; } else { String sName = sheetIden.SheetID.Name; if (sheetIden.BookName == null) { extIx = book.GetExternalSheetIndex(sName); } else { extIx = book.GetExternalSheetIndex(sheetIden.BookName, sName); } } Ptg ptg; if (part2 == null) { CellReference cr = part1.getCellReference(); if (sheetIden == null) { ptg = new RefPtg(cr); } else { ptg = new Ref3DPtg(cr, extIx); } } else { AreaReference areaRef = CreateAreaRef(part1, part2); if (sheetIden == null) { ptg = new AreaPtg(areaRef); } else { ptg = new Area3DPtg(areaRef, extIx); } } return new ParseNode(ptg); }
/** * Creates a non shared formula from the shared formula counterpart<br/> * * Perhaps this functionality could be implemented in terms of the raw * byte array inside {@link Formula}. */ public static Ptg[] ConvertSharedFormulas(Ptg[] ptgs, int formulaRow, int formulaColumn) { Ptg[] newPtgStack = new Ptg[ptgs.Length]; for (int k = 0; k < ptgs.Length; k++) { Ptg ptg = ptgs[k]; byte originalOperandClass = unchecked((byte)-1); if (!ptg.IsBaseToken) { originalOperandClass = ptg.PtgClass; } if (ptg is RefPtgBase) { RefPtgBase refNPtg = (RefPtgBase)ptg; ptg = new RefPtg(FixupRelativeRow(formulaRow, refNPtg.Row, refNPtg.IsRowRelative), FixupRelativeColumn(formulaColumn, refNPtg.Column, refNPtg.IsColRelative), refNPtg.IsRowRelative, refNPtg.IsColRelative); ptg.PtgClass = originalOperandClass; } else if (ptg is AreaPtgBase) { AreaPtgBase areaNPtg = (AreaPtgBase)ptg; ptg = new AreaPtg(FixupRelativeRow(formulaRow, areaNPtg.FirstRow, areaNPtg.IsFirstRowRelative), FixupRelativeRow(formulaRow, areaNPtg.LastRow, areaNPtg.IsLastRowRelative), FixupRelativeColumn(formulaColumn, areaNPtg.FirstColumn, areaNPtg.IsFirstColRelative), FixupRelativeColumn(formulaColumn, areaNPtg.LastColumn, areaNPtg.IsLastColRelative), areaNPtg.IsFirstRowRelative, areaNPtg.IsLastRowRelative, areaNPtg.IsFirstColRelative, areaNPtg.IsLastColRelative); ptg.PtgClass = originalOperandClass; } else if (ptg is OperandPtg) { // Any subclass of OperandPtg is mutable, so it's safest to not share these instances. ptg = ((OperandPtg)ptg).Copy(); } else { // all other Ptgs are immutable and can be shared } newPtgStack[k] = ptg; } return newPtgStack; }
private static CellRangeAddress ShiftRange(FormulaShifter shifter, CellRangeAddress cra, int currentExternSheetIx) { // FormulaShifter works well in terms of Ptgs - so convert CellRangeAddress to AreaPtg (and back) here AreaPtg aptg = new AreaPtg(cra.FirstRow, cra.LastRow, cra.FirstColumn, cra.LastColumn, false, false, false, false); Ptg[] ptgs = { aptg, }; if (!shifter.AdjustFormula(ptgs, currentExternSheetIx)) { return cra; } Ptg ptg0 = ptgs[0]; if (ptg0 is AreaPtg) { AreaPtg bptg = (AreaPtg) ptg0; return new CellRangeAddress(bptg.FirstRow, bptg.LastRow, bptg.FirstColumn, bptg.LastColumn); } if (ptg0 is AreaErrPtg) { return null; } throw new InvalidCastException("Unexpected shifted ptg class (" + ptg0.GetType().Name + ")"); }
/** * Creates a non shared formula from the shared formula * counter part */ public static Ptg[] ConvertSharedFormulas(Ptg[] ptgs, int formulaRow, int formulaColumn) { if (false) { /* * TODO - (May-2008) Stop Converting relative ref Ptgs in shared formula records. * If/when POI Writes out the workbook, this conversion makes an Unnecessary diff in the BIFF records. * Disabling this code breaks one existing junit. * Some fix-up will be required to make Ptg.ToFormulaString(HSSFWorkbook) work properly. * That method will need 2 extra params: rowIx and colIx. */ return ptgs; } Ptg[] newPtgStack = new Ptg[ptgs.Length]; if (ptgs != null) for (int k = 0; k < ptgs.Length; k++) { Ptg ptg = (Ptg)ptgs[k]; byte originalOperandClass = unchecked((byte)-1); if (!ptg.IsBaseToken) { originalOperandClass = ptg.PtgClass; } if (ptg is RefNPtg) { RefNPtg refNPtg = (RefNPtg)ptg; ptg = new RefPtg(FixupRelativeRow(formulaRow, refNPtg.Row, refNPtg.IsRowRelative), FixupRelativeColumn(formulaColumn, refNPtg.Column, refNPtg.IsColRelative), refNPtg.IsRowRelative, refNPtg.IsColRelative); } else if (ptg is AreaNPtg) { AreaNPtg areaNPtg = (AreaNPtg)ptg; ptg = new AreaPtg(FixupRelativeRow(formulaRow, areaNPtg.FirstRow, areaNPtg.IsFirstRowRelative), FixupRelativeRow(formulaRow, areaNPtg.LastRow, areaNPtg.IsLastRowRelative), FixupRelativeColumn(formulaColumn, areaNPtg.FirstColumn, areaNPtg.IsFirstColRelative), FixupRelativeColumn(formulaColumn, areaNPtg.LastColumn, areaNPtg.IsLastColRelative), areaNPtg.IsFirstRowRelative, areaNPtg.IsLastRowRelative, areaNPtg.IsFirstColRelative, areaNPtg.IsLastColRelative); } if (!ptg.IsBaseToken) { ptg.PtgClass=originalOperandClass; } newPtgStack[k] = ptg; } return newPtgStack; }