Skip to content

Commit 8266a2c

Browse files
committed
add openSettings option to ignore missing calcChain parts
1 parent f7954e5 commit 8266a2c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/DocumentFormat.OpenXml.Framework/Packaging/OpenSettings.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class OpenSettings
1212
{
1313
private bool? _autoSave;
1414
private MarkupCompatibilityProcessSettings? _mcSettings;
15+
private bool? _ignoreExceptionsOnCalcChainPartMissing;
1516

1617
/// <summary>
1718
/// Initializes a new instance of the <see cref="OpenSettings"/> class.
@@ -32,6 +33,7 @@ internal OpenSettings(OpenSettings? other)
3233
MarkupCompatibilityProcessSettings.TargetFileFormatVersions = other.MarkupCompatibilityProcessSettings.TargetFileFormatVersions;
3334
MaxCharactersInPart = other.MaxCharactersInPart;
3435
RelationshipErrorHandlerFactory = other.RelationshipErrorHandlerFactory;
36+
IgnoreExceptionOnCalcChainPartMissing = other.IgnoreExceptionOnCalcChainPartMissing;
3537
}
3638

3739
/// <summary>
@@ -77,5 +79,15 @@ public MarkupCompatibilityProcessSettings MarkupCompatibilityProcessSettings
7779
/// Gets or sets a delegate that is used to create a handler to rewrite relationships that are malformed. On platforms after .NET 4.5, <see cref="Uri"/> parsing will fail on malformed strings.
7880
/// </summary>
7981
public Func<OpenXmlPackage, RelationshipErrorHandler>? RelationshipErrorHandlerFactory { get; set; }
82+
83+
/// <summary>
84+
/// Gets or sets a value indicating whether to ignore an exception if the calcChain part is missing.
85+
/// The default value is false which means missing calcChain part will throw an exception upon package open.
86+
/// </summary>
87+
public bool IgnoreExceptionOnCalcChainPartMissing
88+
{
89+
get => _ignoreExceptionsOnCalcChainPartMissing ?? false;
90+
set => _ignoreExceptionsOnCalcChainPartMissing = value;
91+
}
8092
}
8193
}

src/DocumentFormat.OpenXml.Framework/Packaging/OpenXmlPartContainer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,17 +1794,18 @@ internal OpenXmlPart CreateOpenXmlPart(string relationshipType)
17941794
/// <param name="loadedParts">Temp collection to detect loaded (shared) parts.</param>
17951795
internal void LoadReferencedPartsAndRelationships(OpenXmlPackage openXmlPackage, OpenXmlPart? sourcePart, RelationshipCollection relationshipCollection, Dictionary<Uri, OpenXmlPart> loadedParts)
17961796
{
1797-
List<string> partsToIgnore = new()
1797+
Dictionary<string, bool> partsToIgnore = new()
17981798
{
17991799
// Fix bug https://github.com/OfficeDev/Open-XML-SDK/issues/1205
1800-
@"http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain",
1800+
{ @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain", openXmlPackage.OpenSettings.IgnoreExceptionOnCalcChainPartMissing },
18011801

18021802
// Fix bug https://github.com/OfficeDev/Open-XML-SDK/issues/1205
1803-
@"http://schemas.microsoft.com/office/2006/relationships/recovered",
1803+
{ @"http://schemas.microsoft.com/office/2006/relationships/recovered", true },
18041804
};
1805+
18051806
foreach (var relationship in relationshipCollection)
18061807
{
1807-
if (partsToIgnore.Contains(relationship.RelationshipType))
1808+
if (partsToIgnore.ContainsKey(relationship.RelationshipType) && partsToIgnore[relationship.RelationshipType])
18081809
{
18091810
continue;
18101811
}

0 commit comments

Comments
 (0)