xmlWorksheet.go 14 KB

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