EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
289 stars 50 forks source link

Figuring out NestedTables #244

Closed ChrisBellBO closed 2 months ago

ChrisBellBO commented 2 months ago

Hi I'm trying to figure out how nested tables work. I have a Word document with a table containing two nested tables. I can see WordTable has a NestedTables property which contains the two tables but I can't find a way to get the parent cell for each nested table, am I missing something?

PrzemyslawKlys commented 2 months ago

I am not sure I understand your question. Each Table has Cells/Rows and so on. Same with nested tables?

PrzemyslawKlys commented 2 months ago

Oh, i get it. You want to know which cell has nested table. Hrmms

ChrisBellBO commented 2 months ago

Yes, that's it

PrzemyslawKlys commented 2 months ago

I guess you would need add new property in Cell to include something. This is in the Table itself

        public bool HasNestedTables {
            get {
                foreach (var cell in this.Cells) {
                    var list = cell._tableCell.Descendants<Table>().ToList();
                    if (list.Count > 0) {
                        return true;
                    }
                }
                return false;
            }
        }
        public List<WordTable> NestedTables {
            get {
                var listReturn = new List<WordTable>();
                foreach (var cell in this.Cells) {
                    var list = cell._tableCell.Descendants<Table>().ToList();
                    foreach (var table in list) {
                        listReturn.Add(new WordTable(this._document, table));
                    }
                }
                return listReturn;
            }
        }

So as part of specific Cell, just adjust the logic to it can be per Cell as well.

ChrisBellBO commented 2 months ago

Looks good, I will give it a go, thanks

PrzemyslawKlys commented 2 months ago

Solved by: https://github.com/EvotecIT/OfficeIMO/pull/240