xmlWorksheet.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. }
  100. // xlsxSheetViews directly maps the sheetViews element in the namespace
  101. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  102. // currently I have not checked it for completeness - it does as much
  103. // as I need.
  104. type xlsxSheetViews struct {
  105. SheetView []xlsxSheetView `xml:"sheetView"`
  106. }
  107. // xlsxSheetView directly maps the sheetView element in the namespace
  108. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  109. // currently I have not checked it for completeness - it does as much
  110. // as I need.
  111. type xlsxSheetView struct {
  112. WindowProtection bool `xml:"windowProtection,attr"`
  113. ShowFormulas bool `xml:"showFormulas,attr"`
  114. ShowGridLines bool `xml:"showGridLines,attr"`
  115. ShowRowColHeaders bool `xml:"showRowColHeaders,attr"`
  116. ShowZeros bool `xml:"showZeros,attr"`
  117. RightToLeft bool `xml:"rightToLeft,attr"`
  118. TabSelected bool `xml:"tabSelected,attr"`
  119. ShowOutlineSymbols bool `xml:"showOutlineSymbols,attr"`
  120. DefaultGridColor bool `xml:"defaultGridColor,attr"`
  121. View string `xml:"view,attr"`
  122. TopLeftCell string `xml:"topLeftCell,attr"`
  123. ColorId int `xml:"colorId,attr"`
  124. ZoomScale float64 `xml:"zoomScale,attr"`
  125. ZoomScaleNormal float64 `xml:"zoomScaleNormal,attr"`
  126. ZoomScalePageLayoutView float64 `xml:"zoomScalePageLayoutView,attr"`
  127. WorkbookViewId int `xml:"workbookViewId,attr"`
  128. Selection []xlsxSelection `xml:"selection"`
  129. Pane *xlsxPane `xml:"pane"`
  130. }
  131. // xlsxSelection directly maps the selection element in the namespace
  132. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  133. // currently I have not checked it for completeness - it does as much
  134. // as I need.
  135. type xlsxSelection struct {
  136. Pane string `xml:"pane,attr"`
  137. ActiveCell string `xml:"activeCell,attr"`
  138. ActiveCellId int `xml:"activeCellId,attr"`
  139. SQRef string `xml:"sqref,attr"`
  140. }
  141. // xlsxSelection directly maps the selection element in the namespace
  142. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  143. // currently I have not checked it for completeness - it does as much
  144. // as I need.
  145. type xlsxPane struct {
  146. XSplit float64 `xml:"xSplit,attr"`
  147. YSplit float64 `xml:"ySplit,attr"`
  148. TopLeftCell string `xml:"topLeftCell,attr"`
  149. ActivePane string `xml:"activePane,attr"`
  150. State string `xml:"state,attr"` // Either "split" or "frozen"
  151. }
  152. // xlsxSheetPr directly maps the sheetPr element in the namespace
  153. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  154. // currently I have not checked it for completeness - it does as much
  155. // as I need.
  156. type xlsxSheetPr struct {
  157. FilterMode bool `xml:"filterMode,attr"`
  158. PageSetUpPr []xlsxPageSetUpPr `xml:"pageSetUpPr"`
  159. }
  160. // xlsxPageSetUpPr directly maps the pageSetupPr element in the namespace
  161. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  162. // currently I have not checked it for completeness - it does as much
  163. // as I need.
  164. type xlsxPageSetUpPr struct {
  165. FitToPage bool `xml:"fitToPage,attr"`
  166. }
  167. // xlsxCols directly maps the cols element in the namespace
  168. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  169. // currently I have not checked it for completeness - it does as much
  170. // as I need.
  171. type xlsxCols struct {
  172. Col []xlsxCol `xml:"col"`
  173. }
  174. // xlsxCol directly maps the col element in the namespace
  175. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  176. // currently I have not checked it for completeness - it does as much
  177. // as I need.
  178. type xlsxCol struct {
  179. Collapsed bool `xml:"collapsed,attr"`
  180. Hidden bool `xml:"hidden,attr"`
  181. Max int `xml:"max,attr"`
  182. Min int `xml:"min,attr"`
  183. Style int `xml:"style,attr"`
  184. Width float64 `xml:"width,attr"`
  185. CustomWidth int `xml:"customWidth,attr,omitempty"`
  186. }
  187. // xlsxDimension directly maps the dimension element in the namespace
  188. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  189. // currently I have not checked it for completeness - it does as much
  190. // as I need.
  191. type xlsxDimension struct {
  192. Ref string `xml:"ref,attr"`
  193. }
  194. // xlsxSheetData directly maps the sheetData element in the namespace
  195. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  196. // currently I have not checked it for completeness - it does as much
  197. // as I need.
  198. type xlsxSheetData struct {
  199. XMLName xml.Name `xml:"sheetData"`
  200. Row []xlsxRow `xml:"row"`
  201. }
  202. // xlsxRow directly maps the row element in the namespace
  203. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  204. // currently I have not checked it for completeness - it does as much
  205. // as I need.
  206. type xlsxRow struct {
  207. R int `xml:"r,attr"`
  208. Spans string `xml:"spans,attr,omitempty"`
  209. Hidden bool `xml:"hidden,attr,omitempty"`
  210. C []xlsxC `xml:"c"`
  211. Ht string `xml:"ht,attr,omitempty"`
  212. CustomHeight bool `xml:"customHeight,attr,omitempty"`
  213. }
  214. type xlsxMergeCell struct {
  215. Ref string `xml:"ref,attr"` // ref: horiz "A1:C1", vert "B3:B6", both "D3:G4"
  216. }
  217. type xlsxMergeCells struct {
  218. XMLName xml.Name //`xml:"mergeCells,omitempty"`
  219. Count int `xml:"count,attr,omitempty"`
  220. Cells []xlsxMergeCell `xml:"mergeCell,omitempty"`
  221. }
  222. // Return the cartesian extent of a merged cell range from its origin
  223. // cell (the closest merged cell to the to left of the sheet.
  224. func (mc *xlsxMergeCells) getExtent(cellRef string) (int, int, error) {
  225. if mc == nil {
  226. return 0, 0, nil
  227. }
  228. for _, cell := range mc.Cells {
  229. if strings.HasPrefix(cell.Ref, cellRef) {
  230. parts := strings.Split(cell.Ref, ":")
  231. startx, starty, err := getCoordsFromCellIDString(parts[0])
  232. if err != nil {
  233. return -1, -1, err
  234. }
  235. endx, endy, err := getCoordsFromCellIDString(parts[1])
  236. if err != nil {
  237. return -2, -2, err
  238. }
  239. return endx - startx, endy - starty, nil
  240. }
  241. }
  242. return 0, 0, nil
  243. }
  244. // xlsxC directly maps the c element in the namespace
  245. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  246. // currently I have not checked it for completeness - it does as much
  247. // as I need.
  248. type xlsxC struct {
  249. R string `xml:"r,attr"` // Cell ID, e.g. A1
  250. S int `xml:"s,attr,omitempty"` // Style reference.
  251. T string `xml:"t,attr,omitempty"` // Type.
  252. V string `xml:"v,omitempty"` // Value
  253. F *xlsxF `xml:"f,omitempty"` // Formula
  254. }
  255. // xlsxF directly maps the f element in the namespace
  256. // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
  257. // currently I have not checked it for completeness - it does as much
  258. // as I need.
  259. type xlsxF struct {
  260. Content string `xml:",chardata"`
  261. T string `xml:"t,attr,omitempty"` // Formula type
  262. Ref string `xml:"ref,attr,omitempty"` // Shared formula ref
  263. Si int `xml:"si,attr,omitempty"` // Shared formula index
  264. }
  265. // Create a new XLSX Worksheet with default values populated.
  266. // Strictly for internal use only!
  267. func newXlsxWorksheet() (worksheet *xlsxWorksheet) {
  268. worksheet = &xlsxWorksheet{}
  269. worksheet.SheetPr.FilterMode = false
  270. worksheet.SheetPr.PageSetUpPr = make([]xlsxPageSetUpPr, 1)
  271. worksheet.SheetPr.PageSetUpPr[0] = xlsxPageSetUpPr{FitToPage: false}
  272. worksheet.SheetViews.SheetView = make([]xlsxSheetView, 1)
  273. worksheet.SheetViews.SheetView[0] = xlsxSheetView{
  274. ColorId: 64,
  275. DefaultGridColor: true,
  276. RightToLeft: false,
  277. Selection: make([]xlsxSelection, 1),
  278. ShowFormulas: false,
  279. ShowGridLines: true,
  280. ShowOutlineSymbols: true,
  281. ShowRowColHeaders: true,
  282. ShowZeros: true,
  283. TabSelected: false,
  284. TopLeftCell: "A1",
  285. View: "normal",
  286. WindowProtection: false,
  287. WorkbookViewId: 0,
  288. ZoomScale: 100,
  289. ZoomScaleNormal: 100,
  290. ZoomScalePageLayoutView: 100}
  291. worksheet.SheetViews.SheetView[0].Selection[0] = xlsxSelection{
  292. Pane: "topLeft",
  293. ActiveCell: "A1",
  294. ActiveCellId: 0,
  295. SQRef: "A1"}
  296. worksheet.SheetFormatPr.DefaultRowHeight = 12.85
  297. worksheet.PrintOptions.Headings = false
  298. worksheet.PrintOptions.GridLines = false
  299. worksheet.PrintOptions.GridLinesSet = true
  300. worksheet.PrintOptions.HorizontalCentered = false
  301. worksheet.PrintOptions.VerticalCentered = false
  302. worksheet.PageMargins.Left = 0.7875
  303. worksheet.PageMargins.Right = 0.7875
  304. worksheet.PageMargins.Top = 1.05277777777778
  305. worksheet.PageMargins.Bottom = 1.05277777777778
  306. worksheet.PageMargins.Header = 0.7875
  307. worksheet.PageMargins.Footer = 0.7875
  308. worksheet.PageSetUp.PaperSize = "9"
  309. worksheet.PageSetUp.Scale = 100
  310. worksheet.PageSetUp.FirstPageNumber = 1
  311. worksheet.PageSetUp.FitToWidth = 1
  312. worksheet.PageSetUp.FitToHeight = 1
  313. worksheet.PageSetUp.PageOrder = "downThenOver"
  314. worksheet.PageSetUp.Orientation = "portrait"
  315. worksheet.PageSetUp.UsePrinterDefaults = false
  316. worksheet.PageSetUp.BlackAndWhite = false
  317. worksheet.PageSetUp.Draft = false
  318. worksheet.PageSetUp.CellComments = "none"
  319. worksheet.PageSetUp.UseFirstPageNumber = true
  320. worksheet.PageSetUp.HorizontalDPI = 300
  321. worksheet.PageSetUp.VerticalDPI = 300
  322. worksheet.PageSetUp.Copies = 1
  323. worksheet.HeaderFooter.OddHeader = make([]xlsxOddHeader, 1)
  324. worksheet.HeaderFooter.OddHeader[0] = xlsxOddHeader{Content: `&C&"Times New Roman,Regular"&12&A`}
  325. worksheet.HeaderFooter.OddFooter = make([]xlsxOddFooter, 1)
  326. worksheet.HeaderFooter.OddFooter[0] = xlsxOddFooter{Content: `&C&"Times New Roman,Regular"&12Page &P`}
  327. return
  328. }