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

Sheet Order #257

Closed
crash83k opened this issue Jan 27, 2017 · 5 comments
Closed

Sheet Order #257

crash83k opened this issue Jan 27, 2017 · 5 comments

Comments

@crash83k
Copy link

crash83k commented Jan 27, 2017

I'm not sure why, but if I open a book with multiple sheets, and then immediately save it to a new file, the sheets are out of order.

Is there a way to ensure that the sheets are saved in the same order?

BTW, this is a fantastic library. Seems very complete (especially for our needs).

@crash83k
Copy link
Author

crash83k commented Jan 27, 2017

Also, another note:

Looking at the workbook model, I see an object key called sheets that has the worksheets in their proper order. I'm digging a little deeper to see if this is the only place where the order of the sheets is set, and if that order is being used when writing the book to a new file.

edit For clarity, I'm talking about the model.sheets array created at Line: 139 in lib/xlsx/xlsx.js

@crash83k
Copy link
Author

Update:

I haven't been able to get the sheets to read in the correct order, but I've been able to get them to write in the correct order. I'm sure this isn't the best way to go about this, but maybe it will give you some clue as to what would be the best way to do this.

What I'm doing is saving the sheet order from the default case in the createInputStream method as self.orderedSheets like so:

          promise = self.parseWorksheet(entry)
              .then(function (worksheet) {
                worksheet.sheetNo = sheetNo;
                model.worksheetHash[entry.path] = worksheet;
                model.worksheets.push(worksheet);
              })
              .then(()=>{
                if(model.worksheets.length === model.sheets.length) {
                  self.orderedSheets = model.sheets;
                }
              });

Then in the write method, I'm popping off the sheets from the model.worksheets array. (Sorting didn't seem to work.) And then I push them back to the model.worksheets array in the order they appear in the self.orderedSheets array. I also do a quick check to see if there's any more sheets in the tmpSheets object just in case they were programmatically added, and push them if they were.

Like so:

    var tmpSheets = {};
    while (model.worksheets.length > 0) {
      let sheet = model.worksheets.pop();
      tmpSheets[ sheet.name ] = sheet;
    }

    this.orderedSheets.forEach(orderedSheet => {
      model.worksheets.push(tmpSheets[ orderedSheet.name ]);
      delete tmpSheets[ orderedSheet.name ]
    });

    const keys = Object.keys(tmpSheets);
    if (keys.length > 0) {
      keys.forEach(key => {
        model.worksheets.push(tmpSheets[ key ]);
      });
    }

However, I haven't tested the additional sheets part. I won't be adding sheets in my usage. Only modifying sheets as they go out.

@crash83k
Copy link
Author

I can create a PR for this. Just let me know...
https://github.com/crash83k/exceljs

@pawsong
Copy link

pawsong commented Oct 9, 2017

Same issue here. eachSheet was expected to trigger callback in the sheet order, but it was not.

robbi pushed a commit to robbi/exceljs that referenced this issue Jan 7, 2018
@guyonroche
Copy link
Collaborator

Thanks to Robbi for his fix. I'll close this now.

erezny pushed a commit to atriumlts/exceljs that referenced this issue Jan 2, 2019
commit 52ddb6b
Author: Elliott Rezny <elliott@atrium.co>
Date:   Mon Dec 31 16:47:16 2018 -0800

    build

commit d924091
Author: scott <scott@atriumlts.com>
Date:   Wed Jan 3 13:14:28 2018 -0800

    Missing dist files

commit e966e71
Author: scott <scott@atriumlts.com>
Date:   Wed Jan 3 13:13:57 2018 -0800

    Remove namespace specific things to parse malformed XML

commit f6f9cb4
Author: scott <scott@atriumlts.com>
Date:   Tue Jan 2 14:11:38 2018 -0800

    Sax shim for XMLNS parsing for importing excel files which have namespaces for whatever reason

commit 66f8c2e
Author: scott <scott@justmachinery.net>
Date:   Fri Dec 29 17:27:50 2017 -0800

    Build for previous commit

commit ce033ef
Author: scott <scott@justmachinery.net>
Date:   Fri Dec 29 17:27:07 2017 -0800

    Tentative fix for malformed app.xml

commit 2a2b091
Author: scott <scott@justmachinery.net>
Date:   Fri Dec 29 16:39:13 2017 -0800

    Unignore build/dist to host package on git

commit b8a21fe
Author: scott <scott@justmachinery.net>
Date:   Fri Dec 22 19:25:29 2017 -0800

    Testing iterative calculation output

commit f83fad4
Author: Guyon <guyon@live.com>
Date:   Tue Jan 16 20:05:30 2018 +0000

    0.8.2

commit 0eacc22
Author: guyon <guyon@live.com>
Date:   Tue Jan 16 20:04:46 2018 +0000

    Doc

commit ace1e4d
Merge: d17b361 b9059a3
Author: Guyon Roche <guyon@live.com>
Date:   Tue Jan 16 19:56:06 2018 +0000

    Merge pull request exceljs#471 from robbi/hotfix/sheet_order#257

    Hotfix/sheet order#257

commit d17b361
Merge: 3950e0e fd0bf4c
Author: Guyon Roche <guyon@live.com>
Date:   Tue Jan 16 19:51:08 2018 +0000

    Merge pull request exceljs#466 from peakon/fix/unbreakChartsheet

    Don't break when loading an Excel file containing a chartsheet

commit b9059a3
Author: Olivier Robin <olivier.robin@bpce-it.fr>
Date:   Sun Jan 7 18:42:58 2018 +0100

    Fix linting issue

commit 7a53221
Author: Olivier Robin <olivier.robin@bpce-it.fr>
Date:   Sun Jan 7 18:40:14 2018 +0100

    Add worksheet order number

commit db408fc
Author: Olivier Robin <olivier.robin@bpce-it.fr>
Date:   Sun Jan 7 18:39:20 2018 +0100

    Add test case for the issue exceljs#257

commit fd0bf4c
Author: Andreas Lind <andreas.lind@peakon.com>
Date:   Tue Dec 19 14:12:10 2017 +0100

    Don't break when loading an Excel file containing a chartsheet

    Test case found here: http://www.excel-easy.com/examples/excel-files/chart-sheet.xlsx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants