lib_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package xlsx
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. // "strconv"
  6. "strings"
  7. . "gopkg.in/check.v1"
  8. )
  9. type LibSuite struct{}
  10. var _ = Suite(&LibSuite{})
  11. // which they are contained from the XLSX file, even when the
  12. // worksheet files have arbitrary, non-numeric names.
  13. func (l *LibSuite) TestReadWorkbookRelationsFromZipFileWithFunnyNames(c *C) {
  14. var xlsxFile *File
  15. var err error
  16. xlsxFile, err = OpenFile("./testdocs/testrels.xlsx")
  17. c.Assert(err, IsNil)
  18. bob := xlsxFile.Sheet["Bob"]
  19. row1 := bob.Rows[0]
  20. cell1 := row1.Cells[0]
  21. c.Assert(cell1.String(), Equals, "I am Bob")
  22. }
  23. // We can marshal WorkBookRels to an xml file
  24. func (l *LibSuite) TestWorkBookRelsMarshal(c *C) {
  25. var rels WorkBookRels = make(WorkBookRels)
  26. rels["rId1"] = "worksheets/sheet.xml"
  27. expectedXML := `<?xml version="1.0" encoding="UTF-8"?>
  28. <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  29. <Relationship Id="rId1" Target="worksheets/sheet.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"></Relationship>
  30. <Relationship Id="rId2" Target="sharedStrings.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"></Relationship>
  31. <Relationship Id="rId3" Target="styles.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"></Relationship>
  32. </Relationships>`
  33. xRels := rels.MakeXLSXWorkbookRels()
  34. output := bytes.NewBufferString(xml.Header)
  35. body, err := xml.MarshalIndent(xRels, " ", " ")
  36. c.Assert(err, IsNil)
  37. c.Assert(body, NotNil)
  38. _, err = output.Write(body)
  39. c.Assert(err, IsNil)
  40. c.Assert(output.String(), Equals, expectedXML)
  41. }
  42. // Excel column codes are a special form of base26 that doesn't allow
  43. // zeros, except in the least significant part of the code. Test we
  44. // can smoosh the numbers in a normal base26 representation (presented
  45. // as a slice of integers) down to this form.
  46. func (l *LibSuite) TestSmooshBase26Slice(c *C) {
  47. input := []int{20, 0, 1}
  48. expected := []int{19, 26, 1}
  49. c.Assert(smooshBase26Slice(input), DeepEquals, expected)
  50. }
  51. // formatColumnName converts slices of base26 integers to alphabetical
  52. // column names. Note that the least signifcant character has a
  53. // different numeric offset (Yuck!)
  54. func (l *LibSuite) TestFormatColumnName(c *C) {
  55. c.Assert(formatColumnName([]int{0}), Equals, "A")
  56. c.Assert(formatColumnName([]int{25}), Equals, "Z")
  57. c.Assert(formatColumnName([]int{1, 25}), Equals, "AZ")
  58. c.Assert(formatColumnName([]int{26, 25}), Equals, "ZZ")
  59. c.Assert(formatColumnName([]int{26, 26, 25}), Equals, "ZZZ")
  60. }
  61. // getLargestDenominator returns the largest power of a provided value
  62. // that can fit within a given value.
  63. func (l *LibSuite) TestGetLargestDenominator(c *C) {
  64. d, p := getLargestDenominator(0, 1, 2, 0)
  65. c.Assert(d, Equals, 1)
  66. c.Assert(p, Equals, 0)
  67. d, p = getLargestDenominator(1, 1, 2, 0)
  68. c.Assert(d, Equals, 1)
  69. c.Assert(p, Equals, 0)
  70. d, p = getLargestDenominator(2, 1, 2, 0)
  71. c.Assert(d, Equals, 2)
  72. c.Assert(p, Equals, 1)
  73. d, p = getLargestDenominator(4, 1, 2, 0)
  74. c.Assert(d, Equals, 4)
  75. c.Assert(p, Equals, 2)
  76. d, p = getLargestDenominator(8, 1, 2, 0)
  77. c.Assert(d, Equals, 8)
  78. c.Assert(p, Equals, 3)
  79. d, p = getLargestDenominator(9, 1, 2, 0)
  80. c.Assert(d, Equals, 8)
  81. c.Assert(p, Equals, 3)
  82. d, p = getLargestDenominator(15, 1, 2, 0)
  83. c.Assert(d, Equals, 8)
  84. c.Assert(p, Equals, 3)
  85. d, p = getLargestDenominator(16, 1, 2, 0)
  86. c.Assert(d, Equals, 16)
  87. c.Assert(p, Equals, 4)
  88. }
  89. func (l *LibSuite) TestLettersToNumeric(c *C) {
  90. cases := map[string]int{"A": 0, "G": 6, "z": 25, "AA": 26, "Az": 51,
  91. "BA": 52, "BZ": 77, "ZA": 26*26 + 0, "ZZ": 26*26 + 25,
  92. "AAA": 26*26 + 26 + 0, "AMI": 1022}
  93. for input, ans := range cases {
  94. output := lettersToNumeric(input)
  95. c.Assert(output, Equals, ans)
  96. }
  97. }
  98. func (l *LibSuite) TestNumericToLetters(c *C) {
  99. cases := map[string]int{
  100. "A": 0,
  101. "G": 6,
  102. "Z": 25,
  103. "AA": 26,
  104. "AZ": 51,
  105. "BA": 52,
  106. "BZ": 77, "ZA": 26 * 26, "ZB": 26*26 + 1,
  107. "ZZ": 26*26 + 25,
  108. "AAA": 26*26 + 26 + 0, "AMI": 1022}
  109. for ans, input := range cases {
  110. output := numericToLetters(input)
  111. c.Assert(output, Equals, ans)
  112. }
  113. }
  114. func (l *LibSuite) TestLetterOnlyMapFunction(c *C) {
  115. var input string = "ABC123"
  116. var output string = strings.Map(letterOnlyMapF, input)
  117. c.Assert(output, Equals, "ABC")
  118. input = "abc123"
  119. output = strings.Map(letterOnlyMapF, input)
  120. c.Assert(output, Equals, "ABC")
  121. }
  122. func (l *LibSuite) TestIntOnlyMapFunction(c *C) {
  123. var input string = "ABC123"
  124. var output string = strings.Map(intOnlyMapF, input)
  125. c.Assert(output, Equals, "123")
  126. }
  127. func (l *LibSuite) TestGetCoordsFromCellIDString(c *C) {
  128. var cellIDString string = "A3"
  129. var x, y int
  130. var err error
  131. x, y, err = getCoordsFromCellIDString(cellIDString)
  132. c.Assert(err, IsNil)
  133. c.Assert(x, Equals, 0)
  134. c.Assert(y, Equals, 2)
  135. }
  136. func (l *LibSuite) TestGetCellIDStringFromCoords(c *C) {
  137. c.Assert(getCellIDStringFromCoords(0, 0), Equals, "A1")
  138. c.Assert(getCellIDStringFromCoords(2, 2), Equals, "C3")
  139. }
  140. func (l *LibSuite) TestGetMaxMinFromDimensionRef(c *C) {
  141. var dimensionRef string = "A1:B2"
  142. var minx, miny, maxx, maxy int
  143. var err error
  144. minx, miny, maxx, maxy, err = getMaxMinFromDimensionRef(dimensionRef)
  145. c.Assert(err, IsNil)
  146. c.Assert(minx, Equals, 0)
  147. c.Assert(miny, Equals, 0)
  148. c.Assert(maxx, Equals, 1)
  149. c.Assert(maxy, Equals, 1)
  150. }
  151. func (l *LibSuite) TestCalculateMaxMinFromWorksheet(c *C) {
  152. var sheetxml = bytes.NewBufferString(`
  153. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  154. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  155. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
  156. xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main"
  157. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  158. xmlns:mv="urn:schemas-microsoft-com:mac:vml"
  159. xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"
  160. xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"
  161. xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">
  162. <sheetViews>
  163. <sheetView workbookViewId="0"/>
  164. </sheetViews>
  165. <sheetFormatPr customHeight="1" defaultColWidth="14.43" defaultRowHeight="15.75"/>
  166. <sheetData>
  167. <row r="1">
  168. <c t="s" s="1" r="A1">
  169. <v>0</v>
  170. </c>
  171. <c t="s" s="1" r="B1">
  172. <v>1</v>
  173. </c>
  174. </row>
  175. <row r="2">
  176. <c t="s" s="1" r="A2">
  177. <v>2</v>
  178. </c>
  179. <c t="s" s="1" r="B2">
  180. <v>3</v>
  181. </c>
  182. </row>
  183. </sheetData>
  184. <drawing r:id="rId1"/>
  185. </worksheet>`)
  186. worksheet := new(xlsxWorksheet)
  187. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  188. c.Assert(err, IsNil)
  189. minx, miny, maxx, maxy, err := calculateMaxMinFromWorksheet(worksheet)
  190. c.Assert(err, IsNil)
  191. c.Assert(minx, Equals, 0)
  192. c.Assert(miny, Equals, 0)
  193. c.Assert(maxx, Equals, 1)
  194. c.Assert(maxy, Equals, 1)
  195. }
  196. func (l *LibSuite) TestGetRangeFromString(c *C) {
  197. var rangeString string
  198. var lower, upper int
  199. var err error
  200. rangeString = "1:3"
  201. lower, upper, err = getRangeFromString(rangeString)
  202. c.Assert(err, IsNil)
  203. c.Assert(lower, Equals, 1)
  204. c.Assert(upper, Equals, 3)
  205. }
  206. func (l *LibSuite) TestMakeRowFromSpan(c *C) {
  207. var rangeString string
  208. var row *Row
  209. var length int
  210. rangeString = "1:3"
  211. row = makeRowFromSpan(rangeString)
  212. length = len(row.Cells)
  213. c.Assert(length, Equals, 3)
  214. rangeString = "5:7" // Note - we ignore lower bound!
  215. row = makeRowFromSpan(rangeString)
  216. length = len(row.Cells)
  217. c.Assert(length, Equals, 7)
  218. rangeString = "1:1"
  219. row = makeRowFromSpan(rangeString)
  220. length = len(row.Cells)
  221. c.Assert(length, Equals, 1)
  222. }
  223. func (l *LibSuite) TestReadRowsFromSheet(c *C) {
  224. var sharedstringsXML = bytes.NewBufferString(`
  225. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  226. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
  227. <si>
  228. <t>Foo</t>
  229. </si>
  230. <si>
  231. <t>Bar</t>
  232. </si>
  233. <si>
  234. <t xml:space="preserve">Baz </t>
  235. </si>
  236. <si>
  237. <t>Quuk</t>
  238. </si>
  239. </sst>`)
  240. var sheetxml = bytes.NewBufferString(`
  241. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  242. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  243. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  244. <dimension ref="A1:B2"/>
  245. <sheetViews>
  246. <sheetView tabSelected="1" workbookViewId="0">
  247. <selection activeCell="C2" sqref="C2"/>
  248. </sheetView>
  249. </sheetViews>
  250. <sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
  251. <sheetData>
  252. <row r="1" spans="1:2">
  253. <c r="A1" t="s">
  254. <v>0</v>
  255. </c>
  256. <c r="B1" t="s">
  257. <v>1</v>
  258. </c>
  259. </row>
  260. <row r="2" spans="1:2">
  261. <c r="A2" t="s">
  262. <v>2</v>
  263. </c>
  264. <c r="B2" t="s">
  265. <v>3</v>
  266. </c>
  267. </row>
  268. </sheetData>
  269. <pageMargins left="0.7" right="0.7"
  270. top="0.78740157499999996"
  271. bottom="0.78740157499999996"
  272. header="0.3"
  273. footer="0.3"/>
  274. </worksheet>`)
  275. worksheet := new(xlsxWorksheet)
  276. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  277. c.Assert(err, IsNil)
  278. sst := new(xlsxSST)
  279. err = xml.NewDecoder(sharedstringsXML).Decode(sst)
  280. c.Assert(err, IsNil)
  281. file := new(File)
  282. file.referenceTable = MakeSharedStringRefTable(sst)
  283. rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
  284. c.Assert(maxRows, Equals, 2)
  285. c.Assert(maxCols, Equals, 2)
  286. row := rows[0]
  287. c.Assert(len(row.Cells), Equals, 2)
  288. cell1 := row.Cells[0]
  289. c.Assert(cell1.Value, Equals, "Foo")
  290. cell2 := row.Cells[1]
  291. c.Assert(cell2.Value, Equals, "Bar")
  292. }
  293. func (l *LibSuite) TestReadRowsFromSheetWithLeadingEmptyRows(c *C) {
  294. var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  295. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="2" uniqueCount="2"><si><t>ABC</t></si><si><t>DEF</t></si></sst>`)
  296. var sheetxml = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  297. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
  298. <dimension ref="A4:A5"/>
  299. <sheetViews>
  300. <sheetView tabSelected="1" workbookViewId="0">
  301. <selection activeCell="A2" sqref="A2"/>
  302. </sheetView>
  303. </sheetViews>
  304. <sheetFormatPr baseColWidth="10" defaultRowHeight="15" x14ac:dyDescent="0"/>
  305. <sheetData>
  306. <row r="4" spans="1:1">
  307. <c r="A4" t="s">
  308. <v>0</v>
  309. </c>
  310. </row>
  311. <row r="5" spans="1:1">
  312. <c r="A5" t="s">
  313. <v>1</v>
  314. </c>
  315. </row>
  316. </sheetData>
  317. <pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5"/>
  318. <pageSetup paperSize="9" orientation="portrait" horizontalDpi="4294967292" verticalDpi="4294967292"/>
  319. <extLst>
  320. <ext uri="{64002731-A6B0-56B0-2670-7721B7C09600}" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main">
  321. <mx:PLV Mode="0" OnePage="0" WScale="0"/>
  322. </ext>
  323. </extLst>
  324. </worksheet>
  325. `)
  326. worksheet := new(xlsxWorksheet)
  327. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  328. c.Assert(err, IsNil)
  329. sst := new(xlsxSST)
  330. err = xml.NewDecoder(sharedstringsXML).Decode(sst)
  331. c.Assert(err, IsNil)
  332. file := new(File)
  333. file.referenceTable = MakeSharedStringRefTable(sst)
  334. _, maxCols, maxRows := readRowsFromSheet(worksheet, file)
  335. c.Assert(maxRows, Equals, 2)
  336. c.Assert(maxCols, Equals, 1)
  337. }
  338. func (l *LibSuite) TestReadRowsFromSheetWithEmptyCells(c *C) {
  339. var sharedstringsXML = bytes.NewBufferString(`
  340. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  341. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="8" uniqueCount="5">
  342. <si>
  343. <t>Bob</t>
  344. </si>
  345. <si>
  346. <t>Alice</t>
  347. </si>
  348. <si>
  349. <t>Sue</t>
  350. </si>
  351. <si>
  352. <t>Yes</t>
  353. </si>
  354. <si>
  355. <t>No</t>
  356. </si>
  357. </sst>
  358. `)
  359. var sheetxml = bytes.NewBufferString(`
  360. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  361. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:C3"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="D3" sqref="D3"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
  362. <sheetData>
  363. <row r="1" spans="1:3">
  364. <c r="A1" t="s">
  365. <v>
  366. 0
  367. </v>
  368. </c>
  369. <c r="B1" t="s">
  370. <v>
  371. 1
  372. </v>
  373. </c>
  374. <c r="C1" t="s">
  375. <v>
  376. 2
  377. </v>
  378. </c>
  379. </row>
  380. <row r="2" spans="1:3">
  381. <c r="A2" t="s">
  382. <v>
  383. 3
  384. </v>
  385. </c>
  386. <c r="B2" t="s">
  387. <v>
  388. 4
  389. </v>
  390. </c>
  391. <c r="C2" t="s">
  392. <v>
  393. 3
  394. </v>
  395. </c>
  396. </row>
  397. <row r="3" spans="1:3">
  398. <c r="A3" t="s">
  399. <v>
  400. 4
  401. </v>
  402. </c>
  403. <c r="C3" t="s">
  404. <v>
  405. 3
  406. </v>
  407. </c>
  408. </row>
  409. </sheetData>
  410. <pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
  411. </worksheet>
  412. `)
  413. worksheet := new(xlsxWorksheet)
  414. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  415. c.Assert(err, IsNil)
  416. sst := new(xlsxSST)
  417. err = xml.NewDecoder(sharedstringsXML).Decode(sst)
  418. c.Assert(err, IsNil)
  419. file := new(File)
  420. file.referenceTable = MakeSharedStringRefTable(sst)
  421. rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
  422. c.Assert(maxRows, Equals, 3)
  423. c.Assert(maxCols, Equals, 3)
  424. row := rows[2]
  425. c.Assert(len(row.Cells), Equals, 3)
  426. cell1 := row.Cells[0]
  427. c.Assert(cell1.Value, Equals, "No")
  428. cell2 := row.Cells[1]
  429. c.Assert(cell2.Value, Equals, "")
  430. cell3 := row.Cells[2]
  431. c.Assert(cell3.Value, Equals, "Yes")
  432. }
  433. func (l *LibSuite) TestReadRowsFromSheetWithTrailingEmptyCells(c *C) {
  434. var row *Row
  435. var cell1, cell2, cell3, cell4 *Cell
  436. var sharedstringsXML = bytes.NewBufferString(`
  437. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  438. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4"><si><t>A</t></si><si><t>B</t></si><si><t>C</t></si><si><t>D</t></si></sst>`)
  439. var sheetxml = bytes.NewBufferString(`
  440. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  441. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:D8"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="A7" sqref="A7"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/><sheetData><row r="1" spans="1:4"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="s"><v>2</v></c><c r="D1" t="s"><v>3</v></c></row><row r="2" spans="1:4"><c r="A2"><v>1</v></c></row><row r="3" spans="1:4"><c r="B3"><v>1</v></c></row><row r="4" spans="1:4"><c r="C4"><v>1</v></c></row><row r="5" spans="1:4"><c r="D5"><v>1</v></c></row><row r="6" spans="1:4"><c r="C6"><v>1</v></c></row><row r="7" spans="1:4"><c r="B7"><v>1</v></c></row><row r="8" spans="1:4"><c r="A8"><v>1</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/></worksheet>
  442. `)
  443. worksheet := new(xlsxWorksheet)
  444. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  445. c.Assert(err, IsNil)
  446. sst := new(xlsxSST)
  447. err = xml.NewDecoder(sharedstringsXML).Decode(sst)
  448. c.Assert(err, IsNil)
  449. file := new(File)
  450. file.referenceTable = MakeSharedStringRefTable(sst)
  451. rows, maxCol, maxRow := readRowsFromSheet(worksheet, file)
  452. c.Assert(maxCol, Equals, 4)
  453. c.Assert(maxRow, Equals, 8)
  454. row = rows[0]
  455. c.Assert(len(row.Cells), Equals, 4)
  456. cell1 = row.Cells[0]
  457. c.Assert(cell1.Value, Equals, "A")
  458. cell2 = row.Cells[1]
  459. c.Assert(cell2.Value, Equals, "B")
  460. cell3 = row.Cells[2]
  461. c.Assert(cell3.Value, Equals, "C")
  462. cell4 = row.Cells[3]
  463. c.Assert(cell4.Value, Equals, "D")
  464. row = rows[1]
  465. c.Assert(len(row.Cells), Equals, 4)
  466. cell1 = row.Cells[0]
  467. c.Assert(cell1.Value, Equals, "1")
  468. cell2 = row.Cells[1]
  469. c.Assert(cell2.Value, Equals, "")
  470. cell3 = row.Cells[2]
  471. c.Assert(cell3.Value, Equals, "")
  472. cell4 = row.Cells[3]
  473. c.Assert(cell4.Value, Equals, "")
  474. }
  475. func (l *LibSuite) TestReadRowsFromSheetWithMultipleSpans(c *C) {
  476. var sharedstringsXML = bytes.NewBufferString(`
  477. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  478. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
  479. <si>
  480. <t>Foo</t>
  481. </si>
  482. <si>
  483. <t>Bar</t>
  484. </si>
  485. <si>
  486. <t xml:space="preserve">Baz </t>
  487. </si>
  488. <si>
  489. <t>Quuk</t>
  490. </si>
  491. </sst>`)
  492. var sheetxml = bytes.NewBufferString(`
  493. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  494. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  495. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  496. <dimension ref="A1:D2"/>
  497. <sheetViews>
  498. <sheetView tabSelected="1" workbookViewId="0">
  499. <selection activeCell="C2" sqref="C2"/>
  500. </sheetView>
  501. </sheetViews>
  502. <sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
  503. <sheetData>
  504. <row r="1" spans="1:2 3:4">
  505. <c r="A1" t="s">
  506. <v>0</v>
  507. </c>
  508. <c r="B1" t="s">
  509. <v>1</v>
  510. </c>
  511. <c r="C1" t="s">
  512. <v>0</v>
  513. </c>
  514. <c r="D1" t="s">
  515. <v>1</v>
  516. </c>
  517. </row>
  518. <row r="2" spans="1:2 3:4">
  519. <c r="A2" t="s">
  520. <v>2</v>
  521. </c>
  522. <c r="B2" t="s">
  523. <v>3</v>
  524. </c>
  525. <c r="C2" t="s">
  526. <v>2</v>
  527. </c>
  528. <c r="D2" t="s">
  529. <v>3</v>
  530. </c>
  531. </row>
  532. </sheetData>
  533. <pageMargins left="0.7" right="0.7"
  534. top="0.78740157499999996"
  535. bottom="0.78740157499999996"
  536. header="0.3"
  537. footer="0.3"/>
  538. </worksheet>`)
  539. worksheet := new(xlsxWorksheet)
  540. err := xml.NewDecoder(sheetxml).Decode(worksheet)
  541. c.Assert(err, IsNil)
  542. sst := new(xlsxSST)
  543. err = xml.NewDecoder(sharedstringsXML).Decode(sst)
  544. c.Assert(err, IsNil)
  545. file := new(File)
  546. file.referenceTable = MakeSharedStringRefTable(sst)
  547. rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
  548. c.Assert(maxRows, Equals, 2)
  549. c.Assert(maxCols, Equals, 4)
  550. row := rows[0]
  551. c.Assert(len(row.Cells), Equals, 4)
  552. cell1 := row.Cells[0]
  553. c.Assert(cell1.Value, Equals, "Foo")
  554. cell2 := row.Cells[1]
  555. c.Assert(cell2.Value, Equals, "Bar")
  556. cell3 := row.Cells[2]
  557. c.Assert(cell3.Value, Equals, "Foo")
  558. cell4 := row.Cells[3]
  559. c.Assert(cell4.Value, Equals, "Bar")
  560. }