Skip to content

XLSX Module : create a subloop which merge cells instead of repeating them #571

@LudovicLaloux

Description

@LudovicLaloux

Environment

  • Version of docxtemplater : 3.19.6
  • Used docxtemplater-modules : "docxtemplater-xlsx-module"
  • Runner : Browser

How to reproduce my problem :

My template is the following (rename template.xlsx) :
template.zip

With the following js file :

var fs = require('fs');
var Docxtemplater = require('docxtemplater');
var XlsxModule = require("./docxtemplater-xlsx-module");
var xlsxModule = new XlsxModule({});

//Load the docx file as a binary
var content = fs
    .readFileSync(__dirname + "/template.zip", "binary");

var zip = new PizZip(content);
var doc=new Docxtemplater()
doc.attachModule(xlsxModule)
doc.loadZip(zip)

//set the templateVariables
const data = {
	name: "John Doe",
	category: [
		{
            CategoryName: "Category 1",
            items: [
                {
                    name: "product 1",
                    quantity: 1,
                },
                {
                    name: "Product 2",
                    quantity: 3,
                },
            ],
        },
        {
            CategoryName: "Category 2",
            items: [
                {
                    name: "Product 3",
                    quantity: 7,
                },
                {
                    name: "Product 4",
                    quantity: 5,
                },
            ],
        },
        {
            CategoryName: "Category 3",
            items: [
                {
                    name: "Product 5",
                    quantity: 42,
                },
            ]
        }
    ]
};

//apply them (replace all occurences of {first_name} by Hipp, ...)
doc.render();

var buf = doc.getZip()
             .generate({type:"nodebuffer"});

fs.writeFileSync(__dirname+"/output.docx",buf);

I would expect it to return the following template :
generated.zip

When doing subloops, the row is repeted as many times as available entries. It would be nice to merge the repeted cells instead of repeting them.
We can use a "slice" filter to add subloops without repeating rows like this :

expressions.filters.slice = function(array, num) {
	return array.slice(num);
}

but this method would create unwanted lines if there is only one item on the subloop.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions