2022-01-09 00:20:42 +08:00
|
|
|
// Copyright 2016 - 2022 The excelize Authors. All rights reserved. Use of
|
2019-06-04 23:30:55 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
2022-02-17 00:09:11 +08:00
|
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
|
|
|
|
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
|
|
|
|
// Supports complex components by high compatibility, and provided streaming
|
|
|
|
// API for generating or reading data from a worksheet with huge amounts of
|
|
|
|
// data. This library needs Go version 1.15 or later.
|
2019-06-04 23:30:55 +08:00
|
|
|
|
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
2019-12-20 00:30:48 +08:00
|
|
|
"bytes"
|
2019-06-04 23:30:55 +08:00
|
|
|
"encoding/xml"
|
2019-12-20 00:30:48 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-06-04 23:30:55 +08:00
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2021-12-26 14:55:53 +08:00
|
|
|
// SetAppProps provides a function to set document application properties. The
|
|
|
|
// properties that can be set are:
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// Property | Description
|
|
|
|
// -------------------+--------------------------------------------------------------------------
|
|
|
|
// Application | The name of the application that created this document.
|
|
|
|
// |
|
|
|
|
// ScaleCrop | Indicates the display mode of the document thumbnail. Set this element
|
|
|
|
// | to 'true' to enable scaling of the document thumbnail to the display. Set
|
|
|
|
// | this element to 'false' to enable cropping of the document thumbnail to
|
|
|
|
// | show only sections that will fit the display.
|
|
|
|
// |
|
|
|
|
// DocSecurity | Security level of a document as a numeric value. Document security is
|
|
|
|
// | defined as:
|
|
|
|
// | 1 - Document is password protected.
|
|
|
|
// | 2 - Document is recommended to be opened as read-only.
|
|
|
|
// | 3 - Document is enforced to be opened as read-only.
|
|
|
|
// | 4 - Document is locked for annotation.
|
|
|
|
// |
|
|
|
|
// Company | The name of a company associated with the document.
|
|
|
|
// |
|
|
|
|
// LinksUpToDate | Indicates whether hyperlinks in a document are up-to-date. Set this
|
|
|
|
// | element to 'true' to indicate that hyperlinks are updated. Set this
|
|
|
|
// | element to 'false' to indicate that hyperlinks are outdated.
|
|
|
|
// |
|
|
|
|
// HyperlinksChanged | Specifies that one or more hyperlinks in this part were updated
|
|
|
|
// | exclusively in this part by a producer. The next producer to open this
|
|
|
|
// | document shall update the hyperlink relationships with the new
|
|
|
|
// | hyperlinks specified in this part.
|
|
|
|
// |
|
|
|
|
// AppVersion | Specifies the version of the application which produced this document.
|
|
|
|
// | The content of this element shall be of the form XX.YYYY where X and Y
|
|
|
|
// | represent numerical values, or the document shall be considered
|
|
|
|
// | non-conformant.
|
2021-12-26 14:55:53 +08:00
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err := f.SetAppProps(&excelize.AppProperties{
|
|
|
|
// Application: "Microsoft Excel",
|
|
|
|
// ScaleCrop: true,
|
|
|
|
// DocSecurity: 3,
|
|
|
|
// Company: "Company Name",
|
|
|
|
// LinksUpToDate: true,
|
|
|
|
// HyperlinksChanged: true,
|
|
|
|
// AppVersion: "16.0000",
|
|
|
|
// })
|
2021-12-26 14:55:53 +08:00
|
|
|
func (f *File) SetAppProps(appProperties *AppProperties) (err error) {
|
|
|
|
var (
|
|
|
|
app *xlsxProperties
|
|
|
|
fields []string
|
|
|
|
output []byte
|
|
|
|
immutable, mutable reflect.Value
|
|
|
|
field string
|
|
|
|
)
|
|
|
|
app = new(xlsxProperties)
|
2022-01-09 00:20:42 +08:00
|
|
|
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathDocPropsApp)))).
|
2021-12-26 14:55:53 +08:00
|
|
|
Decode(app); err != nil && err != io.EOF {
|
|
|
|
err = fmt.Errorf("xml decode error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fields = []string{"Application", "ScaleCrop", "DocSecurity", "Company", "LinksUpToDate", "HyperlinksChanged", "AppVersion"}
|
|
|
|
immutable, mutable = reflect.ValueOf(*appProperties), reflect.ValueOf(app).Elem()
|
|
|
|
for _, field = range fields {
|
|
|
|
immutableField := immutable.FieldByName(field)
|
|
|
|
switch immutableField.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
mutable.FieldByName(field).SetBool(immutableField.Bool())
|
|
|
|
case reflect.Int:
|
|
|
|
mutable.FieldByName(field).SetInt(immutableField.Int())
|
|
|
|
default:
|
|
|
|
mutable.FieldByName(field).SetString(immutableField.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.Vt = NameSpaceDocumentPropertiesVariantTypes.Value
|
|
|
|
output, err = xml.Marshal(app)
|
2022-01-09 00:20:42 +08:00
|
|
|
f.saveFileList(defaultXMLPathDocPropsApp, output)
|
2021-12-26 14:55:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAppProps provides a function to get document application properties.
|
|
|
|
func (f *File) GetAppProps() (ret *AppProperties, err error) {
|
2022-01-23 00:32:34 +08:00
|
|
|
app := new(xlsxProperties)
|
2022-01-09 00:20:42 +08:00
|
|
|
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathDocPropsApp)))).
|
2021-12-26 14:55:53 +08:00
|
|
|
Decode(app); err != nil && err != io.EOF {
|
|
|
|
err = fmt.Errorf("xml decode error: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret, err = &AppProperties{
|
|
|
|
Application: app.Application,
|
|
|
|
ScaleCrop: app.ScaleCrop,
|
|
|
|
DocSecurity: app.DocSecurity,
|
|
|
|
Company: app.Company,
|
|
|
|
LinksUpToDate: app.LinksUpToDate,
|
|
|
|
HyperlinksChanged: app.HyperlinksChanged,
|
|
|
|
AppVersion: app.AppVersion,
|
|
|
|
}, nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-04 23:30:55 +08:00
|
|
|
// SetDocProps provides a function to set document core properties. The
|
|
|
|
// properties that can be set are:
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// Property | Description
|
|
|
|
// ----------------+-----------------------------------------------------------------------------
|
|
|
|
// Title | The name given to the resource.
|
|
|
|
// |
|
|
|
|
// Subject | The topic of the content of the resource.
|
|
|
|
// |
|
|
|
|
// Creator | An entity primarily responsible for making the content of the resource.
|
|
|
|
// |
|
|
|
|
// Keywords | A delimited set of keywords to support searching and indexing. This is
|
|
|
|
// | typically a list of terms that are not available elsewhere in the properties.
|
|
|
|
// |
|
|
|
|
// Description | An explanation of the content of the resource.
|
|
|
|
// |
|
|
|
|
// LastModifiedBy | The user who performed the last modification. The identification is
|
|
|
|
// | environment-specific.
|
|
|
|
// |
|
|
|
|
// Language | The language of the intellectual content of the resource.
|
|
|
|
// |
|
|
|
|
// Identifier | An unambiguous reference to the resource within a given context.
|
|
|
|
// |
|
|
|
|
// Revision | The topic of the content of the resource.
|
|
|
|
// |
|
|
|
|
// ContentStatus | The status of the content. For example: Values might include "Draft",
|
|
|
|
// | "Reviewed" and "Final"
|
|
|
|
// |
|
|
|
|
// Category | A categorization of the content of this package.
|
|
|
|
// |
|
|
|
|
// Version | The version number. This value is set by the user or by the application.
|
2019-06-04 23:30:55 +08:00
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
//
|
2022-08-13 11:21:59 +08:00
|
|
|
// err := f.SetDocProps(&excelize.DocProperties{
|
|
|
|
// Category: "category",
|
|
|
|
// ContentStatus: "Draft",
|
|
|
|
// Created: "2019-06-04T22:00:10Z",
|
|
|
|
// Creator: "Go Excelize",
|
|
|
|
// Description: "This file created by Go Excelize",
|
|
|
|
// Identifier: "xlsx",
|
|
|
|
// Keywords: "Spreadsheet",
|
|
|
|
// LastModifiedBy: "Go Author",
|
|
|
|
// Modified: "2019-06-04T22:00:10Z",
|
|
|
|
// Revision: "0",
|
|
|
|
// Subject: "Test Subject",
|
|
|
|
// Title: "Test Title",
|
|
|
|
// Language: "en-US",
|
|
|
|
// Version: "1.0.0",
|
|
|
|
// })
|
2019-12-20 00:30:48 +08:00
|
|
|
func (f *File) SetDocProps(docProperties *DocProperties) (err error) {
|
|
|
|
var (
|
|
|
|
core *decodeCoreProperties
|
|
|
|
newProps *xlsxCoreProperties
|
|
|
|
fields []string
|
|
|
|
output []byte
|
|
|
|
immutable, mutable reflect.Value
|
|
|
|
field, val string
|
|
|
|
)
|
|
|
|
|
|
|
|
core = new(decodeCoreProperties)
|
2022-01-09 00:20:42 +08:00
|
|
|
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathDocPropsCore)))).
|
2019-12-20 00:30:48 +08:00
|
|
|
Decode(core); err != nil && err != io.EOF {
|
|
|
|
err = fmt.Errorf("xml decode error: %s", err)
|
|
|
|
return
|
2019-06-04 23:30:55 +08:00
|
|
|
}
|
2019-12-20 00:30:48 +08:00
|
|
|
newProps, err = &xlsxCoreProperties{
|
2019-06-04 23:30:55 +08:00
|
|
|
Dc: NameSpaceDublinCore,
|
|
|
|
Dcterms: NameSpaceDublinCoreTerms,
|
2022-01-09 00:20:42 +08:00
|
|
|
Dcmitype: NameSpaceDublinCoreMetadataInitiative,
|
2019-06-04 23:30:55 +08:00
|
|
|
XSI: NameSpaceXMLSchemaInstance,
|
|
|
|
Title: core.Title,
|
|
|
|
Subject: core.Subject,
|
|
|
|
Creator: core.Creator,
|
|
|
|
Keywords: core.Keywords,
|
|
|
|
Description: core.Description,
|
|
|
|
LastModifiedBy: core.LastModifiedBy,
|
|
|
|
Language: core.Language,
|
|
|
|
Identifier: core.Identifier,
|
|
|
|
Revision: core.Revision,
|
|
|
|
ContentStatus: core.ContentStatus,
|
|
|
|
Category: core.Category,
|
|
|
|
Version: core.Version,
|
2019-12-20 00:30:48 +08:00
|
|
|
}, nil
|
2022-07-14 00:17:51 +08:00
|
|
|
if core.Created != nil {
|
|
|
|
newProps.Created = &xlsxDcTerms{Type: core.Created.Type, Text: core.Created.Text}
|
|
|
|
}
|
|
|
|
if core.Modified != nil {
|
|
|
|
newProps.Modified = &xlsxDcTerms{Type: core.Modified.Type, Text: core.Modified.Text}
|
|
|
|
}
|
2019-12-20 00:30:48 +08:00
|
|
|
fields = []string{
|
|
|
|
"Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords",
|
|
|
|
"LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version",
|
2019-06-04 23:30:55 +08:00
|
|
|
}
|
2019-12-20 00:30:48 +08:00
|
|
|
immutable, mutable = reflect.ValueOf(*docProperties), reflect.ValueOf(newProps).Elem()
|
|
|
|
for _, field = range fields {
|
|
|
|
if val = immutable.FieldByName(field).String(); val != "" {
|
2019-06-04 23:30:55 +08:00
|
|
|
mutable.FieldByName(field).SetString(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if docProperties.Created != "" {
|
2022-07-14 00:17:51 +08:00
|
|
|
newProps.Created = &xlsxDcTerms{Type: "dcterms:W3CDTF", Text: docProperties.Created}
|
2019-06-04 23:30:55 +08:00
|
|
|
}
|
|
|
|
if docProperties.Modified != "" {
|
2022-07-14 00:17:51 +08:00
|
|
|
newProps.Modified = &xlsxDcTerms{Type: "dcterms:W3CDTF", Text: docProperties.Modified}
|
2019-06-04 23:30:55 +08:00
|
|
|
}
|
2019-12-20 00:30:48 +08:00
|
|
|
output, err = xml.Marshal(newProps)
|
2022-01-09 00:20:42 +08:00
|
|
|
f.saveFileList(defaultXMLPathDocPropsCore, output)
|
2019-12-20 00:30:48 +08:00
|
|
|
|
|
|
|
return
|
2019-06-04 23:30:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDocProps provides a function to get document core properties.
|
2019-12-20 00:30:48 +08:00
|
|
|
func (f *File) GetDocProps() (ret *DocProperties, err error) {
|
2022-01-23 00:32:34 +08:00
|
|
|
core := new(decodeCoreProperties)
|
2019-12-20 00:30:48 +08:00
|
|
|
|
2022-01-09 00:20:42 +08:00
|
|
|
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathDocPropsCore)))).
|
2019-12-20 00:30:48 +08:00
|
|
|
Decode(core); err != nil && err != io.EOF {
|
|
|
|
err = fmt.Errorf("xml decode error: %s", err)
|
|
|
|
return
|
2019-06-04 23:30:55 +08:00
|
|
|
}
|
2019-12-20 00:30:48 +08:00
|
|
|
ret, err = &DocProperties{
|
2019-06-04 23:30:55 +08:00
|
|
|
Category: core.Category,
|
|
|
|
ContentStatus: core.ContentStatus,
|
|
|
|
Creator: core.Creator,
|
|
|
|
Description: core.Description,
|
|
|
|
Identifier: core.Identifier,
|
|
|
|
Keywords: core.Keywords,
|
|
|
|
LastModifiedBy: core.LastModifiedBy,
|
|
|
|
Revision: core.Revision,
|
|
|
|
Subject: core.Subject,
|
|
|
|
Title: core.Title,
|
|
|
|
Language: core.Language,
|
|
|
|
Version: core.Version,
|
|
|
|
}, nil
|
2022-07-14 00:17:51 +08:00
|
|
|
if core.Created != nil {
|
|
|
|
ret.Created = core.Created.Text
|
|
|
|
}
|
|
|
|
if core.Modified != nil {
|
|
|
|
ret.Modified = core.Modified.Text
|
|
|
|
}
|
2019-12-20 00:30:48 +08:00
|
|
|
return
|
2019-06-04 23:30:55 +08:00
|
|
|
}
|