Giter Club home page Giter Club logo

xlsx-populate's Introduction

view on npm npm module downloads per month Build Status Dependency Status

xlsx-populate

Node.js module to populate Excel XLSX templates. This module does not parse Excel workbooks. There are good modules for this already. The purpose of this module is to open existing Excel XLSX workbook templates that have styling in place and populate with data.

Installation

$ npm install xlsx-populate

Usage

Here is a basic example:

var Workbook = require('xlsx-populate');

// Load the input workbook from file.
var workbook = Workbook.fromBlankSync();

// Modify the workbook.
workbook.getSheet(0).getCell("A1").setValue("This is neat!");

// Write to file.
workbook.toFileSync("./out.xlsx");

Getting Sheets

You can get sheets from a Workbook object by either name or index (0-based):

// Get sheet with name "Sheet1".
var sheet = workbook.getSheet("Sheet1");

// Get the first sheet.
var sheet = workbook.getSheet(0);

Getting Cells

You can get a cell from a sheet by either address or row and column:

// Get cell "A5" by address.
var cell = sheet.getCell("A5");

// Get cell "A5" by row and column.
var cell = sheet.getCell(5, 1);

You can also get named cells directly from the Workbook:

// Get cell named "Foo".
var cell = sheet.getNamedCell("Foo");

Setting Cell Contents

You can set the cell value or formula:

cell.setValue("foo");
cell.setValue(5.6);
cell.setFormula("SUM(A1:A5)");

You can set multiple values along a row:

// Operate on row 4, set values at A4 and D4
sheet.setColumnValues(4, { 'A': 'abc', 'D': 123 });

// Same operation
sheet.setColumnValues(4, { 1: 'abc', 4: 123 });

Share Formula with Cells

You can share a formula at a given cell, along a row or column:

// Share formula at A3 along row ending with C3
sheet.getCell('A3').shareFormulaUntil('C3');

// Share formula at C1 along column ending with C3
sheet.getCell('C1').shareFormulaUntil('C3');

Get Range of Cells

You can get a region of cells:

// The following performs the same operation
sheet.getCellRange('A1:B2');
sheet.getCellRange(['A1', 'B2']);
sheet.getCellRange('A1', 'B2');
sheet.getCellRange(sheet.getCell('A1'), sheet.getCell('B2'));

// Each returns:
// [ sheet.getCell('A1'), sheet.getCell('A2'), sheet.getCell('B1'), sheet.getCell('B2') ]

Serving from Express

You can serve the workbook with express with a route like this:

router.get("/download", function (req, res) {
    // Open the workbook.
    var workbook = Workbook.fromFile("input.xlsx", function (err, workbook) {
        if (err) return res.status(500).send(err);

        // Make edits.
        workbook.getSheet(0).getCell("A1").setValue("foo");

        // Set the output file name.
        res.attachment("output.xlsx");

        // Send the workbook.
        res.send(workbook.output());
    });
});

Development

Running Tests

Tests are run automatically on Travis CI. They can (and should) be triggered locally with:

$ npm test

Code Linting

JSHint and JSCS are used to ensure code quality. To run these, run:

$ npm run jshint
$ npm run jscs

Generating Documentation

The API reference documentation below is generated by jsdoc-to-markdown. To generate an updated README.md, run:

$ npm run docs

API Reference

Classes

Cell
Sheet
Workbook
## Cell **Kind**: global class

new Cell(sheet, row, column, cellNode)

Initializes a new Cell.

Param Type
sheet Sheet
row number
column number
cellNode etree.SubElement

cell.getSheet() ⇒ Sheet

Gets the parent sheet.

Kind: instance method of Cell

cell.getRow() ⇒ number

Gets the row of the cell.

Kind: instance method of Cell

cell.getColumn() ⇒ number

Gets the column of the cell.

Kind: instance method of Cell

cell.getAddress() ⇒ string

Gets the address of the cell (e.g. "A5").

Kind: instance method of Cell

cell.getFullAddress() ⇒ string

Gets the full address of the cell including sheet (e.g. "Sheet1!A5").

Kind: instance method of Cell

cell.setValue(value) ⇒ Cell

Sets the value of the cell. Returns itself.

