44 public void Init(
Workbook workbook, IOptions readerOptions, Action<Workbook, string, IOptions, int?> inlinePluginHandler)
57 FinalizeDefinedNames();
69 private void FinalizeDefinedNames()
71 List<WorksheetDefinition> worksheetDefinitions =
Workbook.AuxiliaryData.GetDataList<WorksheetDefinition>(PlugInUUID.WorkbookReader, PlugInUUID.WorksheetDefinitionEntity);
72 if (
Workbook.Worksheets.Count < worksheetDefinitions.Count)
76 List<DefinedNameDefinition> definitions =
Workbook.AuxiliaryData.GetDataList<DefinedNameDefinition>(PlugInUUID.WorkbookReader, PlugInUUID.DefinedNameEntity);
77 foreach (DefinedNameDefinition definition
in definitions)
79 Worksheet localSheet =
null;
80 if (definition.LocalSheetId.HasValue)
82 int i = definition.LocalSheetId.Value;
83 if (i >= 0 && i <
Workbook.Worksheets.Count)
88 DefinedName resolvedDefinedName = DefinedName.ResolveDefinedName(definition.Name, definition.Reference,
Workbook, localSheet, definition.Comment);
89 Workbook.AddDefinedName(resolvedDefinedName);
91 RetagReferenceCells();
99 private void RetagReferenceCells()
101 if (!
Workbook.Features.ContainsDefinedNames)
105 foreach (Worksheet ws
in Workbook.Worksheets)
107 Dictionary<string, Tuple<Cell, DefinedName>> referenceCellCopies =
new Dictionary<string, Tuple<Cell, DefinedName>>(StringComparer.Ordinal);
108 foreach (Cell cell
in ws.Cells.Values)
110 if (cell.DataType == Cell.CellType.Formula && cell.Formula !=
null)
112 DefinedName definedName =
Workbook.GetDefinedName(cell.Formula.Expression, ws)
113 ??
Workbook.GetDefinedName(cell.Formula.Expression);
114 if (definedName !=
null)
116 referenceCellCopies.Add(cell.CellAddress,
new Tuple<Cell, DefinedName>(cell.Copy(), definedName));
120 if (referenceCellCopies.Count == 0)
124 HashSet<string> processedAddresses =
new HashSet<string>(StringComparer.Ordinal);
125 foreach (KeyValuePair<
string, Tuple<Cell, DefinedName>> cell
in referenceCellCopies)
127 if (processedAddresses.Contains(cell.Key))
131 IReadOnlyList<Address> addresses;
132 object cachedValue = cell.Value.Item1.Formula.CachedValue;
133 if (cell.Value.Item1.CellStyle ==
null)
135 addresses = ws.AddCellReference(cell.Value.Item2, cell.Key, cachedValue);
139 addresses = ws.AddCellReference(cell.Value.Item2, cell.Key, cell.Value.Item1.CellStyle, cachedValue);
141 FormulaData restoredFormula = ws.Cells[cell.Key].Formula;
142 restoredFormula.CachedValue = cachedValue;
143 restoredFormula.CachedValueType = cell.Value.Item1.Formula.CachedValueType;
144 foreach (Address address
in addresses)
146 processedAddresses.Add(address.ToString());