xmlWorksheet_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package xlsx
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. . "gopkg.in/check.v1"
  6. )
  7. type WorksheetSuite struct{}
  8. var _ = Suite(&WorksheetSuite{})
  9. // Test we can succesfully unmarshal the sheetN.xml files within and
  10. // XLSX file into an xlsxWorksheet struct (and it's related children).
  11. func (w *WorksheetSuite) TestUnmarshallWorksheet(c *C) {
  12. var sheetxml = bytes.NewBufferString(
  13. `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  14. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  15. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  16. <sheetPr filterMode="false">
  17. <pageSetUpPr fitToPage="false"/>
  18. </sheetPr>
  19. <dimension ref="A1:B2"/>
  20. <sheetViews>
  21. <sheetView colorId="64"
  22. defaultGridColor="true"
  23. rightToLeft="false"
  24. showFormulas="false"
  25. showGridLines="true"
  26. showOutlineSymbols="true"
  27. showRowColHeaders="true"
  28. showZeros="true"
  29. tabSelected="true"
  30. topLeftCell="A1"
  31. view="normal"
  32. windowProtection="false"
  33. workbookViewId="0"
  34. zoomScale="100"
  35. zoomScaleNormal="100"
  36. zoomScalePageLayoutView="100">
  37. <selection activeCell="B2"
  38. activeCellId="0"
  39. pane="topLeft"
  40. sqref="B2"/>
  41. </sheetView>
  42. </sheetViews>
  43. <sheetFormatPr defaultRowHeight="15" defaultColWidth="8">
  44. </sheetFormatPr>
  45. <cols>
  46. <col collapsed="false"
  47. hidden="false"
  48. max="1025"
  49. min="1"
  50. style="0"
  51. width="10.5748987854251"/>
  52. </cols>
  53. <sheetData>
  54. <row collapsed="false"
  55. customFormat="false"
  56. customHeight="false"
  57. hidden="false"
  58. ht="14.9"
  59. outlineLevel="0"
  60. r="1">
  61. <c r="A1"
  62. s="1"
  63. t="s">
  64. <v>0</v>
  65. </c>
  66. <c r="B1"
  67. s="0"
  68. t="s">
  69. <v>1</v>
  70. </c>
  71. </row>
  72. <row collapsed="false"
  73. customFormat="false"
  74. customHeight="false"
  75. hidden="false"
  76. ht="14.9"
  77. outlineLevel="0"
  78. r="2">
  79. <c r="A2"
  80. s="0"
  81. t="s">
  82. <v>2</v>
  83. </c>
  84. <c r="B2"
  85. s="2"
  86. t="s">
  87. <v>3</v>
  88. </c>
  89. </row>
  90. </sheetData>
  91. <autoFilter ref="A1:Z4" />
  92. <printOptions headings="false"
  93. gridLines="false"
  94. gridLinesSet="true"
  95. horizontalCentered="false"
  96. verticalCentered="false"/>
  97. <pageMargins left="0.7"
  98. right="0.7"
  99. top="0.7875"
  100. bottom="0.7875"
  101. header="0.511805555555555"
  102. footer="0.511805555555555"/>
  103. <pageSetup blackAndWhite="false"
  104. cellComments="none"
  105. copies="1"
  106. draft="false"
  107. firstPageNumber="0"
  108. fitToHeight="1"
  109. fitToWidth="1"
  110. horizontalDpi="300"
  111. orientation="portrait"
  112. pageOrder="downThenOver"
  113. paperSize="9"
  114. scale="100"
  115. useFirstPageNumber="false"
  116. usePrinterDefaults="false"
  117. verticalDpi="300"/>
  118. <headerFooter differentFirst="false"
  119. differentOddEven="false">
  120. <oddHeader>
  121. </oddHeader>
  122. <oddFooter>
  123. </oddFooter>
  124. </headerFooter>
  125. </worksheet>`)
  126. worksheet := new(xlsxWorksheet)
  127. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  128. c.Assert(err, IsNil)
  129. c.Assert(worksheet.Dimension.Ref, Equals, "A1:B2")
  130. c.Assert(worksheet.SheetData.Row, HasLen, 2)
  131. c.Assert(worksheet.SheetFormatPr.DefaultRowHeight, Equals, 15.0)
  132. c.Assert(worksheet.SheetFormatPr.DefaultColWidth, Equals, 8.0)
  133. row := worksheet.SheetData.Row[0]
  134. c.Assert(row.R, Equals, 1)
  135. c.Assert(row.C, HasLen, 2)
  136. cell := row.C[0]
  137. c.Assert(cell.R, Equals, "A1")
  138. c.Assert(cell.T, Equals, "s")
  139. c.Assert(cell.V, Equals, "0")
  140. c.Assert(worksheet.AutoFilter, NotNil)
  141. c.Assert(worksheet.AutoFilter.Ref, Equals, "A1:Z4")
  142. }
  143. // MergeCells information is correctly read from the worksheet.
  144. func (w *WorksheetSuite) TestUnmarshallWorksheetWithMergeCells(c *C) {
  145. var sheetxml = bytes.NewBufferString(
  146. `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  147. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">
  148. <sheetViews>
  149. <sheetView workbookViewId="0"/>
  150. </sheetViews>
  151. <sheetFormatPr customHeight="1" defaultColWidth="17.29" defaultRowHeight="15.0"/>
  152. <cols>
  153. <col customWidth="1" min="1" max="6" width="14.43"/>
  154. </cols>
  155. <sheetData>
  156. <row r="1" ht="15.75" customHeight="1">
  157. <c r="A1" s="1" t="s">
  158. <v>0</v>
  159. </c>
  160. </row>
  161. <row r="2" ht="15.75" customHeight="1">
  162. <c r="A2" s="1" t="s">
  163. <v>1</v>
  164. </c>
  165. <c r="B2" s="1" t="s">
  166. <v>2</v>
  167. </c>
  168. </row>
  169. </sheetData>
  170. <mergeCells count="1">
  171. <mergeCell ref="A1:B1"/>
  172. </mergeCells>
  173. <drawing r:id="rId1"/>
  174. </worksheet>
  175. `)
  176. worksheet := new(xlsxWorksheet)
  177. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  178. c.Assert(err, IsNil)
  179. c.Assert(worksheet.MergeCells, NotNil)
  180. c.Assert(worksheet.MergeCells.Count, Equals, 1)
  181. mergeCell := worksheet.MergeCells.Cells[0]
  182. c.Assert(mergeCell.Ref, Equals, "A1:B1")
  183. }
  184. // MergeCells.getExtents returns the horizontal and vertical extent of
  185. // a merge that begins at a given reference.
  186. func (w *WorksheetSuite) TestMergeCellsGetExtent(c *C) {
  187. mc := xlsxMergeCells{Count: 2}
  188. mc.Cells = make([]xlsxMergeCell, 2)
  189. mc.Cells[0] = xlsxMergeCell{Ref: "A11:A12"}
  190. mc.Cells[1] = xlsxMergeCell{Ref: "A1:C5"}
  191. h, v, err := mc.getExtent("A1")
  192. c.Assert(err, IsNil)
  193. c.Assert(h, Equals, 2)
  194. c.Assert(v, Equals, 4)
  195. h, v, err = mc.getExtent("A11")
  196. c.Assert(err, IsNil)
  197. c.Assert(h, Equals, 0)
  198. c.Assert(v, Equals, 1)
  199. }