Docx: Numbering levels do not apply when called as instance methods (static required)

Created on 27 Aug 2019  路  5Comments  路  Source: dolanmiu/docx

Numbering does not appear to be responding to what is defined in createLevel method. See the screenshot below, which is generated by the code that follows.

image

Adjusting the indent levels or list type (e.g. upperRoman vs decimal) in createLevel() has no effect. I tested this in three different ways, commented as OPTION 1, OPTION 2, OPTION 3 in the code below. In each option the output looks the same.

var fs = require('fs')
var docx = require('docx');

const doc = new docx.Document();

const numbering = new docx.Numbering();

const abstractNum = numbering.createAbstractNumbering();

// OPTION 1: No createLevel

// OPTION 2:
// abstractNum.createLevel(0, "upperRoman", "%1", "start").addParagraphProperty(new docx.Indent(360, 130));
// abstractNum.createLevel(1, "decimal", "%2", "start").addParagraphProperty(new docx.Indent(1200, 3000));
// abstractNum.createLevel(2, "lowerLetter", "%3    ", "start").addParagraphProperty(new docx.Indent(4000, 5000));

// OPTION 3;
// abstractNum.createLevel(0, "upperRoman", "%1", "start").indent({ left: 360, hanging: 130 });
// abstractNum.createLevel(1, "decimal", "%1.%2.", "start").indent({ left: 1200, hanging: 3000 });
// abstractNum.createLevel(2, "lowerLetter", "%3)", "start").indent({ left: 4000, hanging: 5000 });

const concrete = numbering.createConcreteNumbering(abstractNum);

doc.addSection({
    children: [
        new docx.Paragraph({ text: "Item 1", numbering: { num: concrete, level: 0 }}),
        new docx.Paragraph({ text: "Item 1.1", numbering: { num: concrete, level: 1 }}),
        new docx.Paragraph({ text: "Item 1.2", numbering: { num: concrete, level: 1 }}),
        new docx.Paragraph({ text: "Item 1.2.1", numbering: { num: concrete, level: 2 }}),
        new docx.Paragraph({ text: "Item 1.2.2", numbering: { num: concrete, level: 2 }}),
        new docx.Paragraph({ text: "Item 1.2.3", numbering: { num: concrete, level: 2 }}),
        new docx.Paragraph({ text: "Item 1.3", numbering: { num: concrete, level: 1 }}),
        new docx.Paragraph({ text: "Item 2", numbering: { num: concrete, level: 0 }}),
        new docx.Paragraph({ text: "Item 3", numbering: { num: concrete, level: 0 }}),
    ],
});

docx.Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("docx-simple-num.docx", buffer);
});

Using docx version 5.0.0-rc5 and Node v10.16.3

Most helpful comment

EDIT: This comment has been moved to a separate issue, #396.

Perhaps this is a separate issue, but in the interest of not flooding the issues list I tack it on here: paragraphs with numbering defined do not respond to style. See the example below, followed by the code that generated it.

image

Code:

var fs = require('fs')
var docx = require('docx');

const doc = new docx.Document();

doc.Styles.createParagraphStyle("numberdemo", "Number Demo")
    .basedOn("Normal")
    .next("Normal")
    .font("Arial")
    .quickFormat()
    .size(40);

const numbering = new docx.Numbering();

const abstractNum = numbering.createAbstractNumbering();

abstractNum.createLevel(0, "upperRoman", "%1", "start").indent({ left: 360, hanging: 130 });
abstractNum.createLevel(1, "decimal", "%1.%2.", "start").indent({ left: 1200, hanging: 3000 });
abstractNum.createLevel(2, "lowerLetter", "%3)", "start").indent({ left: 4000, hanging: 5000 });

const concrete = numbering.createConcreteNumbering(abstractNum);

