lib_test.go 13 KB

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