Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XLSX Module : create new sheets in workbook with a simple loop #570

Open
LudovicLaloux opened this issue Nov 2, 2020 · 1 comment
Open

Comments

@LudovicLaloux
Copy link

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
doc.setData({
	name: "John Doe",
	totalPrice: {
		type: "currency",
		value: 100,
	},
	discount: {
		type: "percent",
		value: 0.195,
	},
	items: [
		{
			name: "First product",
			quantity: 1,
			unit_price: { type: "currency", value: 200 },
		},
		{
			name: "Other product",
			quantity: 3,
			unit_price: { type: "currency", value: 100 },
		},
	],
};);

//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, with new sheets created inside the workbook:
generated.zip

With the module pptx-slides, we can create new slides inside a pptx template with a simple loop.
It would be a great improvement to create new sheets inside an Excel Workbook with the same kind of syntax.

@edi9999
Copy link
Member

edi9999 commented Nov 3, 2020

Hello, this is definitely a feature that I will add sometime.

Right now, I have a lot of priorities and not enough time, so it will have to wait.

For the API, I'm not sure the loop would be directly in the sheetname, like you did here :

Selection_001

Maybe this could be a tag like what we do for the slides module :

{:repeat-sheet items}

Also, I'm not sure yet how to handle the names of the generated sheetnames.

Maybe requiring to have a datastructure like this :

{
  items: [
    {
      sheet: "Item Foo",
      data: {
        name: "First product",
        quantity: 1,
        unit_price: { type: "currency", value: 200 },
      },
    },
    {
      sheet: "Item Bar",
      data: {
        name: "Other product",
        quantity: 3,
        unit_price: { type: "currency", value: 100 },
      },
    },
  ];
}

To generate two sheets named Item Foo and Item Bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants