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.

Element classes do not contain all child element-related properties

See original GitHub issue

Description

The DocumentFormat.OpenXml.Spreadsheet.Worksheet class only has properties for 4 out of 38 child elements. For example, there is no property for the only mandatory child element, x:sheetData, which is likely among the most frequently referenced elements.

As per the schema, those child elements have to be in a defined order, which, in the absence of SDK support, needs to be ensured by the caller’s code. That increases the size and complexity of the code.

Information

  • .NET Target: all
  • DocumentFormat.OpenXml Version: 3cc26570f2964a5d56a5988f9464b2e4b29812c7

Observed

The Worksheet class defines the following child elements:

    [ChildElementInfo(typeof(SheetProperties))]
    [ChildElementInfo(typeof(SheetDimension))]
    [ChildElementInfo(typeof(SheetViews))]
    [ChildElementInfo(typeof(SheetFormatProperties))]
    [ChildElementInfo(typeof(Columns))]
    [ChildElementInfo(typeof(SheetData))]
    [ChildElementInfo(typeof(SheetCalculationProperties))]
    [ChildElementInfo(typeof(SheetProtection))]
    [ChildElementInfo(typeof(ProtectedRanges))]
    [ChildElementInfo(typeof(Scenarios))]
    [ChildElementInfo(typeof(AutoFilter))]
    [ChildElementInfo(typeof(SortState))]
    [ChildElementInfo(typeof(DataConsolidate))]
    [ChildElementInfo(typeof(CustomSheetViews))]
    [ChildElementInfo(typeof(MergeCells))]
    [ChildElementInfo(typeof(PhoneticProperties))]
    [ChildElementInfo(typeof(ConditionalFormatting))]
    [ChildElementInfo(typeof(DataValidations))]
    [ChildElementInfo(typeof(Hyperlinks))]
    [ChildElementInfo(typeof(PrintOptions))]
    [ChildElementInfo(typeof(PageMargins))]
    [ChildElementInfo(typeof(PageSetup))]
    [ChildElementInfo(typeof(HeaderFooter))]
    [ChildElementInfo(typeof(RowBreaks))]
    [ChildElementInfo(typeof(ColumnBreaks))]
    [ChildElementInfo(typeof(CustomProperties))]
    [ChildElementInfo(typeof(CellWatches))]
    [ChildElementInfo(typeof(IgnoredErrors))]
    [ChildElementInfo(typeof(Drawing))]
    [ChildElementInfo(typeof(LegacyDrawing))]
    [ChildElementInfo(typeof(LegacyDrawingHeaderFooter))]
    [ChildElementInfo(typeof(DrawingHeaderFooter))]
    [ChildElementInfo(typeof(Picture))]
    [ChildElementInfo(typeof(OleObjects))]
    [ChildElementInfo(typeof(Controls))]
    [ChildElementInfo(typeof(WebPublishItems))]
    [ChildElementInfo(typeof(TableParts))]
    [ChildElementInfo(typeof(WorksheetExtensionList))]

However, the Worksheet class only provides the following properties:

    /// <summary>
    /// <para> SheetProperties.</para>
    /// <para> Represents the following element tag in the schema: x:sheetPr </para>
    /// </summary>
    /// <remark>
    /// xmlns:x = http://schemas.openxmlformats.org/spreadsheetml/2006/main
    /// </remark>
    public SheetProperties SheetProperties
    {
        get => GetElement<SheetProperties>(0);
        set => SetElement(0, value);
    }

    /// <summary>
    /// <para> SheetDimension.</para>
    /// <para> Represents the following element tag in the schema: x:dimension </para>
    /// </summary>
    /// <remark>
    /// xmlns:x = http://schemas.openxmlformats.org/spreadsheetml/2006/main
    /// </remark>
    public SheetDimension SheetDimension
    {
        get => GetElement<SheetDimension>(1);
        set => SetElement(1, value);
    }

    /// <summary>
    /// <para> SheetViews.</para>
    /// <para> Represents the following element tag in the schema: x:sheetViews </para>
    /// </summary>
    /// <remark>
    /// xmlns:x = http://schemas.openxmlformats.org/spreadsheetml/2006/main
    /// </remark>
    public SheetViews SheetViews
    {
        get => GetElement<SheetViews>(2);
        set => SetElement(2, value);
    }

    /// <summary>
    /// <para> SheetFormatProperties.</para>
    /// <para> Represents the following element tag in the schema: x:sheetFormatPr </para>
    /// </summary>
    /// <remark>
    /// xmlns:x = http://schemas.openxmlformats.org/spreadsheetml/2006/main
    /// </remark>
    public SheetFormatProperties SheetFormatProperties
    {
        get => GetElement<SheetFormatProperties>(3);
        set => SetElement(3, value);
    }

Expected

Given that the child elements must be in a defined order, the Worksheet class defines properties for all child elements.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ThomasBarnekowcommented, Jun 16, 2021

@twsouthwick, just checking whether this issue is still open. Otherwise, we might want to close it.

0reactions
jptemplincommented, Aug 25, 2020

For example, while AppendChild does nothing wrong on a technical level, it makes it easy to do the wrong thing on an Open XML schema level. For example, when you want to add a Paragraph child element to a Body parent element that already has a SectionProperties child element, using AppendChild adds the Paragraph after the SectionProperties, leading to invalid markup.

I’m late to the game here, but this newbie got caught up by this.

My use case: I use a Word doc seeded with 35 styles as a template to create nicely-formatted cooking recipes read from a database. I copy this “empty” doc, and append the recipe text to the copy. The styles do all the formatting. It took me awhile to figure out that my paragraphs appended to the end of the copied doc were being added AFTER the SectionProperties already at the end of the document, and that’s incorrect. Because I couldn’t figure out how to insert new paragraphs BEFORE the existing SectionProperties, what I ended up doing is opening the copy, deleting all the children from the body, appending new parargraphs to my heart’s content, then programmatically create and append the SectionProperties at the end. I had hoped to have the template define those section properties rather than code, but it worked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Element classes do not contain all child element-related ...
Worksheet class only has properties for 4 out of 38 child elements. For example, there is no property for the only mandatory child...
Read more >
Get every div that doesn't contain a child specific element - ...
Step 1: Get all div elements. Step 2: Filter the collection, keeping those that do not have an h1 descendant.
Read more >
Element: children property - Web APIs | MDN
Element.children includes only element nodes. To get all child nodes, including non-element nodes like text and comment nodes, use Node.
Read more >
5 Selectors
Note: If an element has multiple class attributes, their values must be concatenated with spaces between the values before searching for the class....
Read more >
CSS inheritance: inherit, initial, unset, and revert
The child element will naturally inherit a CSS property with its value from the parent element if the CSS property is not specified....
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