Create An Ordered List In DOCX JS
JavaScript
06/10/2022
Regarding ordered or numbered lists, I've found the DOCX documentation to be quite sparse, even differing π from provided examples on their GitHub page. Here's what has worked for me.
The configuration of your ordered list is set in the Document
object:
JAVASCRIPT
const doc = new docx.Document({ // numbering: { ... } - insert ordered list configuration here})
In the configuration object, you may have one or multiple styles defined. Notice π that the leveling starts at 0
.
JAVASCRIPT
const numbering = { config: [ // May contain multiple styles { reference: "numbering-style-1", levels: [ { level: 0, format: "upperRoman", text: "%1)", // results in '1)', '2)', etc. alignment: AlignmentType.START, style: { paragraph: { indent: { left: 720, hanging: 260 }, }, }, }, { level: 1, format: "decimal", text: "%2.", // results in '1.', '2.', etc. alignment: AlignmentType.START, style: { paragraph: { // Increase indentation for nested levels indent: { left: 1440, hanging: 980 }, }, }, }, // And so on... ], }, ],}
In the body of your document, you need to explicitly inform your list to use your custom numbering style by referencing its ID. In addition, you also need to define the level of each list item.
JAVASCRIPT
new Paragraph({ text: "Some ordered item", numbering: { reference: "numbering-style-1", level: 0, },})