Skip to content

Commit

Permalink
fix for issue1281, ignore missing calcChain part (#1288)
Browse files Browse the repository at this point in the history
* fix issue1281 by adding an openSettings option to ignore missing calcChain parts
  • Loading branch information
tomjebo authored Jan 6, 2023
1 parent 35141f2 commit dad8328
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
19 changes: 9 additions & 10 deletions src/DocumentFormat.OpenXml.Framework/Packaging/OpenSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace DocumentFormat.OpenXml.Packaging
/// </summary>
public class OpenSettings
{
private bool? _autoSave;
private MarkupCompatibilityProcessSettings? _mcSettings;

/// <summary>
Expand All @@ -32,17 +31,14 @@ internal OpenSettings(OpenSettings? other)
MarkupCompatibilityProcessSettings.TargetFileFormatVersions = other.MarkupCompatibilityProcessSettings.TargetFileFormatVersions;
MaxCharactersInPart = other.MaxCharactersInPart;
RelationshipErrorHandlerFactory = other.RelationshipErrorHandlerFactory;
IgnoreExceptionOnCalcChainPartMissing = other.IgnoreExceptionOnCalcChainPartMissing;
}

/// <summary>
/// Gets or sets a value indicating whether to auto save document modifications.
/// The default value is true.
/// </summary>
public bool AutoSave
{
get => _autoSave ?? true;
set => _autoSave = value;
}
public bool AutoSave { get; set; } = true;

/// <summary>
/// Gets or sets the value of the markup compatibility processing mode.
Expand All @@ -51,10 +47,7 @@ public MarkupCompatibilityProcessSettings MarkupCompatibilityProcessSettings
{
get
{
if (_mcSettings is null)
{
_mcSettings = new MarkupCompatibilityProcessSettings(MarkupCompatibilityProcessMode.NoProcess, FileFormatVersions.Office2007);
}
_mcSettings ??= new MarkupCompatibilityProcessSettings(MarkupCompatibilityProcessMode.NoProcess, FileFormatVersions.Office2007);

return _mcSettings;
}
Expand All @@ -77,5 +70,11 @@ public MarkupCompatibilityProcessSettings MarkupCompatibilityProcessSettings
/// 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.
/// </summary>
public Func<OpenXmlPackage, RelationshipErrorHandler>? RelationshipErrorHandlerFactory { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to ignore an exception if the calcChain part is missing.
/// The default value is false which means missing calcChain part will throw an exception upon package open.
/// </summary>
public bool IgnoreExceptionOnCalcChainPartMissing { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1794,10 +1794,18 @@ internal OpenXmlPart CreateOpenXmlPart(string relationshipType)
/// <param name="loadedParts">Temp collection to detect loaded (shared) parts.</param>
internal void LoadReferencedPartsAndRelationships(OpenXmlPackage openXmlPackage, OpenXmlPart? sourcePart, RelationshipCollection relationshipCollection, Dictionary<Uri, OpenXmlPart> loadedParts)
{
foreach (var relationship in relationshipCollection)
Dictionary<string, bool> partsToIgnore = new()
{
// Fix bug https://github.com/OfficeDev/Open-XML-SDK/issues/1205
if (relationship.RelationshipType == @"http://schemas.microsoft.com/office/2006/relationships/recovered")
{ @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain", openXmlPackage.OpenSettings.IgnoreExceptionOnCalcChainPartMissing },

// Fix bug https://github.com/OfficeDev/Open-XML-SDK/issues/1205
{ @"http://schemas.microsoft.com/office/2006/relationships/recovered", true },
};

foreach (var relationship in relationshipCollection)
{
if (partsToIgnore.ContainsKey(relationship.RelationshipType) && partsToIgnore[relationship.RelationshipType])
{
continue;
}
Expand Down

0 comments on commit dad8328

Please sign in to comment.