xmlWorkbook.go 16 KB

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