Index 0 worksheet creation in existing workbook creates worksheet group

Issue #878 closed
Aaron Black
created an issue

When opening an existing workbook and creating a new worksheet at index 0, a worksheet group is created that persists on save. The group consists of the worksheet that was active when excel was last saved, as well as the new worksheet. This is mostly an annoyance, as many functions will fail as a result of the multiply selected worksheets, and it's not immediately clear to the user why.

Note that this issues does not seem to manifest when the worksheet is created at another index. For example, this creates the worksheet group

import openpyxl
workbook = openpyxl.load_workbook("whatever.xlsx")
workbook.create_sheet("new_name", 0)
workbook.save("whatever.xlsx")

... while this does not.

import openpyxl
workbook = openpyxl.load_workbook("whatever.xlsx")
workbook.create_sheet("new_name", 1)
workbook.save("whatever.xlsx")

This group also seems to be created when any worksheet is moved to index 0 within the workbook.

import openpyxl
workbook = openpyxl.load_workbook("whatever.xlsx")
workbook._sheets.insert(0, workbook._sheets.pop())
workbook.save("whatever.xlsx")

As above, this does not create the worksheet group when inserting at another index.

A screenshot of the group is attached.

Comments (4)

  1. CharlieC

    Worksheets are just a list to openpyxl so it doesn't really matter where you insert a sheet.

    What are worksheet groups? I can't find anything in the specification about them but I've hit a few problems when creating workbooks with hidden sheets in Office 2010. A workaround I've found for this is to play around with the workbook views: wb.views[0].firstSheet = 1. I suspect you might need to do something similar here.

  2. Aaron Black reporter

    Worksheet groups is what I've seen used as the name for multi-selecting worksheets so that operations in one are duplicated across all. I don't know if that's what it's called in the spec though.

    I'll play around with the workbook views. Thanks for the suggestion.

  3. Log in to comment