├── LICENSE ├── README.md ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Dominic Tarr 2 | 3 | Permission is hereby granted, free of charge, 4 | to any person obtaining a copy of this software and 5 | associated documentation files (the "Software"), to 6 | deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom 10 | the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 20 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # excel-stream 2 | 3 | A stream that converts excel spreadsheets into JSON object arrays. 4 | 5 | 6 | # Examples 7 | 8 | ``` js 9 | // stream rows from the first sheet on the file 10 | var excel = require('excel-stream') 11 | var fs = require('fs') 12 | 13 | fs.createReadStream('accounts.xlsx') 14 | .pipe(excel()) // same as excel({sheetIndex: 0}) 15 | .on('data', console.log) 16 | 17 | ``` 18 | 19 | ``` js 20 | // stream rows from the sheet named 'Your sheet name' 21 | var excel = require('excel-stream') 22 | var fs = require('fs') 23 | 24 | fs.createReadStream('accounts.xlsx') 25 | .pipe(excel({ 26 | sheet: 'Your sheet name' 27 | })) 28 | .on('data', console.log) 29 | 30 | ``` 31 | 32 | # stream options 33 | 34 | The `options` object may have the same properties as [csv-stream](https://www.npmjs.com/package/csv-stream) and these two additional properties: 35 | 36 | * `sheet`: the name of the sheet you want to stream. Case sensitive. 37 | * `sheetIndex`: the sheet number you want to stream (0-based). 38 | 39 | # Usage 40 | 41 | ``` js 42 | npm install -g excel-stream 43 | excel-stream < accounts.xlsx > account.json 44 | ``` 45 | 46 | # options 47 | 48 | newline delimited json: 49 | 50 | ```js 51 | excel-stream --newlines 52 | ``` 53 | 54 | # formats 55 | 56 | each row becomes a javascript object, so input like 57 | 58 | ``` csv 59 | foo, bar, baz 60 | 1, 2, 3 61 | 4, 5, 6 62 | ``` 63 | 64 | will become 65 | 66 | ``` js 67 | [{ 68 | foo: 1, 69 | bar: 2, 70 | baz: 3 71 | }, { 72 | foo: 4, 73 | bar: 5, 74 | baz: 6 75 | }] 76 | 77 | ``` 78 | 79 | # Don't Look Now 80 | 81 | So, excel isn't really a streamable format. 82 | But it's easy to work with streams because everything is a stream. 83 | This writes to a tmp file, then pipes it through the unfortunately named [j](https://npm.im/j) 84 | then into [csv-stream](https://npm.im/csv-stream) 85 | 86 | 87 | ## License 88 | 89 | MIT 90 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('fs') 4 | var os = require('os') 5 | var path = require('path') 6 | var chpro = require('child_process') 7 | 8 | var through = require('through') 9 | var csv = require('csv-stream') 10 | var osenv = require('osenv') 11 | var duplexer = require('duplexer') 12 | var concat = require('concat-stream') 13 | 14 | var spawn = chpro.spawn 15 | if (os.type() === 'Windows_NT') spawn = require('win-spawn') 16 | 17 | module.exports = function (options) { 18 | 19 | var read = through() 20 | var duplex 21 | 22 | var filename = path.join(osenv.tmpdir(), '_'+Date.now()) 23 | 24 | var spawnArgs = [] 25 | 26 | if (options) { 27 | options.sheet && spawnArgs.push('--sheet') && spawnArgs.push(options.sheet) && delete options.sheet 28 | options.sheetIndex && spawnArgs.push('--sheet-index') && spawnArgs.push(options.sheetIndex) && delete options.sheetIndex 29 | } 30 | 31 | spawnArgs.push(filename) 32 | 33 | var write = fs.createWriteStream(filename) 34 | .on('close', function () { 35 | var child = spawn(require.resolve('j/bin/j.njs'), spawnArgs) 36 | child.stdout.pipe(csv.createStream(options)) 37 | .pipe(through(function (data) { 38 | var _data = {} 39 | for(var k in data) { 40 | var value = data[k].trim() 41 | _data[k.trim()] = isNaN(value) ? value : +value 42 | } 43 | this.queue(_data) 44 | })) 45 | .pipe(read) 46 | child.on('exit', function(code, sig) { 47 | if(code === null || code !== 0) { 48 | child.stderr.pipe(concat(function(errstr) { 49 | duplex.emit('error', new Error(errstr)) 50 | })) 51 | } 52 | }) 53 | }) 54 | 55 | return (duplex = duplexer(write, read)) 56 | 57 | } 58 | 59 | 60 | if(!module.parent) { 61 | var JSONStream = require('JSONStream') 62 | var args = require('minimist')(process.argv.slice(2)) 63 | process.stdin 64 | .pipe(module.exports()) 65 | .pipe(args.lines || args.newlines 66 | ? JSONStream.stringify('', '\n', '\n', 0) 67 | : JSONStream.stringify() 68 | ) 69 | .pipe(process.stdout) 70 | } 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "excel-stream", 3 | "description": "convert a stream of xls or xlsx into json on the command line or in node", 4 | "version": "1.1.1", 5 | "bin": "index.js", 6 | "homepage": "https://github.com/dominictarr/excel-stream", 7 | "repository": { 8 | "type": "git", 9 | "url": "git://github.com/dominictarr/excel-stream.git" 10 | }, 11 | "dependencies": { 12 | "JSONStream": "^1.0.4", 13 | "concat-stream": "^1.4.6", 14 | "csv-stream": "~0.1.3", 15 | "duplexer": "~0.1.1", 16 | "j": "^0.4.3", 17 | "minimist": "^1.1.1", 18 | "osenv": "^0.1.3", 19 | "through": "~2.3.4", 20 | "win-spawn": "^2.0.0" 21 | }, 22 | "devDependencies": {}, 23 | "scripts": { 24 | "test": "set -e; for t in test/*.js; do node $t; done" 25 | }, 26 | "author": "Dominic Tarr (http://dominictarr.com)", 27 | "license": "MIT" 28 | } 29 | --------------------------------------------------------------------------------