Kind: instance method of Cell

Param Type
value *

cell.setFormula(formula, calculatedValue, sharedIndex, sharedRef) ⇒ Cell

Sets the formula for a cell (with optional precalculated value). Returns itself.

Kind: instance method of Cell

Param Type
formula string
calculatedValue *
sharedIndex number
sharedRef string

cell.isSharedFormula(isSource) ⇒ boolean

Check if the current cell is a shared formula. When isSource is set true, the function will check if the formula is defined.

Kind: instance method of Cell

Param Type
isSource boolean

cell.hasSameRow(otherCell) ⇒ boolean

Check if both cells share the same row. Return true if rows are equal to otherCell, otherwise false.

Kind: instance method of Cell

Param Type
otherCell Cell

cell.hasSameColumn(otherCell) ⇒ boolean

Check if both cells share the same column. Return true if columns are equal to otherCell, otherwise false.

Kind: instance method of Cell

Param Type
otherCell Cell

cell.isSame(otherCell) ⇒ boolean

Check if both cells share the same row and column. Return true if row and column are equal to otherCell, otherwise false.

Kind: instance method of Cell

Param Type
otherCell Cell

cell.shareFormulaUntil(lastSharedCell) ⇒ Cell

If this cell is the source of a shared formula, then assign all cells from this cell to lastSharedCell. Note that lastSharedCell must share the same row or column. Returns itself.

Kind: instance method of Cell

Param Type
lastSharedCell Cell

cell.getRelativeCell(rowOffset, columnOffset) ⇒ Cell

Returns a cell with a relative position to the offsets provided.

Kind: instance method of Cell

Param Type
rowOffset number
columnOffset number

Sheet

Kind: global class

new Sheet(workbook, name, sheetNode, sheetXML)

Initializes a new Sheet.

Param Type Description
workbook Workbook
name String
sheetNode etree.Element The node defining the sheet in the workbook.xml.
sheetXML etree.Element

sheet.getWorkbook() ⇒ Workbook

Gets the parent workbook.

Kind: instance method of Sheet

sheet.getName() ⇒ String

Gets the name of the sheet.

Kind: instance method of Sheet

sheet.getCell() ⇒ Cell

Gets the cell with either the provided row and column or address. If passed nothing, then null is returned. If passed a Cell, then the Cell is returned.

Kind: instance method of Sheet

sheet.getCellRange() ⇒ Array

Gets the cells within a described cell region. Input parameters are of two cells which are endpoints of a rectangluar region. Returns an array of Cells.

Kind: instance method of Sheet

sheet.setColumnValues() ⇒ Sheet

Given a row, assign column values to the row for each column name and value pair provided.

Kind: instance method of Sheet

Workbook

Kind: global class

new Workbook(data)

Initializes a new Workbook.

Param Type
data Buffer

workbook.getSheet(sheetNameOrIndex) ⇒ Sheet

Gets the sheet with the provided name or index (0-based).

Kind: instance method of Workbook

Param Type
sheetNameOrIndex string | number

workbook.getNamedCell(cellName) ⇒ Cell

Get a named cell. (Assumes names with workbook scope pointing to single cells.)

Kind: instance method of Workbook

Param Type
cellName string

workbook.output() ⇒ Buffer

Gets the output.

Kind: instance method of Workbook

workbook.toFile(path, cb)

Writes to file with the given path.

Kind: instance method of Workbook

Param Type
path string
cb function

workbook.toFileSync(path)

Wirtes to file with the given path synchronously.

Kind: instance method of Workbook

Param Type
path string

Workbook.fromFile(path, cb)

Creates a Workbook from the file with the given path.

Kind: static method of Workbook

Param Type
path string
cb function

Workbook.fromFileSync(path) ⇒ Workbook

Creates a Workbook from the file with the given path synchronously.

Kind: static method of Workbook

Param
path

Workbook.fromBlank(cb)

Creates a blank Workbook.

Kind: static method of Workbook

Param Type
cb function

Workbook.fromBlankSync() ⇒ Workbook

Creates a blank Workbook synchronously.

Kind: static method of Workbook

xlsx-populate's People

Contributors

eddiecorrigall avatar dtjohnson avatar djohnson-modernatx avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.