Skip to content

Add CSV write buffer support #549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="node" />

import { Writable, Stream } from 'stream';
declare interface Buffer { }
declare interface Stream { }
declare interface Writable { }

export const enum RelationshipType {
None = 0,
Expand Down Expand Up @@ -1082,6 +1082,11 @@ export interface Xlsx {
*/
createInputStream(): Writable;

/**
* write to a buffer
*/
writeBuffer(): Promise<Buffer>;

/**
* write to a file
*/
Expand Down Expand Up @@ -1119,6 +1124,11 @@ export interface Csv {
*/
createInputStream(): Writable;

/**
* write to a buffer
*/
writeBuffer(): Promise<Buffer>;

/**
* write to a file
*/
Expand Down
9 changes: 9 additions & 0 deletions lib/csv/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var fs = require('fs');
var csv = require('fast-csv');
var moment = require('moment');
var PromishLib = require('../utils/promish');
var StreamBuf = require('../utils/stream-buf');

var utils = require('../utils/utils');

Expand Down Expand Up @@ -166,5 +167,13 @@ CSV.prototype = {
var stream = fs.createWriteStream(filename, streamOptions);

return this.write(stream, options);
},
writeBuffer: function(options) {
var self = this;
var stream = new StreamBuf();
return self.write(stream, options)
.then(function() {
return stream.read();
});
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"underscore": "^1.8.3"
},
"main": "./dist/es5/index.js",
"types": "./index.d.ts",
"files": [
"dist",
"LICENSE",
Expand Down
19 changes: 19 additions & 0 deletions spec/browser/exceljs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,23 @@ describe('ExcelJS', function() {
})
.catch(unexpectedError(done));
});
it('should write csv via buffer', function(done) {
var wb = new ExcelJS.Workbook();
var ws = wb.addWorksheet('blort');

ws.getCell('A1').value = 'Hello, World!';
ws.getCell('B1').value = 'What time is it?';
ws.getCell('A2').value = 7;
ws.getCell('B2').value = '12pm';

wb.csv.writeBuffer()
.then(function(buffer) {
expect(buffer.toString()).toEqual('"Hello, World!",What time is it?\n7,12pm')
done();
})
.catch(function(error) {
throw error;
})
.catch(unexpectedError(done));
});
});