docProps.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // 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. package excelize
  10. import (
  11. "bytes"
  12. "encoding/xml"
  13. "fmt"
  14. "io"
  15. "reflect"
  16. )
  17. // SetDocProps provides a function to set document core properties. The
  18. // properties that can be set are:
  19. //
  20. // Property | Description
  21. // ----------------+-----------------------------------------------------------------------------
  22. // Title | The name given to the resource.
  23. // |
  24. // Subject | The topic of the content of the resource.
  25. // |
  26. // Creator | An entity primarily responsible for making the content of the resource.
  27. // |
  28. // Keywords | A delimited set of keywords to support searching and indexing. This is
  29. // | typically a list of terms that are not available elsewhere in the properties.
  30. // |
  31. // Description | An explanation of the content of the resource.
  32. // |
  33. // LastModifiedBy | The user who performed the last modification. The identification is
  34. // | environment-specific.
  35. // |
  36. // Language | The language of the intellectual content of the resource.
  37. // |
  38. // Identifier | An unambiguous reference to the resource within a given context.
  39. // |
  40. // Revision | The topic of the content of the resource.
  41. // |
  42. // ContentStatus | The status of the content. For example: Values might include "Draft",
  43. // | "Reviewed" and "Final"
  44. // |
  45. // Category | A categorization of the content of this package.
  46. // |
  47. // Version | The version number. This value is set by the user or by the application.
  48. //
  49. // For example:
  50. //
  51. // err := f.SetDocProps(&excelize.DocProperties{
  52. // Category: "category",
  53. // ContentStatus: "Draft",
  54. // Created: "2019-06-04T22:00:10Z",
  55. // Creator: "Go Excelize",
  56. // Description: "This file created by Go Excelize",
  57. // Identifier: "xlsx",
  58. // Keywords: "Spreadsheet",
  59. // LastModifiedBy: "Go Author",
  60. // Modified: "2019-06-04T22:00:10Z",
  61. // Revision: "0",
  62. // Subject: "Test Subject",
  63. // Title: "Test Title",
  64. // Language: "en-US",
  65. // Version: "1.0.0",
  66. // })
  67. //
  68. func (f *File) SetDocProps(docProperties *DocProperties) (err error) {
  69. var (
  70. core *decodeCoreProperties
  71. newProps *xlsxCoreProperties
  72. fields []string
  73. output []byte
  74. immutable, mutable reflect.Value
  75. field, val string
  76. )
  77. core = new(decodeCoreProperties)
  78. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("docProps/core.xml")))).
  79. Decode(core); err != nil && err != io.EOF {
  80. err = fmt.Errorf("xml decode error: %s", err)
  81. return
  82. }
  83. newProps, err = &xlsxCoreProperties{
  84. Dc: NameSpaceDublinCore,
  85. Dcterms: NameSpaceDublinCoreTerms,
  86. Dcmitype: NameSpaceDublinCoreMetadataIntiative,
  87. XSI: NameSpaceXMLSchemaInstance,
  88. Title: core.Title,
  89. Subject: core.Subject,
  90. Creator: core.Creator,
  91. Keywords: core.Keywords,
  92. Description: core.Description,
  93. LastModifiedBy: core.LastModifiedBy,
  94. Language: core.Language,
  95. Identifier: core.Identifier,
  96. Revision: core.Revision,
  97. ContentStatus: core.ContentStatus,
  98. Category: core.Category,
  99. Version: core.Version,
  100. }, nil
  101. newProps.Created.Text, newProps.Created.Type, newProps.Modified.Text, newProps.Modified.Type =
  102. core.Created.Text, core.Created.Type, core.Modified.Text, core.Modified.Type
  103. fields = []string{
  104. "Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords",
  105. "LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version",
  106. }
  107. immutable, mutable = reflect.ValueOf(*docProperties), reflect.ValueOf(newProps).Elem()
  108. for _, field = range fields {
  109. if val = immutable.FieldByName(field).String(); val != "" {
  110. mutable.FieldByName(field).SetString(val)
  111. }
  112. }
  113. if docProperties.Created != "" {
  114. newProps.Created.Text = docProperties.Created
  115. }
  116. if docProperties.Modified != "" {
  117. newProps.Modified.Text = docProperties.Modified
  118. }
  119. output, err = xml.Marshal(newProps)
  120. f.saveFileList("docProps/core.xml", output)
  121. return
  122. }
  123. // GetDocProps provides a function to get document core properties.
  124. func (f *File) GetDocProps() (ret *DocProperties, err error) {
  125. var core = new(decodeCoreProperties)
  126. if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("docProps/core.xml")))).
  127. Decode(core); err != nil && err != io.EOF {
  128. err = fmt.Errorf("xml decode error: %s", err)
  129. return
  130. }
  131. ret, err = &DocProperties{
  132. Category: core.Category,
  133. ContentStatus: core.ContentStatus,
  134. Created: core.Created.Text,
  135. Creator: core.Creator,
  136. Description: core.Description,
  137. Identifier: core.Identifier,
  138. Keywords: core.Keywords,
  139. LastModifiedBy: core.LastModifiedBy,
  140. Modified: core.Modified.Text,
  141. Revision: core.Revision,
  142. Subject: core.Subject,
  143. Title: core.Title,
  144. Language: core.Language,
  145. Version: core.Version,
  146. }, nil
  147. return
  148. }