示例#1
0
        private int ExtendExternalBookBlocks(ExternalBookBlock newBlock)
        {
            ExternalBookBlock[] tmp = new ExternalBookBlock[_externalBookBlocks.Length + 1];
            Array.Copy(_externalBookBlocks, 0, tmp, 0, _externalBookBlocks.Length);
            tmp[tmp.Length - 1] = newBlock;
            _externalBookBlocks = tmp;

            return(_externalBookBlocks.Length - 1);
        }
示例#2
0
        public String ResolveNameXText(int refIndex, int definedNameIndex, InternalWorkbook workbook)
        {
            int extBookIndex  = _externSheetRecord.GetExtbookIndexFromRefIndex(refIndex);
            int firstTabIndex = _externSheetRecord.GetFirstSheetIndexFromRefIndex(refIndex);

            if (firstTabIndex == -1)
            {
                // The referenced sheet could not be found
                throw new RuntimeException("Referenced sheet could not be found");
            }

            // Does it exist via the external book block?
            ExternalBookBlock externalBook = _externalBookBlocks[extBookIndex];

            if (externalBook._externalNameRecords.Length > definedNameIndex)
            {
                return(_externalBookBlocks[extBookIndex].GetNameText(definedNameIndex));
            }
            else if (firstTabIndex == -2)
            {
                // Workbook scoped name, not actually external after all
                NameRecord nr          = GetNameRecord(definedNameIndex);
                int        sheetNumber = nr.SheetNumber;

                StringBuilder text = new StringBuilder();
                if (sheetNumber > 0)
                {
                    String sheetName = workbook.GetSheetName(sheetNumber - 1);
                    SheetNameFormatter.AppendFormat(text, sheetName);
                    text.Append("!");
                }
                text.Append(nr.NameText);
                return(text.ToString());
            }
            else
            {
                throw new IndexOutOfRangeException(
                          "Ext Book Index relative but beyond the supported length, was " +
                          extBookIndex + " but maximum is " + _externalBookBlocks.Length
                          );
            }
        }
示例#3
0
        public int LinkExternalWorkbook(String name, IWorkbook externalWorkbook)
        {
            int extBookIndex = GetExternalWorkbookIndex(name);

            if (extBookIndex != -1)
            {
                // Already linked!
                return(extBookIndex);
            }

            // Create a new SupBookRecord
            String[] sheetNames = new String[externalWorkbook.NumberOfSheets];
            for (int sn = 0; sn < sheetNames.Length; sn++)
            {
                sheetNames[sn] = externalWorkbook.GetSheetName(sn);
            }
            //\000 is octal digit in java, but c# think it is a '\0' and two zero.
            String            url   = "\0" + name;
            ExternalBookBlock block = new ExternalBookBlock(url, sheetNames);

            // Add it into the list + records
            extBookIndex = ExtendExternalBookBlocks(block);

            // add the created SupBookRecord before ExternSheetRecord
            int idx = FindFirstRecordLocBySid(ExternSheetRecord.sid);

            if (idx == -1)
            {
                idx = _workbookRecordList.Count;
            }
            _workbookRecordList.Add(idx, block.GetExternalBookRecord());

            // Setup links for the sheets
            for (int sn = 0; sn < sheetNames.Length; sn++)
            {
                _externSheetRecord.AddRef(extBookIndex, sn, sn);
            }

            // Report where it went
            return(extBookIndex);
        }
示例#4
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));
        }
示例#5
0
        public int LinkExternalWorkbook(String name, IWorkbook externalWorkbook)
        {
            int extBookIndex = GetExternalWorkbookIndex(name);
            if (extBookIndex != -1)
            {
                // Already linked!
                return extBookIndex;
            }

            // Create a new SupBookRecord
            String[] sheetNames = new String[externalWorkbook.NumberOfSheets];
            for (int sn = 0; sn < sheetNames.Length; sn++)
            {
                sheetNames[sn] = externalWorkbook.GetSheetName(sn);
            }
            //\000 is octal digit in java, but c# think it is a '\0' and two zero.
            String url = "\0" + name;
            ExternalBookBlock block = new ExternalBookBlock(url, sheetNames);

            // Add it into the list + records
            extBookIndex = ExtendExternalBookBlocks(block);

            // add the created SupBookRecord before ExternSheetRecord
            int idx = FindFirstRecordLocBySid(ExternSheetRecord.sid);
            if (idx == -1)
            {
                idx = _workbookRecordList.Count;
            }
            _workbookRecordList.Add(idx, block.GetExternalBookRecord());

            // Setup links for the sheets
            for (int sn = 0; sn < sheetNames.Length; sn++)
            {
                _externSheetRecord.AddRef(extBookIndex, sn, sn);
            }

            // Report where it went
            return extBookIndex;
        }
示例#6
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);
        }
示例#7
0
        private int ExtendExternalBookBlocks(ExternalBookBlock newBlock)
        {
            ExternalBookBlock[] tmp = new ExternalBookBlock[_externalBookBlocks.Length + 1];
            Array.Copy(_externalBookBlocks, 0, tmp, 0, _externalBookBlocks.Length);
            tmp[tmp.Length - 1] = newBlock;
            _externalBookBlocks = tmp;

            return (_externalBookBlocks.Length - 1);
        }