xmlWorkbook.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 "encoding/xml"
  11. // xlsxRelationships describe references from parts to other internal resources in the package or to external resources.
  12. type xlsxRelationships struct {
  13. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/relationships Relationships"`
  14. Relationships []xlsxRelationship `xml:"Relationship"`
  15. }
  16. // xlsxRelationship contains relations which maps id and XML.
  17. type xlsxRelationship struct {
  18. ID string `xml:"Id,attr"`
  19. Target string `xml:",attr"`
  20. Type string `xml:",attr"`
  21. TargetMode string `xml:",attr,omitempty"`
  22. }
  23. // xlsxWorkbook directly maps the workbook element from the namespace
  24. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  25. // not checked it for completeness - it does as much as I need.
  26. type xlsxWorkbook struct {
  27. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main workbook"`
  28. FileVersion *xlsxFileVersion `xml:"fileVersion"`
  29. WorkbookPr *xlsxWorkbookPr `xml:"workbookPr"`
  30. WorkbookProtection *xlsxWorkbookProtection `xml:"workbookProtection"`
  31. BookViews *xlsxBookViews `xml:"bookViews"`
  32. Sheets xlsxSheets `xml:"sheets"`
  33. ExternalReferences *xlsxExternalReferences `xml:"externalReferences"`
  34. DefinedNames *xlsxDefinedNames `xml:"definedNames"`
  35. CalcPr *xlsxCalcPr `xml:"calcPr"`
  36. CustomWorkbookViews *xlsxCustomWorkbookViews `xml:"customWorkbookViews"`
  37. PivotCaches *xlsxPivotCaches `xml:"pivotCaches"`
  38. ExtLst *xlsxExtLst `xml:"extLst"`
  39. FileRecoveryPr *xlsxFileRecoveryPr `xml:"fileRecoveryPr"`
  40. }
  41. // xlsxFileRecoveryPr maps sheet recovery information. This element defines
  42. // properties that track the state of the workbook file, such as whether the
  43. // file was saved during a crash, or whether it should be opened in auto-recover
  44. // mode.
  45. type xlsxFileRecoveryPr struct {
  46. AutoRecover bool `xml:"autoRecover,attr,omitempty"`
  47. CrashSave bool `xml:"crashSave,attr,omitempty"`
  48. DataExtractLoad bool `xml:"dataExtractLoad,attr,omitempty"`
  49. RepairLoad bool `xml:"repairLoad,attr,omitempty"`
  50. }
  51. // xlsxWorkbookProtection directly maps the workbookProtection element. This
  52. // element specifies options for protecting data in the workbook. Applications
  53. // might use workbook protection to prevent anyone from accidentally changing,
  54. // moving, or deleting important data. This protection can be ignored by
  55. // applications which choose not to support this optional protection mechanism.
  56. // When a password is to be hashed and stored in this element, it shall be
  57. // hashed as defined below, starting from a UTF-16LE encoded string value. If
  58. // there is a leading BOM character (U+FEFF) in the encoded password it is
  59. // removed before hash calculation.
  60. type xlsxWorkbookProtection struct {
  61. LockRevision bool `xml:"lockRevision,attr,omitempty"`
  62. LockStructure bool `xml:"lockStructure,attr,omitempty"`
  63. LockWindows bool `xml:"lockWindows,attr,omitempty"`
  64. RevisionsAlgorithmName string `xml:"revisionsAlgorithmName,attr,omitempty"`
  65. RevisionsHashValue string `xml:"revisionsHashValue,attr,omitempty"`
  66. RevisionsSaltValue string `xml:"revisionsSaltValue,attr,omitempty"`
  67. RevisionsSpinCount int `xml:"revisionsSpinCount,attr,omitempty"`
  68. WorkbookAlgorithmName string `xml:"workbookAlgorithmName,attr,omitempty"`
  69. WorkbookHashValue string `xml:"workbookHashValue,attr,omitempty"`
  70. WorkbookSaltValue string `xml:"workbookSaltValue,attr,omitempty"`
  71. WorkbookSpinCount int `xml:"workbookSpinCount,attr,omitempty"`
  72. }
  73. // xlsxFileVersion directly maps the fileVersion element. This element defines
  74. // properties that track which version of the application accessed the data and
  75. // source code contained in the file.
  76. type xlsxFileVersion struct {
  77. AppName string `xml:"appName,attr,omitempty"`
  78. CodeName string `xml:"codeName,attr,omitempty"`
  79. LastEdited string `xml:"lastEdited,attr,omitempty"`
  80. LowestEdited string `xml:"lowestEdited,attr,omitempty"`
  81. RupBuild string `xml:"rupBuild,attr,omitempty"`
  82. }
  83. // xlsxWorkbookPr directly maps the workbookPr element from the namespace
  84. // http://schemas.openxmlformats.org/spreadsheetml/2006/main This element
  85. // defines a collection of workbook properties.
  86. type xlsxWorkbookPr struct {
  87. AllowRefreshQuery bool `xml:"allowRefreshQuery,attr,omitempty"`
  88. AutoCompressPictures bool `xml:"autoCompressPictures,attr,omitempty"`
  89. BackupFile bool `xml:"backupFile,attr,omitempty"`
  90. CheckCompatibility bool `xml:"checkCompatibility,attr,omitempty"`
  91. CodeName string `xml:"codeName,attr,omitempty"`
  92. Date1904 bool `xml:"date1904,attr,omitempty"`
  93. DefaultThemeVersion string `xml:"defaultThemeVersion,attr,omitempty"`
  94. FilterPrivacy bool `xml:"filterPrivacy,attr,omitempty"`
  95. HidePivotFieldList bool `xml:"hidePivotFieldList,attr,omitempty"`
  96. PromptedSolutions bool `xml:"promptedSolutions,attr,omitempty"`
  97. PublishItems bool `xml:"publishItems,attr,omitempty"`
  98. RefreshAllConnections bool `xml:"refreshAllConnections,attr,omitempty"`
  99. SaveExternalLinkValues bool `xml:"saveExternalLinkValues,attr,omitempty"`
  100. ShowBorderUnselectedTables bool `xml:"showBorderUnselectedTables,attr,omitempty"`
  101. ShowInkAnnotation bool `xml:"showInkAnnotation,attr,omitempty"`
  102. ShowObjects string `xml:"showObjects,attr,omitempty"`
  103. ShowPivotChartFilter bool `xml:"showPivotChartFilter,attr,omitempty"`
  104. UpdateLinks string `xml:"updateLinks,attr,omitempty"`
  105. }
  106. // xlsxBookViews directly maps the bookViews element. This element specifies the
  107. // collection of workbook views of the enclosing workbook. Each view can specify
  108. // a window position, filter options, and other configurations. There is no
  109. // limit on the number of workbook views that can be defined for a workbook.
  110. type xlsxBookViews struct {
  111. WorkBookView []xlsxWorkBookView `xml:"workbookView"`
  112. }
  113. // xlsxWorkBookView directly maps the workbookView element from the namespace
  114. // http://schemas.openxmlformats.org/spreadsheetml/2006/main This element
  115. // specifies a single Workbook view.
  116. type xlsxWorkBookView struct {
  117. ActiveTab int `xml:"activeTab,attr,omitempty"`
  118. AutoFilterDateGrouping bool `xml:"autoFilterDateGrouping,attr,omitempty"`
  119. FirstSheet int `xml:"firstSheet,attr,omitempty"`
  120. Minimized bool `xml:"minimized,attr,omitempty"`
  121. ShowHorizontalScroll bool `xml:"showHorizontalScroll,attr,omitempty"`
  122. ShowSheetTabs bool `xml:"showSheetTabs,attr,omitempty"`
  123. ShowVerticalScroll bool `xml:"showVerticalScroll,attr,omitempty"`
  124. TabRatio int `xml:"tabRatio,attr,omitempty"`
  125. Visibility string `xml:"visibility,attr,omitempty"`
  126. WindowHeight int `xml:"windowHeight,attr,omitempty"`
  127. WindowWidth int `xml:"windowWidth,attr,omitempty"`
  128. XWindow string `xml:"xWindow,attr,omitempty"`
  129. YWindow string `xml:"yWindow,attr,omitempty"`
  130. }
  131. // xlsxSheets directly maps the sheets element from the namespace
  132. // http://schemas.openxmlformats.org/spreadsheetml/2006/main.
  133. type xlsxSheets struct {
  134. Sheet []xlsxSheet `xml:"sheet"`
  135. }
  136. // xlsxSheet defines a sheet in this workbook. Sheet data is stored in a
  137. // separate part.
  138. type xlsxSheet struct {
  139. Name string `xml:"name,attr,omitempty"`
  140. SheetID int `xml:"sheetId,attr,omitempty"`
  141. ID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  142. State string `xml:"state,attr,omitempty"`
  143. }
  144. // xlsxExternalReferences directly maps the externalReferences element of the
  145. // external workbook references part.
  146. type xlsxExternalReferences struct {
  147. ExternalReference []xlsxExternalReference `xml:"externalReference"`
  148. }
  149. // xlsxExternalReference directly maps the externalReference element of the
  150. // external workbook references part.
  151. type xlsxExternalReference struct {
  152. RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  153. }
  154. // xlsxPivotCaches element enumerates pivot cache definition parts used by pivot
  155. // tables and formulas in this workbook.
  156. type xlsxPivotCaches struct {
  157. PivotCache []xlsxPivotCache `xml:"pivotCache"`
  158. }
  159. // xlsxPivotCache directly maps the pivotCache element.
  160. type xlsxPivotCache struct {
  161. CacheID int `xml:"cacheId,attr"`
  162. RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  163. }
  164. // extLst element provides a convention for extending spreadsheetML in
  165. // predefined locations. The locations shall be denoted with the extLst element,
  166. // and are called extension lists. Extension list locations within the markup
  167. // document are specified in the markup specification and can be used to store
  168. // extensions to the markup specification, whether those are future version
  169. // extensions of the markup specification or are private extensions implemented
  170. // independently from the markup specification. Markup within an extension might
  171. // not be understood by a consumer.
  172. type xlsxExtLst struct {
  173. Ext string `xml:",innerxml"`
  174. }
  175. // xlsxDefinedNames directly maps the definedNames element. This element defines
  176. // the collection of defined names for this workbook. Defined names are
  177. // descriptive names to represent cells, ranges of cells, formulas, or constant
  178. // values. Defined names can be used to represent a range on any worksheet.
  179. type xlsxDefinedNames struct {
  180. DefinedName []xlsxDefinedName `xml:"definedName"`
  181. }
  182. // xlsxDefinedName directly maps the definedName element from the namespace
  183. // http://schemas.openxmlformats.org/spreadsheetml/2006/main This element
  184. // defines a defined name within this workbook. A defined name is descriptive
  185. // text that is used to represents a cell, range of cells, formula, or constant
  186. // value. For a descriptions of the attributes see https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.definedname
  187. type xlsxDefinedName struct {
  188. Comment string `xml:"comment,attr,omitempty"`
  189. CustomMenu string `xml:"customMenu,attr,omitempty"`
  190. Description string `xml:"description,attr,omitempty"`
  191. Function bool `xml:"function,attr,omitempty"`
  192. FunctionGroupID int `xml:"functionGroupId,attr,omitempty"`
  193. Help string `xml:"help,attr,omitempty"`
  194. Hidden bool `xml:"hidden,attr,omitempty"`
  195. LocalSheetID *int `xml:"localSheetId,attr"`
  196. Name string `xml:"name,attr,omitempty"`
  197. PublishToServer bool `xml:"publishToServer,attr,omitempty"`
  198. ShortcutKey string `xml:"shortcutKey,attr,omitempty"`
  199. StatusBar string `xml:"statusBar,attr,omitempty"`
  200. VbProcedure bool `xml:"vbProcedure,attr,omitempty"`
  201. WorkbookParameter bool `xml:"workbookParameter,attr,omitempty"`
  202. Xlm bool `xml:"xml,attr,omitempty"`
  203. Data string `xml:",chardata"`
  204. }
  205. // xlsxCalcPr directly maps the calcPr element. This element defines the
  206. // collection of properties the application uses to record calculation status
  207. // and details. Calculation is the process of computing formulas and then
  208. // displaying the results as values in the cells that contain the formulas.
  209. type xlsxCalcPr struct {
  210. CalcCompleted bool `xml:"calcCompleted,attr,omitempty"`
  211. CalcID string `xml:"calcId,attr,omitempty"`
  212. CalcMode string `xml:"calcMode,attr,omitempty"`
  213. CalcOnSave bool `xml:"calcOnSave,attr,omitempty"`
  214. ConcurrentCalc *bool `xml:"concurrentCalc,attr"`
  215. ConcurrentManualCount int `xml:"concurrentManualCount,attr,omitempty"`
  216. ForceFullCalc bool `xml:"forceFullCalc,attr,omitempty"`
  217. FullCalcOnLoad bool `xml:"fullCalcOnLoad,attr,omitempty"`
  218. FullPrecision bool `xml:"fullPrecision,attr,omitempty"`
  219. Iterate bool `xml:"iterate,attr,omitempty"`
  220. IterateCount int `xml:"iterateCount,attr,omitempty"`
  221. IterateDelta float64 `xml:"iterateDelta,attr,omitempty"`
  222. RefMode string `xml:"refMode,attr,omitempty"`
  223. }
  224. // xlsxCustomWorkbookViews defines the collection of custom workbook views that
  225. // are defined for this workbook. A customWorkbookView is similar in concept to
  226. // a workbookView in that its attributes contain settings related to the way
  227. // that the workbook should be displayed on a screen by a spreadsheet
  228. // application.
  229. type xlsxCustomWorkbookViews struct {
  230. CustomWorkbookView []xlsxCustomWorkbookView `xml:"customWorkbookView"`
  231. }
  232. // xlsxCustomWorkbookView directly maps the customWorkbookView element. This
  233. // element specifies a single custom workbook view. A custom workbook view
  234. // consists of a set of display and print settings that you can name and apply
  235. // to a workbook. You can create more than one custom workbook view of the same
  236. // workbook. Custom Workbook Views are not required in order to construct a
  237. // valid SpreadsheetML document, and are not necessary if the document is never
  238. // displayed by a spreadsheet application, or if the spreadsheet application has
  239. // a fixed display for workbooks. However, if a spreadsheet application chooses
  240. // to implement configurable display modes, the customWorkbookView element
  241. // should be used to persist the settings for those display modes.
  242. type xlsxCustomWorkbookView struct {
  243. ActiveSheetID *int `xml:"activeSheetId,attr"`
  244. AutoUpdate *bool `xml:"autoUpdate,attr"`
  245. ChangesSavedWin *bool `xml:"changesSavedWin,attr"`
  246. GUID *string `xml:"guid,attr"`
  247. IncludeHiddenRowCol *bool `xml:"includeHiddenRowCol,attr"`
  248. IncludePrintSettings *bool `xml:"includePrintSettings,attr"`
  249. Maximized *bool `xml:"maximized,attr"`
  250. MergeInterval int `xml:"mergeInterval,attr"`
  251. Minimized *bool `xml:"minimized,attr"`
  252. Name *string `xml:"name,attr"`
  253. OnlySync *bool `xml:"onlySync,attr"`
  254. PersonalView *bool `xml:"personalView,attr"`
  255. ShowComments *string `xml:"showComments,attr"`
  256. ShowFormulaBar *bool `xml:"showFormulaBar,attr"`
  257. ShowHorizontalScroll *bool `xml:"showHorizontalScroll,attr"`
  258. ShowObjects *string `xml:"showObjects,attr"`
  259. ShowSheetTabs *bool `xml:"showSheetTabs,attr"`
  260. ShowStatusbar *bool `xml:"showStatusbar,attr"`
  261. ShowVerticalScroll *bool `xml:"showVerticalScroll,attr"`
  262. TabRatio *int `xml:"tabRatio,attr"`
  263. WindowHeight *int `xml:"windowHeight,attr"`
  264. WindowWidth *int `xml:"windowWidth,attr"`
  265. XWindow *int `xml:"xWindow,attr"`
  266. YWindow *int `xml:"yWindow,attr"`
  267. }
  268. // DefinedName directly maps the name for a cell or cell range on a
  269. // worksheet.
  270. type DefinedName struct {
  271. Name string
  272. Comment string
  273. RefersTo string
  274. Scope string
  275. }