doc.addSection({
    children: [
        new docx.Paragraph({ text: "No numbering", style: "numberdemo" }),
        new docx.Paragraph({ text: "No numbering, no style" }),
        new docx.Paragraph({ text: "Item 1", numbering: { num: concrete, level: 0 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "Item 1.1", numbering: { num: concrete, level: 1 }}),
        new docx.Paragraph({ text: "Item 1.2", numbering: { num: concrete, level: 1 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "Item 1.2.1", numbering: { num: concrete, level: 2 }}),
        new docx.Paragraph({ text: "Item 1.2.2", numbering: { num: concrete, level: 2 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "Item 1.2.3", numbering: { num: concrete, level: 2 }}),
        new docx.Paragraph({ text: "Item 1.3", numbering: { num: concrete, level: 1 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "Item 2", numbering: { num: concrete, level: 0 }}),
        new docx.Paragraph({ text: "Item 3", numbering: { num: concrete, level: 0 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "No numbering 2", style: "numberdemo" }),
        new docx.Paragraph({ text: "No numbering, no style 2" })
    ],
});

docx.Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("docx-simple-num.docx", buffer);
});

All 5 comments

EDIT: This comment has been moved to a separate issue, #396.

Perhaps this is a separate issue, but in the interest of not flooding the issues list I tack it on here: paragraphs with numbering defined do not respond to style. See the example below, followed by the code that generated it.

image

Code:

var fs = require('fs')
var docx = require('docx');

const doc = new docx.Document();

doc.Styles.createParagraphStyle("numberdemo", "Number Demo")
    .basedOn("Normal")
    .next("Normal")
    .font("Arial")
    .quickFormat()
    .size(40);

const numbering = new docx.Numbering();

const abstractNum = numbering.createAbstractNumbering();

abstractNum.createLevel(0, "upperRoman", "%1", "start").indent({ left: 360, hanging: 130 });
abstractNum.createLevel(1, "decimal", "%1.%2.", "start").indent({ left: 1200, hanging: 3000 });
abstractNum.createLevel(2, "lowerLetter", "%3)", "start").indent({ left: 4000, hanging: 5000 });

const concrete = numbering.createConcreteNumbering(abstractNum);

doc.addSection({
    children: [
        new docx.Paragraph({ text: "No numbering", style: "numberdemo" }),
        new docx.Paragraph({ text: "No numbering, no style" }),
        new docx.Paragraph({ text: "Item 1", numbering: { num: concrete, level: 0 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "Item 1.1", numbering: { num: concrete, level: 1 }}),
        new docx.Paragraph({ text: "Item 1.2", numbering: { num: concrete, level: 1 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "Item 1.2.1", numbering: { num: concrete, level: 2 }}),
        new docx.Paragraph({ text: "Item 1.2.2", numbering: { num: concrete, level: 2 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "Item 1.2.3", numbering: { num: concrete, level: 2 }}),
        new docx.Paragraph({ text: "Item 1.3", numbering: { num: concrete, level: 1 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "Item 2", numbering: { num: concrete, level: 0 }}),
        new docx.Paragraph({ text: "Item 3", numbering: { num: concrete, level: 0 }, style: "numberdemo" }),
        new docx.Paragraph({ text: "No numbering 2", style: "numberdemo" }),
        new docx.Paragraph({ text: "No numbering, no style 2" })
    ],
});

docx.Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("docx-simple-num.docx", buffer);
});

I have the exact same problem

I've been trying to troubleshoot this and identify where the problem started. In doing so, I noticed that the RunKit specified in the README for 4.0.0 as well as 1.0.0 are exhibiting these issues as well (i.e. not showing upperRoman or lowerLetter list types). I tested 5.x, 4.x, and 3.x versions and found this issue everywhere, but then realized that 3.6.0/demo/demo2.js _does work_ (i.e. displays list as lowerLetter), but 3.6.0/demo/demo3.js _does not_ (displays default decimal). Following demo2.js as an example, I applied the following diff to 3.6.0/demo/demo3.js to make it work:

 var doc = new docx.Document();

-const numbering = new docx.Numbering();
+const abstractNum = doc.Numbering.createAbstractNumbering();

-const abstractNum = numbering.createAbstractNumbering();
 abstractNum.createLevel(0, "upperRoman", "%1", "start")
     .addParagraphProperty(new docx.Indent(720, 260));
 abstractNum.createLevel(1, "decimal", "%2.", "start")
     .addParagraphProperty(new docx.Indent(1440, 980));
 abstractNum.createLevel(2, "lowerLetter", "%3)", "start")
     .addParagraphProperty(new docx.Indent(2160, 1700));

-const concrete = numbering.createConcreteNumbering(abstractNum);
+const concrete = doc.Numbering.createConcreteNumbering(abstractNum);

 var topLevelP = new docx.Paragraph("Hey you");
 var subP = new docx.Paragraph("What's up fam");

I applied this same fix to the RunKit demo here, and it works. Also works locally with 5.x.

In summary, it appears to be working if createAbstractNumbering() and createConcreteNumbering() are called as static vs instance methods.

Renamed issue to account for the fact that it is possible to alter numbering format, bu only if done as described in the comment above. Additionally I'll open a separate issue for the paragraph styling problem (comment 2).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

liquidslr picture liquidslr  路  4Comments

barberousse picture barberousse  路  5Comments

iainfreestone picture iainfreestone  路  3Comments

zahrafali picture zahrafali  路  3Comments

GiladFeder picture GiladFeder  路  6Comments