Skip to content

Adding item to existing numbered list with patchDocument #3399

@voussoir

Description

@voussoir

Hi, thanks for this project.

I am creating this as an issue rather than a pull request as I am not familiar enough with your project to make a PR-ready contribution. I just needed to hack a change for my own use case.

This is basically what I did:

if (options.bullet) {
this.push(new NumberProperties(1, options.bullet.level));
}
if (options.numbering) {

       if (options.bullet) {
         this.push(new NumberProperties(1, options.bullet.level));
       }
+      if (options.abstractNumbering) {
+        this.push(new NumberProperties(options.abstractNumbering.id, options.abstractNumbering.level));
+      }
       if (options.numbering) {

Then calling:

new docx.Paragraph({text: "SAMPLE TEXT", abstractNumbering:{id: 10, level:0})

Here is the background:

I am using patchDocument to fill out a template, and I'm making one item in a numbered list optional. If the user wants to use that item, I do patchDocument with PatchType.DOCUMENT and provide a children array with the numbered list item; and if they don't then I provide an empty array which deletes the paragraph. Because it's a numbered list, the subsequent items continue the count.

Because I am not creating the document from scratch, I am not defining the Numbering references myself. I am just trying to insert an item into the existing numbered list that was already on the template. I opened the Word file's internal XML directly and saw that the existing numbered list uses <w:numId w:val="10">. I could not figure out how to make docx.js output a simple "10" because properties.ts always outputs ${options.numbering.reference}-${options.numbering.instance ?? 0}.

if (options.numbering) {
this.numberingReferences.push({
reference: options.numbering.reference,
instance: options.numbering.instance ?? 0,
});
this.push(new NumberProperties(`${options.numbering.reference}-${options.numbering.instance ?? 0}`, options.numbering.level));

So I could not figure out what to pass into docx.js's paragraph option for numbering: {reference: "10", level: 0}, because the output was always <w:numId w:val="{10-0}"> and corrupt/unreadable by Word.

I needed some way to get <w:numId w:val="10"> so I added an extra clause for options.abstractNumbering and passed abstractNumbering in my Paragraph constructor (this is maybe a misinterpretation of what your AbstractNumbering class is supposed to mean, but I guess the naming doesn't matter for what I'm doing with it).

This is my template file:

Image

Called without the optional item:

Image

Called with the optional item:

const dynamic_children = [
    new docx.Paragraph({children: [new docx.TextRun({text: "SAMPLE TEXT", bold: true})], abstractNumbering:{id: 10, level:0}, spacing:{after: 120}}),
    new docx.Paragraph({text: "Alpha", indent: {left: 720, hanging: 0}, spacing:{after: 120}}),
];

const replaced = await docx.patchDocument({
    outputType: "arraybuffer",
    data: buffer,
    patches: {
        dynamic_section: {
            type: docx.PatchType.DOCUMENT,
            children: dynamic_children,
        },
    },
});

Image

Is there another way I was supposed to do this? I kinda struggled with the documentation.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions