Skip to content

Commit

Permalink
Fix problem exporting a single sheet without using a workbook.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyroberts committed Apr 17, 2015
1 parent 2a31434 commit ca8feff
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions xltable/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def to_excel(self,
:param workbook: xlwriter.Workbook this sheet belongs to.
:param worksheet: Excel COM Worksheet instance to write to.
:param xl_app: Excel COM Excel Application to write to.
:param bool clear: clear worksheet before writing.
:param bool clear: if a worksheet is provided, clear worksheet before writing.
:param bool rename: if a worksheet is provided, rename self to match the worksheet.
:param bool resize_columns: resize sheet columns after writing.
"""
Expand All @@ -256,23 +256,18 @@ def to_excel(self,

xl = xl_app = gencache.EnsureDispatch(xl_app)

if worksheet is None:
# If there's no workbook and no worksheet then create a new workbook
# just this sheet and use it to write to excel.
if workbook is None:
from .workbook import Workbook
workbook = Workbook(worksheets=[self])
return workbook.to_excel(xl_app=xl_app)
# Create a workbook if there isn't one already
if not workbook:
from .workbook import Workbook
workbook = Workbook(worksheets=[self])

# Otherwise create a new worksheet.
worksheet = workbook.add_excel_worksheet()
worksheet.Name = self.name
if worksheet is None:
# If there's no worksheet then call Workbook.to_excel which will create one
return workbook.to_excel(xl_app=xl_app, resize_columns=resize_columns)

if rename:
self.__name = worksheet.Name

table_ref = self if workbook is None else workbook

# set manual calculation and turn off screen updating while we update the cells
calculation = xl.Calculation
screen_updating = xl.ScreenUpdating
Expand All @@ -290,7 +285,7 @@ def to_excel(self,

origin = worksheet.Range("A1")
xl_cell = origin
for row in self.iterrows(table_ref):
for row in self.iterrows(workbook):
row = _to_pywintypes(row)

# set the value and formulae to the excel range (it's much quicker to
Expand Down

0 comments on commit ca8feff

Please sign in to comment.