dtjohnson / xlsx-populate

Excel XLSX parser/generator written in JavaScript with Node.js and browser support, jQuery/d3-style method chaining, encryption, and a focus on keeping existing workbook features and styles in tact.
MIT License
954 stars 182 forks source link

Workaround: page setup, fit to page, page breaks #140

Open coreprocess opened 6 years ago

coreprocess commented 6 years ago

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',
    }
});
dtjohnson commented 6 years ago

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

Sanitha-11224 commented 2 years ago

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

thisadee2897 commented 1 year ago

sheet.printOptions().orientation("landscape"); šŸ„²

Sheet.printOptions: Invalid arguments.