xmlWorksheet_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. <printOptions headings="false"
  92. gridLines="false"
  93. gridLinesSet="true"
  94. horizontalCentered="false"
  95. verticalCentered="false"/>
  96. <pageMargins left="0.7"
  97. right="0.7"
  98. top="0.7875"
  99. bottom="0.7875"
  100. header="0.511805555555555"
  101. footer="0.511805555555555"/>
  102. <pageSetup blackAndWhite="false"
  103. cellComments="none"
  104. copies="1"
  105. draft="false"
  106. firstPageNumber="0"
  107. fitToHeight="1"
  108. fitToWidth="1"
  109. horizontalDpi="300"
  110. orientation="portrait"
  111. pageOrder="downThenOver"
  112. paperSize="9"
  113. scale="100"
  114. useFirstPageNumber="false"
  115. usePrinterDefaults="false"
  116. verticalDpi="300"/>
  117. <headerFooter differentFirst="false"
  118. differentOddEven="false">
  119. <oddHeader>
  120. </oddHeader>
  121. <oddFooter>
  122. </oddFooter>
  123. </headerFooter>
  124. </worksheet>`)
  125. worksheet := new(xlsxWorksheet)
  126. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  127. c.Assert(err, IsNil)
  128. c.Assert(worksheet.Dimension.Ref, Equals, "A1:B2")
  129. c.Assert(worksheet.SheetData.Row, HasLen, 2)
  130. c.Assert(worksheet.SheetFormatPr.DefaultRowHeight, Equals, 15.0)
  131. c.Assert(worksheet.SheetFormatPr.DefaultColWidth, Equals, 8.0)
  132. row := worksheet.SheetData.Row[0]
  133. c.Assert(row.R, Equals, 1)
  134. c.Assert(row.C, HasLen, 2)
  135. cell := row.C[0]
  136. c.Assert(cell.R, Equals, "A1")
  137. c.Assert(cell.T, Equals, "s")
  138. c.Assert(cell.V, Equals, "0")
  139. }
  140. // MergeCells information is correctly read from the worksheet.
  141. func (w *WorksheetSuite) TestUnmarshallWorksheetWithMergeCells(c *C) {
  142. var sheetxml = bytes.NewBufferString(
  143. `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  144. <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">
  145. <sheetViews>
  146. <sheetView workbookViewId="0"/>
  147. </sheetViews>
  148. <sheetFormatPr customHeight="1" defaultColWidth="17.29" defaultRowHeight="15.0"/>
  149. <cols>
  150. <col customWidth="1" min="1" max="6" width="14.43"/>
  151. </cols>
  152. <sheetData>
  153. <row r="1" ht="15.75" customHeight="1">
  154. <c r="A1" s="1" t="s">
  155. <v>0</v>
  156. </c>
  157. </row>
  158. <row r="2" ht="15.75" customHeight="1">
  159. <c r="A2" s="1" t="s">
  160. <v>1</v>
  161. </c>
  162. <c r="B2" s="1" t="s">
  163. <v>2</v>
  164. </c>
  165. </row>
  166. </sheetData>
  167. <mergeCells count="1">
  168. <mergeCell ref="A1:B1"/>
  169. </mergeCells>
  170. <drawing r:id="rId1"/>
  171. </worksheet>
  172. `)
  173. worksheet := new(xlsxWorksheet)
  174. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  175. c.Assert(err, IsNil)
  176. c.Assert(worksheet.MergeCells, NotNil)
  177. c.Assert(worksheet.MergeCells.Count, Equals, 1)
  178. mergeCell := worksheet.MergeCells.Cells[0]
  179. c.Assert(mergeCell.Ref, Equals, "A1:B1")
  180. }
  181. // MergeCells.getExtents returns the horizontal and vertical extent of
  182. // a merge that begins at a given reference.
  183. func (w *WorksheetSuite) TestMergeCellsGetExtent(c *C) {
  184. mc := xlsxMergeCells{Count: 2}
  185. mc.Cells = make([]xlsxMergeCell, 2)
  186. mc.Cells[0] = xlsxMergeCell{Ref: "A11:A12"}
  187. mc.Cells[1] = xlsxMergeCell{Ref: "A1:C5"}
  188. h, v, err := mc.getExtent("A1")
  189. c.Assert(err, IsNil)
  190. c.Assert(h, Equals, 2)
  191. c.Assert(v, Equals, 4)
  192. h, v, err = mc.getExtent("A11")
  193. c.Assert(err, IsNil)
  194. c.Assert(h, Equals, 0)
  195. c.Assert(v, Equals, 1)
  196. }