question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding parts to WorksheetPart derived class throws exception

See original GitHub issue

Description When I try to add parts to a worksheet part within a class derived from WorksheetPart, such as SpreadsheetPrinterSettingsPart or VmlDrawingPart, an OpenXmlPackageException is thrown with a message “The part cannot be added here.” This just started when I updated the reference SDK to 10.0 or 10.1. The code works as expected when built and run against SDK version 9.1 or earlier.

I’m still trying different things to understand this issue better but this is what I am observing in my code. However, if I open and existing workbook and simply add the part to a worksheet, it works fine. The main difference that I can tell is that the error happens in my code that inherits from WorksheetPart. Please let me know if there are any additional questions.

Information

  • netstandard2.0
  • DocumentFormat.OpenXml Version: 10.0+

Repro

// Please add a self-contained, minimum viable repro of the issue.
// If you require external resources, please provide a gist or GitHub repro
// An Xunit style test is preferred, but a console application would work too.
public class SampleWorksheetPart : WorksheetPart
{
  public SampleWorksheetPart() {}

  public void AddPrinterSettingsPart()
  {
    // The error would occur when this method is called in the line
    // below in 10.0+ but works as expected
    // in 9.1 or earlier.
    var psPart = this.AddNewPart<SpreadsheetPrinterSettingsPart>();
  }
}

class Program
{
  static void Main(string[] args)
  {
    var fInfo = new FileInfo(@"C:\Temp\NewWorkbook.xlsx");
    // This assumes that file that fInfo references already exists
    using (var xlsx = fInfo.Open(FileMode.Open, FileAcess.ReadWrite))
    {
      using (var doc = SpreadsheetDocument.Open(xlsx, true))
      {
        var workbook = doc.WorkbookPart;
        var worksheet = workbook.Parts.FirstOrDefault(w => w.OpenXmlPart is WorksheetPart).OpenXmlPart;

        // This works fine in all versions for some reason.
        var ps = worksheet.AddNewPart<SpreadsheetPrinterSettingsPart>();
      }
    }
  }
}

Observed OpenXmlPackageException: The part cannot be added here –> Inner exception: ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Expected

The part is added to the current object.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rmboggscommented, May 19, 2020

Sounds good. Again, thanks for taking the time to address.

0reactions
twsouthwickcommented, May 19, 2020

We’re planning on releasing 2.11.0 soon - have to renew a cert for signing and then that’ll be released. So it’ll just go into that

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding parts to WorksheetPart derived class throws ...
When I try to add parts to a worksheet part within a class derived from WorksheetPart, such as SpreadsheetPrinterSettingsPart or VmlDrawingPart, ...
Read more >
java - checked exception for 'throws' in overridden method
So, the base class should declare a super-set i.e. either the same exception class or base exception class of the derived class.
Read more >
SpreadsheetDocument Class (DocumentFormat.OpenXml. ...
Adds the specified part to the document. Use the returned part to operate on the part added to the document. (Inherited from OpenXmlPackage)....
Read more >
Exception Handling with Method Overriding in Java
An Exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run-time, that disrupts the normal ......
Read more >
Creating and Throwing Exceptions
Learn about creating and throwing exceptions. Exceptions are used to indicate that an error has occurred while running a program.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found