lib_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package xlsx
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. )
  9. // Test we can correctly open a XSLX file and return a xlsx.File
  10. // struct.
  11. func TestOpenFile(t *testing.T) {
  12. var xlsxFile *File
  13. var error error
  14. xlsxFile, error = OpenFile("testfile.xlsx")
  15. if error != nil {
  16. t.Error(error.Error())
  17. return
  18. }
  19. if xlsxFile == nil {
  20. t.Error("OpenFile returned nil FileInterface without generating an os.Error")
  21. return
  22. }
  23. }
  24. // Test that when we open a real XLSX file we create xlsx.Sheet
  25. // objects for the sheets inside the file and that these sheets are
  26. // themselves correct.
  27. func TestCreateSheet(t *testing.T) {
  28. var xlsxFile *File
  29. var error error
  30. var sheet *Sheet
  31. var row *Row
  32. xlsxFile, error = OpenFile("testfile.xlsx")
  33. if error != nil {
  34. t.Error(error.Error())
  35. return
  36. }
  37. if xlsxFile == nil {
  38. t.Error("OpenFile returned a nil File pointer but did not generate an error.")
  39. return
  40. }
  41. sheetLen := len(xlsxFile.Sheets)
  42. if sheetLen == 0 {
  43. t.Error("Expected len(xlsxFile.Sheets) > 0, but got ", sheetLen)
  44. return
  45. }
  46. sheet = xlsxFile.Sheets[0]
  47. rowLen := len(sheet.Rows)
  48. if rowLen != 2 {
  49. t.Error("Expected len(sheet.Rows) == 2, but got ", rowLen)
  50. return
  51. }
  52. row = sheet.Rows[0]
  53. if len(row.Cells) != 2 {
  54. t.Error("Expected len(row.Cells) == 2")
  55. return
  56. }
  57. cell := row.Cells[0]
  58. cellstring := cell.String()
  59. if cellstring != "Foo" {
  60. t.Error("Expected cell.String() == 'Foo', got ", cellstring)
  61. }
  62. }
  63. // Test that we can correctly extract a reference table from the
  64. // sharedStrings.xml file embedded in the XLSX file and return a
  65. // reference table of string values from it.
  66. func TestReadSharedStringsFromZipFile(t *testing.T) {
  67. var xlsxFile *File
  68. var error error
  69. xlsxFile, error = OpenFile("testfile.xlsx")
  70. if error != nil {
  71. t.Error(error.Error())
  72. return
  73. }
  74. if xlsxFile.referenceTable == nil {
  75. t.Error("expected non nil xlsxFile.referenceTable")
  76. return
  77. }
  78. }
  79. // We can correctly extract a style table from the style.xml file
  80. // embedded in the XLSX file and return a styles struct from it.
  81. func TestReadStylesFromZipFile(t *testing.T) {
  82. var xlsxFile *File
  83. var error error
  84. xlsxFile, error = OpenFile("testfile.xlsx")
  85. if error != nil {
  86. t.Error(error.Error())
  87. return
  88. }
  89. if xlsxFile.styles == nil {
  90. t.Error("expected non nil xlsx.styles")
  91. return
  92. }
  93. }
  94. func TestLettersToNumeric(t *testing.T) {
  95. cases := map[string]int{"A": 0, "G": 6, "z": 25, "AA": 26, "Az": 51,
  96. "BA": 52, "Bz": 77, "ZA": 26*26 + 0, "ZZ": 26*26 + 25,
  97. "AAA": 26*26 + 26 + 0, "AMI": 1022}
  98. for input, ans := range cases {
  99. output := lettersToNumeric(input)
  100. if output != ans {
  101. t.Error("Expected output '"+input+"' == ", ans,
  102. "but got ", strconv.Itoa(output))
  103. }
  104. }
  105. }
  106. func TestLetterOnlyMapFunction(t *testing.T) {
  107. var input string = "ABC123"
  108. var output string = strings.Map(letterOnlyMapF, input)
  109. if output != "ABC" {
  110. t.Error("Expected output == 'ABC' but got ", output)
  111. }
  112. input = "abc123"
  113. output = strings.Map(letterOnlyMapF, input)
  114. if output != "ABC" {
  115. t.Error("Expected output == 'ABC' but got ", output)
  116. }
  117. }
  118. func TestIntOnlyMapFunction(t *testing.T) {
  119. var input string = "ABC123"
  120. var output string = strings.Map(intOnlyMapF, input)
  121. if output != "123" {
  122. t.Error("Expected output == '123' but got ", output)
  123. }
  124. }
  125. func TestGetCoordsFromCellIDString(t *testing.T) {
  126. var cellIDString string = "A3"
  127. var x, y int
  128. var error error
  129. x, y, error = getCoordsFromCellIDString(cellIDString)
  130. if error != nil {
  131. t.Error(error)
  132. }
  133. if x != 0 {
  134. t.Error("Expected x == 0, but got ", strconv.Itoa(x))
  135. }
  136. if y != 2 {
  137. t.Error("Expected y == 2, but got ", strconv.Itoa(y))
  138. }
  139. }
  140. func TestGetRangeFromString(t *testing.T) {
  141. var rangeString string
  142. var lower, upper int
  143. var error error
  144. rangeString = "1:3"
  145. lower, upper, error = getRangeFromString(rangeString)
  146. if error != nil {
  147. t.Error(error)
  148. }
  149. if lower != 1 {
  150. t.Error("Expected lower bound == 1, but got ", strconv.Itoa(lower))
  151. }
  152. if upper != 3 {
  153. t.Error("Expected upper bound == 3, but got ", strconv.Itoa(upper))
  154. }
  155. }
  156. func TestMakeRowFromSpan(t *testing.T) {
  157. var rangeString string
  158. var row *Row
  159. var length int
  160. rangeString = "1:3"
  161. row = makeRowFromSpan(rangeString)
  162. length = len(row.Cells)
  163. if length != 3 {
  164. t.Error("Expected a row with 3 cells, but got ", strconv.Itoa(length))
  165. }
  166. rangeString = "5:7" // Note - we ignore lower bound!
  167. row = makeRowFromSpan(rangeString)
  168. length = len(row.Cells)
  169. if length != 7 {
  170. t.Error("Expected a row with 7 cells, but got ", strconv.Itoa(length))
  171. }
  172. rangeString = "1:1"
  173. row = makeRowFromSpan(rangeString)
  174. length = len(row.Cells)
  175. if length != 1 {
  176. t.Error("Expected a row with 1 cells, but got ", strconv.Itoa(length))
  177. }
  178. }
  179. func TestReadRowsFromSheet(t *testing.T) {
  180. var sharedstringsXML = bytes.NewBufferString(`
  181. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  182. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
  183. <si>
  184. <t>Foo</t>
  185. </si>
  186. <si>
  187. <t>Bar</t>
  188. </si>
  189. <si>
  190. <t xml:space="preserve">Baz </t>
  191. </si>
  192. <si>
  193. <t>Quuk</t>
  194. </si>
  195. </sst>`)
  196. var sheetxml = bytes.NewBufferString(`
  197. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  198. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  199. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  200. <dimension ref="A1:B2"/>
  201. <sheetViews>
  202. <sheetView tabSelected="1" workbookViewId="0">
  203. <selection activeCell="C2" sqref="C2"/>
  204. </sheetView>
  205. </sheetViews>
  206. <sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
  207. <sheetData>
  208. <row r="1" spans="1:2">
  209. <c r="A1" t="s">
  210. <v>0</v>
  211. </c>
  212. <c r="B1" t="s">
  213. <v>1</v>
  214. </c>
  215. </row>
  216. <row r="2" spans="1:2">
  217. <c r="A2" t="s">
  218. <v>2</v>
  219. </c>
  220. <c r="B2" t="s">
  221. <v>3</v>
  222. </c>
  223. </row>
  224. </sheetData>
  225. <pageMargins left="0.7" right="0.7"
  226. top="0.78740157499999996"
  227. bottom="0.78740157499999996"
  228. header="0.3"
  229. footer="0.3"/>
  230. </worksheet>`)
  231. worksheet := new(xlsxWorksheet)
  232. error := xml.NewDecoder(sheetxml).Decode(worksheet)
  233. if error != nil {
  234. t.Error(error.Error())
  235. return
  236. }
  237. sst := new(xlsxSST)
  238. error = xml.NewDecoder(sharedstringsXML).Decode(sst)
  239. if error != nil {
  240. t.Error(error.Error())
  241. return
  242. }
  243. file := new(File)
  244. file.referenceTable = MakeSharedStringRefTable(sst)
  245. rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
  246. if maxRows != 2 {
  247. t.Error("Expected maxRows == 2")
  248. }
  249. if maxCols != 2 {
  250. t.Error("Expected maxCols == 2")
  251. }
  252. row := rows[0]
  253. if len(row.Cells) != 2 {
  254. t.Error("Expected len(row.Cells) == 2, got ", strconv.Itoa(len(row.Cells)))
  255. }
  256. cell1 := row.Cells[0]
  257. if cell1.String() != "Foo" {
  258. t.Error("Expected cell1.String() == 'Foo', got ", cell1.String())
  259. }
  260. cell2 := row.Cells[1]
  261. if cell2.String() != "Bar" {
  262. t.Error("Expected cell2.String() == 'Bar', got ", cell2.String())
  263. }
  264. }
  265. func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
  266. var sharedstringsXML = bytes.NewBufferString(`
  267. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  268. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="8" uniqueCount="5"><si><t>Bob</t></si><si><t>Alice</t></si><si><t>Sue</t></si><si><t>Yes</t></si><si><t>No</t></si></sst>`)
  269. var sheetxml = bytes.NewBufferString(`
  270. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  271. <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"/>
  272. <sheetData>
  273. <row r="1" spans="1:3">
  274. <c r="A1" t="s">
  275. <v>
  276. 0
  277. </v>
  278. </c>
  279. <c r="B1" t="s">
  280. <v>
  281. 1
  282. </v>
  283. </c>
  284. <c r="C1" t="s">
  285. <v>
  286. 2
  287. </v>
  288. </c>
  289. </row>
  290. <row r="2" spans="1:3">
  291. <c r="A2" t="s">
  292. <v>
  293. 3
  294. </v>
  295. </c>
  296. <c r="B2" t="s">
  297. <v>
  298. 4
  299. </v>
  300. </c>
  301. <c r="C2" t="s">
  302. <v>
  303. 3
  304. </v>
  305. </c>
  306. </row>
  307. <row r="3" spans="1:3">
  308. <c r="A3" t="s">
  309. <v>
  310. 4
  311. </v>
  312. </c>
  313. <c r="C3" t="s">
  314. <v>
  315. 3
  316. </v>
  317. </c>
  318. </row>
  319. </sheetData>
  320. <pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
  321. </worksheet>
  322. `)
  323. worksheet := new(xlsxWorksheet)
  324. error := xml.NewDecoder(sheetxml).Decode(worksheet)
  325. if error != nil {
  326. t.Error(error.Error())
  327. return
  328. }
  329. sst := new(xlsxSST)
  330. error = xml.NewDecoder(sharedstringsXML).Decode(sst)
  331. if error != nil {
  332. t.Error(error.Error())
  333. return
  334. }
  335. file := new(File)
  336. file.referenceTable = MakeSharedStringRefTable(sst)
  337. rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
  338. if maxRows != 3 {
  339. t.Error("Expected maxRows == 3, got ", strconv.Itoa(len(rows)))
  340. }
  341. if maxCols != 3 {
  342. t.Error("Expected maxCols == 3, got ", strconv.Itoa(maxCols))
  343. }
  344. row := rows[2]
  345. if len(row.Cells) != 3 {
  346. t.Error("Expected len(row.Cells) == 3, got ", strconv.Itoa(len(row.Cells)))
  347. }
  348. cell1 := row.Cells[0]
  349. if cell1.String() != "No" {
  350. t.Error("Expected cell1.String() == 'No', got ", cell1.String())
  351. }
  352. cell2 := row.Cells[1]
  353. if cell2.String() != "" {
  354. t.Error("Expected cell2.String() == '', got ", cell2.String())
  355. }
  356. cell3 := row.Cells[2]
  357. if cell3.String() != "Yes" {
  358. t.Error("Expected cell3.String() == 'Yes', got ", cell3.String())
  359. }
  360. }
  361. func TestReadRowsFromSheetWithTrailingEmptyCells(t *testing.T) {
  362. var row *Row
  363. var cell1, cell2, cell3, cell4 *Cell
  364. var sharedstringsXML = bytes.NewBufferString(`
  365. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  366. <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>`)
  367. var sheetxml = bytes.NewBufferString(`
  368. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  369. <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>
  370. `)
  371. worksheet := new(xlsxWorksheet)
  372. error := xml.NewDecoder(sheetxml).Decode(worksheet)
  373. if error != nil {
  374. t.Error(error.Error())
  375. return
  376. }
  377. sst := new(xlsxSST)
  378. error = xml.NewDecoder(sharedstringsXML).Decode(sst)
  379. if error != nil {
  380. t.Error(error.Error())
  381. return
  382. }
  383. file := new(File)
  384. file.referenceTable = MakeSharedStringRefTable(sst)
  385. rows, maxCol, maxRow := readRowsFromSheet(worksheet, file)
  386. if maxCol != 4 {
  387. t.Error("Expected maxCol == 4, got ", strconv.Itoa(maxCol))
  388. }
  389. if maxRow != 8 {
  390. t.Error("Expected maxRow == 8, got ", strconv.Itoa(maxRow))
  391. }
  392. row = rows[0]
  393. if len(row.Cells) != 4 {
  394. t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
  395. }
  396. cell1 = row.Cells[0]
  397. if cell1.String() != "A" {
  398. t.Error("Expected cell1.String() == 'A', got ", cell1.String())
  399. }
  400. cell2 = row.Cells[1]
  401. if cell2.String() != "B" {
  402. t.Error("Expected cell2.String() == 'B', got ", cell2.String())
  403. }
  404. cell3 = row.Cells[2]
  405. if cell3.String() != "C" {
  406. t.Error("Expected cell3.String() == 'C', got ", cell3.String())
  407. }
  408. cell4 = row.Cells[3]
  409. if cell4.String() != "D" {
  410. t.Error("Expected cell4.String() == 'D', got ", cell4.String())
  411. }
  412. row = rows[1]
  413. if len(row.Cells) != 4 {
  414. t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
  415. }
  416. cell1 = row.Cells[0]
  417. if cell1.String() != "1" {
  418. t.Error("Expected cell1.String() == '1', got ", cell1.String())
  419. }
  420. cell2 = row.Cells[1]
  421. if cell2.String() != "" {
  422. t.Error("Expected cell2.String() == '', got ", cell2.String())
  423. }
  424. cell3 = row.Cells[2]
  425. if cell3.String() != "" {
  426. t.Error("Expected cell3.String() == '', got ", cell3.String())
  427. }
  428. cell4 = row.Cells[3]
  429. if cell4.String() != "" {
  430. t.Error("Expected cell4.String() == '', got ", cell4.String())
  431. }
  432. }