docProps.go 5.7 KB
Newer Older
1 2 3 4 5 6 7
// 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
8
// charts of XLSX. This library needs Go version 1.10 or later.
9 10 11 12

package excelize

import (
13
	"bytes"
14
	"encoding/xml"
15 16
	"fmt"
	"io"
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
	"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
xurime's avatar
xurime 已提交
37
//                    | environment-specific.
38 39 40 41 42 43 44 45
//                    |
//     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",
xurime's avatar
xurime 已提交
46
//                    | "Reviewed" and "Final"
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
//                    |
//     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",
//    })
//
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
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)
	if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("docProps/core.xml")))).
		Decode(core); err != nil && err != io.EOF {
		err = fmt.Errorf("xml decode error: %s", err)
		return
86
	}
87
	newProps, err = &xlsxCoreProperties{
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
		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,
104 105 106 107 108 109
	}, nil
	newProps.Created.Text, newProps.Created.Type, newProps.Modified.Text, newProps.Modified.Type =
		core.Created.Text, core.Created.Type, core.Modified.Text, core.Modified.Type
	fields = []string{
		"Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords",
		"LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version",
110
	}
111 112 113
	immutable, mutable = reflect.ValueOf(*docProperties), reflect.ValueOf(newProps).Elem()
	for _, field = range fields {
		if val = immutable.FieldByName(field).String(); val != "" {
114 115 116 117 118 119 120 121 122
			mutable.FieldByName(field).SetString(val)
		}
	}
	if docProperties.Created != "" {
		newProps.Created.Text = docProperties.Created
	}
	if docProperties.Modified != "" {
		newProps.Modified.Text = docProperties.Modified
	}
123
	output, err = xml.Marshal(newProps)
124
	f.saveFileList("docProps/core.xml", output)
125 126

	return
127 128 129
}

// GetDocProps provides a function to get document core properties.
130 131 132 133 134 135 136
func (f *File) GetDocProps() (ret *DocProperties, err error) {
	var core = new(decodeCoreProperties)

	if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("docProps/core.xml")))).
		Decode(core); err != nil && err != io.EOF {
		err = fmt.Errorf("xml decode error: %s", err)
		return
137
	}
138
	ret, err = &DocProperties{
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		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
154 155

	return
156
}