xmlWorksheet.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package xlsx
  2. import (
  3. "encoding/xml"
  4. "strings"
  5. )
  6. // xlsxWorksheet directly maps the worksheet element in the namespace
  7. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  8. // currently I have not checked it for completeness - it does as much
  9. // as I need.
  10. type xlsxWorksheet struct {
  11. XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main worksheet"`
  12. SheetPr xlsxSheetPr `xml:"sheetPr"`
  13. Dimension xlsxDimension `xml:"dimension"`
  14. SheetViews xlsxSheetViews `xml:"sheetViews"`
  15. SheetFormatPr xlsxSheetFormatPr `xml:"sheetFormatPr"`
  16. Cols *xlsxCols `xml:"cols,omitempty"`
  17. SheetData xlsxSheetData `xml:"sheetData"`
  18. AutoFilter *xlsxAutoFilter `xml:"autoFilter,omitempty"`
  19. MergeCells *xlsxMergeCells `xml:"mergeCells,omitempty"`
  20. PrintOptions xlsxPrintOptions `xml:"printOptions"`
  21. PageMargins xlsxPageMargins `xml:"pageMargins"`
  22. PageSetUp xlsxPageSetUp `xml:"pageSetup"`
  23. HeaderFooter xlsxHeaderFooter `xml:"headerFooter"`
  24. }
  25. // xlsxHeaderFooter directly maps the headerFooter element in the namespace
  26. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  27. // currently I have not checked it for completeness - it does as much
  28. // as I need.
  29. type xlsxHeaderFooter struct {
  30. DifferentFirst bool `xml:"differentFirst,attr"`
  31. DifferentOddEven bool `xml:"differentOddEven,attr"`
  32. OddHeader []xlsxOddHeader `xml:"oddHeader"`
  33. OddFooter []xlsxOddFooter `xml:"oddFooter"`
  34. }
  35. // xlsxOddHeader directly maps the oddHeader element in the namespace
  36. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  37. // currently I have not checked it for completeness - it does as much
  38. // as I need.
  39. type xlsxOddHeader struct {
  40. Content string `xml:",chardata"`
  41. }
  42. // xlsxOddFooter directly maps the oddFooter element in the namespace
  43. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  44. // currently I have not checked it for completeness - it does as much
  45. // as I need.
  46. type xlsxOddFooter struct {
  47. Content string `xml:",chardata"`
  48. }
  49. // xlsxPageSetUp directly maps the pageSetup element in the namespace
  50. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  51. // currently I have not checked it for completeness - it does as much
  52. // as I need.
  53. type xlsxPageSetUp struct {
  54. PaperSize string `xml:"paperSize,attr"`
  55. Scale int `xml:"scale,attr"`
  56. FirstPageNumber int `xml:"firstPageNumber,attr"`
  57. FitToWidth int `xml:"fitToWidth,attr"`
  58. FitToHeight int `xml:"fitToHeight,attr"`
  59. PageOrder string `xml:"pageOrder,attr"`
  60. Orientation string `xml:"orientation,attr"`
  61. UsePrinterDefaults bool `xml:"usePrinterDefaults,attr"`
  62. BlackAndWhite bool `xml:"blackAndWhite,attr"`
  63. Draft bool `xml:"draft,attr"`
  64. CellComments string `xml:"cellComments,attr"`
  65. UseFirstPageNumber bool `xml:"useFirstPageNumber,attr"`
  66. HorizontalDPI float32 `xml:"horizontalDpi,attr"`
  67. VerticalDPI float32 `xml:"verticalDpi,attr"`
  68. Copies int `xml:"copies,attr"`
  69. }
  70. // xlsxPrintOptions directly maps the printOptions element in the namespace
  71. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  72. // currently I have not checked it for completeness - it does as much
  73. // as I need.
  74. type xlsxPrintOptions struct {
  75. Headings bool `xml:"headings,attr"`
  76. GridLines bool `xml:"gridLines,attr"`
  77. GridLinesSet bool `xml:"gridLinesSet,attr"`
  78. HorizontalCentered bool `xml:"horizontalCentered,attr"`
  79. VerticalCentered bool `xml:"verticalCentered,attr"`
  80. }
  81. // xlsxPageMargins directly maps the pageMargins element in the namespace
  82. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  83. // currently I have not checked it for completeness - it does as much
  84. // as I need.
  85. type xlsxPageMargins struct {
  86. Left float64 `xml:"left,attr"`
  87. Right float64 `xml:"right,attr"`
  88. Top float64 `xml:"top,attr"`
  89. Bottom float64 `xml:"bottom,attr"`
  90. Header float64 `xml:"header,attr"`
  91. Footer float64 `xml:"footer,attr"`
  92. }
  93. // xlsxSheetFormatPr directly maps the sheetFormatPr element in the namespace
  94. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  95. // currently I have not checked it for completeness - it does as much
  96. // as I need.
  97. type xlsxSheetFormatPr struct {
  98. DefaultColWidth float64 `xml:"defaultColWidth,attr,omitempty"`
  99. DefaultRowHeight float64 `xml:"defaultRowHeight,attr"`
  100. OutlineLevelCol uint8 `xml:"outlineLevelCol,attr,omitempty"`
  101. OutlineLevelRow uint8 `xml:"outlineLevelRow,attr,omitempty"`
  102. }
  103. // xlsxSheetViews directly maps the sheetViews element in the namespace
  104. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  105. // currently I have not checked it for completeness - it does as much
  106. // as I need.
  107. type xlsxSheetViews struct {
  108. SheetView []xlsxSheetView `xml:"sheetView"`
  109. }
  110. // xlsxSheetView directly maps the sheetView element in the namespace
  111. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  112. // currently I have not checked it for completeness - it does as much
  113. // as I need.
  114. type xlsxSheetView struct {
  115. WindowProtection bool `xml:"windowProtection,attr"`
  116. ShowFormulas bool `xml:"showFormulas,attr"`
  117. ShowGridLines bool `xml:"showGridLines,attr"`
  118. ShowRowColHeaders bool `xml:"showRowColHeaders,attr"`
  119. ShowZeros bool `xml:"showZeros,attr"`
  120. RightToLeft bool `xml:"rightToLeft,attr"`
  121. TabSelected bool `xml:"tabSelected,attr"`
  122. ShowOutlineSymbols bool `xml:"showOutlineSymbols,attr"`
  123. DefaultGridColor bool `xml:"defaultGridColor,attr"`
  124. View string `xml:"view,attr"`
  125. TopLeftCell string `xml:"topLeftCell,attr"`
  126. ColorId int `xml:"colorId,attr"`
  127. ZoomScale float64 `xml:"zoomScale,attr"`
  128. ZoomScaleNormal float64 `xml:"zoomScaleNormal,attr"`
  129. ZoomScalePageLayoutView float64 `xml:"zoomScalePageLayoutView,attr"`
  130. WorkbookViewId int `xml:"workbookViewId,attr"`
  131. Pane *xlsxPane `xml:"pane"`
  132. Selection []xlsxSelection `xml:"selection"`
  133. }
  134. // xlsxSelection directly maps the selection element in the namespace
  135. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  136. // currently I have not checked it for completeness - it does as much
  137. // as I need.
  138. type xlsxSelection struct {
  139. Pane string `xml:"pane,attr"`
  140. ActiveCell string `xml:"activeCell,attr"`
  141. ActiveCellId int `xml:"activeCellId,attr"`
  142. SQRef string `xml:"sqref,attr"`
  143. }
  144. // xlsxSelection directly maps the selection element in the namespace
  145. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  146. // currently I have not checked it for completeness - it does as much
  147. // as I need.
  148. type xlsxPane struct {
  149. XSplit float64 `xml:"xSplit,attr"`
  150. YSplit float64 `xml:"ySplit,attr"`
  151. TopLeftCell string `xml:"topLeftCell,attr"`
  152. ActivePane string `xml:"activePane,attr"`
  153. State string `xml:"state,attr"` // Either "split" or "frozen"
  154. }
  155. // xlsxSheetPr directly maps the sheetPr element in the namespace
  156. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  157. // currently I have not checked it for completeness - it does as much
  158. // as I need.
  159. type xlsxSheetPr struct {
  160. FilterMode bool `xml:"filterMode,attr"`
  161. PageSetUpPr []xlsxPageSetUpPr `xml:"pageSetUpPr"`
  162. }
  163. // xlsxPageSetUpPr directly maps the pageSetupPr element in the namespace
  164. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  165. // currently I have not checked it for completeness - it does as much
  166. // as I need.
  167. type xlsxPageSetUpPr struct {
  168. FitToPage bool `xml:"fitToPage,attr"`
  169. }
  170. // xlsxCols directly maps the cols element in the namespace
  171. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  172. // currently I have not checked it for completeness - it does as much
  173. // as I need.
  174. type xlsxCols struct {
  175. Col []xlsxCol `xml:"col"`
  176. }
  177. // xlsxCol directly maps the col element in the namespace
  178. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  179. // currently I have not checked it for completeness - it does as much
  180. // as I need.
  181. type xlsxCol struct {
  182. Collapsed bool `xml:"collapsed,attr"`
  183. Hidden bool `xml:"hidden,attr"`
  184. Max int `xml:"max,attr"`
  185. Min int `xml:"min,attr"`
  186. Style int `xml:"style,attr"`
  187. Width float64 `xml:"width,attr"`
  188. CustomWidth bool `xml:"customWidth,attr,omitempty"`
  189. OutlineLevel uint8 `xml:"outlineLevel,attr,omitempty"`
  190. }
  191. // xlsxDimension directly maps the dimension element in the namespace
  192. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  193. // currently I have not checked it for completeness - it does as much
  194. // as I need.
  195. type xlsxDimension struct {
  196. Ref string `xml:"ref,attr"`
  197. }
  198. // xlsxSheetData directly maps the sheetData element in the namespace
  199. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  200. // currently I have not checked it for completeness - it does as much
  201. // as I need.
  202. type xlsxSheetData struct {
  203. XMLName xml.Name `xml:"sheetData"`
  204. Row []xlsxRow `xml:"row"`
  205. }
  206. // xlsxRow directly maps the row element in the namespace
  207. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  208. // currently I have not checked it for completeness - it does as much
  209. // as I need.
  210. type xlsxRow struct {
  211. R int `xml:"r,attr"`
  212. Spans string `xml:"spans,attr,omitempty"`
  213. Hidden bool `xml:"hidden,attr,omitempty"`
  214. C []xlsxC `xml:"c"`
  215. Ht string `xml:"ht,attr,omitempty"`
  216. CustomHeight bool `xml:"customHeight,attr,omitempty"`
  217. OutlineLevel uint8 `xml:"outlineLevel,attr,omitempty"`
  218. }
  219. type xlsxAutoFilter struct {
  220. Ref string `xml:"ref,attr"`
  221. }
  222. type xlsxMergeCell struct {
  223. Ref string `xml:"ref,attr"` // ref: horiz "A1:C1", vert "B3:B6", both "D3:G4"
  224. }
  225. type xlsxMergeCells struct {
  226. XMLName xml.Name //`xml:"mergeCells,omitempty"`
  227. Count int `xml:"count,attr,omitempty"`
  228. Cells []xlsxMergeCell `xml:"mergeCell,omitempty"`
  229. }
  230. // Return the cartesian extent of a merged cell range from its origin
  231. // cell (the closest merged cell to the to left of the sheet.
  232. func (mc *xlsxMergeCells) getExtent(cellRef string) (int, int, error) {
  233. if mc == nil {
  234. return 0, 0, nil
  235. }
  236. for _, cell := range mc.Cells {
  237. if strings.HasPrefix(cell.Ref, cellRef+":") {
  238. parts := strings.Split(cell.Ref, ":")
  239. startx, starty, err := GetCoordsFromCellIDString(parts[0])
  240. if err != nil {
  241. return -1, -1, err
  242. }
  243. endx, endy, err := GetCoordsFromCellIDString(parts[1])
  244. if err != nil {
  245. return -2, -2, err
  246. }
  247. return endx - startx, endy - starty, nil
  248. }
  249. }
  250. return 0, 0, nil
  251. }
  252. // xlsxC directly maps the c element in the namespace
  253. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  254. // currently I have not checked it for completeness - it does as much
  255. // as I need.
  256. type xlsxC struct {
  257. R string `xml:"r,attr"` // Cell ID, e.g. A1
  258. S int `xml:"s,attr,omitempty"` // Style reference.
  259. T string `xml:"t,attr,omitempty"` // Type.
  260. F *xlsxF `xml:"f,omitempty"` // Formula
  261. V string `xml:"v,omitempty"` // Value
  262. Is *xlsxSI `xml:"is,omitempty"` // Inline String.
  263. }
  264. // xlsxF directly maps the f element in the namespace
  265. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  266. // currently I have not checked it for completeness - it does as much
  267. // as I need.
  268. type xlsxF struct {
  269. Content string `xml:",chardata"`
  270. T string `xml:"t,attr,omitempty"` // Formula type
  271. Ref string `xml:"ref,attr,omitempty"` // Shared formula ref
  272. Si int `xml:"si,attr,omitempty"` // Shared formula index
  273. }
  274. // Create a new XLSX Worksheet with default values populated.
  275. // Strictly for internal use only!
  276. func newXlsxWorksheet() (worksheet *xlsxWorksheet) {
  277. worksheet = &xlsxWorksheet{}
  278. worksheet.SheetPr.FilterMode = false
  279. worksheet.SheetPr.PageSetUpPr = make([]xlsxPageSetUpPr, 1)
  280. worksheet.SheetPr.PageSetUpPr[0] = xlsxPageSetUpPr{FitToPage: false}
  281. worksheet.SheetViews.SheetView = make([]xlsxSheetView, 1)
  282. worksheet.SheetViews.SheetView[0] = xlsxSheetView{
  283. ColorId: 64,
  284. DefaultGridColor: true,
  285. RightToLeft: false,
  286. Selection: make([]xlsxSelection, 1),
  287. ShowFormulas: false,
  288. ShowGridLines: true,
  289. ShowOutlineSymbols: true,
  290. ShowRowColHeaders: true,
  291. ShowZeros: true,
  292. TabSelected: false,
  293. TopLeftCell: "A1",
  294. View: "normal",
  295. WindowProtection: false,
  296. WorkbookViewId: 0,
  297. ZoomScale: 100,
  298. ZoomScaleNormal: 100,
  299. ZoomScalePageLayoutView: 100}
  300. worksheet.SheetViews.SheetView[0].Selection[0] = xlsxSelection{
  301. Pane: "topLeft",
  302. ActiveCell: "A1",
  303. ActiveCellId: 0,
  304. SQRef: "A1"}
  305. worksheet.SheetFormatPr.DefaultRowHeight = 12.85
  306. worksheet.PrintOptions.Headings = false
  307. worksheet.PrintOptions.GridLines = false
  308. worksheet.PrintOptions.GridLinesSet = true
  309. worksheet.PrintOptions.HorizontalCentered = false
  310. worksheet.PrintOptions.VerticalCentered = false
  311. worksheet.PageMargins.Left = 0.7875
  312. worksheet.PageMargins.Right = 0.7875
  313. worksheet.PageMargins.Top = 1.05277777777778
  314. worksheet.PageMargins.Bottom = 1.05277777777778
  315. worksheet.PageMargins.Header = 0.7875
  316. worksheet.PageMargins.Footer = 0.7875
  317. worksheet.PageSetUp.PaperSize = "9"
  318. worksheet.PageSetUp.Scale = 100
  319. worksheet.PageSetUp.FirstPageNumber = 1
  320. worksheet.PageSetUp.FitToWidth = 1
  321. worksheet.PageSetUp.FitToHeight = 1
  322. worksheet.PageSetUp.PageOrder = "downThenOver"
  323. worksheet.PageSetUp.Orientation = "portrait"
  324. worksheet.PageSetUp.UsePrinterDefaults = false
  325. worksheet.PageSetUp.BlackAndWhite = false
  326. worksheet.PageSetUp.Draft = false
  327. worksheet.PageSetUp.CellComments = "none"
  328. worksheet.PageSetUp.UseFirstPageNumber = true
  329. worksheet.PageSetUp.HorizontalDPI = 300
  330. worksheet.PageSetUp.VerticalDPI = 300
  331. worksheet.PageSetUp.Copies = 1
  332. worksheet.HeaderFooter.OddHeader = make([]xlsxOddHeader, 1)
  333. worksheet.HeaderFooter.OddHeader[0] = xlsxOddHeader{Content: `&C&"Times New Roman,Regular"&12&A`}
  334. worksheet.HeaderFooter.OddFooter = make([]xlsxOddFooter, 1)
  335. worksheet.HeaderFooter.OddFooter[0] = xlsxOddFooter{Content: `&C&"Times New Roman,Regular"&12Page &P`}
  336. return
  337. }