Skip to content

Commit 09b56ec

Browse files
authored
Add new function: unset_conditional_format (#28)
- Update unit tests
1 parent c1c7535 commit 09b56ec

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

excelize.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5668,6 +5668,30 @@ def unprotect_workbook(self, *password: str) -> None:
56685668
if err != "":
56695669
raise RuntimeError(err)
56705670

5671+
def unset_conditional_format(self, sheet: str, range_ref: str) -> None:
5672+
"""
5673+
Unset the conditional format by given worksheet name and range
5674+
reference.
5675+
5676+
Args:
5677+
sheet (str): The worksheet name
5678+
range_ref (str): The top-left and right-bottom cell range reference
5679+
5680+
Returns:
5681+
None: Return None if no error occurred, otherwise raise a
5682+
RuntimeError with the message.
5683+
"""
5684+
prepare_args(
5685+
[sheet, range_ref],
5686+
[argsRule("sheet", [str]), argsRule("range_ref", [str])],
5687+
)
5688+
lib.UnsetConditionalFormat.restype = c_char_p
5689+
err = lib.UnsetConditionalFormat(
5690+
self.file_index, sheet.encode(ENCODE), range_ref.encode(ENCODE)
5691+
).decode(ENCODE)
5692+
if err != "":
5693+
raise RuntimeError(err)
5694+
56715695
def update_linked_value(self) -> None:
56725696
"""
56735697
Fix linked values within a spreadsheet are not updating in Office Excel

main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,21 @@ func UnprotectWorkbook(idx int, password *C.char, verify bool) *C.char {
28072807
return C.CString(emptyString)
28082808
}
28092809

2810+
// UnsetConditionalFormat provides a function to unset the conditional format
2811+
// by given worksheet name and range reference.
2812+
//
2813+
//export UnsetConditionalFormat
2814+
func UnsetConditionalFormat(idx int, sheet, rangeRef *C.char) *C.char {
2815+
f, ok := files.Load(idx)
2816+
if !ok {
2817+
return C.CString(errFilePtr)
2818+
}
2819+
if err := f.(*excelize.File).UnsetConditionalFormat(C.GoString(sheet), C.GoString(rangeRef)); err != nil {
2820+
return C.CString(err.Error())
2821+
}
2822+
return C.CString(emptyString)
2823+
}
2824+
28102825
// UpdateLinkedValue fix linked values within a spreadsheet are not updating in
28112826
// Office Excel application. This function will be remove value tag when met a
28122827
// cell have a linked value.

test_excelize.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,17 @@ def test_conditional_format(self):
19801980
str(context.exception),
19811981
"expected type list for argument 'opts', but got int",
19821982
)
1983+
1984+
self.assertIsNone(f.unset_conditional_format("Sheet1", "A1:A10"))
1985+
with self.assertRaises(RuntimeError) as context:
1986+
f.unset_conditional_format("SheetN", "A1:A10")
1987+
self.assertEqual(str(context.exception), "sheet SheetN does not exist")
1988+
with self.assertRaises(TypeError) as context:
1989+
f.unset_conditional_format("Sheet1", 1)
1990+
self.assertEqual(
1991+
str(context.exception),
1992+
"expected type str for argument 'range_ref', but got int",
1993+
)
19831994
self.assertIsNone(f.save_as(os.path.join("test", "TestConditionalFormat.xlsx")))
19841995
self.assertIsNone(f.close())
19851996

0 commit comments

Comments
 (0)