Skip to content
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

fix several issues under Windows #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Example: Create a new sheet named 'sheet1' with 5 columns and 8 rows
var sheet1 = workbook.createSheet('sheet1', 5, 8);
```

### Workbook.saveSync()
### Workbook.save(callback)

Save current workbook.
Expand Down
122 changes: 70 additions & 52 deletions lib/msexcel-builder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/msexcel-builder.js.map

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions src/msexcel-builder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
###
JSZip = require 'jszip'
xml = require 'xmlbuilder'
path = require 'path'

tool =
i2a : (i) ->
Expand Down Expand Up @@ -339,14 +340,19 @@ class Workbook
sheet = new Sheet(@,name,cols,rows)
@sheets.push sheet
return sheet

saveSync: () =>
@_save(null, true)
save: (cb) =>
target = @fpath + '/' + @fname
@_save(cb, false)
_save: (cb, sync) =>
fs = require 'fs'
write = if sync then fs.writeFileSync else fs.writeFile
target = @fpath + path.sep + @fname
@generate (err, zip) ->
buffer = zip.generate(type: 'nodebuffer')
# dependence on file system isolated to this function
require('fs').writeFile target, buffer, cb

if sync then write target, buffer else write target, buffer, cb
# takes a callback function(err, zip) and returns a JSZip object on success
generate: (cb) =>

Expand Down
10 changes: 6 additions & 4 deletions test/example.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var fs = require('fs');
var assert = require('assert');
var JSZip = require('jszip');
const os = require('os')
const path = require('path')

var excelbuilder = require('..');

Expand Down Expand Up @@ -38,18 +40,18 @@ describe('It generates a simple workbook', function() {
if (err) throw err;
else {
var buffer = zip.generate({type: "nodebuffer"});
var OUTFILE = '/tmp/example.xlsx';
const OUTFILE = [ os.tmpdir(), 'example.xlsx' ].join(path.sep)
fs.writeFile(OUTFILE, buffer, function (err) {
console.log('Test file written to ' + OUTFILE);
compareWorkbooks('./test/files/example.xlsx', OUTFILE)
compareWorkbooks([ 'test', 'files', 'example.xlsx' ].join(path.sep), OUTFILE)
done(err);
});
}
});
})

it ('Supports the prior constructor syntax', function(done) {
var PATH = '/tmp';
var PATH = os.tmpdir();
var FILENAME = 'example2.xlsx';
var workbook = excelbuilder.createWorkbook(PATH, FILENAME);
var sheet1 = workbook.createSheet('sheet1', 10, 12);
Expand All @@ -61,7 +63,7 @@ describe('It generates a simple workbook', function() {
workbook.save(function (err) {
if (err) throw err;
else {
var OUTFILE = PATH + "/" + FILENAME;
const OUTFILE = [ PATH, FILENAME ].join(path.sep)
console.log('Test file written to ' + OUTFILE);
done()
}
Expand Down