Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
15 changes: 11 additions & 4 deletions lib/doc/workbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ var Workbook = module.exports = function() {

Workbook.prototype = {
get xlsx() {
return this._xlsx || (this._xlsx = new XLSX(this));
if (!this._xlsx) this._xlsx = new XLSX(this);
return this._xlsx;
},
get csv() {
return this._csv || (this._csv = new CSV(this));
if (!this._csv) this._csv = new CSV(this);
return this._csv;
},
get nextId() {
// find the next unique spot to add worksheet
Expand Down Expand Up @@ -69,9 +71,11 @@ Workbook.prototype = {
}
}

var lastOrderNo = this._worksheets.reduce((acc, ws) => ((ws && ws.orderNo) > acc ? ws.orderNo : acc), 0);
var worksheetOptions = Object.assign({}, options, {
id: id,
name: name,
orderNo: lastOrderNo + 1,
workbook: this
});

Expand Down Expand Up @@ -105,11 +109,11 @@ Workbook.prototype = {

get worksheets() {
// return a clone of _worksheets
return this._worksheets.filter(Boolean);
return this._worksheets.sort((a, b) => a.orderNo - b.orderNo).filter(Boolean);
},

eachSheet: function(iteratee) {
this._worksheets.forEach(function(sheet) {
this._worksheets.sort((a, b) => a.orderNo - b.orderNo).forEach((sheet) => {
iteratee(sheet, sheet.id);
});
},
Expand Down Expand Up @@ -149,6 +153,7 @@ Workbook.prototype = {
modified: this.modified,
properties: this.properties,
worksheets: this._worksheets.filter(Boolean).map(function(worksheet) { return worksheet.model; }),
sheets: this._worksheets.sort((a, b) => a.orderNo - b.orderNo).map(ws => ws.model).filter(Boolean),
definedNames: this._definedNames.model,
views: this.views,
company: this.company,
Expand Down Expand Up @@ -187,9 +192,11 @@ Workbook.prototype = {
value.worksheets.forEach(worksheetModel => {
var id = worksheetModel.id;
var name = worksheetModel.name;
var orderNo = value.sheets.findIndex(ws => ws.id === id);
var worksheet = this._worksheets[id] = new Worksheet({
id: id,
name: name,
orderNo,
workbook: this
});

Expand Down
1 change: 1 addition & 0 deletions lib/doc/worksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var Worksheet = module.exports = function(options) {

// in a workbook, each sheet will have a number
this.id = options.id;
this.orderNo = options.orderNo;

// and a name
this.name = options.name || 'Sheet' + this.id;
Expand Down
Binary file added spec/integration/data/test-issue-257.xlsx
Binary file not shown.
18 changes: 18 additions & 0 deletions spec/integration/issues/issue-257-sheet-order.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

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

var Excel = verquire('excel');

var expect = chai.expect;

describe('github issues', function() {
it('issue 257 - worksheet order is not respected', function() {
var wb = new Excel.Workbook();
return wb.xlsx.readFile('./spec/integration/data/test-issue-257.xlsx')
.then(function() {
expect(wb.worksheets.map(ws => ws.name)).to.deep.equal(['First', 'Second']);
});
});
});