Skip to content

Commit 51766c0

Browse files
committed
Add new functions: get_calc_props and set_calc_props
- Update unit tests - Automating dependabot tasks with GitHub Actions
1 parent b338298 commit 51766c0

File tree

8 files changed

+180
-3
lines changed

8 files changed

+180
-3
lines changed

.github/dependabot.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 2
2+
updates:
3+
4+
- package-ecosystem: "github-actions"
5+
directory: "/"
6+
schedule:
7+
interval: "weekly"

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
language: ['go', 'python']
19+
language: ['c-cpp', 'go', 'python']
2020

2121
steps:
2222
- name: Checkout repository

excelize.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,6 +2735,21 @@ def get_app_props(self) -> Optional[AppProperties]:
27352735
return c_value_to_py(res.opts, AppProperties())
27362736
raise RuntimeError(err)
27372737

2738+
def get_calc_props(self) -> Optional[CalcPropsOptions]:
2739+
"""
2740+
Gets calculation properties.
2741+
2742+
Returns:
2743+
Optional[CalcPropsOptions]: Return the calculation properties if no
2744+
error occurred, otherwise raise a RuntimeError with the message.
2745+
"""
2746+
lib.GetCalcProps.restype = types_go._GetCalcPropsResult
2747+
res = lib.GetCalcProps(self.file_index)
2748+
err = res.err.decode(ENCODE)
2749+
if not err:
2750+
return c_value_to_py(res.opts, CalcPropsOptions())
2751+
raise RuntimeError(err)
2752+
27382753
def get_cell_formula(self, sheet: str, cell: str) -> str:
27392754
"""
27402755
Get formula from cell by given worksheet name and cell reference in
@@ -4334,6 +4349,26 @@ def set_app_props(self, app_properties: AppProperties) -> None:
43344349
if err != "":
43354350
raise RuntimeError(err)
43364351

4352+
def set_calc_props(self, opts: CalcPropsOptions) -> None:
4353+
"""
4354+
Sets calculation properties. Optional value of `calc_mode` property is:
4355+
`manual`, `auto` or `autoNoTable`. Optional value of "ref_mode" property
4356+
is: `A1` or `R1C1`.
4357+
4358+
Args:
4359+
opts (CalcPropsOptions): The calculation properties
4360+
4361+
Returns:
4362+
None: Return None if no error occurred, otherwise raise a
4363+
RuntimeError with the message.
4364+
"""
4365+
prepare_args([opts], [argsRule("opts", [CalcPropsOptions])])
4366+
lib.SetCalcProps.restype = c_char_p
4367+
options = py_value_to_c(opts, types_go._CalcPropsOptions())
4368+
err = lib.SetCalcProps(self.file_index, byref(options)).decode(ENCODE)
4369+
if err != "":
4370+
raise RuntimeError(err)
4371+
43374372
def set_cell_bool(self, sheet: str, cell: str, value: bool) -> None:
43384373
"""
43394374
Set bool type value of a cell by given worksheet name, cell reference

main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,25 @@ func GetAppProps(idx int) C.struct_GetAppPropsResult {
10321032
return C.struct_GetAppPropsResult{opts: cVal.Elem().Interface().(C.struct_AppProperties), err: C.CString(emptyString)}
10331033
}
10341034

1035+
// GetCalcProps provides a function to gets calculation properties.
1036+
//
1037+
//export GetCalcProps
1038+
func GetCalcProps(idx int) C.struct_GetCalcPropsResult {
1039+
f, ok := files.Load(idx)
1040+
if !ok {
1041+
return C.struct_GetCalcPropsResult{err: C.CString(errFilePtr)}
1042+
}
1043+
opts, err := f.(*excelize.File).GetCalcProps()
1044+
if err != nil {
1045+
return C.struct_GetCalcPropsResult{err: C.CString(err.Error())}
1046+
}
1047+
cVal, err := goValueToC(reflect.ValueOf(opts), reflect.ValueOf(&C.struct_CalcPropsOptions{}))
1048+
if err != nil {
1049+
return C.struct_GetCalcPropsResult{err: C.CString(err.Error())}
1050+
}
1051+
return C.struct_GetCalcPropsResult{opts: cVal.Elem().Interface().(C.struct_CalcPropsOptions), err: C.CString(emptyString)}
1052+
}
1053+
10351054
// GetCellFormula provides a function to get formula from cell by given
10361055
// worksheet name and cell reference in spreadsheet.
10371056
//
@@ -2285,6 +2304,27 @@ func SetAppProps(idx int, opts *C.struct_AppProperties) *C.char {
22852304
return C.CString(emptyString)
22862305
}
22872306

