npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tormozz48/xlsx-template

v1.0.4

Published

Library for applying your data for given xlsx template with some advanced features

Downloads

5

Readme

xlsx-template

Fill workbook by given data into places marked by special placeholders.

Build Status codecov

Install

Install package via npm:

npm install @tormozz48/xlsx-template

Usage Example

Assume there ./template-simple.xlsx XLSX file in your working directory with given content:

| # | A | B | C | D | E | | --- | --- | --- | --- | --- | --- | | 1 | str(data.strVal) | number(data.numberVal 0.00) | date(data.dateVal dd/mm/yyyy) | link(data.linkVal) | {str(data.strVal)} | | 2 | | number(data.numberVal) | date(data.dateVal) | | |

const {XLSXPopulateTemplate} = require('xlsx-template');
const xlsxPopulateTemplate = new XLSXPopulateTemplate();

await xlsxPopulateTemplate.loadTemplate('./template-simple.xlsx');
xlsxPopulateTemplate.applyData({
    data: {
        strVal: 'Some String value',
        numberVal: 3.14159,
        dateVal: new Date(2020, 0, 6),
        linkVal: {text: 'some link', ref: 'http://github.com'}
    }
});
await xlsxPopulateTemplate.toFile('./output.xlsx');

Code above will create new file ./output.xlsx with replacing all placeholders from ./template-simple.xlsx on provided data. It also will set valid format to cells according to used placeholders.

| # | A | B | C | D | E | | --- | --- | --- | --- | --- | --- | | 1 | Some String value | 3,14 | 06/01/2020 | some link | str(data.strVal) | | 2 | | 3,14159 | 06-01-2020 | | |

Apply values given by arrays.

It is possible to fill range of rows by values given as array. For example array of objects such as:

[
    {
        strVal: 'Github',
        numberVal: 134.1222,
        dateVal: new Date(2020, 1, 1),
        linkVal: {text: 'github', ref: 'https://github.com'}
    },
    {
        strVal: 'Facebook',
        numberVal: 4352.232,
        dateVal: new Date(2020, 2, 23),
        linkVal: {text: 'facebook', ref: 'https://facebook.com'}
    },
    {
        strVal: 'Google',
        numberVal: 733.2122321,
        dateVal: new Date(2020, 3, 7),
        linkVal: {text: 'google', ref: 'https://google.com'}
    }
]

applied to following template:

| # | A | B | C | D | | --- | --- | --- | --- | --- | | 1 | str(data[i].strVal) | number(data[i].numberVal 0.00) | date(data[i].dateVal dd/mm/yyyy) | link(data[i].linkVal) |

will fill first three rows by corresponded values from data array:

| # | A | B | C | D | | --- | --- | --- | --- | --- | | 1 | Github | 134,12 | 01/02/2020 | github | | 2 | Facebook | 4352,23 | 23/03/2020 | facebook | | 3 | Google | 733,21 | 07/04/2020 | google |

Placeholders

str()

Paste given value to a cell marked with placeholder and use default cell string formatter.

| # | A | | --- | --- | | 1 | str(data.foo) |

xlsxPopulateTemplate.applyData({data: {foo: 'Hello World'}});

| # | A | | --- | --- | | 1 | Hello World |

number()

Paste given value to a cell and use number formatter. Optionally use second argument of number placeholder function to specify number format which should be applied to cell value.

| # | A | | --- | --- | | 1 | number(data.foo, 0.00) |

xlsxPopulateTemplate.applyData({data: {foo: 3.14159}});

| # | A | | --- | --- | | 1 | 3.14 |

date()

Paste given value to a cell and use date formatter. Optionally use second argument of date placeholder function to specify date format which should be applied to cell value. Default date format is dd-mm-yyyy.

| # | A | | --- | --- | | 1 | date(data.foo dd/mm/yyyy) |

xlsxPopulateTemplate.applyData({data: {foo: new Date(2020, 0, 9)}});

