/** * 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(); ExternalBookBlock[] tmp = new ExternalBookBlock[_externalBookBlocks.Length + 1]; Array.Copy(_externalBookBlocks, 0, tmp, 0, _externalBookBlocks.Length); tmp[tmp.Length - 1] = extBlock; _externalBookBlocks = tmp; extBlockIndex = _externalBookBlocks.Length - 1; // 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 ix = _externSheetRecord.GetRefIxForSheet(extBlockIndex, -2 /* the scope is workbook*/); return new NameXPtg(ix, nameIndex); }
public int AddExternalName(ExternalNameRecord rec) { ExternalNameRecord[] tmp = new ExternalNameRecord[_externalNameRecords.Length + 1]; Array.Copy(_externalNameRecords, 0, tmp, 0, _externalNameRecords.Length); tmp[tmp.Length - 1] = rec; _externalNameRecords = tmp; return _externalNameRecords.Length - 1; }