Skip to content

Commit d1e457a

Browse files
committed
Add 2 new functions: set_cell_default and set_cell_float
- Update unit tests - FIx build break on Windows caused by missing header file included - Fix missing function parameter checking for set_cell_bool function - Remove CodeQL pipeline config
1 parent 5a2155b commit d1e457a

File tree

5 files changed

+156
-37
lines changed

5 files changed

+156
-37
lines changed

.github/workflows/codeql.yml

Lines changed: 0 additions & 35 deletions
This file was deleted.

excelize.py

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4438,8 +4438,12 @@ def set_cell_bool(self, sheet: str, cell: str, value: bool) -> None:
44384438
RuntimeError with the message.
44394439
"""
44404440
prepare_args(
4441-
[sheet, cell],
4442-
[argsRule("sheet", [str]), argsRule("cell", [str])],
4441+
[sheet, cell, value],
4442+
[
4443+
argsRule("sheet", [str]),
4444+
argsRule("cell", [str]),
4445+
argsRule("value", [bool]),
4446+
],
44434447
)
44444448
err, lib.SetCellBool.restype = None, c_char_p
44454449
err = lib.SetCellBool(
@@ -4448,6 +4452,96 @@ def set_cell_bool(self, sheet: str, cell: str, value: bool) -> None:
44484452
if err != "":
44494453
raise RuntimeError(err)
44504454

4455+
def set_cell_default(self, sheet: str, cell: str, value: str) -> None:
4456+
"""
4457+
Set string type value of a cell as default format without escaping the
4458+
cell.
4459+
4460+
Args:
4461+
sheet (str): The worksheet name
4462+
cell (str): The cell reference
4463+
value (str): The cell value
4464+
4465+
Returns:
4466+
None: Return None if no error occurred, otherwise raise a
4467+
RuntimeError with the message.
4468+
"""
4469+
prepare_args(
4470+
[sheet, cell, value],
4471+
[
4472+
argsRule("sheet", [str]),
4473+
argsRule("cell", [str]),
4474+
argsRule("value", [str]),
4475+
],
4476+
)
4477+
err, lib.SetCellDefault.restype = None, c_char_p
4478+
err = lib.SetCellDefault(
4479+
self.file_index,
4480+
sheet.encode(ENCODE),
4481+
cell.encode(ENCODE),
4482+
value.encode(ENCODE),
4483+
).decode(ENCODE)
4484+
if err != "":
4485+
raise RuntimeError(err)
4486+
4487+
def set_cell_float(
4488+
self,
4489+
sheet: str,
4490+
cell: str,
4491+
value: Union[float, int],
4492+
precision: int,
4493+
bit_size: int,
4494+
) -> None:
4495+
"""
4496+
Sets a floating point value into a cell. The precision parameter
4497+
specifies how many places after the decimal will be shown while -1 is a
4498+
special value that will use as many decimal places as necessary to
4499+
represent the number. bitSize is 32 or 64 depending on if a `float32` or
4500+
`float64` was originally used for the value.
4501+
4502+
Args:
4503+
sheet (str): The worksheet name
4504+
cell (str): The cell reference
4505+
value (float): The cell value
4506+
precision (int): The cell value precision
4507+
bit_size (int): The cell value bit size
4508+
4509+
Returns:
4510+
None: Return None if no error occurred, otherwise raise a
4511+
RuntimeError with the message.
4512+
4513+
Example:
4514+
For example set column width for column A to H on Sheet1:
4515+
4516+
```python
4517+
try:
4518+
f.set_cell_float("Sheet1", "A1", 1.325, 2, 32)
4519+
except (RuntimeError, TypeError) as err:
4520+
print(err)
4521+
```
4522+
"""
4523+
prepare_args(
4524+
[sheet, cell, value, precision, bit_size],
4525+
[
4526+
argsRule("sheet", [str]),
4527+
argsRule("cell", [str]),
4528+
argsRule("value", [int, float]),
4529+
argsRule("precision", [int]),
4530+
argsRule("bit_size", [int]),
4531+
],
4532+
)
4533+
err, lib.SetCellFloat.restype = None, c_char_p
4534+
err = lib.SetCellFloat(
4535+
self.file_index,
4536+
sheet.encode(ENCODE),
4537+
cell.encode(ENCODE),
4538+
c_double(value),
4539+
c_longlong(precision),
4540+
c_longlong(bit_size),
4541+
).decode(ENCODE)
4542+
if err != "":
4543+
raise RuntimeError(err)
4544+
44514545
def set_cell_formula(
44524546
self, sheet: str, cell: str, formula: str, *opts: FormulaOpts
44534547
) -> None:

main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,6 +2383,39 @@ func SetCellBool(idx int, sheet, cell *C.char, value bool) *C.char {
23832383
return C.CString(emptyString)
23842384
}
23852385

2386+
// SetCellDefault provides a function to set string type value of a cell as
2387+
// default format without escaping the cell.
2388+
//
2389+
//export SetCellDefault
2390+
func SetCellDefault(idx int, sheet, cell, value *C.char) *C.char {
2391+
f, ok := files.Load(idx)
2392+
if !ok {
2393+
return C.CString(errFilePtr)
2394+
}
2395+
if err := f.(*excelize.File).SetCellDefault(C.GoString(sheet), C.GoString(cell), C.GoString(value)); err != nil {
2396+
return C.CString(err.Error())
2397+
}
2398+
return C.CString(emptyString)
2399+
}
2400+
2401+
// SetCellFloat sets a floating point value into a cell. The precision
2402+
// parameter specifies how many places after the decimal will be shown
2403+
// while -1 is a special value that will use as many decimal places as
2404+
// necessary to represent the number. bitSize is 32 or 64 depending on if a
2405+
// float32 or float64 was originally used for the value.
2406+
//
2407+
//export SetCellFloat
2408+
func SetCellFloat(idx int, sheet, cell *C.char, value float64, precision, bitSize int) *C.char {
2409+
f, ok := files.Load(idx)
2410+
if !ok {
2411+
return C.CString(errFilePtr)
2412+
}
2413+
if err := f.(*excelize.File).SetCellFloat(C.GoString(sheet), C.GoString(cell), value, precision, bitSize); err != nil {
2414+
return C.CString(err.Error())
2415+
}
2416+
return C.CString(emptyString)
2417+
}
2418+
23862419
// SetCellFormula provides a function to set formula on the cell is taken
23872420
// according to the given worksheet name and cell formula settings. The result
23882421
// of the formula cell can be calculated when the worksheet is opened by the

test_excelize.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,28 @@ def test_style(self):
481481
f.set_cell_value("SheetN", "A9", None)
482482
self.assertEqual(str(context.exception), "sheet SheetN does not exist")
483483

484+
self.assertIsNone(f.set_cell_default("Sheet1", "A13", "default"))
485+
with self.assertRaises(TypeError) as context:
486+
f.set_cell_default("Sheet1", "A13", 1)
487+
self.assertEqual(
488+
str(context.exception),
489+
"expected type str for argument 'value', but got int",
490+
)
491+
with self.assertRaises(RuntimeError) as context:
492+
f.set_cell_default("SheetN", "A13", "default")
493+
self.assertEqual(str(context.exception), "sheet SheetN does not exist")
494+
495+
self.assertIsNone(f.set_cell_float("Sheet1", "A14", 1.325, 2, 32))
496+
with self.assertRaises(TypeError) as context:
497+
f.set_cell_float("Sheet1", 1, 1.325, 2, 32)
498+
self.assertEqual(
499+
str(context.exception),
500+
"expected type str for argument 'cell', but got int",
501+
)
502+
with self.assertRaises(RuntimeError) as context:
503+
f.set_cell_float("SheetN", "A14", 1.325, 2, 32)
504+
self.assertEqual(str(context.exception), "sheet SheetN does not exist")
505+
484506
val = f.get_cell_value("Sheet1", "A2")
485507
self.assertEqual("", val)
486508

@@ -762,6 +784,8 @@ def test_style(self):
762784
["FALSE"],
763785
["100"],
764786
["Hello"],
787+
["default"],
788+
["1.33"],
765789
],
766790
)
767791
rows = f.get_rows("Sheet1", excelize.Options(raw_cell_value=True))
@@ -780,6 +804,8 @@ def test_style(self):
780804
["0"],
781805
["100"],
782806
["Hello"],
807+
["default"],
808+
["1.33"],
783809
],
784810
)
785811
with self.assertRaises(RuntimeError) as context:

types_c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// or later.
1212

1313
#include <stdbool.h>
14+
#include <stdint.h>
1415
#include <stdlib.h>
1516
#include <time.h>
1617

0 commit comments

Comments
 (0)