docProps.go 5.8 KB

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