Socket
Socket
Sign inDemoInstall

node-xlsx

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-xlsx - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0

.jshintrc

69

lib/node-xlsx.js
'use strict';
var debug = require('debug')('node-xlsx');
var fs = require('fs');
var xlsx = require(__dirname + '/xlsx/xlsx.js');
var XLSX = require('xlsx');
var _ = {defaults: require('lodash.defaults')};
function datenum(v, date1904) {
if(date1904) v += 1462;
var epoch = Date.parse(v);
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
}
function sheet_from_array_of_arrays(data) {
var ws = {};
var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0}};
for(var R = 0; R !== data.length; ++R) {
for(var C = 0; C !== data[R].length; ++C) {
if(range.s.r > R) range.s.r = R;
if(range.s.c > C) range.s.c = C;
if(range.e.r < R) range.e.r = R;
if(range.e.c < C) range.e.c = C;
var cell = {v: data[R][C] };
if(cell.v === null) continue;
var cell_ref = XLSX.utils.encode_cell({c:C,r:R});
if(typeof cell.v === 'number') cell.t = 'n';
else if(typeof cell.v === 'boolean') cell.t = 'b';
else if(cell.v instanceof Date) {
cell.t = 'n'; cell.z = XLSX.SSF._table[14];
cell.v = datenum(cell.v);
}
else cell.t = 's';
ws[cell_ref] = cell;
}
}
if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
return ws;
}
function Workbook() {
if(!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}
module.exports = {
parse: function(mixed, options) {
if(typeof mixed === 'string') mixed = fs.readFileSync(mixed);
debug('parsed a %d-sized xml', mixed.length);
return xlsx(mixed.toString('base64'), options);
if(typeof mixed === 'string') return XLSX.readFile(mixed, options);
return XLSX.read(mixed, options);
},
build: function(object, options) {
var data = xlsx(object, options); // [ 'base64', 'zipTime', 'processTime', 'href' ]
if(!data.base64) return false;
var buffer = new Buffer(data.base64, 'base64');
debug('built a %d-sized xml, processTime:%dms, zipTime:%dms', buffer.length, data.processTime, data.zipTime);
var defaults = {
bookType:'xlsx',
bookSST: false,
type:'binary'
};
var wb = new Workbook();
(object.Sheets || object.worksheets).forEach(function(worksheet) {
var name = worksheet.name || 'Sheet';
var data = sheet_from_array_of_arrays(worksheet.data || []);
wb.SheetNames.push(name);
wb.Sheets[name] = data;
});
var data = XLSX.write(wb, _.defaults(options || {}, defaults));
if(!data) return false;
var buffer = new Buffer(data, 'binary');
return buffer;
}
};

115

lib/xlsx/xlsx.js

@@ -10,4 +10,5 @@ var JSZip = null

// https://raw.github.com/stephen-hardy/xlsx.js/master/LICENSE.txt
// Copyright (C) Choi Goon-ho regarding the node-xlsx patches
//----------------------------------------------------------
function xlsx(file, options) {
function xlsx(file, options) {
'use strict'; // v2.3.0

@@ -31,12 +32,12 @@

function numAlpha(i) {
var t = Math.floor(i / 26) - 1; return (t > -1 ? numAlpha(t) : '') + alphabet.charAt(i % 26);
function numAlpha(i) {
var t = Math.floor(i / 26) - 1; return (t > -1 ? numAlpha(t) : '') + alphabet.charAt(i % 26);
}
function alphaNum(s) {
var t = 0; if (s.length === 2) { t = alphaNum(s.charAt(0)) + 1; } return t * 26 + alphabet.indexOf(s.substr(-1));
function alphaNum(s) {
var t = 0; if (s.length === 2) { t = alphaNum(s.charAt(0)) + 1; } return t * 26 + alphabet.indexOf(s.substr(-1));
}
function convertDate(input) {
return typeof input === 'object' ? ((input - new Date(1900, 0, 0)) / 86400000) + 1 : new Date(+new Date(1900, 0, 0) + (input - 1) * 86400000);
function convertDate(input) {
return typeof input === 'object' ? ((input - new Date(1900, 0, 0)) / 86400000) + 1 : new Date(+new Date(1900, 0, 0) + (input - 1) * 86400000);
}

@@ -47,16 +48,16 @@

}
function getAttr(s, n) {
s = s.substr(s.indexOf(n + '="') + n.length + 2); return s.substring(0, s.indexOf('"'));
function getAttr(s, n) {
s = s.substr(s.indexOf(n + '="') + n.length + 2); return s.substring(0, s.indexOf('"'));
}
function escapeXML(s) {
return (s || '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;');
function escapeXML(s) {
return (s || '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;');
} // see http://www.w3.org/TR/xml/#syntax
function unescapeXML(s) {
return (s || '').replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#x27;/g, '\'');
function unescapeXML(s) {
return (s || '').replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#x27;/g, '\'');
}
if (typeof file === 'string') {
if (typeof file === 'string') {
// Load

@@ -89,7 +90,7 @@ zipTime = Date.now();

if (index > 0) {
s = s.substr(index + 11);
s = s.substr(index + 11);
// Must eliminate first 11 characters before finding the index of " on the next line. Otherwise, it finds the " before the value.
result.activeWorksheet = +s.substring(0, s.indexOf('"'));
} else {
result.activeWorksheet = 0;
} else {
result.activeWorksheet = 0;
}

@@ -105,4 +106,4 @@ s = s.split('<sheet '); i = s.length;

s = zip.file('xl/styles.xml').asText().split('<numFmt '); i = s.length;
while (--i) {
t = s[i]; numFmts[+getAttr(t, 'numFmtId')] = getAttr(t, 'formatCode');
while (--i) {
t = s[i]; numFmts[+getAttr(t, 'numFmtId')] = getAttr(t, 'formatCode');
}

@@ -113,9 +114,9 @@ s = s[s.length - 1]; s = s.substr(s.indexOf('cellXfs')).split('<xf '); i = s.length;

if (f.indexOf('m') > -1) {
t = 'date';
} else if (f.indexOf('0') > -1) {
t = 'number';
} else if (f === '@') {
t = 'string';
} else {
t = 'unknown';
t = 'date';
} else if (f.indexOf('0') > -1) {
t = 'number';
} else if (f === '@') {
t = 'string';
} else {
t = 'unknown';
}

@@ -181,3 +182,3 @@ styles.unshift({ formatCode: f, type: t });

w = file.worksheets.length;
while (w--) {
while (w--) {
// Generate worksheet (gather sharedStrings), and possibly table files, then generate entries for constant files below

@@ -196,6 +197,6 @@ id = w + 1;

while (++j < k) {
cell = data[i][j]; val = cell.hasOwnProperty('value') ? cell.value : cell; t = '';
cell = data[i][j]; val = cell.hasOwnProperty('value') ? cell.value : cell; t = '';
// supported styles: borders, hAlign, formatCode and font style
style = {
borders: cell.borders || defaultCellBorders,
borders: cell.borders || defaultCellBorders,
hAlign: cell.hAlign || defaultHAlign,

@@ -212,3 +213,3 @@ textRotation: cell.textRotation || defaultTextRotation,

colWidth = 0;
if (val && typeof val === 'string' && !isFinite(val) || cell.forceString) {
if (val && typeof val === 'string' && !isFinite(val) || cell.forceString) {
// If value is string, and not string of just a number, place a sharedString reference instead of the value

@@ -220,12 +221,12 @@ val = escapeXML(val);

if (index < 0) {
index = sharedStrings[0].push(val.replace(/\n/g, '&#10;')) - 1;
index = sharedStrings[0].push(val.replace(/\n/g, '&#10;')) - 1;
}
val = index;
t = 's';
} else if (typeof val === 'boolean') {
val = (val ? 1 : 0); t = 'b';
} else if (typeof val === 'boolean') {
val = (val ? 1 : 0); t = 'b';
colWidth = 1;
} else if (typeOf(val) === 'date') {
val = convertDate(val);
style.formatCode = cell.formatCode || 'mm-dd-yy';
} else if (typeOf(val) === 'date') {
val = convertDate(val);
style.formatCode = cell.formatCode || 'mm-dd-yy';
colWidth = val.length;

@@ -236,13 +237,13 @@ } else if (typeof val === 'object') {

} else {
// number, or string which is a number
// number, or string which is a number
colWidth = (''+val).length;
}
// use stringified version as unic and reproductible style signature
style = JSON.stringify(style);
index = styles.indexOf(style);
if (index < 0) {
style = styles.push(style) - 1;
} else {
style = index;
if (index < 0) {
style = styles.push(style) - 1;
} else {
style = index;
}

@@ -343,3 +344,3 @@ // keeps largest cell in column, and autoWidth flag that may be set on any cell

+ '<sheetData>'
+ s
+ s
+ '</sheetData>';

@@ -365,4 +366,4 @@ if (merges.length > 0) {

s += '<pageSetup paperSize="' + paper_size + '" orientation="' + orientation + '" scale="' + page_scale + '"/>'
if (worksheet.table) {
s += '<tableParts count="1"><tablePart r:id="rId1"/></tableParts>';
if (worksheet.table) {
s += '<tableParts count="1"><tablePart r:id="rId1"/></tableParts>';
}

@@ -375,4 +376,4 @@ xlWorksheets.file('sheet' + id + '.xml', s + '</worksheet>');

+ '" name="Table' + id + '" displayName="Table' + id + '" ref="A1:' + t + '" totalsRowShown="0"><autoFilter ref="A1:' + t + '"/><tableColumns count="' + data[0].length + '">';
while (++i < l) {
s += '<tableColumn id="' + (i + 1) + '" name="' + (data[0][i].hasOwnProperty('value') ? data[0][i].value : data[0][i]) + '"/>';
while (++i < l) {
s += '<tableColumn id="' + (i + 1) + '" name="' + (data[0][i].hasOwnProperty('value') ? data[0][i].value : data[0][i]) + '"/>';
}

@@ -394,3 +395,3 @@ s += '</tableColumns><tableStyleInfo name="TableStyleMedium2" showFirstColumn="0" showLastColumn="0" showRowStripes="1" showColumnStripes="0"/></table>';

i = styles.length; t = [];
while (--i) {
while (--i) {
// Don't process index 0, already added

@@ -402,5 +403,5 @@ style = JSON.parse(styles[i]);

index = numFmts.indexOf(style.formatCode);
if (index < 0) {
index = 164 + t.length;
t.push('<numFmt formatCode="' + style.formatCode + '" numFmtId="' + index + '"/>');
if (index < 0) {
index = 164 + t.length;
t.push('<numFmt formatCode="' + style.formatCode + '" numFmtId="' + index + '"/>');
}

@@ -461,4 +462,4 @@ style.formatCode = index

// declares style, and refer to optionnal formatCode, font and borders
styles[i] = ['<xf xfId="0" fillId="0" borderId="',
borderIndex,
styles[i] = ['<xf xfId="0" fillId="0" borderId="',
borderIndex,
'" fontId="',

@@ -543,2 +544,2 @@ fontIndex,

module.exports = xlsx;
}
}

@@ -1,22 +0,17 @@

This license governs use of the accompanying software. If you use the software, you
accept this license. If you do not accept the license, do not use the software.
Copyright (C) 2012-2014 Olivier Louvignes
1. Definitions
The terms "reproduce," "reproduction," "derivative works," and "distribution" have the
same meaning here as under U.S. copyright law.
A "contribution" is the original software, or any additions or changes to the software.
A "contributor" is any person that distributes its contribution under this license.
"Licensed patents" are a contributor's patent claims that read directly on its contribution.
"Excluded Products” are software products or components, or web-based or hosted services, that primarily perform the same general functions as any of the following software applications: Microsoft Office, Word, Excel, PowerPoint, Outlook, OneNote, Publisher, SharePoint, or Access.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
2. Grant of Rights
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
http://www.apache.org/licenses/LICENSE-2.0
3. Conditions and Limitations
(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
(F) Platform Limitation- The licenses granted in sections 2(A) & 2(B) extend only to the software or derivative works that (1) are run on a Microsoft Windows operating system product, and (2) are not Excluded Products.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Except where noted, this license applies to any and all software programs and associated documentation files created by the Original Author and distributed with the Software:
'node-xlsx.js' is a modified version of SheetJS gist examples, Copyright (c) SheetJS.
{
"name": "node-xlsx",
"author": "Olivier Louvignes",
"license": "Microsoft Office Extensible File License",
"version": "0.4.0",
"license": "Apache License, Version 2.0",
"version": "0.5.0",
"description": "Node.js excel parser & builder",

@@ -17,11 +17,11 @@ "main": "index.js",

"excel",
"xml"
"xlsx"
],
"dependencies": {
"debug": ">= 0.7.2",
"node-zip": "~1.0.1"
"lodash.defaults": "^2.4.1",
"xlsx": "^0.7.10"
},
"devDependencies": {
"nodeunit": ">= 0.7.4"
"nodeunit": "^0.9.0"
}
}

@@ -1,18 +0,6 @@

**New Features Added:**
* More customizable options like font name and color.
* Limited support for colSpan AND rowSpan.
* Forced row height and column width.
* Added textRotation.
* Added page scale and sheet view option.
---
[node-xlsx](http://mgcrea.github.com/node-xlsx) [![Build Status](https://secure.travis-ci.org/mgcrea/node-xlsx.png?branch=master)](http://travis-ci.org/#!/mgcrea/node-xlsx)
=================
Excel parser/builder that relies on [xlsx.js](https://raw.github.com/stephen-hardy/xlsx.js) (Microsoft Office Extensible File License), this package had to be released under the same restrictive license. Please read it carefully.
Excel parser/builder that relies on [js-xlsx](https://github.com/SheetJS/js-xlsx).
An opensource alternative is currently being developped by [Niggler](https://github.com/Niggler/js-xlsx), reader-only for now.
Quick start

@@ -35,11 +23,4 @@ -----------

var buffer = xlsx.build({worksheets: [
{"name":"mySheetName", "data":[
["A1", "B1"],
[
{"value":"A2","formatCode":"General"},
{"value":"B2","formatCode":"General"}
]
]}
]}); // returns a buffer
var data = [[1,2,3],[true, false, null, 'sheetjs'],['foo','bar',new Date('2014-02-19T14:30Z'), '0.3'], ['baz', null, 'qux']];
var buffer = xlsx.build({worksheets: [{name: "mySheetName", data: data}); // returns a buffer

@@ -73,23 +54,18 @@ ```

This license governs use of the accompanying software. If you use the software, you
accept this license. If you do not accept the license, do not use the software.
Copyright (C) 2012-2014 Olivier Louvignes
1. Definitions
The terms "reproduce," "reproduction," "derivative works," and "distribution" have the
same meaning here as under U.S. copyright law.
A "contribution" is the original software, or any additions or changes to the software.
A "contributor" is any person that distributes its contribution under this license.
"Licensed patents" are a contributor's patent claims that read directly on its contribution.
"Excluded Products” are software products or components, or web-based or hosted services, that primarily perform the same general functions as any of the following software applications: Microsoft Office, Word, Excel, PowerPoint, Outlook, OneNote, Publisher, SharePoint, or Access.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
2. Grant of Rights
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
http://www.apache.org/licenses/LICENSE-2.0
3. Conditions and Limitations
(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
(F) Platform Limitation- The licenses granted in sections 2(A) & 2(B) extend only to the software or derivative works that (1) are run on a Microsoft Windows operating system product, and (2) are not Excluded Products.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Except where noted, this license applies to any and all software programs and associated documentation files created by the Original Author and distributed with the Software:
'node-xlsx.js' is a modified version of SheetJS gist examples, Copyright (c) SheetJS.

@@ -12,3 +12,3 @@ 'use strict';

var plist = require('../index');
var xlsx = require('../index');

@@ -22,8 +22,8 @@ module.exports.build = function(assert) {

// build file
xlsData = plist.build({worksheets: fixture});
xlsData = xlsx.build({worksheets: fixture});
assert.equal(xlsData instanceof Buffer, true);
assert.equal(xlsData.toString('base64').substr(0, 12), fs.readFileSync(filename).toString('base64').substr(0, 12));
// fs.writeFileSync('/tmp/foo.xlsx', xlsData);
assert.done();
};
[
{
"name": "hello",
"data": [
[
{
"value": "A1",
"formatCode": "General"
},
{
"value": "B1",
"formatCode": "General"
}
],
[
{
"value": "A2",
"formatCode": "General"
},
{
"value": "B2",
"formatCode": "General"
}
]
],
"table": false,
"maxCol": 2,
"maxRow": 2
}
]
[1, 2, 3],
[true, false, null, "sheetjs"],
["foo", "bar", "2014-02-19T14:30Z", "0.3"],
["baz", null, "qux"]
]

@@ -12,7 +12,7 @@ 'use strict';

var plist = require('../index');
var xlsx = require('../index');
module.exports.parse = function(assert) {
var fixture = JSON.parse(fs.readFileSync(__dirname + '/fixtures/test.json'));
var fixture = JSON.parse(fs.readFileSync(__dirname + '/fixtures/parsed.json'));
var filename = __dirname + '/fixtures/test.xlsx';

@@ -22,10 +22,10 @@ var xlsObject;

// parse file
xlsObject = plist.parse(filename);
assert.equal(!!(xlsObject && xlsObject.worksheets), true);
assert.deepEqual(xlsObject.worksheets, fixture);
xlsObject = xlsx.parse(filename);
assert.equal(!!(xlsObject && xlsObject.Sheets), true);
assert.deepEqual(xlsObject.Sheets, fixture);
// parse buffer
xlsObject = plist.parse(fs.readFileSync(filename));
assert.equal(!!(xlsObject && xlsObject.worksheets), true);
assert.deepEqual(xlsObject.worksheets, fixture);
xlsObject = xlsx.parse(fs.readFileSync(filename));
assert.equal(!!(xlsObject && xlsObject.Sheets), true);
assert.deepEqual(xlsObject.Sheets, fixture);

@@ -32,0 +32,0 @@ assert.done();

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc