Skip to content

Commit afb9fc4

Browse files
committed
FIX: fixed copying a sheet when the target key is an int (closes #1092)
1 parent fddbbf0 commit afb9fc4

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/changes/version_0_34_3.rst.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ Fixes
5858
* fixed something (closes :issue:`1`).
5959

6060
* fixed converting a scalar Array (an Array with 0 dimensions) to string with numpy 1.22+.
61+
62+
* fixed copying a sheet from one Excel workbook to another when the destination
63+
sheet is given by position (closes :issue:`1092`).

larray/inout/xw_excel.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,11 @@ def __setitem__(self, key, value):
239239
value.xw_sheet.api.Copy(None, target_sheet.api)
240240
if key_in_self:
241241
target_sheet.delete()
242-
# rename the new sheet
243-
self[target_idx].name = key
242+
243+
# rename the new sheet if necessary
244+
# if the key is an integer, we keep the name of the source sheet
245+
if isinstance(key, str):
246+
self[target_idx].name = key
244247
return
245248
if key_in_self:
246249
sheet = self[key]

larray/tests/test_excel.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ def test_setitem(self):
128128
assert wb.sheet_names() == ['sheet1', 'sheet2', 'sheet3']
129129
assert wb['sheet2']['A1'].value == 'sheet1 content'
130130

131+
# reset sheet 3 content (for next test)
132+
wb['sheet3'] = 'sheet3 content'
133+
assert wb['sheet3']['A1'].value == 'sheet3 content'
134+
135+
# sheet did exist, Sheet value (int key)
136+
wb[1] = wb['sheet3']
137+
assert wb.sheet_names() == ['sheet1', 'sheet3 (2)', 'sheet3']
138+
assert wb[1]['A1'].value == 'sheet3 content'
139+
131140
with open_excel(visible=False, app="new") as wb2:
132141
assert wb.app != wb2.app
133142
with must_raise(ValueError, msg="cannot copy a sheet from one instance of Excel to another"):

0 commit comments

Comments
 (0)