Issue with TOC Update After Using `.MergeCell()` in Document Builder

Do you want to: Report a bug
For bug reports, provide the steps to reproduce and if possible a minimal demo of the problem:

When generating a document that includes tables utilizing the .MergeCell() method, updating the table of contents (TOC) with the .UpdateAllTOC() method causes the Document Builder to throw the following error:

TypeError: Cannot read property 'p2a' of undefined
  at GC (<anonymous>:19529:375)
  at oe.z.OMa (<anonymous>:19731:237)
  at Je.z.OMa (<anonymous>:20937:261)
  at kj.z.OMa (<anonymous>:21794:370)
  at HK.z.rZ (<anonymous>:21113:597)
  at ne.z.eya (<anonymous>:20410:337)
  at HK.z.oXb (<anonymous>:21171:436)
  at RK (<anonymous>:20550:354)
  at ne.z.jI (<anonymous>:20357:154)
  at ne.z.lh (<anonymous>:20433:253)

This issue occurs when .MergeCell() is applied to a table row or individual Cells, as tested with the following methods:

When the .MergeCell() method is excluded, the TOC updates correctly.

Steps to Reproduce

  1. Generate a document with a table and TOC using the script below.
  2. Verify that the table of contents was not updated as shown in the picture bellow. (And that the Builder throws a TypeError).
  3. Generate a document with a table and TOC using the script below, but this time removing the .MergeCell() Method (Follow the Script Comments).
  4. Verify that the table of contents was updated as shown in the picture bellow.

Expected Behavior
The TOC should update without any errors.

Actual Behavior
The Document Builder throws a TypeError during TOC update.

Document Builder Script

// Create a text document in .docx format
builder.CreateFile("docx");
var oDocument = Api.GetDocument();

// Add TOC
var oTocPr = { "ShowPageNums": true, "RightAlgn": true, "LeaderType": "dot", "FormatAsLinks": true, "BuildFrom": { "OutlineLvls": 2 }, "TocStyle": "simple" };
oDocument.AddTableOfContents(oTocPr);

// Add headings and paragraphs
var oHeadingLvl1Style = oDocument.GetStyle("Heading 1");
var oHeadingLvl2Style = oDocument.GetStyle("Heading 2");

var oParagraph = Api.CreateParagraph();
oParagraph.AddText("Heading 1 title (generated)");
oParagraph.SetStyle(oHeadingLvl1Style);
oParagraph.SetIndLeft(550);
oDocument.Push(oParagraph);

oParagraph = Api.CreateParagraph();
oParagraph.AddText("This is some demonstrative paragraph just to fill out the page.");
oDocument.Push(oParagraph);

oParagraph = Api.CreateParagraph();
oParagraph.AddText("Heading 2 title (generated)");
oParagraph.SetStyle(oHeadingLvl2Style);
oParagraph.SetIndLeft(550);
oDocument.Push(oParagraph);

// Create a table
var table = Api.CreateTable(3, 3);
table.SetWidth("percent", 100);
oDocument.Push(table);

// Fill table cells
for (var i = 0; i < 2; i++) {
    for (var j = 0; j < 3; j++) {
        var oCell = table.GetCell(i, j);
        var oCellParagraph = oCell.GetContent().GetElement(0);
        oCellParagraph.AddText("Some text.");
    }
}

// Merge table cells (causes the error)
var lastRow = table.GetRow(2);
lastRow.MergeCells();

// Fill merged cell
var lastRowCell = table.GetCell(2, 0);
var lastRowCellParagraph = lastRowCell.GetContent().GetElement(0);
lastRowCellParagraph.SetJc("both");
lastRowCellParagraph.AddText("Last Row Text");

// Add more content
oParagraph = Api.CreateParagraph();
oParagraph.AddText("Heading 2 title (generated)");
oParagraph.SetStyle(oHeadingLvl2Style);
oParagraph.SetIndLeft(550);
oDocument.Push(oParagraph);

oParagraph = Api.CreateParagraph();
oParagraph.AddText("This is some demonstrative paragraph just to fill out the page.");
oDocument.Push(oParagraph);

// Update TOC
oDocument.UpdateAllTOC(false);

// Save document
builder.SaveFile("docx", "te.docx");
builder.CloseFile();

Attachments

  1. Cells Not Merged (TOC Updated)
  2. Cells Merged (TOC Update Error)

DocumentBuilder version: latest using the Python Builder.Framework with the .Run() method (documentation here)
Installation method: Downloaded the package from the official page and installed it using sudo dnf install <package name> .
OS: Rocky Linux 9 in a docker container

Hello @mrmikept
Thank you for the detailed description!
I was able to reproduce the first issue with the provided script (‘Cannot read property ‘p2a’ of undefined’ error entry). We need some time to take a closer look at it.

