Skip to content

Commit

Permalink
feat: return worksheet when create and rename the sheet in workbook
Browse files Browse the repository at this point in the history
  • Loading branch information
Zncl2222 committed Dec 30, 2024
1 parent e62fc6c commit 28cb54a
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pyfastexcel/workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def rename_sheet(self, old_sheet_name: str, new_sheet_name: str) -> None:
Args:
old_sheet_name (str): The name of the sheet to rename.
new_sheet_name (str): The new name for the sheet.
Return:
WorkSheet instance
"""
if self.workbook.get(old_sheet_name) is None:
raise IndexError(f'Sheet {old_sheet_name} does not exist.')
Expand All @@ -82,13 +84,14 @@ def rename_sheet(self, old_sheet_name: str, new_sheet_name: str) -> None:
[new_sheet_name if x == old_sheet_name else x for x in self._sheet_list]
)
self.sheet = new_sheet_name
return self.workbook[self.sheet]

def create_sheet(
self,
sheet_name: str,
pre_allocate: dict[str, int] = None,
plain_data: list[list] = None,
) -> None:
) -> WorkSheet:
"""
Creates a new sheet, and set it as current self.sheet.
Expand All @@ -99,12 +102,15 @@ def create_sheet(
for pre-allocating data in new sheet.
plain_data (list[list[str]], optional): A 2D list of strings
representing initial data to populate new sheet.
Return:
WorkSheet instance.
"""
if self.workbook.get(sheet_name) is not None:
raise ValueError(f'Sheet {sheet_name} already exists.')
self.workbook[sheet_name] = WorkSheet(pre_allocate=pre_allocate, plain_data=plain_data)
self.sheet = sheet_name
self._sheet_list = tuple([x for x in self._sheet_list] + [sheet_name])
return self.workbook[sheet_name]

def switch_sheet(self, sheet_name: str) -> None:
"""
Expand Down

0 comments on commit 28cb54a

Please sign in to comment.