Active Sheet Index Not Updated On Sheet Insert/Delete

Issue #1033 wontfix
flywire
created an issue

Inserting or deleting a sheet in a workbook can change the active sheet. When sheets are inserted or deleted the active index should also be updated.

In example below 'alpha' sheet should remain active:

>>> print(wb.sheetnames)
[u'alpha', u'bravo']
>>> print(wb.active)
<Worksheet "alpha">
>>>
>>> ws = wb.create_sheet('charlie',0)  # insert at index 0
>>> print(wb.sheetnames)
[u'charlie', u'alpha', u'bravo']
>>> print(wb.active)
<Worksheet "charlie">

Comments (5)

  1. CharlieC

    I don't think it's possible to guess the correct behaviour here. openpyxl itself doesn't really care about active sheets except for convenience. If you need to care about the active sheet for Excel you should manage it explicitly yourself. The code for this could be improved so that a sheet or sheet name instead of an index can be used. But that is a separate issue.

  2. flywire reporter

    The fix is robust: if inserted sheet index is less than or equal to active sheet index increment active sheet index.

    I agree the way openpxl code is written it couldn't be fixed.

  3. Log in to comment