Fixes for being able to compile with playground (#380)

This commit is contained in:
Vyacheslav Zgordan 2020-04-13 15:04:57 +03:00 committed by GitHub
parent 72ad869a28
commit 0e183a6e7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 8 deletions

View File

@ -26,10 +26,14 @@ func main() {
fmt.Println("Non-existent", cp.GetPropertyByName("nonexistentproperty")) fmt.Println("Non-existent", cp.GetPropertyByName("nonexistentproperty"))
// And change them as well // 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.SetPropertyAsLpwstr("Another text property", "My text value") // text
cp.SetPropertyAsI4("Another integer number property", 42) // int23 cp.SetPropertyAsI4("Another integer number property", 42) // int32
cp.SetPropertyAsR8("Another float number property", 3.14) // float64 cp.SetPropertyAsR8("Another float number property", 3.14) // float64
cp.SetPropertyAsDate("Another date property", time.Now()) // date cp.SetPropertyAsDate("Another date property", time.Now()) // date
doc.SaveToFile("document_customized.docx") doc.SaveToFile("document_customized.docx")
@ -38,9 +42,9 @@ func main() {
cpNew := docNew.GetOrCreateCustomProperties() cpNew := docNew.GetOrCreateCustomProperties()
cpNew.SetPropertyAsLpwstr("Another text property", "My text value") // text cpNew.SetPropertyAsLpwstr("Another text property", "My text value") // text
cpNew.SetPropertyAsI4("Another integer number property", 42) // int23 cpNew.SetPropertyAsI4("Another integer number property", 42) // int23
cpNew.SetPropertyAsR8("Another float number property", 3.14) // float64 cpNew.SetPropertyAsR8("Another float number property", 3.14) // float64
cpNew.SetPropertyAsDate("Another date property", time.Now()) // date cpNew.SetPropertyAsDate("Another date property", time.Now()) // date
docNew.SaveToFile("document_new.docx") docNew.SaveToFile("document_new.docx")
} }

View File

@ -37,10 +37,18 @@ func (c CustomProperties) PropertiesList() []*custom_properties.CT_Property {
} }
func (c CustomProperties) GetPropertyByName(name string) CustomProperty { 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 propsList := c.x.Property
for _, property := range propsList { for _, property := range propsList {
if *property.NameAttr == name { if *property.NameAttr == name {
return CustomProperty(property) return property
} }
} }
return nil 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) { func (c CustomProperties) setProperty(newProperty *custom_properties.CT_Property) {
existingProperty := c.GetPropertyByName(*newProperty.NameAttr) existingProperty := c.getPropertyByName(*newProperty.NameAttr)
if existingProperty == nil { if existingProperty == nil {
c.x.Property = append(c.x.Property, newProperty) c.x.Property = append(c.x.Property, newProperty)
} else { } else {