/** * used internally -- given a cell value record, figure out its type */ private CellType DetermineType(CellValueRecordInterface cval) { if (cval is FormulaRecordAggregate) { return(CellType.Formula); } Record record = (Record)cval; int sid = record.Sid; switch (sid) { case NumberRecord.sid: return(CellType.Numeric); case BlankRecord.sid: return(CellType.Blank); case LabelSSTRecord.sid: return(CellType.String); case FormulaRecordAggregate.sid: return(CellType.Formula); case BoolErrRecord.sid: BoolErrRecord boolErrRecord = (BoolErrRecord)record; return((boolErrRecord.IsBoolean) ? CellType.Boolean : CellType.Error); } throw new Exception("Bad cell value rec (" + cval.GetType().Name + ")"); }
public void TestError() { byte[] data = HexRead.ReadFromString( "00 00 00 00 0F 00 " + // row, col, xfIndex "07 01 " // #DIV/0!, isError ); RecordInputStream in1 = TestcaseRecordInputStream.Create(BoolErrRecord.sid, data); BoolErrRecord ber = new BoolErrRecord(in1); Assert.IsTrue(ber.IsError); Assert.AreEqual(7, ber.ErrorValue); TestcaseRecordInputStream.ConfirmRecordEncoding(BoolErrRecord.sid, data, ber.Serialize()); }
public void TestOooBadFormat_bug47479() { byte[] data = HexRead.ReadFromString( "05 02 09 00 " + // sid, size "00 00 00 00 0F 00 " + // row, col, xfIndex "01 00 00 " // extra 00 byte here ); RecordInputStream in1 = TestcaseRecordInputStream.Create(data); BoolErrRecord ber = new BoolErrRecord(in1); bool hasMore; try { hasMore = in1.HasNextRecord; } catch (LeftoverDataException e) { if ("Initialisation of record 0x205 left 1 bytes remaining still to be Read.".Equals(e.Message)) { throw new AssertFailedException("Identified bug 47479"); } throw e; } Assert.IsFalse(hasMore); Assert.IsTrue(ber.IsBoolean); Assert.AreEqual(true, ber.BooleanValue); // Check that the record re-Serializes correctly byte[] outData = ber.Serialize(); byte[] expData = HexRead.ReadFromString( "05 02 08 00 " + "00 00 00 00 0F 00 " + "01 00 " // normal number of data bytes ); Assert.IsTrue(Arrays.Equals(expData, outData)); }
/// <summary> /// Sets the cell type. The SetValue flag indicates whether to bother about /// trying to preserve the current value in the new record if one is Created. /// The SetCellValue method will call this method with false in SetValue /// since it will overWrite the cell value later /// </summary> /// <param name="cellType">Type of the cell.</param> /// <param name="setValue">if set to <c>true</c> [set value].</param> /// <param name="row">The row.</param> /// <param name="col">The col.</param> /// <param name="styleIndex">Index of the style.</param> private void SetCellType(CellType cellType, bool setValue, int row, int col, short styleIndex) { if (cellType > CellType.Error) { throw new Exception("I have no idea what type that Is!"); } switch (cellType) { case CellType.Formula: FormulaRecordAggregate frec = null; if (cellType != this.cellType) { frec = _sheet.Sheet.RowsAggregate.CreateFormula(row, col); } else { frec = (FormulaRecordAggregate)_record; } frec.Column = col; if (setValue) { frec.FormulaRecord.Value = NumericCellValue; } frec.XFIndex = styleIndex; frec.Row = row; _record = frec; break; case CellType.Numeric: NumberRecord nrec = null; if (cellType != this.cellType) { nrec = new NumberRecord(); } else { nrec = (NumberRecord)_record; } nrec.Column = col; if (setValue) { nrec.Value = NumericCellValue; } nrec.XFIndex = styleIndex; nrec.Row = row; _record = nrec; break; case CellType.String: LabelSSTRecord lrec = null; if (cellType != this.cellType) { lrec = new LabelSSTRecord(); } else { lrec = (LabelSSTRecord)_record; } lrec.Column = col; lrec.Row = row; lrec.XFIndex = styleIndex; if (setValue) { String str = ConvertCellValueToString(); int sstIndex = book.Workbook.AddSSTString(new UnicodeString(str)); lrec.SSTIndex = (sstIndex); UnicodeString us = book.Workbook.GetSSTString(sstIndex); stringValue = new HSSFRichTextString(); stringValue.UnicodeString = us; } _record = lrec; break; case CellType.Blank: BlankRecord brec = null; if (cellType != this.cellType) { brec = new BlankRecord(); } else { brec = (BlankRecord)_record; } brec.Column = col; // During construction the cellStyle may be null for a Blank cell. brec.XFIndex = styleIndex; brec.Row = row; _record = brec; break; case CellType.Boolean: BoolErrRecord boolRec = null; if (cellType != this.cellType) { boolRec = new BoolErrRecord(); } else { boolRec = (BoolErrRecord)_record; } boolRec.Column = col; if (setValue) { boolRec.SetValue(ConvertCellValueToBoolean()); } boolRec.XFIndex = styleIndex; boolRec.Row = row; _record = boolRec; break; case CellType.Error: BoolErrRecord errRec = null; if (cellType != this.cellType) { errRec = new BoolErrRecord(); } else { errRec = (BoolErrRecord)_record; } errRec.Column = col; if (setValue) { errRec.SetValue((byte)HSSFErrorConstants.ERROR_VALUE); } errRec.XFIndex = styleIndex; errRec.Row = row; _record = errRec; break; } if (cellType != this.cellType && this.cellType != CellType.Unknown) // Special Value to indicate an Uninitialized Cell { _sheet.Sheet.ReplaceValueRecord(_record); } this.cellType = cellType; }
private Biff GetCorrectRecord(GenericBiff record, Stream stream, SstRecord sst) { Biff ret = record; switch (record.Id) { case (ushort)RecordType.Bof: BofRecord bof = new BofRecord(record); if (bof.Version < 0x0600) { throw new Exception("Versions below Excel 97/2000 are currently not supported."); } ret = bof; break; case (ushort)RecordType.Boundsheet: ret = new BoundSheetRecord(record); break; case (ushort)RecordType.Index: ret = new IndexRecord(record); break; case (ushort)RecordType.DbCell: ret = new DbCellRecord(record); break; case (ushort)RecordType.Row: ret = new RowRecord(record); break; case (ushort)RecordType.Continue: ret = new ContinueRecord(record); break; case (ushort)RecordType.Blank: ret = new BlankRecord(record); break; case (ushort)RecordType.BoolErr: ret = new BoolErrRecord(record); break; case (ushort)RecordType.Formula: ret = new FormulaRecord(record, stream); break; case (ushort)RecordType.Label: ret = new LabelRecord(record); break; case (ushort)RecordType.LabelSst: ret = new LabelSstRecord(record, sst); break; case (ushort)RecordType.MulBlank: ret = new MulBlankRecord(record); break; case (ushort)RecordType.MulRk: ret = new MulRkRecord(record); break; case (ushort)RecordType.String: ret = new StringValueRecord(record); break; case (ushort)RecordType.Xf: ret = new XfRecord(record); break; case (ushort)RecordType.Rk: ret = new RkRecord(record); break; case (ushort)RecordType.Number: ret = new NumberRecord(record); break; case (ushort)RecordType.Array: ret = new ArrayRecord(record); break; case (ushort)RecordType.ShrFmla: ret = new SharedFormulaRecord(record); break; case (ushort)RecordType.Table: ret = new TableRecord(record); break; case (ushort)RecordType.Sst: ret = new SstRecord(record, stream); break; case (ushort)RecordType.Eof: ret = new EofRecord(record); break; case (ushort)RecordType.Font: ret = new FontRecord(record); break; case (ushort)RecordType.Format: ret = new Net.SourceForge.Koogra.Excel.Records.FormatRecord(record); break; case (ushort)RecordType.Palette: ret = new PaletteRecord(record); break; case (ushort)RecordType.Hyperlink: ret = new HyperLinkRecord(record); break; } return(ret); }