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

@momsfriendlydevco/spreadsheet-templater

v1.1.10

Published

Simple templates markup for spreadsheets

Downloads

7

Readme

Spreadsheet-Templater

Simple templates markup for spreadsheets (via XLSX).

This plugin allows a spreadsheet to use handlebars-like notation to replace cell contents which enables an input spreadsheet to act as a template for incoming data.

var SpreadsheetTemplater = require('@momsfriendlydevco/spreadsheet-templater');

new SpreadsheetTemplater()
	.read('input.xlsx')
	.data({...})
	.apply()
	.write('output.xlsx')

See the test directory for some example spreadsheets.

Limitations

There are a few restrictions with this module, mainly due to time and technical limitations:

  • Nested {{#each}} + {{/each}} statements are not supported
  • Due to no support for dynamic row adding, at the time of writing, in the upstream xlsx-populate library this module will overwrite all rows below the {{each}} blocks with however many rows of data need placing - the spreadsheet contents below the {{each}} blocks will not be moved down

Debugging

This module uses the Debug NPM. To enable simply set the DEBUG environment variable to include spreadsheet-templater

Markup

This module reads all cells in all sheets and applies simple substitutions based on a Handlebars like template based on an input data set.

Simple substitution

Simple substitution is performed by putting a lodash compatible dotted notation path inside double braces. For example {{people.0.name}} - extracts from the data object the key people, the first element of the array and the subkey name.

Repeaters

Basic support is provided for single level repeaters. Repeaters start ({{#each ITERABLE}}) in the first cell and are read horizontally until the end is encountered ({{/each}}).

For example assuming the following CSV spreadsheet layout:

Name,Email,Phone,Address
{{#each people}}{{name}},{{email}},{{phone}},"{{address.street}}, {{address.city}}, {{address.zipcode}}{{/each}}"

The spreadsheet would be populated with all items in the people collection until exhausted.

If no specific data path is specified for each (i.e. {{#each}}) the main data object is assumed to be an array and it is used instead.

API

The module exposes a single object.

This module supports the following options:

| Option | Type | Default | Description | |-------------------------|----------|----------------------------|---------------------------------------------------------------------------------| | re | Object | | The regular expressions used when detecting markup | | re.expression | RegExp | /{{(.+?)}}/ | RegExp to detect a single expression replacement | | re.repeatStart | RegExp | /{{#?\s*each\s+(.+?)}}/g | How to identify the start of a repeater | | re.repeatEnd | RegExp | /{{\/each.*?}}/ | How to identify the end of a repeater | | repeaterSilentOnError | Boolean | false | Whether the module should throw when a non-array path is provided to a repeater | | template | Object | | Options to control templates | | template.path | String | | The source file to process the template from | | data | Object | {} | The data object used when marking up the template output | | defaultValue | Any | '' | The value used when no corresponding simple dotted path can be located | | templateDetect | Function | See code | How to determine if a cell needs to be templated | | templatePreProcess | Array | [] | Array of functions that can mutate a cell template before processing | | templateSettings | Object | See code | Settings passed to the templating NPM | | dateDetect | RegEx | See code | RegEx for detecting date output before formatting | | dateFormat | String | "dd/mm/yyyy" | The date format to use when dateDetect succeeds |

Constructor([options])

Setup the initial object with options.

set(key, [val])

Set a single or multiple options (if key is an object). Lodash array and dotted notation is supported for the key.

read([path])

Parse the input template file. This function is automatically called if constructor is given a filename when initialized.

apply([data])

Apply the given data (or the data specified in options.data) to the loaded template.

data([data])

Alternate way to set template data.

json()

Convenience function to return the workbook as a JSON object This will return an object with each key as the sheet ID and a 2D array of cells