xmlWorksheet.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package excelize
  2. import "encoding/xml"
  3. // xlsxWorksheet directly maps the worksheet element in the namespace
  4. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  5. // not checked it for completeness - it does as much as I need.
  6. type xlsxWorksheet struct {
  7. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
  8. SheetPr *xlsxSheetPr `xml:"sheetPr"`
  9. Dimension xlsxDimension `xml:"dimension"`
  10. SheetViews xlsxSheetViews `xml:"sheetViews"`
  11. SheetFormatPr *xlsxSheetFormatPr `xml:"sheetFormatPr"`
  12. Cols *xlsxCols `xml:"cols,omitempty"`
  13. SheetData xlsxSheetData `xml:"sheetData"`
  14. SheetProtection *xlsxSheetProtection `xml:"sheetProtection"`
  15. MergeCells *xlsxMergeCells `xml:"mergeCells,omitempty"`
  16. ConditionalFormatting *xlsxConditionalFormatting `xml:"conditionalFormatting"`
  17. Hyperlinks *xlsxHyperlinks `xml:"hyperlinks"`
  18. PrintOptions *xlsxPrintOptions `xml:"printOptions"`
  19. PageMargins *xlsxPageMargins `xml:"pageMargins"`
  20. PageSetUp *xlsxPageSetUp `xml:"pageSetup"`
  21. HeaderFooter *xlsxHeaderFooter `xml:"headerFooter"`
  22. Drawing *xlsxDrawing `xml:"drawing"`
  23. LegacyDrawing *xlsxLegacyDrawing `xml:"legacyDrawing"`
  24. Picture *xlsxPicture `xml:"picture"`
  25. TableParts *xlsxTableParts `xml:"tableParts"`
  26. ExtLst *xlsxExtLst `xml:"extLst"`
  27. }
  28. // xlsxDrawing change r:id to rid in the namespace.
  29. type xlsxDrawing struct {
  30. RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  31. }
  32. // xlsxHeaderFooter directly maps the headerFooter element in the namespace
  33. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - When printed or
  34. // viewed in page layout view (§18.18.69), each page of a worksheet can have a
  35. // page header, a page footer, or both. The headers and footers on odd-numbered
  36. // pages can differ from those on even-numbered pages, and the headers and
  37. // footers on the first page can differ from those on odd- and even-numbered
  38. // pages. In the latter case, the first page is not considered an odd page.
  39. type xlsxHeaderFooter struct {
  40. DifferentFirst bool `xml:"differentFirst,attr,omitempty"`
  41. DifferentOddEven bool `xml:"differentOddEven,attr,omitempty"`
  42. OddHeader []*xlsxOddHeader `xml:"oddHeader"`
  43. OddFooter []*xlsxOddFooter `xml:"oddFooter"`
  44. }
  45. // xlsxOddHeader directly maps the oddHeader element in the namespace
  46. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  47. // not checked it for completeness - it does as much as I need.
  48. type xlsxOddHeader struct {
  49. Content string `xml:",chardata"`
  50. }
  51. // xlsxOddFooter directly maps the oddFooter element in the namespace
  52. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  53. // not checked it for completeness - it does as much as I need.
  54. type xlsxOddFooter struct {
  55. Content string `xml:",chardata"`
  56. }
  57. // xlsxPageSetUp directly maps the pageSetup element in the namespace
  58. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - Page setup
  59. // settings for the worksheet.
  60. type xlsxPageSetUp struct {
  61. BlackAndWhite bool `xml:"blackAndWhite,attr,omitempty"`
  62. CellComments string `xml:"cellComments,attr,omitempty"`
  63. Copies int `xml:"copies,attr,omitempty"`
  64. Draft bool `xml:"draft,attr,omitempty"`
  65. Errors string `xml:"errors,attr,omitempty"`
  66. FirstPageNumber int `xml:"firstPageNumber,attr,omitempty"`
  67. FitToHeight int `xml:"fitToHeight,attr,omitempty"`
  68. FitToWidth int `xml:"fitToWidth,attr,omitempty"`
  69. HorizontalDPI float32 `xml:"horizontalDpi,attr,omitempty"`
  70. RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  71. Orientation string `xml:"orientation,attr,omitempty"`
  72. PageOrder string `xml:"pageOrder,attr,omitempty"`
  73. PaperHeight string `xml:"paperHeight,attr,omitempty"`
  74. PaperSize string `xml:"paperSize,attr,omitempty"`
  75. PaperWidth string `xml:"paperWidth,attr,omitempty"`
  76. Scale int `xml:"scale,attr,omitempty"`
  77. UseFirstPageNumber bool `xml:"useFirstPageNumber,attr,omitempty"`
  78. UsePrinterDefaults bool `xml:"usePrinterDefaults,attr,omitempty"`
  79. VerticalDPI float32 `xml:"verticalDpi,attr,omitempty"`
  80. }
  81. // xlsxPrintOptions directly maps the printOptions element in the namespace
  82. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - Print options for
  83. // the sheet. Printer-specific settings are stored separately in the Printer
  84. // Settings part.
  85. type xlsxPrintOptions struct {
  86. GridLines bool `xml:"gridLines,attr,omitempty"`
  87. GridLinesSet bool `xml:"gridLinesSet,attr,omitempty"`
  88. Headings bool `xml:"headings,attr,omitempty"`
  89. HorizontalCentered bool `xml:"horizontalCentered,attr,omitempty"`
  90. VerticalCentered bool `xml:"verticalCentered,attr,omitempty"`
  91. }
  92. // xlsxPageMargins directly maps the pageMargins element in the namespace
  93. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - Page margins for
  94. // a sheet or a custom sheet view.
  95. type xlsxPageMargins struct {
  96. Bottom float64 `xml:"bottom,attr"`
  97. Footer float64 `xml:"footer,attr"`
  98. Header float64 `xml:"header,attr"`
  99. Left float64 `xml:"left,attr"`
  100. Right float64 `xml:"right,attr"`
  101. Top float64 `xml:"top,attr"`
  102. }
  103. // xlsxSheetFormatPr directly maps the sheetFormatPr element in the namespace
  104. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  105. // not checked it for completeness - it does as much as I need.
  106. type xlsxSheetFormatPr struct {
  107. DefaultColWidth float64 `xml:"defaultColWidth,attr,omitempty"`
  108. DefaultRowHeight float64 `xml:"defaultRowHeight,attr"`
  109. CustomHeight float64 `xml:"customHeight,attr,omitempty"`
  110. ZeroHeight float64 `xml:"zeroHeight,attr,omitempty"`
  111. OutlineLevelCol uint8 `xml:"outlineLevelCol,attr,omitempty"`
  112. OutlineLevelRow uint8 `xml:"outlineLevelRow,attr,omitempty"`
  113. }
  114. // xlsxSheetViews directly maps the sheetViews element in the namespace
  115. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - Worksheet views
  116. // collection.
  117. type xlsxSheetViews struct {
  118. SheetView []xlsxSheetView `xml:"sheetView"`
  119. }
  120. // xlsxSheetView directly maps the sheetView element in the namespace
  121. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  122. // not checked it for completeness - it does as much as I need. A single sheet
  123. // view definition. When more than one sheet view is defined in the file, it
  124. // means that when opening the workbook, each sheet view corresponds to a
  125. // separate window within the spreadsheet application, where each window is
  126. // showing the particular sheet containing the same workbookViewId value, the
  127. // last sheetView definition is loaded, and the others are discarded. When
  128. // multiple windows are viewing the same sheet, multiple sheetView elements
  129. // (with corresponding workbookView entries) are saved.
  130. type xlsxSheetView struct {
  131. WindowProtection bool `xml:"windowProtection,attr,omitempty"`
  132. ShowFormulas bool `xml:"showFormulas,attr,omitempty"`
  133. ShowGridLines string `xml:"showGridLines,attr,omitempty"`
  134. ShowRowColHeaders bool `xml:"showRowColHeaders,attr,omitempty"`
  135. ShowZeros bool `xml:"showZeros,attr,omitempty"`
  136. RightToLeft bool `xml:"rightToLeft,attr,omitempty"`
  137. TabSelected bool `xml:"tabSelected,attr,omitempty"`
  138. ShowOutlineSymbols bool `xml:"showOutlineSymbols,attr,omitempty"`
  139. DefaultGridColor bool `xml:"defaultGridColor,attr"`
  140. View string `xml:"view,attr,omitempty"`
  141. TopLeftCell string `xml:"topLeftCell,attr,omitempty"`
  142. ColorID int `xml:"colorId,attr,omitempty"`
  143. ZoomScale float64 `xml:"zoomScale,attr,omitempty"`
  144. ZoomScaleNormal float64 `xml:"zoomScaleNormal,attr,omitempty"`
  145. ZoomScalePageLayoutView float64 `xml:"zoomScalePageLayoutView,attr,omitempty"`
  146. WorkbookViewID int `xml:"workbookViewId,attr"`
  147. Pane *xlsxPane `xml:"pane,omitempty"`
  148. Selection []*xlsxSelection `xml:"selection"`
  149. }
  150. // xlsxSelection directly maps the selection element in the namespace
  151. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - Worksheet view
  152. // selection.
  153. type xlsxSelection struct {
  154. ActiveCell string `xml:"activeCell,attr,omitempty"`
  155. ActiveCellID int `xml:"activeCellId,attr"`
  156. Pane string `xml:"pane,attr,omitempty"`
  157. SQRef string `xml:"sqref,attr,omitempty"`
  158. }
  159. // xlsxSelection directly maps the selection element. Worksheet view pane.
  160. type xlsxPane struct {
  161. ActivePane string `xml:"activePane,attr,omitempty"`
  162. State string `xml:"state,attr,omitempty"` // Either "split" or "frozen"
  163. TopLeftCell string `xml:"topLeftCell,attr,omitempty"`
  164. XSplit float64 `xml:"xSplit,attr,omitempty"`
  165. YSplit float64 `xml:"ySplit,attr,omitempty"`
  166. }
  167. // xlsxSheetPr directly maps the sheetPr element in the namespace
  168. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - Sheet-level
  169. // properties.
  170. type xlsxSheetPr struct {
  171. XMLName xml.Name `xml:"sheetPr"`
  172. CodeName string `xml:"codeName,attr,omitempty"`
  173. EnableFormatConditionsCalculation bool `xml:"enableFormatConditionsCalculation,attr,omitempty"`
  174. FilterMode bool `xml:"filterMode,attr,omitempty"`
  175. Published bool `xml:"published,attr,omitempty"`
  176. SyncHorizontal bool `xml:"syncHorizontal,attr,omitempty"`
  177. SyncVertical bool `xml:"syncVertical,attr,omitempty"`
  178. TransitionEntry bool `xml:"transitionEntry,attr,omitempty"`
  179. TabColor *xlsxTabColor `xml:"tabColor,omitempty"`
  180. PageSetUpPr *xlsxPageSetUpPr `xml:"pageSetUpPr,omitempty"`
  181. }
  182. // xlsxPageSetUpPr directly maps the pageSetupPr element in the namespace
  183. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - Page setup
  184. // properties of the worksheet.
  185. type xlsxPageSetUpPr struct {
  186. AutoPageBreaks bool `xml:"autoPageBreaks,attr,omitempty"`
  187. FitToPage bool `xml:"fitToPage,attr,omitempty"` // Flag indicating whether the Fit to Page print option is enabled.
  188. }
  189. // xlsxTabColor directly maps the tabColor element in the namespace currently I
  190. // have not checked it for completeness - it does as much as I need.
  191. type xlsxTabColor struct {
  192. Theme int `xml:"theme,attr,omitempty"`
  193. Tint uint8 `xml:"tint,attr,omitempty"`
  194. }
  195. // xlsxCols directly maps the cols element in the namespace
  196. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  197. // not checked it for completeness - it does as much as I need.
  198. type xlsxCols struct {
  199. Col []xlsxCol `xml:"col"`
  200. }
  201. // xlsxCol directly maps the col (Column Width & Formatting). Defines column
  202. // width and column formatting for one or more columns of the worksheet.
  203. type xlsxCol struct {
  204. BestFit bool `xml:"bestFit,attr,omitempty"`
  205. Collapsed bool `xml:"collapsed,attr"`
  206. CustomWidth bool `xml:"customWidth,attr,omitempty"`
  207. Hidden bool `xml:"hidden,attr"`
  208. Max int `xml:"max,attr"`
  209. Min int `xml:"min,attr"`
  210. OutlineLevel uint8 `xml:"outlineLevel,attr,omitempty"`
  211. Phonetic bool `xml:"phonetic,attr,omitempty"`
  212. Style int `xml:"style,attr"`
  213. Width float64 `xml:"width,attr"`
  214. }
  215. // xlsxDimension directly maps the dimension element in the namespace
  216. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - This element
  217. // specifies the used range of the worksheet. It specifies the row and column
  218. // bounds of used cells in the worksheet. This is optional and is not required.
  219. // Used cells include cells with formulas, text content, and cell formatting.
  220. // When an entire column is formatted, only the first cell in that column is
  221. // considered used.
  222. type xlsxDimension struct {
  223. Ref string `xml:"ref,attr"`
  224. }
  225. // xlsxSheetData directly maps the sheetData element in the namespace
  226. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  227. // not checked it for completeness - it does as much as I need.
  228. type xlsxSheetData struct {
  229. XMLName xml.Name `xml:"sheetData"`
  230. Row []xlsxRow `xml:"row"`
  231. }
  232. // xlsxRow directly maps the row element. The element expresses information
  233. // about an entire row of a worksheet, and contains all cell definitions for a
  234. // particular row in the worksheet.
  235. type xlsxRow struct {
  236. Collapsed bool `xml:"collapsed,attr,omitempty"`
  237. CustomFormat bool `xml:"customFormat,attr,omitempty"`
  238. CustomHeight bool `xml:"customHeight,attr,omitempty"`
  239. Hidden bool `xml:"hidden,attr,omitempty"`
  240. Ht string `xml:"ht,attr,omitempty"`
  241. OutlineLevel uint8 `xml:"outlineLevel,attr,omitempty"`
  242. Ph bool `xml:"ph,attr,omitempty"`
  243. R int `xml:"r,attr,omitempty"`
  244. S int `xml:"s,attr,omitempty"`
  245. Spans string `xml:"spans,attr,omitempty"`
  246. ThickBot bool `xml:"thickBot,attr,omitempty"`
  247. ThickTop bool `xml:"thickTop,attr,omitempty"`
  248. C []xlsxC `xml:"c"`
  249. }
  250. // xlsxMergeCell directly maps the mergeCell element. A single merged cell.
  251. type xlsxMergeCell struct {
  252. Ref string `xml:"ref,attr,omitempty"`
  253. }
  254. // xlsxMergeCells directly maps the mergeCells element. This collection
  255. // expresses all the merged cells in the sheet.
  256. type xlsxMergeCells struct {
  257. Count int `xml:"count,attr,omitempty"`
  258. Cells []*xlsxMergeCell `xml:"mergeCell,omitempty"`
  259. }
  260. // xlsxC directly maps the c element in the namespace
  261. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  262. // not checked it for completeness - it does as much as I need.
  263. type xlsxC struct {
  264. R string `xml:"r,attr"` // Cell ID, e.g. A1
  265. S int `xml:"s,attr,omitempty"` // Style reference.
  266. // Str string `xml:"str,attr,omitempty"` // Style reference.
  267. T string `xml:"t,attr,omitempty"` // Type.
  268. F *xlsxF `xml:"f,omitempty"` // Formula
  269. V string `xml:"v,omitempty"` // Value
  270. }
  271. // xlsxF directly maps the f element in the namespace
  272. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
  273. // not checked it for completeness - it does as much as I need.
  274. type xlsxF struct {
  275. Content string `xml:",chardata"`
  276. T string `xml:"t,attr,omitempty"` // Formula type
  277. Ref string `xml:"ref,attr,omitempty"` // Shared formula ref
  278. Si string `xml:"si,attr,omitempty"` // Shared formula index
  279. }
  280. // xlsxSheetProtection collection expresses the sheet protection options to
  281. // enforce when the sheet is protected.
  282. type xlsxSheetProtection struct {
  283. AlgorithmName string `xml:"algorithmName,attr,omitempty"`
  284. AutoFilter int `xml:"autoFilter,attr,omitempty"`
  285. DeleteColumns int `xml:"deleteColumns,attr,omitempty"`
  286. DeleteRows int `xml:"deleteRows,attr,omitempty"`
  287. FormatCells int `xml:"formatCells,attr,omitempty"`
  288. FormatColumns int `xml:"formatColumns,attr,omitempty"`
  289. FormatRows int `xml:"formatRows,attr,omitempty"`
  290. HashValue string `xml:"hashValue,attr,omitempty"`
  291. InsertColumns int `xml:"insertColumns,attr,omitempty"`
  292. InsertHyperlinks int `xml:"insertHyperlinks,attr,omitempty"`
  293. InsertRows int `xml:"insertRows,attr,omitempty"`
  294. Objects int `xml:"objects,attr,omitempty"`
  295. PivotTables int `xml:"pivotTables,attr,omitempty"`
  296. SaltValue string `xml:"saltValue,attr,omitempty"`
  297. Scenarios int `xml:"scenarios,attr,omitempty"`
  298. SelectLockedCells int `xml:"selectLockedCells,attr,omitempty"`
  299. SelectUnlockedCell int `xml:"selectUnlockedCell,attr,omitempty"`
  300. Sheet int `xml:"sheet,attr,omitempty"`
  301. Sort int `xml:"sort,attr,omitempty"`
  302. SpinCount int `xml:"spinCount,attr,omitempty"`
  303. }
  304. // A Conditional Format is a format, such as cell shading or font color, that a
  305. // spreadsheet application can automatically apply to cells if a specified
  306. // condition is true. This collection expresses conditional formatting rules
  307. // applied to a particular cell or range.
  308. type xlsxConditionalFormatting struct {
  309. CfRule string `xml:",innerxml"`
  310. }
  311. // xlsxHyperlinks directly maps the hyperlinks element in the namespace
  312. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - A hyperlink can
  313. // be stored in a package as a relationship. Hyperlinks shall be identified by
  314. // containing a target which specifies the destination of the given hyperlink.
  315. type xlsxHyperlinks struct {
  316. Hyperlink []xlsxHyperlink `xml:"hyperlink"`
  317. }
  318. // xlsxHyperlink directly maps the hyperlink element in the namespace
  319. // http://schemas.openxmlformats.org/spreadsheetml/2006/main
  320. type xlsxHyperlink struct {
  321. Ref string `xml:"ref,attr"`
  322. Location string `xml:"location,attr,omitempty"`
  323. Display string `xml:"display,attr,omitempty"`
  324. RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  325. }
  326. // xlsxTableParts directly maps the tableParts element in the namespace
  327. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - The table element
  328. // has several attributes applied to identify the table and the data range it
  329. // covers. The table id attribute needs to be unique across all table parts, the
  330. // same goes for the name and displayName. The displayName has the further
  331. // restriction that it must be unique across all defined names in the workbook.
  332. // Later on we will see that you can define names for many elements, such as
  333. // cells or formulas. The name value is used for the object model in Microsoft
  334. // Office Excel. The displayName is used for references in formulas. The ref
  335. // attribute is used to identify the cell range that the table covers. This
  336. // includes not only the table data, but also the table header containing column
  337. // names.
  338. // To add columns to your table you add new tableColumn elements to the
  339. // tableColumns container. Similar to the shared string table the collection
  340. // keeps a count attribute identifying the number of columns. Besides the table
  341. // definition in the table part there is also the need to identify which tables
  342. // are displayed in the worksheet. The worksheet part has a separate element
  343. // tableParts to store this information. Each table part is referenced through
  344. // the relationship ID and again a count of the number of table parts is
  345. // maintained. The following markup sample is taken from the documents
  346. // accompanying this book. The sheet data element has been removed to reduce the
  347. // size of the sample. To reference the table, just add the tableParts element,
  348. // of course after having created and stored the table part. For example:
  349. //
  350. // <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  351. // ...
  352. // <tableParts count="1">
  353. // <tablePart r:id="rId1" />
  354. // </tableParts>
  355. // </worksheet>
  356. //
  357. type xlsxTableParts struct {
  358. Count int `xml:"count,attr,omitempty"`
  359. TableParts []*xlsxTablePart `xml:"tablePart"`
  360. }
  361. // xlsxTablePart directly maps the tablePart element in the namespace
  362. // http://schemas.openxmlformats.org/spreadsheetml/2006/main
  363. type xlsxTablePart struct {
  364. RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  365. }
  366. // xlsxPicture directly maps the picture element in the namespace
  367. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - Background sheet
  368. // image. For example:
  369. //
  370. // <picture r:id="rId1"/>
  371. //
  372. type xlsxPicture struct {
  373. RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  374. }
  375. // xlsxLegacyDrawing directly maps the legacyDrawing element in the namespace
  376. // http://schemas.openxmlformats.org/spreadsheetml/2006/main - A comment is a
  377. // rich text note that is attached to, and associated with, a cell, separate
  378. // from other cell content. Comment content is stored separate from the cell,
  379. // and is displayed in a drawing object (like a text box) that is separate from,
  380. // but associated with, a cell. Comments are used as reminders, such as noting
  381. // how a complex formula works, or to provide feedback to other users. Comments
  382. // can also be used to explain assumptions made in a formula or to call out
  383. // something special about the cell.
  384. type xlsxLegacyDrawing struct {
  385. RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
  386. }