2307+
// SetCalcProps provides a function to sets calculation properties. Optional
2308+
// value of "CalcMode" property is: "manual", "auto" or "autoNoTable". Optional
2309+
// value of "RefMode" property is: "A1" or "R1C1".
2310+
//
2311+
//export SetCalcProps
2312+
func SetCalcProps(idx int, opts *C.struct_CalcPropsOptions) *C.char {
2313+
f, ok := files.Load(idx)
2314+
if !ok {
2315+
return C.CString(errFilePtr)
2316+
}
2317+
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.CalcPropsOptions{}))
2318+
if err != nil {
2319+
return C.CString(err.Error())
2320+
}
2321+
props := goVal.Elem().Interface().(excelize.CalcPropsOptions)
2322+
if err := f.(*excelize.File).SetCalcProps(&props); err != nil {
2323+
return C.CString(err.Error())
2324+
}
2325+
return C.CString(emptyString)
2326+
}
2327+
22882328
// SetCellBool provides a function to set bool type value of a cell by given
22892329
// worksheet name, cell reference and cell value.
22902330
//

test_excelize.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ def test_python_compiler_amd64(
8080
def test_c_value_to_py(self):
8181
self.assertIsNone(excelize.c_value_to_py(None, None))
8282

83+
def test_calc_props(self):
84+
f = excelize.new_file()
85+
opts = excelize.CalcPropsOptions(
86+
full_calc_on_load=True,
87+
calc_id=122211,
88+
concurrent_manual_count=5,
89+
iterate_count=10,
90+
concurrent_calc=True,
91+
)
92+
self.assertIsNone(f.set_calc_props(opts))
93+
with self.assertRaises(TypeError) as context:
94+
f.set_calc_props(0)
95+
self.assertEqual(
96+
str(context.exception),
97+
"expected type CalcPropsOptions for argument 'opts', but got int",
98+
)
99+
with self.assertRaises(RuntimeError) as context:
100+
f.set_calc_props(excelize.CalcPropsOptions(ref_mode="a1"))
101+
self.assertEqual(
102+
str(context.exception),
103+
'invalid RefMode value "a1", acceptable value should be one of A1, R1C1',
104+
)
105+
self.assertEqual(opts, f.get_calc_props())
106+
self.assertIsNone(f.close())
107+
83108
def test_py_value_to_c(self):
84109
self.assertIsNone(excelize.py_value_to_c(None, None))
85110

@@ -901,6 +926,9 @@ def test_none_file_pointer(self):
901926
with self.assertRaises(RuntimeError) as context:
902927
f.get_defined_name()
903928
self.assertEqual(str(context.exception), expected)
929+
with self.assertRaises(RuntimeError) as context:
930+
f.get_calc_props()
931+
self.assertEqual(str(context.exception), expected)
904932
with self.assertRaises(RuntimeError) as context:
905933
f.get_sheet_name(0)
906934
self.assertEqual(str(context.exception), expected)
@@ -2230,7 +2258,7 @@ def test_doc_props(self):
22302258
def test_join_cell_name(self):
22312259
self.assertEqual(excelize.join_cell_name("A", 1), "A1")
22322260
with self.assertRaises(RuntimeError) as context:
2233-
excelize.join_cell_name("", 0)
2261+
excelize.join_cell_name("", 0)
22342262
self.assertEqual(str(context.exception), 'invalid column name ""')
22352263
with self.assertRaises(TypeError) as context:
22362264
excelize.join_cell_name(1, 1)

types_c.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ struct AppProperties
8686
char *AppVersion;
8787
};
8888

