public void TestAddRemoveArrayFormulas_recordUpdates() { IWorkbook wb = new HSSFWorkbook(); ISheet s = wb.CreateSheet("Sheet1"); ICellRange <ICell> cr = s.SetArrayFormula("123", CellRangeAddress.ValueOf("B5:C6")); Record[] recs; int ix; recs = RecordInspector.GetRecords(s, 0); ix = FindRecordOfType(recs, typeof(ArrayRecord), 0); ConfirmRecordClass(recs, ix - 1, typeof(FormulaRecord)); ConfirmRecordClass(recs, ix + 1, typeof(FormulaRecord)); ConfirmRecordClass(recs, ix + 2, typeof(FormulaRecord)); ConfirmRecordClass(recs, ix + 3, typeof(FormulaRecord)); // just one array record Assert.IsTrue(FindRecordOfType(recs, typeof(ArrayRecord), ix + 1) < 0); s.RemoveArrayFormula(cr.TopLeftCell); // Make sure the array formula has been Removed properly recs = RecordInspector.GetRecords(s, 0); Assert.IsTrue(FindRecordOfType(recs, typeof(ArrayRecord), 0) < 0); Assert.IsTrue(FindRecordOfType(recs, typeof(FormulaRecord), 0) < 0); RowRecordsAggregate rra = ((HSSFSheet)s).Sheet.RowsAggregate; SharedValueManager svm = TestSharedValueManager.ExtractFromRRA(rra); if (svm.GetArrayRecord(4, 1) != null) { throw new AssertionException("Array record was not cleaned up properly."); } }
public void TestExtraStringRecord_bug46213() { FormulaRecord fr = new FormulaRecord(); fr.Value = (2.0); StringRecord sr = new StringRecord(); sr.String = ("NA"); SharedValueManager svm = SharedValueManager.CreateEmpty(); FormulaRecordAggregate fra; try { fra = new FormulaRecordAggregate(fr, sr, svm); } catch (RecordFormatException e) { if ("String record was supplied but formula record flag is not set".Equals(e.Message)) { throw new AssertionException("Identified bug 46213"); } throw e; } TestCases.HSSF.UserModel.RecordInspector.RecordCollector rc = new TestCases.HSSF.UserModel.RecordInspector.RecordCollector(); fra.VisitContainedRecords(rc); Record[] vraRecs = rc.Records; Assert.AreEqual(1, vraRecs.Length); Assert.AreEqual(fr, vraRecs[0]); }
public void TestUnknownContinue_bug46280() { byte[] dummtydata = Encoding.GetEncoding(1252).GetBytes("dummydata"); byte[] moredummydata = Encoding.GetEncoding(1252).GetBytes("moredummydata"); Record[] inRecs = { new RowRecord(0), new NumberRecord(), new UnknownRecord(0x5555,dummtydata), new ContinueRecord(moredummydata) //new UnknownRecord(0x5555, "dummydata".getBytes()), //new ContinueRecord("moredummydata".getBytes()), }; RecordStream rs = new RecordStream(Arrays.AsList(inRecs), 0); RowRecordsAggregate rra; try { rra = new RowRecordsAggregate(rs, SharedValueManager.CreateEmpty()); } catch (RuntimeException e) { if (e.Message.StartsWith("Unexpected record type")) { Assert.Fail("Identified bug 46280a"); } throw e; } RecordInspector.RecordCollector rv = new RecordInspector.RecordCollector(); rra.VisitContainedRecords(rv); Record[] outRecs = rv.Records; Assert.AreEqual(5, outRecs.Length); }
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); }
private RowRecordsAggregate(SharedValueManager svm) { _rowRecords = new SortedList(); _valuesAgg = new ValueRecordsAggregate(); _unknownRecords = new List<Record>(); _sharedValueManager = svm; }
public void TestBasic() { FormulaRecord f = new FormulaRecord(); f.SetCachedResultTypeString(); StringRecord s = new StringRecord(); s.String = ("abc"); FormulaRecordAggregate fagg = new FormulaRecordAggregate(f, s, SharedValueManager.CreateEmpty()); Assert.AreEqual("abc", fagg.StringValue); }
private void ConstructValueRecord(IList records) { RowBlocksReader rbr = new RowBlocksReader(new RecordStream(records, 0)); SharedValueManager sfrh = rbr.SharedFormulaManager; RecordStream rs = rbr.PlainRecordStream; while (rs.HasNext()) { Record rec = rs.GetNext(); valueRecord.Construct((CellValueRecordInterface)rec, rs, sfrh); } }
/// <summary> /// Initializes a new instance of the <see cref="FormulaRecordAggregate"/> class. /// </summary> /// <param name="formulaRec">The formula rec.</param> /// <param name="stringRec">The string rec.</param> /// <param name="svm">The SVM.</param> public FormulaRecordAggregate(FormulaRecord formulaRec, StringRecord stringRec, SharedValueManager svm) { if (svm == null) { throw new ArgumentException("sfm must not be null"); } if (formulaRec.HasCachedResultString) { if (stringRec == null) { throw new RecordFormatException("Formula record flag is set but String record was not found"); } _stringRecord = stringRec; } else { // Usually stringRec is null here (in agreement with what the formula rec says). // In the case where an extra StringRecord is erroneously present, Excel (2007) // ignores it (see bug 46213). _stringRecord = null; } _formulaRecord = formulaRec; _sharedValueManager = svm; if (formulaRec.IsSharedFormula) { CellReference firstCell = formulaRec.Formula.ExpReference; if (firstCell == null) { HandleMissingSharedFormulaRecord(formulaRec); } else { _sharedFormulaRecord = svm.LinkSharedFormulaRecord(firstCell, this); } } }
/** * Also collects any loose MergeCellRecords and puts them in the supplied * mergedCellsTable */ public RowBlocksReader(RecordStream rs) { ArrayList plainRecords = new ArrayList(); ArrayList shFrmRecords = new ArrayList(); ArrayList arrayRecords = new ArrayList(); ArrayList tableRecords = new ArrayList(); ArrayList mergeCellRecords = new ArrayList(); List <CellReference> firstCellRefs = new List <CellReference>(); Record prevRec = null; while (!RecordOrderer.IsEndOfRowBlock(rs.PeekNextSid())) { // End of row/cell records for the current sheet // Note - It is important that this code does not inadvertently add any sheet // records from a subsequent sheet. For example, if SharedFormulaRecords // are taken from the wrong sheet, this could cause bug 44449. if (!rs.HasNext()) { throw new InvalidOperationException("Failed to find end of row/cell records"); } Record rec = rs.GetNext(); ArrayList dest; switch (rec.Sid) { case MergeCellsRecord.sid: dest = mergeCellRecords; break; case SharedFormulaRecord.sid: dest = shFrmRecords; if (!(prevRec is FormulaRecord)) { throw new Exception("Shared formula record should follow a FormulaRecord"); } FormulaRecord fr = (FormulaRecord)prevRec; firstCellRefs.Add(new CellReference(fr.Row, fr.Column)); break; case ArrayRecord.sid: dest = arrayRecords; break; case TableRecord.sid: dest = tableRecords; break; default: dest = plainRecords; break; } dest.Add(rec); prevRec = rec; } SharedFormulaRecord[] sharedFormulaRecs = new SharedFormulaRecord[shFrmRecords.Count]; List <ArrayRecord> arrayRecs = new List <ArrayRecord>(arrayRecords.Count); List <TableRecord> tableRecs = new List <TableRecord>(tableRecords.Count); sharedFormulaRecs = (SharedFormulaRecord[])shFrmRecords.ToArray(typeof(SharedFormulaRecord)); CellReference[] firstCells = new CellReference[firstCellRefs.Count]; firstCells = firstCellRefs.ToArray(); arrayRecs = new List <ArrayRecord>((ArrayRecord[])arrayRecords.ToArray(typeof(ArrayRecord))); tableRecs = new List <TableRecord>((TableRecord[])tableRecords.ToArray(typeof(TableRecord))); _plainRecords = plainRecords; _sfm = SharedValueManager.Create(sharedFormulaRecs, firstCells, arrayRecs, tableRecs); _mergedCellsRecords = new MergeCellsRecord[mergeCellRecords.Count]; _mergedCellsRecords = (MergeCellsRecord[])mergeCellRecords.ToArray(typeof(MergeCellsRecord)); }
/** * Also collects any loose MergeCellRecords and puts them in the supplied * mergedCellsTable */ public RowBlocksReader(RecordStream rs) { ArrayList plainRecords = new ArrayList(); ArrayList shFrmRecords = new ArrayList(); ArrayList arrayRecords = new ArrayList(); ArrayList tableRecords = new ArrayList(); ArrayList mergeCellRecords = new ArrayList(); List<CellReference> firstCellRefs = new List<CellReference>(); Record prevRec = null; while (!RecordOrderer.IsEndOfRowBlock(rs.PeekNextSid())) { // End of row/cell records for the current sheet // Note - It is important that this code does not inadvertently add any sheet // records from a subsequent sheet. For example, if SharedFormulaRecords // are taken from the wrong sheet, this could cause bug 44449. if (!rs.HasNext()) { throw new InvalidOperationException("Failed to find end of row/cell records"); } Record rec = rs.GetNext(); ArrayList dest; switch (rec.Sid) { case MergeCellsRecord.sid: dest = mergeCellRecords; break; case SharedFormulaRecord.sid: dest = shFrmRecords; if (!(prevRec is FormulaRecord)) { throw new Exception("Shared formula record should follow a FormulaRecord"); } FormulaRecord fr = (FormulaRecord)prevRec; firstCellRefs.Add(new CellReference(fr.Row, fr.Column)); break; case ArrayRecord.sid: dest = arrayRecords; break; case TableRecord.sid: dest = tableRecords; break; default: dest = plainRecords; break; } dest.Add(rec); prevRec = rec; } SharedFormulaRecord[] sharedFormulaRecs = new SharedFormulaRecord[shFrmRecords.Count]; List<ArrayRecord> arrayRecs = new List<ArrayRecord>(arrayRecords.Count); List<TableRecord> tableRecs = new List<TableRecord>(tableRecords.Count); sharedFormulaRecs = (SharedFormulaRecord[])shFrmRecords.ToArray(typeof(SharedFormulaRecord)); CellReference[] firstCells = new CellReference[firstCellRefs.Count]; firstCells=firstCellRefs.ToArray(); arrayRecs = new List<ArrayRecord>((ArrayRecord[])arrayRecords.ToArray(typeof(ArrayRecord))); tableRecs = new List<TableRecord>((TableRecord[])tableRecords.ToArray(typeof(TableRecord))); _plainRecords = plainRecords; _sfm = SharedValueManager.Create(sharedFormulaRecs,firstCells, arrayRecs, tableRecs); _mergedCellsRecords = new MergeCellsRecord[mergeCellRecords.Count]; _mergedCellsRecords = (MergeCellsRecord[])mergeCellRecords.ToArray(typeof(MergeCellsRecord)); }
public void Construct(CellValueRecordInterface rec, RecordStream rs, SharedValueManager sfh) { if (rec is FormulaRecord) { FormulaRecord formulaRec = (FormulaRecord)rec; // read optional cached text value StringRecord cachedText=null; Type nextClass = rs.PeekNextClass(); if (nextClass == typeof(StringRecord)) { cachedText = (StringRecord)rs.GetNext(); } else { cachedText = null; } InsertCell(new FormulaRecordAggregate(formulaRec, cachedText, sfh)); } else { InsertCell(rec); } }
/** * @param rs record stream with all {@link SharedFormulaRecord} * {@link ArrayRecord}, {@link TableRecord} {@link MergeCellsRecord} Records removed */ public RowRecordsAggregate(RecordStream rs, SharedValueManager svm) : this(svm) { while (rs.HasNext()) { Record rec = rs.GetNext(); switch (rec.Sid) { case RowRecord.sid: InsertRow((RowRecord)rec); continue; case DBCellRecord.sid: // end of 'Row Block'. Should only occur after cell records // ignore DBCELL records because POI generates them upon re-serialization continue; } if (rec is UnknownRecord) { // might need to keep track of where exactly these belong AddUnknownRecord((UnknownRecord)rec); while (rs.PeekNextSid() == ContinueRecord.sid) { AddUnknownRecord(rs.GetNext()); } continue; } if (rec is MulBlankRecord) { _valuesAgg.AddMultipleBlanks((MulBlankRecord) rec); continue; } if (!(rec is CellValueRecordInterface)) { throw new InvalidOperationException("Unexpected record type (" + rec.GetType().Name + ")"); } _valuesAgg.Construct((CellValueRecordInterface)rec, rs, svm); } }