| # | A | | --- | --- | | 1 | 09/01/2020 |

link()

Create link value in cell with placeholder. Needs to receive data item as object with fields: text and ref where text - is link text representation and ref - is link reference url.

| # | A | | --- | --- | | 1 | link(data.foo) |

xlsxPopulateTemplate.applyData({data: {foo: {text: 'Github', ref: 'https://github.com'}}});

| # | A | | --- | --- | | 1 | Github |

{} - raw formatter

A special formatter which allows to simply expand inner placeholder without appliyng it. It is useful when there you need to fill template with some part of needed data at first stage and then fill rest of data at second stage.

| # | A | | --- | --- | | 1 | {str(data.foo)} |

xlsxPopulateTemplate.applyData({});

After first call it simply expand inner placeholder str(data.foo) so it will be ready to use on next call.

| # | A | | --- | --- | | 1 | str(data.foo) |

xlsxPopulateTemplate.applyData({data: {foo: 'Hello World'}});

| # | A | | --- | --- | | 1 | Hello World |

array item placeholders

To fill multiple rows below by corresponded array items you should use [i] to mark data node that should be applied as array of items.

| # | A | | --- | --- | | 1 | str(data[i].name) |

xlsxPopulateTemplate.applyData({data: [
    {name: 'Github'},
    {name: 'Facebook'},
    {name: 'Google'}
]);

| # | A | | --- | --- | | 1 | Github | | 2 | Facebook | | 3 | Google |

API

.loadTemplate(templatePath)

Async function to load XLSX workbook from buffer of file or event create it from scratch. Loads XLSX workbook from file:

const xlsxPopulateTemplate = new XLSXPopulateTemplate();
await xlsxPopulateTemplate.loadTemplate('./template-simple.xlsx');

Loads XLSX workbook from buffer:

const buffer = await fs.readFile('./template-simple.xlsx');
const xlsxPopulateTemplate = new XLSXPopulateTemplate();
await xlsxPopulateTemplate.loadTemplate(buffer);

Creates XLSX workbook from scratch (empty workbook)

const xlsxPopulateTemplate = new XLSXPopulateTemplate();
await xlsxPopulateTemplate.loadTemplate();

.applyData(data)

Fills matched template placeholders with given data.

const xlsxPopulateTemplate = new XLSXPopulateTemplate();
await xlsxPopulateTemplate.loadTemplate();
xlsxPopulateTemplate.applyData({foo: 'bar'})

.toBuffer()

Async function which serializes current workbook into buffer.

const xlsxPopulateTemplate = new XLSXPopulateTemplate();
await xlsxPopulateTemplate.loadTemplate();
const buffer = await xlsxPopulateTemplate.toBuffer();

.toFile(filePath)

Async function which saves XLSX workbook into file with given filePath on local filesystem.

const xlsxPopulateTemplate = new XLSXPopulateTemplate();
await xlsxPopulateTemplate.loadTemplate();
await xlsxPopulateTemplate.toFile('./my-awesome.xlsx');

.workbook

Getter which simply returns XLSX workbook instance of xlsx-populate2 package. It is usefull for direct manipulation with workbook or it inner parts like cells, rows and columns.

const xlsxPopulateTemplate = new XLSXPopulateTemplate();
await xlsxPopulateTemplate.loadTemplate();

const workbook = xlsxPopulateTemplate.workbook;
workbook.sheet('Sheet1').cell('A1').value('Foo');

Development

Some useful commands for development:

  • npm run build - compiles typescript code into javascript code.
  • npm run clean - cleans js dist folder with compiled javascript code.
  • npm run format - performs code formatting via prettier tool.
  • npm run lint - runs tslint syntax checker.
  • npm run test - runs mocha tests.
  • npm run test -watch - runs mocha tests in "watch" mode. Launch tests on every code change.
  • npm run test -cov - runs mocha tests with coverage calculation.