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

Workaround: page setup, fit to page, page breaks

Open coreprocess opened this issue 6 years ago • 3 comments

We found the following workaround regarding a fit to page configuration based on a blank workbook:

import Excel from 'xlsx-populate/browser/xlsx-populate.js';
import downloadjs from 'downloadjs';

// ...

// create workbook
const wb = await Excel.fromBlankAsync();
const ws = wb.sheet(0);

// add some content
// ...

// page and print configuration
ws._sheetPrNode.children.push({
    name: "pageSetUpPr",
    children: [ ],
    attributes: {
        fitToPage: '1',
    }
});

ws._node.children.push({
    name: "pageSetup",
    children: [ ],
    attributes: {
        paperSize: '9',
        orientation: 'landscape',
        fitToWidth: '1',
        fitToHeight: '0',
    }
});

// download as file
downloadjs(
    await wb.outputAsync(),
    fileName + '.xlsx',
    Excel.MIME_TYPE
);

// ...

In case you want to set manual pake breaks, the following snippet might help:

ws._node.children.push({
    name: "colBreaks",
    children: [{
        name: "brk",
        children: [ ],
        attributes: {
            id: '17',  // column number
            max: '1048575',
            man: '1',
        }
    }],
    attributes: {
        count: '1',
        manualBreakCount: '1',
    }
});

coreprocess avatar Jun 14 '18 15:06 coreprocess

Feel free to submit a PR with proper methods for this. :)

dtjohnson avatar Jul 23 '18 19:07 dtjohnson

Can you please help me add a header and footer to all pages while printing?

Sanitha-11224 avatar Mar 16 '22 05:03 Sanitha-11224

sheet.printOptions().orientation("landscape"); 🥲

Sheet.printOptions: Invalid arguments.

thisadee2897 avatar Jun 19 '23 05:06 thisadee2897