Skip to content

Commit bb02bb2

Browse files
YafeiXie1yafei-xie
andauthored
Add new function: set_app_props (#30)
Co-authored-by: oliver <yx1569@york.ac.uk>
1 parent e62db79 commit bb02bb2

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

excelize.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4040,6 +4040,78 @@ def set_active_sheet(self, index: int) -> None:
40404040
if err != "":
40414041
raise RuntimeError(err)
40424042

4043+
def set_app_props(self, app_properties: AppProperties) -> None:
4044+
"""
4045+
Set document application properties. The properties that can be set are:
4046+
4047+
application: The name of the application that created this document.
4048+
4049+
scale_crop: Indicates the display mode of the document thumbnail. Set
4050+
this element to `true` to enable scaling of the document thumbnail to
4051+
the display. Set this element to `false` to enable cropping of the
4052+
document thumbnail to show only sections that will fit the display.
4053+
4054+
doc_security: Security level of a document as a numeric value. Document
4055+
security is defined as:
4056+
1 - Document is password protected.
4057+
2 - Document is recommended to be opened as read-only.
4058+
3 - Document is enforced to be opened as read-only.
4059+
4 - Document is locked for annotation.
4060+
4061+
company: The name of a company associated with the document.
4062+
4063+
links_up_to_date: Indicates whether hyperlinks in a document are
4064+
up-to-date. Set this element to `true` to indicate that hyperlinks are
4065+
updated. Set this element to `false` to indicate that hyperlinks are
4066+
outdated.
4067+
4068+
hyperlinks_changed: Specifies that one or more hyperlinks in this part
4069+
were updated exclusively in this part by a producer. The next producer
4070+
to open this document shall update the hyperlink relationships with the
4071+
new hyperlinks specified in this part.
4072+
4073+
app_version: Specifies the version of the application which produced
4074+
this document. The content of this element shall be of the form XX.YYYY
4075+
where X and Y represent numerical values, or the document shall be
4076+
considered non-conformant.
4077+
4078+
Args:
4079+
app_properties (AppProperties): The application properties
4080+
4081+
Returns:
4082+
None: Return None if no error occurred, otherwise raise a
4083+
RuntimeError with the message.
4084+
4085+
Example:
4086+
For example:
4087+
4088+
```python
4089+
try:
4090+
f.set_app_props(
4091+
excelize.AppProperties(
4092+
application: "Microsoft Excel",
4093+
scale_crop: true,
4094+
doc_security: 3,
4095+
company: "Company Name",
4096+
links_up_to_date: true,
4097+
hyperlinks_changed: true,
4098+
app_version: "16.0000",
4099+
)
4100+
)
4101+
except (RuntimeError, TypeError) as err:
4102+
print(err)
4103+
```
4104+
"""
4105+
prepare_args(
4106+
[app_properties],
4107+
[argsRule("app_properties", [AppProperties])],
4108+
)
4109+
lib.SetAppProps.restype = c_char_p
4110+
options = py_value_to_c(app_properties, types_go._AppProperties())
4111+
err = lib.SetAppProps(self.file_index, byref(options)).decode(ENCODE)
4112+
if err != "":
4113+
raise RuntimeError(err)
4114+
40434115
def set_cell_bool(self, sheet: str, cell: str, value: bool) -> None:
40444116
"""
40454117
Set bool type value of a cell by given worksheet name, cell reference

main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,25 @@ func SetActiveSheet(idx, index int) *C.char {
21122112
return C.CString(emptyString)
21132113
}
21142114

2115+
// SetAppProps provides a function to set document application properties.
2116+
//
2117+
//export SetAppProps
2118+
func SetAppProps(idx int, opts *C.struct_AppProperties) *C.char {
2119+
f, ok := files.Load(idx)
2120+
if !ok {
2121+
return C.CString(errFilePtr)
2122+
}
2123+
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.AppProperties{}))
2124+
if err != nil {
2125+
return C.CString(err.Error())
2126+
}
2127+
appProps := goVal.Elem().Interface().(excelize.AppProperties)
2128+
if err := f.(*excelize.File).SetAppProps(&appProps); err != nil {
2129+
return C.CString(err.Error())
2130+
}
2131+
return C.CString(emptyString)
2132+
}
2133+
21152134
// SetCellBool provides a function to set bool type value of a cell by given
21162135
// worksheet name, cell reference and cell value.
21172136
//

test_excelize.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,15 @@ def test_open_file(self):
8383

8484
def test_app_props(self):
8585
f = excelize.new_file()
86-
props = f.get_app_props()
87-
self.assertEqual(props.application, "Go Excelize")
86+
expected = excelize.AppProperties(
87+
application="excelize-py",
88+
company="Excelize",
89+
app_version="1.0.0",
90+
)
91+
self.assertIsNone(f.set_app_props(expected))
92+
self.assertEqual(f.get_app_props(), expected)
93+
self.assertIsNone(f.save_as(os.path.join("test", "TestAppProps.xlsx")))
94+
self.assertIsNone(f.close())
8895

8996
def test_default_font(self):
9097
f = excelize.new_file()
@@ -888,6 +895,15 @@ def test_none_file_pointer(self):
888895
str(context.exception),
889896
"expected type int for argument 'index', but got str",
890897
)
898+
with self.assertRaises(RuntimeError) as context:
899+
f.set_app_props(excelize.AppProperties())
900+
self.assertEqual(str(context.exception), expected)
901+
with self.assertRaises(TypeError) as context:
902+
f.set_app_props(0)
903+
self.assertEqual(
904+
str(context.exception),
905+
"expected type AppProperties for argument 'app_properties', but got int",
906+
)
891907
with self.assertRaises(RuntimeError) as context:
892908
f.set_default_font("")
893909
self.assertEqual(str(context.exception), expected)

0 commit comments

Comments
 (0)