diff --git a/lib/doc/workbook.js b/lib/doc/workbook.js index c3e8ee9c5..b9122bb6f 100644 --- a/lib/doc/workbook.js +++ b/lib/doc/workbook.js @@ -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 @@ -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 }); @@ -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); }); }, @@ -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, @@ -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 }); diff --git a/lib/doc/worksheet.js b/lib/doc/worksheet.js index 47e5d2000..0c5b4d04b 100644 --- a/lib/doc/worksheet.js +++ b/lib/doc/worksheet.js @@ -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; diff --git a/spec/integration/data/test-issue-257.xlsx b/spec/integration/data/test-issue-257.xlsx new file mode 100644 index 000000000..af16218a3 Binary files /dev/null and b/spec/integration/data/test-issue-257.xlsx differ diff --git a/spec/integration/issues/issue-257-sheet-order.spec.js b/spec/integration/issues/issue-257-sheet-order.spec.js new file mode 100644 index 000000000..8c892f176 --- /dev/null +++ b/spec/integration/issues/issue-257-sheet-order.spec.js @@ -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']); + }); + }); +}); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy