forked from p30928647/excelize
Resolve #415, init set and get doc properties support
This commit is contained in:
parent
d038ca2e9c
commit
db99373b25
|
@ -0,0 +1,140 @@
|
|||
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
|
||||
// this source code is governed by a BSD-style license that can be found in
|
||||
// the LICENSE file.
|
||||
//
|
||||
// Package excelize providing a set of functions that allow you to write to
|
||||
// and read from XLSX files. Support reads and writes XLSX file generated by
|
||||
// Microsoft Excel™ 2007 and later. Support save file without losing original
|
||||
// charts of XLSX. This library needs Go version 1.8 or later.
|
||||
|
||||
package excelize
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// SetDocProps provides a function to set document core properties. The
|
||||
// properties that can be set are:
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// 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",
|
||||
// })
|
||||
//
|
||||
func (f *File) SetDocProps(docProperties *DocProperties) error {
|
||||
core := decodeCoreProperties{}
|
||||
err := xml.Unmarshal(namespaceStrictToTransitional(f.readXML("docProps/core.xml")), &core)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newProps := xlsxCoreProperties{
|
||||
Dc: NameSpaceDublinCore,
|
||||
Dcterms: NameSpaceDublinCoreTerms,
|
||||
Dcmitype: NameSpaceDublinCoreMetadataIntiative,
|
||||
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,
|
||||
}
|
||||
newProps.Created.Text = core.Created.Text
|
||||
newProps.Created.Type = core.Created.Type
|
||||
newProps.Modified.Text = core.Modified.Text
|
||||
newProps.Modified.Type = core.Modified.Type
|
||||
|
||||
fields := []string{"Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords", "LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version"}
|
||||
immutable := reflect.ValueOf(*docProperties)
|
||||
mutable := reflect.ValueOf(&newProps).Elem()
|
||||
for _, field := range fields {
|
||||
val := immutable.FieldByName(field).String()
|
||||
if val != "" {
|
||||
mutable.FieldByName(field).SetString(val)
|
||||
}
|
||||
}
|
||||
if docProperties.Created != "" {
|
||||
newProps.Created.Text = docProperties.Created
|
||||
}
|
||||
if docProperties.Modified != "" {
|
||||
newProps.Modified.Text = docProperties.Modified
|
||||
}
|
||||
output, err := xml.Marshal(&newProps)
|
||||
f.saveFileList("docProps/core.xml", output)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetDocProps provides a function to get document core properties.
|
||||
func (f *File) GetDocProps() (*DocProperties, error) {
|
||||
core := decodeCoreProperties{}
|
||||
err := xml.Unmarshal(namespaceStrictToTransitional(f.readXML("docProps/core.xml")), &core)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &DocProperties{
|
||||
Category: core.Category,
|
||||
ContentStatus: core.ContentStatus,
|
||||
Created: core.Created.Text,
|
||||
Creator: core.Creator,
|
||||
Description: core.Description,
|
||||
Identifier: core.Identifier,
|
||||
Keywords: core.Keywords,
|
||||
LastModifiedBy: core.LastModifiedBy,
|
||||
Modified: core.Modified.Text,
|
||||
Revision: core.Revision,
|
||||
Subject: core.Subject,
|
||||
Title: core.Title,
|
||||
Language: core.Language,
|
||||
Version: core.Version,
|
||||
}, nil
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
|
||||
// this source code is governed by a BSD-style license that can be found in
|
||||
// the LICENSE file.
|
||||
//
|
||||
// Package excelize providing a set of functions that allow you to write to
|
||||
// and read from XLSX files. Support reads and writes XLSX file generated by
|
||||
// Microsoft Excel™ 2007 and later. Support save file without losing original
|
||||
// charts of XLSX. This library needs Go version 1.8 or later.
|
||||
|
||||
package excelize
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
// DocProperties directly maps the document core properties.
|
||||
type DocProperties struct {
|
||||
Category string
|
||||
ContentStatus string
|
||||
Created string
|
||||
Creator string
|
||||
Description string
|
||||
Identifier string
|
||||
Keywords string
|
||||
LastModifiedBy string
|
||||
Modified string
|
||||
Revision string
|
||||
Subject string
|
||||
Title string
|
||||
Language string
|
||||
Version string
|
||||
}
|
||||
|
||||
// decodeCoreProperties directly maps the root element for a part of this
|
||||
// content type shall coreProperties. In order to solve the problem that the
|
||||
// label structure is changed after serialization and deserialization, two
|
||||
// different structures are defined. decodeCoreProperties just for
|
||||
// deserialization.
|
||||
type decodeCoreProperties struct {
|
||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/metadata/core-properties coreProperties"`
|
||||
Title string `xml:"http://purl.org/dc/elements/1.1/ title,omitempty"`
|
||||
Subject string `xml:"http://purl.org/dc/elements/1.1/ subject,omitempty"`
|
||||
Creator string `xml:"http://purl.org/dc/elements/1.1/ creator"`
|
||||
Keywords string `xml:"keywords,omitempty"`
|
||||
Description string `xml:"http://purl.org/dc/elements/1.1/ description,omitempty"`
|
||||
LastModifiedBy string `xml:"lastModifiedBy"`
|
||||
Language string `xml:"http://purl.org/dc/elements/1.1/ language,omitempty"`
|
||||
Identifier string `xml:"http://purl.org/dc/elements/1.1/ identifier,omitempty"`
|
||||
Revision string `xml:"revision,omitempty"`
|
||||
Created struct {
|
||||
Text string `xml:",chardata"`
|
||||
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
|
||||
} `xml:"http://purl.org/dc/terms/ created"`
|
||||
Modified struct {
|
||||
Text string `xml:",chardata"`
|
||||
Type string `xml:"http://www.w3.org/2001/XMLSchema-instance type,attr"`
|
||||
} `xml:"http://purl.org/dc/terms/ modified"`
|
||||
ContentStatus string `xml:"contentStatus,omitempty"`
|
||||
Category string `xml:"category,omitempty"`
|
||||
Version string `xml:"version,omitempty"`
|
||||
}
|
||||
|
||||
// xlsxCoreProperties directly maps the root element for a part of this
|
||||
// content type shall coreProperties.
|
||||
type xlsxCoreProperties struct {
|
||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/metadata/core-properties coreProperties"`
|
||||
Dc string `xml:"xmlns:dc,attr"`
|
||||
Dcterms string `xml:"xmlns:dcterms,attr"`
|
||||
Dcmitype string `xml:"xmlns:dcmitype,attr"`
|
||||
XSI string `xml:"xmlns:xsi,attr"`
|
||||
Title string `xml:"dc:title,omitempty"`
|
||||
Subject string `xml:"dc:subject,omitempty"`
|
||||
Creator string `xml:"dc:creator"`
|
||||
Keywords string `xml:"keywords,omitempty"`
|
||||
Description string `xml:"dc:description,omitempty"`
|
||||
LastModifiedBy string `xml:"lastModifiedBy"`
|
||||
Language string `xml:"dc:language,omitempty"`
|
||||
Identifier string `xml:"dc:identifier,omitempty"`
|
||||
Revision string `xml:"revision,omitempty"`
|
||||
Created struct {
|
||||
Text string `xml:",chardata"`
|
||||
Type string `xml:"xsi:type,attr"`
|
||||
} `xml:"dcterms:created"`
|
||||
Modified struct {
|
||||
Text string `xml:",chardata"`
|
||||
Type string `xml:"xsi:type,attr"`
|
||||
} `xml:"dcterms:modified"`
|
||||
ContentStatus string `xml:"contentStatus,omitempty"`
|
||||
Category string `xml:"category,omitempty"`
|
||||
Version string `xml:"version,omitempty"`
|
||||
}
|
|
@ -31,11 +31,15 @@ const (
|
|||
NameSpaceDrawingMLSpreadSheet = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
|
||||
NameSpaceSpreadSheet = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
|
||||
NameSpaceXML = "http://www.w3.org/XML/1998/namespace"
|
||||
NameSpaceXMLSchemaInstance = "http://www.w3.org/2001/XMLSchema-instance"
|
||||
StrictSourceRelationship = "http://purl.oclc.org/ooxml/officeDocument/relationships"
|
||||
StrictSourceRelationshipChart = "http://purl.oclc.org/ooxml/officeDocument/relationships/chart"
|
||||
StrictSourceRelationshipComments = "http://purl.oclc.org/ooxml/officeDocument/relationships/comments"
|
||||
StrictSourceRelationshipImage = "http://purl.oclc.org/ooxml/officeDocument/relationships/image"
|
||||
StrictNameSpaceSpreadSheet = "http://purl.oclc.org/ooxml/spreadsheetml/main"
|
||||
NameSpaceDublinCore = "http://purl.org/dc/elements/1.1/"
|
||||
NameSpaceDublinCoreTerms = "http://purl.org/dc/terms/"
|
||||
NameSpaceDublinCoreMetadataIntiative = "http://purl.org/dc/dcmitype/"
|
||||
)
|
||||
|
||||
var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png"}
|
||||
|
|
Loading…
Reference in New Issue