Repository: biolab/orange3

Issue:
File: Excel with multiple sheets shows wrong one upon loading
<!-- 
Thanks for taking the time to report a bug!
If you're raising an issue about an add-on (i.e., installed via Options > Add-ons), raise an issue in the relevant add-on's issue tracker instead. See: https://github.com/biolab?q=orange3
To fix the bug, we need to be able to reproduce it. Please answer the following questions to the best of your ability.
-->


- [ ] **What's wrong?**
<!-- Be specific, clear, and concise. Include screenshots if relevant. -->
<!-- If you're getting an error message, copy it, and enclose it with three backticks (```). -->
When loading an Excel file with multiple sheets, the last sheet is loaded, but the dropdown shows as if the first one is shown.




- [ ] **How can we reproduce the problem?** 
<!-- Upload a zip with the .ows file and data. -->
<!-- Describe the steps (open this widget, click there, then add this...) -->
Load an Excel file with multiple sheets and observe the Sheet value.

[exc-multi-sheets.xlsx](https://github.com/biolab/orange3/files/5695852/exc-multi-sheets.xlsx)


- [ ] **What's your environment?**
<!-- To find your Orange version, see "Help → About → Version" or `Orange.version.full_version` in code -->
- Operating system: OSX High Sierra
- Orange version: 3.28.dev
- How you installed Orange: conda/pip



Discussion:

Resolving patch:
diff --git a/Orange/data/io.py b/Orange/data/io.py
--- a/Orange/data/io.py
+++ b/Orange/data/io.py
@@ -264,7 +264,7 @@ def read(self):
             cells = self.get_cells()
             table = self.data_table(cells)
             table.name = path.splitext(path.split(self.filename)[-1])[0]
-            if self.sheet:
+            if self.sheet and len(self.sheets) > 1:
                 table.name = '-'.join((table.name, self.sheet))
         except Exception:
             raise IOError("Couldn't load spreadsheet from " + self.filename)
@@ -277,6 +277,10 @@ class ExcelReader(_BaseExcelReader):
     DESCRIPTION = 'Microsoft Excel spreadsheet'
     ERRORS = ("#VALUE!", "#DIV/0!", "#REF!", "#NUM!", "#NULL!", "#NAME?")
 
+    def __init__(self, filename):
+        super().__init__(filename)
+        self.sheet = self.workbook.active.title
+
     @property
     def workbook(self) -> openpyxl.Workbook:
         if not self._workbook:
@@ -340,6 +344,10 @@ class XlsReader(_BaseExcelReader):
     EXTENSIONS = ('.xls',)
     DESCRIPTION = 'Microsoft Excel 97-2004 spreadsheet'
 
+    def __init__(self, filename):
+        super().__init__(filename)
+        self.sheet = self.workbook.sheet_by_index(0).name
+
     @property
     def workbook(self) -> xlrd.Book:
         if not self._workbook:
diff --git a/Orange/widgets/data/owfile.py b/Orange/widgets/data/owfile.py
--- a/Orange/widgets/data/owfile.py
+++ b/Orange/widgets/data/owfile.py
@@ -409,14 +409,12 @@ def _update_sheet_combo(self):
         self.sheet_box.show()
 
     def _select_active_sheet(self):
-        if self.reader.sheet:
-            try:
-                idx = self.reader.sheets.index(self.reader.sheet)
-                self.sheet_combo.setCurrentIndex(idx)
-            except ValueError:
-                # Requested sheet does not exist in this file
-                self.reader.select_sheet(None)
-        else:
+        try:
+            idx = self.reader.sheets.index(self.reader.sheet)
+            self.sheet_combo.setCurrentIndex(idx)
+        except ValueError:
+            # Requested sheet does not exist in this file
+            self.reader.select_sheet(None)
             self.sheet_combo.setCurrentIndex(0)
 
     @staticmethod


Test patch:
diff --git a/Orange/widgets/data/tests/test_owfile.py b/Orange/widgets/data/tests/test_owfile.py
--- a/Orange/widgets/data/tests/test_owfile.py
+++ b/Orange/widgets/data/tests/test_owfile.py
@@ -603,3 +603,22 @@ def test_files_relocated(self):
             self.assertEqual(w.recent_paths[0].relpath, base_name)
         finally:
             remove(file_name)
+
+    def test_sheets(self):
+        # pylint: disable=protected-access
+        widget = self.widget
+        combo = widget.sheet_combo
+        widget.last_path = \
+            lambda: path.join(path.dirname(__file__), '..', "..", '..',
+                              'tests', 'xlsx_files', 'header_0_sheet.xlsx')
+        widget._try_load()
+        widget.reader.sheet = "my_sheet"
+        widget._select_active_sheet()
+        self.assertEqual(combo.itemText(0), "Sheet1")
+        self.assertEqual(combo.itemText(1), "my_sheet")
+        self.assertEqual(combo.itemText(2), "Sheet3")
+        self.assertEqual(combo.currentIndex(), 1)
+
+        widget.reader.sheet = "no such sheet"
+        widget._select_active_sheet()
+        self.assertEqual(combo.currentIndex(), 0)