Improvement on the invalid inputs

This commit is contained in:
chfw 2015-02-19 08:56:31 +00:00
parent 28cb1b1749
commit 5d736a43e5
2 changed files with 16 additions and 3 deletions

View File

@ -111,7 +111,11 @@ class XLBook(BookReader):
"""Return iterable sheet array"""
if self.sheet_name is not None:
return [self.native_book.sheet_by_name(self.sheet_name)]
try:
sheet = self.native_book.sheet_by_name(self.sheet_name)
return [sheet]
except xlrd.XLRDError:
raise ValueError("%s cannot be found" % self.sheet_name)
elif self.sheet_index is not None:
return [self.native_book.sheet_by_index(self.sheet_index)]
else:

View File

@ -2,6 +2,7 @@ from base import PyexcelMultipleSheetBase
import pyexcel
import os
from pyexcel.ext import xls
from nose.tools import raises
import sys
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
@ -56,9 +57,17 @@ class TestAddBooks:
assert b1['Sheet1'].to_array() == self.content['Sheet1']
def test_load_a_single_sheet2(self):
b1 = pyexcel.load_book(self.testfile, sheet_index=0)
b1 = pyexcel.load_book(self.testfile, sheet_index=2)
assert len(b1.sheet_names()) == 1
assert b1['Sheet1'].to_array() == self.content['Sheet1']
assert b1['Sheet3'].to_array() == self.content['Sheet3']
@raises(IndexError)
def test_load_a_single_sheet3(self):
pyexcel.load_book(self.testfile, sheet_index=10000)
@raises(ValueError)
def test_load_a_single_sheet4(self):
pyexcel.load_book(self.testfile, sheet_name="Not exist")
def test_delete_sheets(self):
b1 = pyexcel.load_book(self.testfile)