From 0e183a6e7bf65e65023915612f7317a115e8668d Mon Sep 17 00:00:00 2001 From: Vyacheslav Zgordan Date: Mon, 13 Apr 2020 15:04:57 +0300 Subject: [PATCH] Fixes for being able to compile with playground (#380) --- _examples/document/doc-custom-properties/main.go | 16 ++++++++++------ common/customproperties.go | 12 ++++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/_examples/document/doc-custom-properties/main.go b/_examples/document/doc-custom-properties/main.go index 10118dda..975dcd56 100644 --- a/_examples/document/doc-custom-properties/main.go +++ b/_examples/document/doc-custom-properties/main.go @@ -26,10 +26,14 @@ func main() { fmt.Println("Non-existent", cp.GetPropertyByName("nonexistentproperty")) // And change them as well + cp.SetPropertyAsLpwstr("Company", "Another company") // text, existing property + fmt.Println("Company", *cp.GetPropertyByName("Company").Lpwstr) + + // Adding new properties cp.SetPropertyAsLpwstr("Another text property", "My text value") // text - cp.SetPropertyAsI4("Another integer number property", 42) // int23 - cp.SetPropertyAsR8("Another float number property", 3.14) // float64 - cp.SetPropertyAsDate("Another date property", time.Now()) // date + cp.SetPropertyAsI4("Another integer number property", 42) // int32 + cp.SetPropertyAsR8("Another float number property", 3.14) // float64 + cp.SetPropertyAsDate("Another date property", time.Now()) // date doc.SaveToFile("document_customized.docx") @@ -38,9 +42,9 @@ func main() { cpNew := docNew.GetOrCreateCustomProperties() cpNew.SetPropertyAsLpwstr("Another text property", "My text value") // text - cpNew.SetPropertyAsI4("Another integer number property", 42) // int23 - cpNew.SetPropertyAsR8("Another float number property", 3.14) // float64 - cpNew.SetPropertyAsDate("Another date property", time.Now()) // date + cpNew.SetPropertyAsI4("Another integer number property", 42) // int23 + cpNew.SetPropertyAsR8("Another float number property", 3.14) // float64 + cpNew.SetPropertyAsDate("Another date property", time.Now()) // date docNew.SaveToFile("document_new.docx") } diff --git a/common/customproperties.go b/common/customproperties.go index e03f2ab3..1b34e2ae 100644 --- a/common/customproperties.go +++ b/common/customproperties.go @@ -37,10 +37,18 @@ func (c CustomProperties) PropertiesList() []*custom_properties.CT_Property { } func (c CustomProperties) GetPropertyByName(name string) CustomProperty { + property := c.getPropertyByName(name) + if property == nil { + return nil + } + return CustomProperty(property) +} + +func (c CustomProperties) getPropertyByName(name string) *custom_properties.CT_Property { propsList := c.x.Property for _, property := range propsList { if *property.NameAttr == name { - return CustomProperty(property) + return property } } return nil @@ -61,7 +69,7 @@ func (c CustomProperties) getNewProperty(name string) *custom_properties.CT_Prop } func (c CustomProperties) setProperty(newProperty *custom_properties.CT_Property) { - existingProperty := c.GetPropertyByName(*newProperty.NameAttr) + existingProperty := c.getPropertyByName(*newProperty.NameAttr) if existingProperty == nil { c.x.Property = append(c.x.Property, newProperty) } else {