Esempio n. 1
0
        public void TestBasicDeserializeReserialize()
        {
            ExternalNameRecord enr = CreateSimpleENR(dataFDS);

            Assert.AreEqual("FDS", enr.Text);

            try
            {
                TestcaseRecordInputStream.ConfirmRecordEncoding(0x0023, dataFDS, enr.Serialize());
            }
            catch (IndexOutOfRangeException e)
            {
                if (e.Message.Equals("15"))
                {
                    throw new AssertionException("Identified bug 44695");
                }
            }
        }
Esempio n. 2
0
        public void TestAddNameX()
        {
            WorkbookRecordList wrl = new WorkbookRecordList();

            wrl.Add(0, new BOFRecord());
            wrl.Add(1, new CountryRecord());
            wrl.Add(2, EOFRecord.instance);

            int       numberOfSheets = 3;
            LinkTable tbl            = new LinkTable(numberOfSheets, wrl);

            // creation of a new LinkTable insert two new records: SupBookRecord followed by ExternSheetRecord
            // assure they are in place:
            //    [BOFRecord]
            //    [CountryRecord]
            //    [SUPBOOK Internal References  nSheets= 3]
            //    [EXTERNSHEET]
            //    [EOFRecord]

            Assert.AreEqual(5, wrl.Records.Count);
            Assert.IsTrue(wrl[(2)] is SupBookRecord);
            SupBookRecord sup1 = (SupBookRecord)wrl[(2)];

            Assert.AreEqual(numberOfSheets, sup1.NumberOfSheets);
            Assert.IsTrue(wrl[(3)] is ExternSheetRecord);
            ExternSheetRecord extSheet = (ExternSheetRecord)wrl[(3)];

            Assert.AreEqual(0, extSheet.NumOfRefs);

            Assert.IsNull(tbl.GetNameXPtg("ISODD", -1));
            Assert.AreEqual(5, wrl.Records.Count);      //still have five records

            NameXPtg namex1 = tbl.AddNameXPtg("ISODD"); // Adds two new rercords

            Assert.AreEqual(0, namex1.SheetRefIndex);
            Assert.AreEqual(0, namex1.NameIndex);
            Assert.AreEqual(namex1.ToString(), tbl.GetNameXPtg("ISODD", -1).ToString());

            // Can only find on the right sheet ref, if restricting
            Assert.AreEqual(namex1.ToString(), tbl.GetNameXPtg("ISODD", 0).ToString());
            Assert.IsNull(tbl.GetNameXPtg("ISODD", 1));
            Assert.IsNull(tbl.GetNameXPtg("ISODD", 2));
            // assure they are in place:
            //    [BOFRecord]
            //    [CountryRecord]
            //    [SUPBOOK Internal References  nSheets= 3]
            //    [SUPBOOK Add-In Functions nSheets= 1]
            //    [EXTERNALNAME .name    = ISODD]
            //    [EXTERNSHEET]
            //    [EOFRecord]

            Assert.AreEqual(7, wrl.Records.Count);
            Assert.IsTrue(wrl[(3)] is SupBookRecord);
            SupBookRecord sup2 = (SupBookRecord)wrl[(3)];

            Assert.IsTrue(sup2.IsAddInFunctions);
            Assert.IsTrue(wrl[(4)] is ExternalNameRecord);
            ExternalNameRecord ext1 = (ExternalNameRecord)wrl[(4)];

            Assert.AreEqual("ISODD", ext1.Text);
            Assert.IsTrue(wrl[(5)] is ExternSheetRecord);
            Assert.AreEqual(1, extSheet.NumOfRefs);

            //check that
            Assert.AreEqual(0, tbl.ResolveNameXIx(namex1.SheetRefIndex, namex1.NameIndex));
            Assert.AreEqual("ISODD", tbl.ResolveNameXText(namex1.SheetRefIndex, namex1.NameIndex, null));

            Assert.IsNull(tbl.GetNameXPtg("ISEVEN", -1));
            NameXPtg namex2 = tbl.AddNameXPtg("ISEVEN");  // Adds two new rercords

            Assert.AreEqual(0, namex2.SheetRefIndex);
            Assert.AreEqual(1, namex2.NameIndex);  // name index increased by one
            Assert.AreEqual(namex2.ToString(), tbl.GetNameXPtg("ISEVEN", -1).ToString());
            Assert.AreEqual(8, wrl.Records.Count);
            // assure they are in place:
            //    [BOFRecord]
            //    [CountryRecord]
            //    [SUPBOOK Internal References  nSheets= 3]
            //    [SUPBOOK Add-In Functions nSheets= 1]
            //    [EXTERNALNAME .name    = ISODD]
            //    [EXTERNALNAME .name    = ISEVEN]
            //    [EXTERNSHEET]
            //    [EOFRecord]
            Assert.IsTrue(wrl[(3)] is SupBookRecord);
            Assert.IsTrue(wrl[(4)] is ExternalNameRecord);
            Assert.IsTrue(wrl[(5)] is ExternalNameRecord);
            Assert.AreEqual("ISODD", ((ExternalNameRecord)wrl[(4)]).Text);
            Assert.AreEqual("ISEVEN", ((ExternalNameRecord)wrl[(5)]).Text);
            Assert.IsTrue(wrl[(6)] is ExternSheetRecord);
            Assert.IsTrue(wrl[(7)] is EOFRecord);

            Assert.AreEqual(0, tbl.ResolveNameXIx(namex2.SheetRefIndex, namex2.NameIndex));
            Assert.AreEqual("ISEVEN", tbl.ResolveNameXText(namex2.SheetRefIndex, namex2.NameIndex, null));
        }
