You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.3KB

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