Skip to content

Commit

Permalink
Add 4 new functions: set_cell_bool, ungroup_sheets, unmerge_cell and …
Browse files Browse the repository at this point in the history
…update_linked_value

- Update unit tests and docs for the function
  • Loading branch information
xuri committed Dec 19, 2024
1 parent 32bf4f7 commit eeafd90
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 15 deletions.
107 changes: 94 additions & 13 deletions excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,8 +1364,8 @@ def set_active_sheet(self, index: int) -> Optional[Exception]:
get_sheet_map. It should be greater than or equal to 0 and less than the
total worksheet numbers.
Parameters:
index (int): The sheet index
Args:
index (int): The sheet index
Returns:
Optional[Exception]: Returns None if no error occurred,
Expand All @@ -1375,6 +1375,26 @@ def set_active_sheet(self, index: int) -> Optional[Exception]:
err = lib.SetActiveSheet(self.file_index, index).decode(ENCODE)
return None if err == "" else Exception(err)

def set_cell_bool(self, sheet: str, cell: str, value: bool) -> Optional[Exception]:
"""
Set bool type value of a cell by given worksheet name, cell reference and
cell value.
Args:
sheet (str): The worksheet name
cell (str): The cell reference
value (bool): The cell value
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
"""
err, lib.SetCellBool.restype = None, c_char_p
err = lib.SetCellBool(
self.file_index, sheet.encode(ENCODE), cell.encode(ENCODE), value
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_cell_formula(
self, sheet: str, cell: str, formula: str, *opts: FormulaOpts
) -> Optional[Exception]:
Expand All @@ -1387,11 +1407,11 @@ def set_cell_formula(
automatically when the workbook has been opened, please call
"update_linked_value" after setting the cell formula functions.
Parameters:
sheet (str): The worksheet name
cell (str): The cell reference
formula (str): The cell formula
*opts (FormulaOpts): The formula options
Args:
sheet (str): The worksheet name
cell (str): The cell reference
formula (str): The cell formula
*opts (FormulaOpts): The formula options
Returns:
Optional[Exception]: Returns None if no error occurred,
Expand Down Expand Up @@ -1430,12 +1450,12 @@ def set_cell_hyperlink(
please use the other functions such as `set_cell_style` or
`set_sheet_row`.
Parameters:
sheet (str): The worksheet name
cell (str): The cell reference
link (str): The hyperlink
link_type (str): The hyperlink type
*opts (HyperlinkOpts): The hyperlink options
Args:
sheet (str): The worksheet name
cell (str): The cell reference
link (str): The hyperlink
link_type (str): The hyperlink type
*opts (HyperlinkOpts): The hyperlink options
Returns:
Optional[Exception]: Returns None if no error occurred,
Expand Down Expand Up @@ -1700,6 +1720,67 @@ def set_workbook_props(self, opts: WorkbookPropsOptions) -> Optional[Exception]:
err = lib.SetWorkbookProps(self.file_index, byref(options)).decode(ENCODE)
return None if err == "" else Exception(err)

def ungroup_sheets(self) -> Optional[Exception]:
"""
Ungroup worksheets.
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
"""
lib.UngroupSheets.restype = c_char_p
err = lib.UngroupSheets(self.file_index).decode(ENCODE)
return None if err == "" else Exception(err)

def unmerge_cell(
self, sheet: str, top_left_cell: str, bottom_right_cell: str
) -> Optional[Exception]:
"""
Unmerge a given range reference.
Args:
sheet (str): The worksheet name
top_left_cell (str): The top-left cell reference
bottom_right_cell (str): The right-bottom cell reference
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
Example:
Unmerge range reference D3:E9 on Sheet1:
.. code-block:: python
err = f.unmerge_cell("Sheet1", "D3", "E9")
"""
lib.UnmergeCell.restype = c_char_p
err = lib.UnmergeCell(
self.file_index,
sheet.encode(ENCODE),
top_left_cell.encode(ENCODE),
bottom_right_cell.encode(ENCODE),
).decode(ENCODE)
return None if err == "" else Exception(err)

def update_linked_value(self) -> Optional[Exception]:
"""
Fix linked values within a spreadsheet are not updating in Office Excel
application. This function will be remove value tag when met a cell have
a linked value.
Notice:
After opening generated workbook, Excel will update the linked value
and generate a new value and will prompt to save the file or not.
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
"""
lib.UpdateLinkedValue.restype = c_char_p
err = lib.UpdateLinkedValue(self.file_index).decode(ENCODE)
return None if err == "" else Exception(err)


def cell_name_to_coordinates(cell: str) -> Tuple[int, int, Optional[Exception]]:
"""
Expand Down
59 changes: 59 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,21 @@ func SetActiveSheet(idx, index int) *C.char {
return C.CString(errNil)
}

// SetCellBool provides a function to set bool type value of a cell by given
// worksheet name, cell reference and cell value.
//
//export SetCellBool
func SetCellBool(idx int, sheet, cell *C.char, value bool) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).SetCellBool(C.GoString(sheet), C.GoString(cell), value); err != nil {
C.CString(err.Error())
}
return C.CString(errNil)
}

// SetCellFormula provides a function to set formula on the cell is taken
// according to the given worksheet name and cell formula settings. The result
// of the formula cell can be calculated when the worksheet is opened by the
Expand Down Expand Up @@ -1479,5 +1494,49 @@ func SetWorkbookProps(idx int, opts *C.struct_WorkbookPropsOptions) *C.char {
return C.CString(errNil)
}

// UngroupSheets provides a function to ungroup worksheets.
//
//export UngroupSheets
func UngroupSheets(idx int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).UngroupSheets(); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

// UnmergeCell provides a function to unmerge a given range reference.
//
//export UnmergeCell
func UnmergeCell(idx int, sheet, topLeftCell, bottomRightCell *C.char) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString("")
}
if err := f.(*excelize.File).UnmergeCell(C.GoString(sheet), C.GoString(topLeftCell), C.GoString(bottomRightCell)); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

// UpdateLinkedValue fix linked values within a spreadsheet are not updating in
// Office Excel application. This function will be remove value tag when met a
// cell have a linked value.
//
//export UpdateLinkedValue
func UpdateLinkedValue(idx int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).UpdateLinkedValue(); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

func main() {
}
13 changes: 11 additions & 2 deletions test_excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ def test_style(self):
f.set_cell_value("Sheet1", "A7", datetime.datetime(2016, 8, 30, 11, 51, 0))
)
self.assertIsNone(f.set_cell_value("Sheet1", "A8", datetime.date(2016, 8, 30)))
self.assertIsNone(f.set_cell_bool("Sheet1", "A9", True))
self.assertIsNone(f.set_cell_bool("Sheet1", "A10", False))
self.assertEqual(
str(f.set_cell_value("SheetN", "A9", None)),
"sheet SheetN does not exist",
Expand All @@ -166,10 +168,11 @@ def test_style(self):
self.assertEqual("100", val)
self.assertIsNone(err)

self.assertIsNone(f.duplicate_row("Sheet1", 9))
self.assertIsNone(f.duplicate_row_to("Sheet1", 10, 10))
self.assertIsNone(f.duplicate_row("Sheet1", 20))
self.assertIsNone(f.duplicate_row_to("Sheet1", 20, 20))

self.assertIsNone(f.merge_cell("Sheet1", "A1", "B2"))
self.assertIsNone(f.unmerge_cell("Sheet1", "A1", "B2"))

idx, err = f.new_sheet("Sheet2")
self.assertEqual(idx, 1)
Expand Down Expand Up @@ -221,6 +224,8 @@ def test_style(self):
["TRUE"],
["8/30/16 11:51"],
["08-30-16"],
["TRUE"],
["FALSE"],
],
)
rows, err = f.get_rows("Sheet1", excelize.Options(raw_cell_value=True))
Expand All @@ -234,9 +239,13 @@ def test_style(self):
["1"],
["42612.49375"],
["42612"],
["1"],
["0"],
],
)

self.assertIsNone(f.ungroup_sheets())
self.assertIsNone(f.update_linked_value())
self.assertIsNone(f.save())
self.assertIsNone(f.save(excelize.Options(password="")))
self.assertIsNone(f.close())
Expand Down

0 comments on commit eeafd90

Please sign in to comment.