How about the provided ‘Cells Merged (TOC Update Error)’ file? How exactly you were able to create it if the DocBuilder reports ‘Cannot read property p2a’ error with the script?

Hi @Alexandre

I am using a custom-developed tool built with the Python Builder.Framework. This tool processes .docbuilder files in separate “parts” and saves the result using the SaveFile() method from the Builder.Framework. Each script executed with the .Run() method represents a distinct “part” of the document. For example, I have scripts to generate the table of contents (TOC), others for headings, tables, and so on. Each object in the document corresponds to its own .docbuilder script.

Here’s an example of how my code looks:

builder = docbuilder.CDocBuilder()
builder.CreateFile("docx")
for script in script_list:
   builder.Run(script)    // This can also be builder.RunText(script_text)
builder.SaveFile("docx", "result.docx")
builder.CloseFile()

This isn’t my exact code, but it represents the “core” functionality in this specific scenario.

Note: The script I shared in the initial post combines multiple .docbuilder files, adding the createFile and SaveFile methods. I’ll include some comments in the script to give you a better idea.

// Create a text document in .docx format
builder.CreateFile("docx"); // The CreateFile method is executed by the Builder.Framework 

var oDocument = Api.GetDocument();

// Beginning of the TOC script
var oTocPr = { "ShowPageNums": true, "RightAlgn": true, "LeaderType": "dot", "FormatAsLinks": true, "BuildFrom": { "OutlineLvls": 2 }, "TocStyle": "simple" };
oDocument.AddTableOfContents(oTocPr);
// Ending of the TOC script

// Beginning  of the styles script
// Add headings and paragraphs
var oHeadingLvl1Style = oDocument.GetStyle("Heading 1");
var oHeadingLvl2Style = oDocument.GetStyle("Heading 2");
// Ending of the styles script

// Beginning of the Heading 1 script
var oParagraph = Api.CreateParagraph();
oParagraph.AddText("Heading 1 title (generated)");
oParagraph.SetStyle(oHeadingLvl1Style);
oParagraph.SetIndLeft(550);
oDocument.Push(oParagraph);
// Ending of the Heading 1 script

// Beginning of the paragraph script
oParagraph = Api.CreateParagraph();
oParagraph.AddText("This is some demonstrative paragraph just to fill out the page.");
oDocument.Push(oParagraph);
// Ending of the paragraph script

// Beginning of the Heading 2 script
oParagraph = Api.CreateParagraph();
oParagraph.AddText("Heading 2 title (generated)");
oParagraph.SetStyle(oHeadingLvl2Style);
oParagraph.SetIndLeft(550);
oDocument.Push(oParagraph);
// Ending of the Heading 2 script

// Beginning of the Table script
// Create a table
var table = Api.CreateTable(3, 3);
table.SetWidth("percent", 100);
oDocument.Push(table);

// Fill table cells
for (var i = 0; i < 2; i++) {
    for (var j = 0; j < 3; j++) {
        var oCell = table.GetCell(i, j);
        var oCellParagraph = oCell.GetContent().GetElement(0);
        oCellParagraph.AddText("Some text.");
    }
}

// Merge table cells (causes the error)
var lastRow = table.GetRow(2);
lastRow.MergeCells();

// Fill merged cell
var lastRowCell = table.GetCell(2, 0);
var lastRowCellParagraph = lastRowCell.GetContent().GetElement(0);
lastRowCellParagraph.SetJc("both");
lastRowCellParagraph.AddText("Last Row Text");
// Ending of the Table script

// Beginning of the Heading 2 script
// Add more content
oParagraph = Api.CreateParagraph();
oParagraph.AddText("Heading 2 title (generated)");
oParagraph.SetStyle(oHeadingLvl2Style);
oParagraph.SetIndLeft(550);
oDocument.Push(oParagraph);
// Ending of the Heading 2 script

// Beginning of the Paragraph script
oParagraph = Api.CreateParagraph();
oParagraph.AddText("This is some demonstrative paragraph just to fill out the page.");
oDocument.Push(oParagraph);
// Ending of the Paragraph script

// Beginning of the update TOC Script
// Update TOC
oDocument.UpdateAllTOC(false);
// Ending of the update TOC Script

// This part is executed with the .SaveFile and .CloseFile methods in the Builder.Framework
// Save document
builder.SaveFile("docx", "te.docx");
builder.CloseFile();

If you’d like more details about my custom-developed tool, I shared a post a few months ago discussing how to use arguments with docbuilder scripts. In that post, I provide an overview of the tool I created for document generation. You can check it out here:

Hello @mrmikept
Thank you for the clarification, we are checking the situation.

Dear @mrmikept
We have found a bug in the described scenario and we have started working on it. Thank you for the valuable data! We will update this thread once we release a fix.

1 Like

Hello @Alexandre,

Thank you for your feedback! I’ll await any updates from you.

1 Like