89+
// CalcPropsOptions defines the collection of properties the application uses to
90+
// record calculation status and details.
91+
struct CalcPropsOptions
92+
{
93+
unsigned int *CalcID;
94+
char **CalcMode;
95+
bool *FullCalcOnLoad;
96+
char **RefMode;
97+
bool *Iterate;
98+
unsigned int *IterateCount;
99+
double *IterateDelta;
100+
bool *FullPrecision;
101+
bool *CalcCompleted;
102+
bool *CalcOnSave;
103+
bool *ConcurrentCalc;
104+
unsigned int *ConcurrentManualCount;
105+
bool *ForceFullCalc;
106+
};
107+
89108
// Cell can be used directly in StreamWriter.SetRow to specify a style and
90109
// a value.
91110
struct Cell
@@ -477,7 +496,8 @@ struct ChartMarker
477496

478497
// ChartDataPoint directly maps the format settings of the chart data point for
479498
// doughnut, pie and 3D pie charts.
480-
struct ChartDataPoint {
499+
struct ChartDataPoint
500+
{
481501
int Index;
482502
struct Fill Fill;
483503
};
@@ -786,6 +806,12 @@ struct IntStringResult
786806
char *V;
787807
};
788808

809+
struct GetCalcPropsResult
810+
{
811+
struct CalcPropsOptions opts;
812+
char *err;
813+
};
814+
789815
struct GetCellHyperLinkResult
790816
{
791817
bool link;

types_go.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ class _AppProperties(Structure):
6161
]
6262

6363

64+
class _CalcPropsOptions(Structure):
65+
_fields_ = [
66+
("CalcID", POINTER(c_uint)),
67+
("CalcMode", POINTER(c_char_p)),
68+
("FullCalcOnLoad", POINTER(c_bool)),
69+
("RefMode", POINTER(c_char_p)),
70+
("Iterate", POINTER(c_bool)),
71+
("IterateCount", POINTER(c_uint)),
72+
("IterateDelta", POINTER(c_double)),
73+
("FullPrecision", POINTER(c_bool)),
74+
("CalcCompleted", POINTER(c_bool)),
75+
("CalcOnSave", POINTER(c_bool)),
76+
("ConcurrentCalc", POINTER(c_bool)),
77+
("ConcurrentManualCount", POINTER(c_uint)),
78+
("ForceFullCalc", POINTER(c_bool)),
79+
]
80+
81+
6482
class _Cell(Structure):
6583
_fields_ = [
6684
("StyleID", c_int),
@@ -777,6 +795,12 @@ class _GetAppPropsResult(Structure):
777795
("err", c_char_p),
778796
]
779797

798+
class _GetCalcPropsResult(Structure):
799+
_fields_ = [
800+
("opts", _CalcPropsOptions),
801+
("err", c_char_p),
802+
]
803+
780804

781805
class _GetCellHyperLinkResult(Structure):
782806
_fields_ = [

types_py.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,23 @@ class AppProperties:
242242
app_version: str = ""
243243

244244

245+
@dataclass
246+
class CalcPropsOptions:
247+
calc_id: Optional[int] = None
248+
calc_mode: Optional[str] = None
249+
full_calc_on_load: Optional[bool] = None
250+
ref_mode: Optional[str] = None
251+
iterate: Optional[bool] = None
252+
iterate_count: Optional[int] = None
253+
iterate_delta: Optional[float] = None
254+
full_precision: Optional[bool] = None
255+
calc_completed: Optional[bool] = None
256+
calc_on_save: Optional[bool] = None
257+
concurrent_calc: Optional[bool] = None
258+
concurrent_manual_count: Optional[int] = None
259+
force_full_calc: Optional[bool] = None
260+
261+
245262
@dataclass
246263
class Cell:
247264
style_id: int = 0

0 commit comments

Comments
 (0)