xlsx-template icon indicating copy to clipboard operation
xlsx-template copied to clipboard

Placeholder in table header names may lead to corrupted generated xlsx file

Open hervenivon opened this issue 8 years ago • 0 comments

Using an object to substitute placeholders in table header names results in an Excel file that needs to be repaired while the replacement is done. Templates are joint.

This works fine:

// Load an XLSX file into memory
fs.readFile('./template2.xlsx', function (err, data) {

    // Create a template
    var template = new XlsxTemplate(data);

    // Replacements take place on first sheet
    var sheetNumber = 1;

    // Set up some placeholder values matching the placeholders in the template
    var values = {
        columnName: 'Will have to be repaired'
    };

    // Perform substitution
    template.substitute(sheetNumber, values);

    // Get binary data
    var fileData = template.generate();

    fs.writeFileSync('./output2.xlsx', fileData, 'binary');
});

This doesn't:

// Load an XLSX file into memory
fs.readFile('./template1.xlsx', function (err, data) {

    // Create a template
    var template = new XlsxTemplate(data);

    // Replacements take place on first sheet
    var sheetNumber = 1;

    // Set up some placeholder values matching the placeholders in the template
    var values = {
        column: { name: 'Will have to be repaired'}
    };

    // Perform substitution
    template.substitute(sheetNumber, values);

    // Get binary data
    var fileData = template.generate();

    fs.writeFileSync('./output1.xlsx', fileData, 'binary');
});

template1.xlsx template2.xlsx

hervenivon avatar Dec 06 '17 22:12 hervenivon