8using NanoXLSX.Exceptions;
10using NanoXLSX.Interfaces.Writer;
11using NanoXLSX.Registry;
12using NanoXLSX.Registry.Attributes;
14using NanoXLSX.Utils.Xml;
15using System.Collections.Generic;
19 [NanoXlsxQueuePlugIn(PlugInUUID =
"EXTERNAL_LINK_INLINE_WORKBOOK_WRITER", QueueUUID = PlugInUUID.WorkbookInlineWriter, PlugInOrder = 100000)]
20 internal class ExternalLinkWorkbookInlineWriter : IPluginInlineWriter
26 public Workbook Workbook {
get;
set; }
30 public IWriteContext WriteContext {
get;
set; }
34 public XmlElement RootElement {
get;
set; }
38 public XmlElement XmlElement {
get; }
47 public void Init(ref XmlElement rootElement, Workbook workbook,
int? index =
null)
50 RootElement = rootElement;
58 XmlElement externalReferences = GetExternalReferences();
59 if (externalReferences ==
null)
64 RootElement.AddChildElementAfter(externalReferences,
"functionGroups",
"sheets");
65 ReplaceDefinedNames();
72 private void ReplaceDefinedNames()
74 Dictionary<int, ExternalLinkResolution> externalLinks = Workbook.AuxiliaryData.GetData<Dictionary<int, ExternalLinkResolution>>(PlugInUUID.CompatibilityInlineProcessor, CompatibilityConstants.EXTERNAL_LINK_RESOLVED_DEFINED_NAMES_ENTITY);
75 if (!Workbook.Features.ContainsDefinedNames || externalLinks ==
null || externalLinks.Count == 0)
79 IEnumerable<XmlElement> definedNames = RootElement.FindChildElementsByNameAndAttribute(
"definedName",
"name");
81 foreach (XmlElement definedName
in definedNames)
83 if (externalLinks.TryGetValue(index, out ExternalLinkResolution value))
85 definedName.InnerValue = value.Expression;
95 private XmlElement GetExternalReferences()
97 List<ExternalLink> externalLinks = Workbook.AuxiliaryData.GetDataList<ExternalLink>(PlugInUUID.CompatibilityInlineProcessor, CompatibilityConstants.EXTERNAL_LINK_OBJECT_ENTITY);
98 if (externalLinks.Count == 0)
103 XmlElement externalReferences = XmlElement.CreateElement(
"externalReferences");
104 for (
int i = 0; i < externalLinks.Count; i++)
106 string uniquePackagePartIndex = CompatibilityConstants.UNIQUE_PACKAGE_PART_INDEX_PREFIX + ParserUtils.ToString(i);
107 string relationshipId = Workbook.AuxiliaryData.GetData<
string>(PlugInUUID.WriterPackageRegistryQueue, PlugInUUID.PackagePartRelationshipId, uniquePackagePartIndex);
108 if (
string.IsNullOrWhiteSpace(relationshipId))
110 throw new IOException(
"The workbook relationship ID for external link package part '" + uniquePackagePartIndex +
"' is not available.");
112 externalReferences.AddChildElementWithAttribute(
"externalReference",
"id", relationshipId,
"",
"r");
114 return externalReferences;