Esempio n. 3
0
        /**
         * Register an external name in this workbook
         *
         * @param name  the name to register
         * @return a NameXPtg describing this name
         */
        public NameXPtg AddNameXPtg(String name)
        {
            int extBlockIndex          = -1;
            ExternalBookBlock extBlock = null;

            // find ExternalBlock for Add-In functions and remember its index
            for (int i = 0; i < _externalBookBlocks.Length; i++)
            {
                SupBookRecord ebr = _externalBookBlocks[i].GetExternalBookRecord();
                if (ebr.IsAddInFunctions)
                {
                    extBlock      = _externalBookBlocks[i];
                    extBlockIndex = i;
                    break;
                }
            }
            // An ExternalBlock for Add-In functions was not found. Create a new one.
            if (extBlock == null)
            {
                extBlock = new ExternalBookBlock();

                extBlockIndex = ExtendExternalBookBlocks(extBlock);

                // add the created SupBookRecord before ExternSheetRecord
                int idx = FindFirstRecordLocBySid(ExternSheetRecord.sid);
                _workbookRecordList.Add(idx, extBlock.GetExternalBookRecord());

                // register the SupBookRecord in the ExternSheetRecord
                // -2 means that the scope of this name is Workbook and the reference applies to the entire workbook.
                _externSheetRecord.AddRef(_externalBookBlocks.Length - 1, -2, -2);
            }

            // create a ExternalNameRecord that will describe this name
            ExternalNameRecord extNameRecord = new ExternalNameRecord();

            extNameRecord.Text = (name);
            // The docs don't explain why Excel set the formula to #REF!
            extNameRecord.SetParsedExpression(new Ptg[] { ErrPtg.REF_INVALID });

            int nameIndex    = extBlock.AddExternalName(extNameRecord);
            int supLinkIndex = 0;

            // find the posistion of the Add-In SupBookRecord in the workbook stream,
            // the created ExternalNameRecord will be appended to it
            for (IEnumerator iterator = _workbookRecordList.GetEnumerator(); iterator.MoveNext(); supLinkIndex++)
            {
                Record record = (Record)iterator.Current;
                if (record is SupBookRecord)
                {
                    if (((SupBookRecord)record).IsAddInFunctions)
                    {
                        break;
                    }
                }
            }
            int numberOfNames = extBlock.NumberOfNames;

            // a new name is inserted in the end of the SupBookRecord, after the last name
            _workbookRecordList.Add(supLinkIndex + numberOfNames, extNameRecord);
            int fakeSheetIdx = -2; /* the scope is workbook*/
            int ix           = _externSheetRecord.GetRefIxForSheet(extBlockIndex, fakeSheetIdx, fakeSheetIdx);

            return(new NameXPtg(ix, nameIndex));
        }
        /**
         * Writes out this sheet to the output file.  First it writes out
         * the standard workbook information required by excel, before calling
         * the write method on each sheet individually
         *
         * @exception IOException
         */
        public override void write()
        {
            // Perform some preliminary sheet check before we start writing out
            // the workbook
            WritableSheetImpl wsi = null;
            for (int i = 0; i < getNumberOfSheets(); i++)
                {
                wsi = (WritableSheetImpl)getSheet(i);

                // Check the merged records.  This has to be done before the
                // globals are written out because some more XF formats might be created
                wsi.checkMergedBorders();

                // Check to see if there are any predefined names
                Range range = wsi.getSettings().getPrintArea();
                if (range != null)
                    {
                    addNameArea(BuiltInName.PRINT_AREA,
                                wsi,
                                range.getTopLeft().getColumn(),
                                range.getTopLeft().getRow(),
                                range.getBottomRight().getColumn(),
                                range.getBottomRight().getRow(),
                                false);
                    }

                // Check to see if print titles by row were set
                Range rangeR = wsi.getSettings().getPrintTitlesRow();
                Range rangeC = wsi.getSettings().getPrintTitlesCol();
                if (rangeR != null && rangeC != null)
                    {
                    addNameArea(BuiltInName.PRINT_TITLES,
                                wsi,
                                rangeR.getTopLeft().getColumn(),
                                rangeR.getTopLeft().getRow(),
                                rangeR.getBottomRight().getColumn(),
                                rangeR.getBottomRight().getRow(),
                                rangeC.getTopLeft().getColumn(),
                                rangeC.getTopLeft().getRow(),
                                rangeC.getBottomRight().getColumn(),
                                rangeC.getBottomRight().getRow(),
                                false);
                    }
                // Check to see if print titles by row were set
                else if (rangeR != null)
                    {
                    addNameArea(BuiltInName.PRINT_TITLES,
                              wsi,
                              rangeR.getTopLeft().getColumn(),
                              rangeR.getTopLeft().getRow(),
                              rangeR.getBottomRight().getColumn(),
                              rangeR.getBottomRight().getRow(),
                              false);
                    }
                // Check to see if print titles by column were set
                else if (rangeC != null)
                    {
                    addNameArea(BuiltInName.PRINT_TITLES,
                                wsi,
                                rangeC.getTopLeft().getColumn(),
                                rangeC.getTopLeft().getRow(),
                                rangeC.getBottomRight().getColumn(),
                                rangeC.getBottomRight().getRow(),
                                false);
                    }
                }

            // Rationalize all the XF and number formats
            if (!settings.getRationalizationDisabled())
                rationalize();

            // Write the workbook globals
            BOFRecord bof = new BOFRecord(BOFRecord.workbookGlobals);
            outputFile.write(bof);

            // Must immediatly follow the BOF record
            if (settings.getTemplate())
                {
                // Only write record if we are a template
                TemplateRecord trec = new TemplateRecord();
                outputFile.write(trec);
                }

            InterfaceHeaderRecord ihr = new InterfaceHeaderRecord();
            outputFile.write(ihr);

            MMSRecord mms = new MMSRecord(0, 0);
            outputFile.write(mms);

            InterfaceEndRecord ier = new InterfaceEndRecord();
            outputFile.write(ier);

            WriteAccessRecord wr = new WriteAccessRecord(settings.getWriteAccess());
            outputFile.write(wr);

            CodepageRecord cp = new CodepageRecord();
            outputFile.write(cp);

            DSFRecord dsf = new DSFRecord();
            outputFile.write(dsf);

            if (settings.getExcel9File())
                {
                // Only write record if we are a template
                // We are not excel 2000, should we still set the flag
                Excel9FileRecord e9rec = new Excel9FileRecord();
                outputFile.write(e9rec);
                }

            TabIdRecord tabid = new TabIdRecord(getNumberOfSheets());
            outputFile.write(tabid);

            if (containsMacros)
                {
                ObjProjRecord objproj = new ObjProjRecord();
                outputFile.write(objproj);
                }

            if (buttonPropertySet != null)
                outputFile.write(buttonPropertySet);

            FunctionGroupCountRecord fgcr = new FunctionGroupCountRecord();
            outputFile.write(fgcr);

            // do not support password protected workbooks
            WindowProtectRecord wpr = new WindowProtectRecord
              (settings.getWindowProtected());
            outputFile.write(wpr);

            ProtectRecord pr = new ProtectRecord(wbProtected);
            outputFile.write(pr);

            PasswordRecord pw = new PasswordRecord(null);
            outputFile.write(pw);

            Prot4RevRecord p4r = new Prot4RevRecord(false);
            outputFile.write(p4r);

            Prot4RevPassRecord p4rp = new Prot4RevPassRecord();
            outputFile.write(p4rp);

            // If no sheet is identified as being selected, then select
            // the first one
            bool sheetSelected = false;
            WritableSheetImpl wsheet = null;
            int selectedSheetIndex = 0;
            for (int i = 0; i < getNumberOfSheets() && !sheetSelected; i++)
                {
                wsheet = (WritableSheetImpl)getSheet(i);
                if (wsheet.getSettings().isSelected())
                    {
                    sheetSelected = true;
                    selectedSheetIndex = i;
                    }
                }

            if (!sheetSelected)
                {
                wsheet = (WritableSheetImpl)getSheet(0);
                wsheet.getSettings().setSelected(true);
                selectedSheetIndex = 0;
                }

            Window1Record w1r = new Window1Record(selectedSheetIndex);
            outputFile.write(w1r);

            BackupRecord bkr = new BackupRecord(false);
            outputFile.write(bkr);

            HideobjRecord ho = new HideobjRecord(settings.getHideobj());
            outputFile.write(ho);

            NineteenFourRecord nf = new NineteenFourRecord(false);
            outputFile.write(nf);

            PrecisionRecord pc = new PrecisionRecord(false);
            outputFile.write(pc);

            RefreshAllRecord rar = new RefreshAllRecord(settings.getRefreshAll());
            outputFile.write(rar);

            BookboolRecord bb = new BookboolRecord(true);
            outputFile.write(bb);

            // Write out all the fonts used
            fonts.write(outputFile);

            // Write out the cell formats used within this workbook
            formatRecords.write(outputFile);

            // Write out the palette, if it exists
            if (formatRecords.getPalette() != null)
                outputFile.write(formatRecords.getPalette());

            // Write out the uses elfs record
            UsesElfsRecord uer = new UsesElfsRecord();
            outputFile.write(uer);

            // Write out the boundsheet records.  Keep a handle to each one's
            // position so we can write in the stream offset later
            int[] boundsheetPos = new int[getNumberOfSheets()];
            Sheet sheet = null;

            for (int i = 0; i < getNumberOfSheets(); i++)
                {
                boundsheetPos[i] = outputFile.getPos();
                sheet = getSheet(i);
                BoundsheetRecord br = new BoundsheetRecord(sheet.getName());
                if (sheet.getSettings().isHidden())
                    br.setHidden();

                if (((WritableSheetImpl)sheets[i]).isChartOnly())
                    br.setChartOnly();

                outputFile.write(br);
                }

            if (countryRecord == null)
                {
                CountryCode lang = CountryCode.getCountryCode(settings.getExcelDisplayLanguage());
                if (lang == CountryCode.UNKNOWN)
                    {
                    //logger.warn("Unknown country code " +
                    //            settings.getExcelDisplayLanguage() +
                    //            " using " + CountryCode.USA.getCode());
                    lang = CountryCode.USA;
                    }
                CountryCode region = CountryCode.getCountryCode(settings.getExcelRegionalSettings());
                countryRecord = new CountryRecord(lang, region);
                if (region == CountryCode.UNKNOWN)
                    {
                    //logger.warn("Unknown country code " +
                    //            settings.getExcelDisplayLanguage() +
                    //            " using " + CountryCode.UK.getCode());
                    region = CountryCode.UK;
                    }
                }

            outputFile.write(countryRecord);

            // Write out the names of any add in functions
            if (addInFunctionNames != null && addInFunctionNames.Length > 0)
                {
                // Write out the supbook record
                //      SupbookRecord supbook = new SupbookRecord();
                //      outputFile.write(supbook);

                for (int i = 0; i < addInFunctionNames.Length; i++)
                    {
                    ExternalNameRecord enr = new ExternalNameRecord(addInFunctionNames[i]);
                    outputFile.write(enr);
                    }
                }

            if (xctRecords != null)
                {
                for (int i = 0; i < xctRecords.Length; i++)
                    outputFile.write(xctRecords[i]);
                }

            // Write out the external sheet record, if it exists
            if (externSheet != null)
                {
                //Write out all the supbook records
                for (int i = 0; i < supbooks.Count; i++)
                    {
                    SupbookRecord supbook = (SupbookRecord)supbooks[i];
                    outputFile.write(supbook);
                    }
                outputFile.write(externSheet);
                }

            // Write out the names, if any exists
            if (names != null)
                {
                for (int i = 0; i < names.Count; i++)
                    {
                    NameRecord n = (NameRecord)names[i];
                    outputFile.write(n);
                    }
                }

            // Write out the mso drawing group, if it exists
            if (drawingGroup != null)
                drawingGroup.write(outputFile);

            sharedStrings.write(outputFile);

            EOFRecord eof = new EOFRecord();
            outputFile.write(eof);

            // Write out the sheets
            for (int i = 0; i < getNumberOfSheets(); i++)
                {
                // first go back and modify the offset we wrote out for the
                // boundsheet record
                outputFile.setData(IntegerHelper.getFourBytes(outputFile.getPos()),boundsheetPos[i] + 4);

                wsheet = (WritableSheetImpl)getSheet(i);
                wsheet.write();
                }
        }