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

js-xlsx-gen

v0.1.3

Published

Generates XLSX files from JSON definition

Downloads

21

Readme

js-xlsx-gen

Simple declarative layer built on top of protobi/js-xlsx which is a fork of SheetJS/js-xlsx that adds styles.

npm version

Installation

npm install js-xlsx-gen

Usage

var xlsGen = require('js-xlsx-gen')({
	defaultDateFormat: 'yyyy-mm-dd' // optional override of default date format of mm/dd/yyyy
});

// define some common styles
// see: https://github.com/protobi/js-xlsx/blob/master/tests/test-style.js for examples of styling AND https://github.com/SheetJS/ssf/blob/master/ssf.js for examples of numFmt
var commonStyles = {
	header: {
		alignment: {horizontal: 'center'},
		font: {bold: true}
	},
	positive: {
		numFmt: '$#,##0.00;$(#,##0.00)',
		font: {color: {rgb: '00ffff'}},
		fill: {fgColor: {rgb: 'ffffff'}}
	}
};
// next define the spreadsheet with each key as the sheet name
var spreadsheet = {
	sheetA: {
		header: {
			styles: {
				row: commonStyles.header,
				columns: {
					1: {font: {italic: true}}
				}
			},
			columns: [
				{v :'ColA', s: commonStyles.header},
				{v :'ColB', s: commonStyles.header}
			]
		},
		data: {
			rows: [
				[1, 2],
				[2, 3]
			]
		}
	},
	sheetB: {
		header: {
			columns: [
				'Column A',
				'Column B',
				'Column C',
				'Column D'
			]
		},
		data: {
			styles: {
				columns: {
					2: {numFmt: '$#,##0.00;$(#,##0.00)'},
					3: {font: {color: {rgb: '00ff00'}}}
				}
			},
			rows: [
				[
					new Date(),
					true,
					{v: new Date(), s: {numFmt: 'mm-dd-yyyy'}},
					20
				],
				[
					{v: -0.02, s: {numFmt: '$#,##0.00;$(#,##0.00)', font: {color: {rgb: 'ffffff'}}, fill: {fgColor: {rgb: 'ff0000'}}}}, // inline styling
					{v: 3, s: commonStyles.positive}, // inline styling to global styling
					1.23,
					40
				]
			]
		}
	}
};
// build workbook with OPTIONAL default cell styling
var workbook = xlsGen.generate(spreadsheet, {
	header: {font: { name: "Verdana", sz: 18, color: {rgb: "FF8800"}}, fill: {fgColor: {rgb: "aa00ff"}}},
	data: {font: { name: "Verdana", sz: 11, color: {rgb: "FF0000"}}, fill: {fgColor: {rgb: "ffaa00"}}}
});
// write out XLSX
xlsGen.writeFile(workbook, 'workbook.xlsx');

API


generate ( spreadsheet, defaultStyle )

Generate a workbook from a spreadsheet definition with default cell styles.

Arguments

  • spreadsheet - Spreadsheet definition
  • defaultStyle - OPTIONAL styling for headers and data

Example

var workbook = xlsGen.generate(spreadsheet, {
	header: {font: { name: "Verdana", sz: 18, color: {rgb: "FF8800"}}, fill: {fgColor: {rgb: "aa00ff"}}},
	data: {font: { name: "Verdana", sz: 11, color: {rgb: "FF0000"}}, fill: {fgColor: {rgb: "ffaa00"}}}
});

write ( workbook, wopts )

Write workbook to Binary string. For more details see js-xlsx write()


writeFile ( workbook, filePath )

Write workbook to Binary string. For more details see js-xlsx writeFile()

Spreadsheet Definition

The Spreadsheet Definition contains multiple sheets:

var spreadsheet = {
	sheetA: {
	},
	sheetB: {		
	}
};

Each sheet has an optional header section and a data section:

var spreadsheet = {
	sheetA: {
		header: {
		},
		data: {
		}
	}
};

The optional header section has an optional style section and columns section:

var spreadsheet = {
	sheetA: {
		header: {
			styles: {
			},
			columns: [
			]
		}
	}
};

The data section has an optional style section and data section:

var spreadsheet = {
	sheetA: {
		data: {
			styles: {
			},
			rows: [
			]
		}
	}
};

The styles section under the header and data sections contains an optional row section and an optional columns section:

var spreadsheet = {
	sheetA: {
		header: {
			styles: {
				row: {
					alignment: {horizontal: 'center'},
					font: {bold: true}
				},
				columns {
					1: {font: {italic: true}}
				}
			},
			columns: [
			]
		},
		data: {
			styles: {
				row: {
					alignment: {horizontal: 'right'},
					font: {bold: true, italic: true, sz: 14}
				},
				columns {
					1: {
						numFmt: '$#,##0.00;$(#,##0.00)',
						font: {color: {rgb: '00ffff'}},
						fill: {fgColor: {rgb: 'ffffff'}}
					}
				}
			},
			rows: [
			]
		}
	}
};

Unfortunately, these styles aren't documented well, but examples can be found in the style test code of protobi/js-xlsx.

The columns array of the header section contains cell data:

var spreadsheet = {
	sheetA: {
		header: {
			columns: [
				{v :'ColA', s: commonStyles.header},
				'ColB'
			]
		},
		data: {
		}
	}
};

Notice that the columns array contains either objects that conform to Cell Objects in protobi/js-xlsx OR raw data, i.e. number, date, string, boolean.

The rows array of the data section contains row data:

var spreadsheet = {
	sheetA: {
		data: {
			rows: [
				[
					new Date(),
					true,
					{v: new Date(), s: {numFmt: 'mm-dd-yyyy'}},
					20
				],
				[
					{v: -0.02, s: {numFmt: '$#,##0.00;$(#,##0.00)', font: {color: {rgb: 'ffffff'}}, fill: {fgColor: {rgb: 'ff0000'}}}},
					{v: 3, s: commonStyles.positive},
					1.23,
					40
				]
			]
		}
	}
};

Notice that the rows array contains an array for each row which contains either objects that conform to Cell Objects in protobi/js-xlsx OR raw data, i.e. number, date, string, boolean.