Skip to content

Commit eeafd90

Browse files
committed
Add 4 new functions: set_cell_bool, ungroup_sheets, unmerge_cell and update_linked_value
- Update unit tests and docs for the function
1 parent 32bf4f7 commit eeafd90

File tree

3 files changed

+164
-15
lines changed

3 files changed

+164
-15
lines changed

excelize.py

Lines changed: 94 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,8 @@ def set_active_sheet(self, index: int) -> Optional[Exception]:
13641364
get_sheet_map. It should be greater than or equal to 0 and less than the
13651365
total worksheet numbers.
13661366
1367-
Parameters:
1368-
index (int): The sheet index
1367+
Args:
1368+
index (int): The sheet index
13691369
13701370
Returns:
13711371
Optional[Exception]: Returns None if no error occurred,
@@ -1375,6 +1375,26 @@ def set_active_sheet(self, index: int) -> Optional[Exception]:
13751375
err = lib.SetActiveSheet(self.file_index, index).decode(ENCODE)
13761376
return None if err == "" else Exception(err)
13771377

1378+
def set_cell_bool(self, sheet: str, cell: str, value: bool) -> Optional[Exception]:
1379+
"""
1380+
Set bool type value of a cell by given worksheet name, cell reference and
1381+
cell value.
1382+
1383+
Args:
1384+
sheet (str): The worksheet name
1385+
cell (str): The cell reference
1386+
value (bool): The cell value
1387+
1388+
Returns:
1389+
Optional[Exception]: Returns None if no error occurred,
1390+
otherwise returns an Exception with the message.
1391+
"""
1392+
err, lib.SetCellBool.restype = None, c_char_p
1393+
err = lib.SetCellBool(
1394+
self.file_index, sheet.encode(ENCODE), cell.encode(ENCODE), value
1395+
).decode(ENCODE)
1396+
return None if err == "" else Exception(err)
1397+
13781398
def set_cell_formula(
13791399
self, sheet: str, cell: str, formula: str, *opts: FormulaOpts
13801400
) -> Optional[Exception]:
@@ -1387,11 +1407,11 @@ def set_cell_formula(
13871407
automatically when the workbook has been opened, please call
13881408
"update_linked_value" after setting the cell formula functions.
13891409
1390-
Parameters:
1391-
sheet (str): The worksheet name
1392-
cell (str): The cell reference
1393-
formula (str): The cell formula
1394-
*opts (FormulaOpts): The formula options
1410+
Args:
1411+
sheet (str): The worksheet name
1412+
cell (str): The cell reference
1413+
formula (str): The cell formula
1414+
*opts (FormulaOpts): The formula options
13951415
13961416
Returns:
13971417
Optional[Exception]: Returns None if no error occurred,
@@ -1430,12 +1450,12 @@ def set_cell_hyperlink(
14301450
please use the other functions such as `set_cell_style` or
14311451
`set_sheet_row`.
14321452
1433-
Parameters:
1434-
sheet (str): The worksheet name
1435-
cell (str): The cell reference
1436-
link (str): The hyperlink
1437-
link_type (str): The hyperlink type
1438-
*opts (HyperlinkOpts): The hyperlink options
1453+
Args:
1454+
sheet (str): The worksheet name
1455+
cell (str): The cell reference
1456+
link (str): The hyperlink
1457+
link_type (str): The hyperlink type
1458+
*opts (HyperlinkOpts): The hyperlink options
14391459
14401460
Returns:
14411461
Optional[Exception]: Returns None if no error occurred,
@@ -1700,6 +1720,67 @@ def set_workbook_props(self, opts: WorkbookPropsOptions) -> Optional[Exception]:
17001720
err = lib.SetWorkbookProps(self.file_index, byref(options)).decode(ENCODE)
17011721
return None if err == "" else Exception(err)
17021722

1723+
def ungroup_sheets(self) -> Optional[Exception]:
1724+
"""
1725+
Ungroup worksheets.
1726+
1727+
Returns:
1728+
Optional[Exception]: Returns None if no error occurred,
1729+
otherwise returns an Exception with the message.
1730+
"""
1731+
lib.UngroupSheets.restype = c_char_p
1732+
err = lib.UngroupSheets(self.file_index).decode(ENCODE)
1733+
return None if err == "" else Exception(err)
1734+
1735+
def unmerge_cell(
1736+
self, sheet: str, top_left_cell: str, bottom_right_cell: str
1737+
) -> Optional[Exception]:
1738+
"""
1739+
Unmerge a given range reference.
1740+
1741+
Args:
1742+
sheet (str): The worksheet name
1743+
top_left_cell (str): The top-left cell reference
1744+
bottom_right_cell (str): The right-bottom cell reference
1745+
1746+
Returns:
1747+
Optional[Exception]: Returns None if no error occurred,
1748+
otherwise returns an Exception with the message.
1749+
1750+
Example:
1751+
Unmerge range reference D3:E9 on Sheet1:
1752+
1753+
.. code-block:: python
1754+
1755+
err = f.unmerge_cell("Sheet1", "D3", "E9")
1756+
"""
1757+
lib.UnmergeCell.restype = c_char_p
1758+
err = lib.UnmergeCell(
1759+
self.file_index,
1760+
sheet.encode(ENCODE),
1761+
top_left_cell.encode(ENCODE),
1762+
bottom_right_cell.encode(ENCODE),
1763+
).decode(ENCODE)
1764+
return None if err == "" else Exception(err)
1765+
1766+
def update_linked_value(self) -> Optional[Exception]:
1767+
"""
1768+
Fix linked values within a spreadsheet are not updating in Office Excel
1769+
application. This function will be remove value tag when met a cell have
1770+
a linked value.
1771+
1772+
Notice:
1773+
After opening generated workbook, Excel will update the linked value
1774+
and generate a new value and will prompt to save the file or not.
1775+
1776+
Returns:
1777+
Optional[Exception]: Returns None if no error occurred,
1778+
otherwise returns an Exception with the message.
1779+
"""
1780+
lib.UpdateLinkedValue.restype = c_char_p
1781+
err = lib.UpdateLinkedValue(self.file_index).decode(ENCODE)
1782+
return None if err == "" else Exception(err)
1783+
17031784

17041785
def cell_name_to_coordinates(cell: str) -> Tuple[int, int, Optional[Exception]]:
17051786
"""

main.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,21 @@ func SetActiveSheet(idx, index int) *C.char {
12661266
return C.CString(errNil)
12671267
}
12681268

1269+
// SetCellBool provides a function to set bool type value of a cell by given
1270+
// worksheet name, cell reference and cell value.
1271+
//
1272+
//export SetCellBool
1273+
func SetCellBool(idx int, sheet, cell *C.char, value bool) *C.char {
1274+
f, ok := files.Load(idx)
1275+
if !ok {
1276+
return C.CString(errFilePtr)
1277+
}
1278+
if err := f.(*excelize.File).SetCellBool(C.GoString(sheet), C.GoString(cell), value); err != nil {
1279+
C.CString(err.Error())
1280+
}
1281+
return C.CString(errNil)
1282+
}
1283+
12691284
// SetCellFormula provides a function to set formula on the cell is taken
12701285
// according to the given worksheet name and cell formula settings. The result
12711286
// of the formula cell can be calculated when the worksheet is opened by the
@@ -1479,5 +1494,49 @@ func SetWorkbookProps(idx int, opts *C.struct_WorkbookPropsOptions) *C.char {
14791494
return C.CString(errNil)
14801495
}
14811496

1497+
// UngroupSheets provides a function to ungroup worksheets.
1498+
//
1499+
//export UngroupSheets
1500+
func UngroupSheets(idx int) *C.char {
1501+
f, ok := files.Load(idx)
1502+
if !ok {
1503+
return C.CString(errFilePtr)
1504+
}
1505+
if err := f.(*excelize.File).UngroupSheets(); err != nil {
1506+
return C.CString(err.Error())
1507+
}
1508+
return C.CString(errNil)
1509+
}
1510+
1511+
// UnmergeCell provides a function to unmerge a given range reference.
1512+
//
1513+
//export UnmergeCell
1514+
func UnmergeCell(idx int, sheet, topLeftCell, bottomRightCell *C.char) *C.char {
1515+
f, ok := files.Load(idx)
1516+
if !ok {
1517+
return C.CString("")
1518+
}
1519+
if err := f.(*excelize.File).UnmergeCell(C.GoString(sheet), C.GoString(topLeftCell), C.GoString(bottomRightCell)); err != nil {
1520+
return C.CString(err.Error())
1521+
}
1522+
return C.CString(errNil)
1523+
}
1524+
1525+
// UpdateLinkedValue fix linked values within a spreadsheet are not updating in
1526+
// Office Excel application. This function will be remove value tag when met a
1527+
// cell have a linked value.
1528+
//
1529+
//export UpdateLinkedValue
1530+
func UpdateLinkedValue(idx int) *C.char {
1531+
f, ok := files.Load(idx)
1532+
if !ok {
1533+
return C.CString(errFilePtr)
1534+
}
1535+
if err := f.(*excelize.File).UpdateLinkedValue(); err != nil {
1536+
return C.CString(err.Error())
1537+
}
1538+
return C.CString(errNil)
1539+
}
1540+
14821541
func main() {
14831542
}

test_excelize.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def test_style(self):
144144
f.set_cell_value("Sheet1", "A7", datetime.datetime(2016, 8, 30, 11, 51, 0))
145145
)
146146
self.assertIsNone(f.set_cell_value("Sheet1", "A8", datetime.date(2016, 8, 30)))
147+
self.assertIsNone(f.set_cell_bool("Sheet1", "A9", True))
148+
self.assertIsNone(f.set_cell_bool("Sheet1", "A10", False))
147149
self.assertEqual(
148150
str(f.set_cell_value("SheetN", "A9", None)),
149151
"sheet SheetN does not exist",
@@ -166,10 +168,11 @@ def test_style(self):
166168
self.assertEqual("100", val)
167169
self.assertIsNone(err)
168170

169-
self.assertIsNone(f.duplicate_row("Sheet1", 9))
170-
self.assertIsNone(f.duplicate_row_to("Sheet1", 10, 10))
171+
self.assertIsNone(f.duplicate_row("Sheet1", 20))
172+
self.assertIsNone(f.duplicate_row_to("Sheet1", 20, 20))
171173

172174
self.assertIsNone(f.merge_cell("Sheet1", "A1", "B2"))
175+
self.assertIsNone(f.unmerge_cell("Sheet1", "A1", "B2"))
173176

174177
idx, err = f.new_sheet("Sheet2")
175178
self.assertEqual(idx, 1)
@@ -221,6 +224,8 @@ def test_style(self):
221224
["TRUE"],
222225
["8/30/16 11:51"],
223226
["08-30-16"],
227+
["TRUE"],
228+
["FALSE"],
224229
],
225230
)
226231
rows, err = f.get_rows("Sheet1", excelize.Options(raw_cell_value=True))
@@ -234,9 +239,13 @@ def test_style(self):
234239
["1"],
235240
["42612.49375"],
236241
["42612"],
242+
["1"],
243+
["0"],
237244
],
238245
)
239246

247+
self.assertIsNone(f.ungroup_sheets())
248+
self.assertIsNone(f.update_linked_value())
240249
self.assertIsNone(f.save())
241250
self.assertIsNone(f.save(excelize.Options(password="")))
242251
self.assertIsNone(f.close())

0 commit comments

Comments
 (0)