Skip to content

Commit 5d736a4

Browse files
committed
Improvement on the invalid inputs
1 parent 28cb1b1 commit 5d736a4

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

pyexcel_xls/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ def sheetIterator(self):
111111
"""Return iterable sheet array"""
112112

113113
if self.sheet_name is not None:
114-
return [self.native_book.sheet_by_name(self.sheet_name)]
114+
try:
115+
sheet = self.native_book.sheet_by_name(self.sheet_name)
116+
return [sheet]
117+
except xlrd.XLRDError:
118+
raise ValueError("%s cannot be found" % self.sheet_name)
115119
elif self.sheet_index is not None:
116120
return [self.native_book.sheet_by_index(self.sheet_index)]
117121
else:

tests/test_mutliple_sheets.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pyexcel
33
import os
44
from pyexcel.ext import xls
5+
from nose.tools import raises
56
import sys
67

78
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
@@ -56,9 +57,17 @@ def test_load_a_single_sheet(self):
5657
assert b1['Sheet1'].to_array() == self.content['Sheet1']
5758

5859
def test_load_a_single_sheet2(self):
59-
b1 = pyexcel.load_book(self.testfile, sheet_index=0)
60+
b1 = pyexcel.load_book(self.testfile, sheet_index=2)
6061
assert len(b1.sheet_names()) == 1
61-
assert b1['Sheet1'].to_array() == self.content['Sheet1']
62+
assert b1['Sheet3'].to_array() == self.content['Sheet3']
63+
64+
@raises(IndexError)
65+
def test_load_a_single_sheet3(self):
66+
pyexcel.load_book(self.testfile, sheet_index=10000)
67+
68+
@raises(ValueError)
69+
def test_load_a_single_sheet4(self):
70+
pyexcel.load_book(self.testfile, sheet_name="Not exist")
6271

6372
def test_delete_sheets(self):
6473
b1 = pyexcel.load_book(self.testfile)

0 commit comments

Comments
 (0)