lib_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. func TestLettersToNumeric(t *testing.T) {
  80. cases := map[string]int{"A": 0, "G": 6, "z": 25, "AA": 26, "Az": 51,
  81. "BA": 52, "Bz": 77, "ZA": 26*26 + 0, "ZZ": 26*26 + 25,
  82. "AAA": 26*26 + 26 + 0, "AMI": 1022}
  83. for input, ans := range cases {
  84. output := lettersToNumeric(input)
  85. if output != ans {
  86. t.Error("Expected output '"+input+"' == ", ans,
  87. "but got ", strconv.Itoa(output))
  88. }
  89. }
  90. }
  91. func TestLetterOnlyMapFunction(t *testing.T) {
  92. var input string = "ABC123"
  93. var output string = strings.Map(letterOnlyMapF, input)
  94. if output != "ABC" {
  95. t.Error("Expected output == 'ABC' but got ", output)
  96. }
  97. input = "abc123"
  98. output = strings.Map(letterOnlyMapF, input)
  99. if output != "ABC" {
  100. t.Error("Expected output == 'ABC' but got ", output)
  101. }
  102. }
  103. func TestIntOnlyMapFunction(t *testing.T) {
  104. var input string = "ABC123"
  105. var output string = strings.Map(intOnlyMapF, input)
  106. if output != "123" {
  107. t.Error("Expected output == '123' but got ", output)
  108. }
  109. }
  110. func TestGetCoordsFromCellIDString(t *testing.T) {
  111. var cellIDString string = "A3"
  112. var x, y int
  113. var error error
  114. x, y, error = getCoordsFromCellIDString(cellIDString)
  115. if error != nil {
  116. t.Error(error)
  117. }
  118. if x != 0 {
  119. t.Error("Expected x == 0, but got ", strconv.Itoa(x))
  120. }
  121. if y != 2 {
  122. t.Error("Expected y == 2, but got ", strconv.Itoa(y))
  123. }
  124. }
  125. func TestGetRangeFromString(t *testing.T) {
  126. var rangeString string
  127. var lower, upper int
  128. var error error
  129. rangeString = "1:3"
  130. lower, upper, error = getRangeFromString(rangeString)
  131. if error != nil {
  132. t.Error(error)
  133. }
  134. if lower != 1 {
  135. t.Error("Expected lower bound == 1, but got ", strconv.Itoa(lower))
  136. }
  137. if upper != 3 {
  138. t.Error("Expected upper bound == 3, but got ", strconv.Itoa(upper))
  139. }
  140. }
  141. func TestMakeRowFromSpan(t *testing.T) {
  142. var rangeString string
  143. var row *Row
  144. var length int
  145. rangeString = "1:3"
  146. row = makeRowFromSpan(rangeString)
  147. length = len(row.Cells)
  148. if length != 3 {
  149. t.Error("Expected a row with 3 cells, but got ", strconv.Itoa(length))
  150. }
  151. rangeString = "5:7" // Note - we ignore lower bound!
  152. row = makeRowFromSpan(rangeString)
  153. length = len(row.Cells)
  154. if length != 7 {
  155. t.Error("Expected a row with 7 cells, but got ", strconv.Itoa(length))
  156. }
  157. rangeString = "1:1"
  158. row = makeRowFromSpan(rangeString)
  159. length = len(row.Cells)
  160. if length != 1 {
  161. t.Error("Expected a row with 1 cells, but got ", strconv.Itoa(length))
  162. }
  163. }
  164. func TestReadRowsFromSheet(t *testing.T) {
  165. var sharedstringsXML = bytes.NewBufferString(`
  166. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  167. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
  168. <si>
  169. <t>Foo</t>
  170. </si>
  171. <si>
  172. <t>Bar</t>
  173. </si>
  174. <si>
  175. <t xml:space="preserve">Baz </t>
  176. </si>
  177. <si>
  178. <t>Quuk</t>
  179. </si>
  180. </sst>`)
  181. var sheetxml = bytes.NewBufferString(`
  182. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  183. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  184. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  185. <dimension ref="A1:B2"/>
  186. <sheetViews>
  187. <sheetView tabSelected="1" workbookViewId="0">
  188. <selection activeCell="C2" sqref="C2"/>
  189. </sheetView>
  190. </sheetViews>
  191. <sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
  192. <sheetData>
  193. <row r="1" spans="1:2">
  194. <c r="A1" t="s">
  195. <v>0</v>
  196. </c>
  197. <c r="B1" t="s">
  198. <v>1</v>
  199. </c>
  200. </row>
  201. <row r="2" spans="1:2">
  202. <c r="A2" t="s">
  203. <v>2</v>
  204. </c>
  205. <c r="B2" t="s">
  206. <v>3</v>
  207. </c>
  208. </row>
  209. </sheetData>
  210. <pageMargins left="0.7" right="0.7"
  211. top="0.78740157499999996"
  212. bottom="0.78740157499999996"
  213. header="0.3"
  214. footer="0.3"/>
  215. </worksheet>`)
  216. worksheet := new(xlsxWorksheet)
  217. error := xml.NewDecoder(sheetxml).Decode(worksheet)
  218. if error != nil {
  219. t.Error(error.Error())
  220. return
  221. }
  222. sst := new(xlsxSST)
  223. error = xml.NewDecoder(sharedstringsXML).Decode(sst)
  224. if error != nil {
  225. t.Error(error.Error())
  226. return
  227. }
  228. file := new(File)
  229. file.referenceTable = MakeSharedStringRefTable(sst)
  230. rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
  231. if maxRows != 2 {
  232. t.Error("Expected maxRows == 2")
  233. }
  234. if maxCols != 2 {
  235. t.Error("Expected maxCols == 2")
  236. }
  237. row := rows[0]
  238. if len(row.Cells) != 2 {
  239. t.Error("Expected len(row.Cells) == 2, got ", strconv.Itoa(len(row.Cells)))
  240. }
  241. cell1 := row.Cells[0]
  242. if cell1.String() != "Foo" {
  243. t.Error("Expected cell1.String() == 'Foo', got ", cell1.String())
  244. }
  245. cell2 := row.Cells[1]
  246. if cell2.String() != "Bar" {
  247. t.Error("Expected cell2.String() == 'Bar', got ", cell2.String())
  248. }
  249. }
  250. func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
  251. var sharedstringsXML = bytes.NewBufferString(`
  252. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  253. <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>`)
  254. var sheetxml = bytes.NewBufferString(`
  255. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  256. <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"/>
  257. <sheetData>
  258. <row r="1" spans="1:3">
  259. <c r="A1" t="s">
  260. <v>
  261. 0
  262. </v>
  263. </c>
  264. <c r="B1" t="s">
  265. <v>
  266. 1
  267. </v>
  268. </c>
  269. <c r="C1" t="s">
  270. <v>
  271. 2
  272. </v>
  273. </c>
  274. </row>
  275. <row r="2" spans="1:3">
  276. <c r="A2" t="s">
  277. <v>
  278. 3
  279. </v>
  280. </c>
  281. <c r="B2" t="s">
  282. <v>
  283. 4
  284. </v>
  285. </c>
  286. <c r="C2" t="s">
  287. <v>
  288. 3
  289. </v>
  290. </c>
  291. </row>
  292. <row r="3" spans="1:3">
  293. <c r="A3" t="s">
  294. <v>
  295. 4
  296. </v>
  297. </c>
  298. <c r="C3" t="s">
  299. <v>
  300. 3
  301. </v>
  302. </c>
  303. </row>
  304. </sheetData>
  305. <pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
  306. </worksheet>
  307. `)
  308. worksheet := new(xlsxWorksheet)
  309. error := xml.NewDecoder(sheetxml).Decode(worksheet)
  310. if error != nil {
  311. t.Error(error.Error())
  312. return
  313. }
  314. sst := new(xlsxSST)
  315. error = xml.NewDecoder(sharedstringsXML).Decode(sst)
  316. if error != nil {
  317. t.Error(error.Error())
  318. return
  319. }
  320. file := new(File)
  321. file.referenceTable = MakeSharedStringRefTable(sst)
  322. rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
  323. if maxRows != 3 {
  324. t.Error("Expected maxRows == 3, got ", strconv.Itoa(len(rows)))
  325. }
  326. if maxCols != 3 {
  327. t.Error("Expected maxCols == 3, got ", strconv.Itoa(maxCols))
  328. }
  329. row := rows[2]
  330. if len(row.Cells) != 3 {
  331. t.Error("Expected len(row.Cells) == 3, got ", strconv.Itoa(len(row.Cells)))
  332. }
  333. cell1 := row.Cells[0]
  334. if cell1.String() != "No" {
  335. t.Error("Expected cell1.String() == 'No', got ", cell1.String())
  336. }
  337. cell2 := row.Cells[1]
  338. if cell2.String() != "" {
  339. t.Error("Expected cell2.String() == '', got ", cell2.String())
  340. }
  341. cell3 := row.Cells[2]
  342. if cell3.String() != "Yes" {
  343. t.Error("Expected cell3.String() == 'Yes', got ", cell3.String())
  344. }
  345. }
  346. func TestReadRowsFromSheetWithTrailingEmptyCells(t *testing.T) {
  347. var row *Row
  348. var cell1, cell2, cell3, cell4 *Cell
  349. var sharedstringsXML = bytes.NewBufferString(`
  350. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  351. <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>`)
  352. var sheetxml = bytes.NewBufferString(`
  353. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  354. <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>
  355. `)
  356. worksheet := new(xlsxWorksheet)
  357. error := xml.NewDecoder(sheetxml).Decode(worksheet)
  358. if error != nil {
  359. t.Error(error.Error())
  360. return
  361. }
  362. sst := new(xlsxSST)
  363. error = xml.NewDecoder(sharedstringsXML).Decode(sst)
  364. if error != nil {
  365. t.Error(error.Error())
  366. return
  367. }
  368. file := new(File)
  369. file.referenceTable = MakeSharedStringRefTable(sst)
  370. rows, maxCol, maxRow := readRowsFromSheet(worksheet, file)
  371. if maxCol != 4 {
  372. t.Error("Expected maxCol == 4, got ", strconv.Itoa(maxCol))
  373. }
  374. if maxRow != 8 {
  375. t.Error("Expected maxRow == 8, got ", strconv.Itoa(maxRow))
  376. }
  377. row = rows[0]
  378. if len(row.Cells) != 4 {
  379. t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
  380. }
  381. cell1 = row.Cells[0]
  382. if cell1.String() != "A" {
  383. t.Error("Expected cell1.String() == 'A', got ", cell1.String())
  384. }
  385. cell2 = row.Cells[1]
  386. if cell2.String() != "B" {
  387. t.Error("Expected cell2.String() == 'B', got ", cell2.String())
  388. }
  389. cell3 = row.Cells[2]
  390. if cell3.String() != "C" {
  391. t.Error("Expected cell3.String() == 'C', got ", cell3.String())
  392. }
  393. cell4 = row.Cells[3]
  394. if cell4.String() != "D" {
  395. t.Error("Expected cell4.String() == 'D', got ", cell4.String())
  396. }
  397. row = rows[1]
  398. if len(row.Cells) != 4 {
  399. t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
  400. }
  401. cell1 = row.Cells[0]
  402. if cell1.String() != "1" {
  403. t.Error("Expected cell1.String() == '1', got ", cell1.String())
  404. }
  405. cell2 = row.Cells[1]
  406. if cell2.String() != "" {
  407. t.Error("Expected cell2.String() == '', got ", cell2.String())
  408. }
  409. cell3 = row.Cells[2]
  410. if cell3.String() != "" {
  411. t.Error("Expected cell3.String() == '', got ", cell3.String())
  412. }
  413. cell4 = row.Cells[3]
  414. if cell4.String() != "" {
  415. t.Error("Expected cell4.String() == '', got ", cell4.String())
  416. }
  417. }