singerla / pptx-automizer

A template based pptx generator for Node.js
MIT License
61 stars 12 forks source link

Unable to modify empty table #76

Closed chriscappy16 closed 1 month ago

chriscappy16 commented 11 months ago

This is probably a fairly small issue, but with the exact sample code in the README, I receive an error when attempting to modify a completely empty table with two cells and no text. The text on the link says "Insert data into table with empty cells," so I assumed no data was needed. Once I add data, it works properly. Note: you have to add data to EVERY cell. If I have a 1 x 2 table and I want to add data to both columns, the template must have text in both in order to not receive an error. https://github.com/singerla/pptx-automizer#modify-tables

Here's the full stack if you want it:

TypeError: Cannot read properties of undefined (reading 'parentNode') at XmlHelper.insertAfter (C:\Code\fortis-financial\node_modules\pptx-automizer\dist\helper\xml-helper.js:329:30) at XmlElements.text (C:\Code\fortis-financial\node_modules\pptx-automizer\dist\helper\xml-elements.js:18:32) at ModifyXmlHelper.createElement (C:\Code\fortis-financial\node_modules\pptx-automizer\dist\helper\modify-xml-helper.js:66:52) at ModifyXmlHelper.assertElement (C:\Code\fortis-financial\node_modules\pptx-automizer\dist\helper\modify-xml-helper.js:46:22) at ModifyXmlHelper.modify (C:\Code\fortis-financial\node_modules\pptx-automizer\dist\helper\modify-xml-helper.js:27:34) at ModifyXmlHelper.modify (C:\Code\fortis-financial\node_modules\pptx-automizer\dist\helper\modify-xml-helper.js:39:22) at ModifyXmlHelper.modify (C:\Code\fortis-financial\node_modules\pptx-automizer\dist\helper\modify-xml-helper.js:39:22) at C:\Code\fortis-financial\node_modules\pptx-automizer\dist\modify\modify-table.js:56:28 at Array.forEach () at C:\Code\fortis-financial\node_modules\pptx-automizer\dist\modify\modify-table.js:54:24 at Array.forEach () at ModifyTable.setRows (C:\Code\fortis-financial\node_modules\pptx-automizer\dist\modify\modify-table.js:53:24) at ModifyTable.modify (C:\Code\fortis-financial\node_modules\pptx-automizer\dist\modify\modify-table.js:46:14) at C:\Code\fortis-financial\node_modules\pptx-automizer\dist\helper\modify-table-helper.js:9:14 at C:\Code\fortis-financial\node_modules\pptx-automizer\dist\classes\shape.js:119:21 at Array.forEach ()

singerla commented 11 months ago

Thanks for pointing this out. A basic principle of pptx-automizer is that it is very good in duplicating existing contents, but quite bad in creating new xml elements. Although this is basically implement (see ModifyXmlHelper.createElement), it is not working in many cases.

Assuming you have full control of all .pptx templates, you will need to insert dummy content in all the places where automizer has no xml snipped to create an empty element.

  createElement(parent: XmlDocument | XmlElement, tag: string): boolean {
    switch (tag) {
      case 'a:t':
        new XmlElements(parent).text();
        return true;
      case 'c:dPt':
        new XmlElements(parent).dataPoint();
        return true;
      case 'c:spPr':
        new XmlElements(parent).shapeProperties();
        return true;
      case 'c:dLbl':
        new XmlElements(parent).dataPointLabel();
        return true;
      case 'a:lnL':
      case 'a:lnR':
      case 'a:lnT':
      case 'a:lnB':
        new XmlElements(parent).tableCellBorder(tag);
        return true;
    }
    return false;
  }