lib_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package xlsx
  2. import (
  3. "bytes"
  4. // "archive/zip"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "xml"
  9. )
  10. func TestOpenFile(t *testing.T) {
  11. var xlsxFile *File
  12. var error os.Error
  13. xlsxFile, error = OpenFile("testfile.xlsx")
  14. if error != nil {
  15. t.Error(error.String())
  16. return
  17. }
  18. if xlsxFile == nil {
  19. t.Error("OpenFile returned nil FileInterface without generating an os.Error")
  20. return
  21. }
  22. }
  23. func TestExtractSheets(t *testing.T) {
  24. var xlsxFile *File
  25. var sheets []*Sheet
  26. xlsxFile, _ = OpenFile("testfile.xlsx")
  27. sheets = xlsxFile.Sheets
  28. if len(sheets) == 0 {
  29. t.Error("No sheets read from XLSX file")
  30. return
  31. }
  32. fmt.Printf("%v\n", len(sheets))
  33. }
  34. func TestMakeSharedStringRefTable(t *testing.T) {
  35. var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  36. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4"><si><t>Foo</t></si><si><t>Bar</t></si><si><t xml:space="preserve">Baz </t></si><si><t>Quuk</t></si></sst>`)
  37. sst := new(XLSXSST)
  38. error := xml.Unmarshal(sharedstringsXML, sst)
  39. if error != nil {
  40. t.Error(error.String())
  41. return
  42. }
  43. reftable := MakeSharedStringRefTable(sst)
  44. if len(reftable) == 0 {
  45. t.Error("Reftable is zero length.")
  46. return
  47. }
  48. if reftable[0] != "Foo" {
  49. t.Error("RefTable lookup failed, expected reftable[0] == 'Foo'")
  50. }
  51. if reftable[1] != "Bar" {
  52. t.Error("RefTable lookup failed, expected reftable[1] == 'Bar'")
  53. }
  54. }
  55. func TestResolveSharedString(t *testing.T) {
  56. var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  57. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4"><si><t>Foo</t></si><si><t>Bar</t></si><si><t xml:space="preserve">Baz </t></si><si><t>Quuk</t></si></sst>`)
  58. sst := new(XLSXSST)
  59. error := xml.Unmarshal(sharedstringsXML, sst)
  60. if error != nil {
  61. t.Error(error.String())
  62. return
  63. }
  64. reftable := MakeSharedStringRefTable(sst)
  65. if ResolveSharedString(reftable, 0) != "Foo" {
  66. t.Error("Expected ResolveSharedString(reftable, 0) == 'Foo'")
  67. }
  68. }
  69. func TestUnmarshallSharedStrings(t *testing.T) {
  70. var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  71. <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4"><si><t>Foo</t></si><si><t>Bar</t></si><si><t xml:space="preserve">Baz </t></si><si><t>Quuk</t></si></sst>`)
  72. sst := new(XLSXSST)
  73. error := xml.Unmarshal(sharedstringsXML, sst)
  74. if error != nil {
  75. t.Error(error.String())
  76. return
  77. }
  78. if sst.Count != "4" {
  79. t.Error(`sst.Count != "4"`)
  80. }
  81. if sst.UniqueCount != "4" {
  82. t.Error(`sst.UniqueCount != 4`)
  83. }
  84. if len(sst.SI) == 0 {
  85. t.Error("Expected 4 sst.SI but found none")
  86. }
  87. si := sst.SI[0]
  88. if si.T.Data != "Foo" {
  89. t.Error("Expected s.T.Data == 'Foo'")
  90. }
  91. }
  92. func TestUnmarshallSheet(t *testing.T) {
  93. var sheetxml = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  94. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:B2"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="C2" sqref="C2"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/><sheetData><row r="1" spans="1:2"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c></row><row r="2" spans="1:2"><c r="A2" t="s"><v>2</v></c><c r="B2" t="s"><v>3</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/></worksheet>`)
  95. worksheet := new(XLSXWorksheet)
  96. error := xml.Unmarshal(sheetxml, worksheet)
  97. if error != nil {
  98. t.Error(error.String())
  99. return
  100. }
  101. if worksheet.Dimension.Ref != "A1:B2" {
  102. t.Error("Expected worksheet.Dimension.Ref == 'A1:B2'")
  103. }
  104. if len(worksheet.SheetViews.SheetView) == 0 {
  105. t.Error("Expected len(worksheet.SheetViews.SheetView) == 1")
  106. }
  107. sheetview := worksheet.SheetViews.SheetView[0]
  108. if sheetview.TabSelected != "1" {
  109. t.Error("Expected sheetview.TabSelected == '1'")
  110. }
  111. if sheetview.WorkbookViewID != "0" {
  112. t.Error("Expected sheetview.WorkbookViewID == '0'")
  113. }
  114. if sheetview.Selection.ActiveCell != "C2" {
  115. t.Error("Expeceted sheetview.Selection.ActiveCell == 'C2'")
  116. }
  117. if sheetview.Selection.SQRef != "C2" {
  118. t.Error("Expected sheetview.Selection.SQRef == 'C2'")
  119. }
  120. if worksheet.SheetFormatPr.BaseColWidth != "10" {
  121. t.Error("Expected worksheet.SheetFormatPr.BaseColWidth == '10'")
  122. }
  123. if worksheet.SheetFormatPr.DefaultRowHeight != "15" {
  124. t.Error("Expected worksheet.SheetFormatPr.DefaultRowHeight == '15'")
  125. }
  126. if len(worksheet.SheetData.Row) == 0 {
  127. t.Error("Expected len(worksheet.SheetData.Row) == '2'")
  128. }
  129. row := worksheet.SheetData.Row[0]
  130. if row.R != "1" {
  131. t.Error("Expected row.r == '1'")
  132. }
  133. if row.Spans != "1:2" {
  134. t.Error("Expected row.Spans == '1:2'")
  135. }
  136. if len(row.C) != 2 {
  137. t.Error("Expected len(row.C) == 2")
  138. }
  139. c := row.C[0]
  140. if c.R != "A1" {
  141. t.Error("Expected c.R == 'A1'")
  142. }
  143. if c.T != "s" {
  144. t.Error("Expected c.T == 's'")
  145. }
  146. if c.V.Data != "0" {
  147. t.Error("Expected c.V.Data == '0'")
  148. }
  149. }
  150. func TestCreateXSLXSheetStruct(t *testing.T) {
  151. var xlsxFile *File
  152. var error os.Error
  153. var sheet *Sheet
  154. xlsxFile, error = OpenFile("testfile.xlsx")
  155. if error != nil {
  156. t.Error(error.String())
  157. return
  158. }
  159. if xlsxFile == nil {
  160. t.Error("OpenFile returned a nil File pointer but did not generate an error.")
  161. return
  162. }
  163. if len(xlsxFile.Sheets) == 0 {
  164. t.Error("Expected len(xlsxFile.Sheets) > 0")
  165. return
  166. }
  167. sheet = xlsxFile.Sheets[0]
  168. if len(sheet.Cells) == 0 {
  169. t.Error("Expected len(sheet.Cells) == 4")
  170. }
  171. }
  172. func TestUnmarshallXML(t *testing.T) {
  173. var error os.Error
  174. var buf = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><fileVersion appName="xl" lastEdited="4" lowestEdited="4" rupBuild="4506"/><workbookPr defaultThemeVersion="124226"/><bookViews><workbookView xWindow="120" yWindow="75" windowWidth="15135" windowHeight="7620"/></bookViews><sheets><sheet name="Sheet1" sheetId="1" r:id="rId1"/><sheet name="Sheet2" sheetId="2" r:id="rId2"/><sheet name="Sheet3" sheetId="3" r:id="rId3"/></sheets><definedNames><definedName name="monitors" localSheetId="0">Sheet1!$A$1533</definedName></definedNames><calcPr calcId="125725"/></workbook>`)
  175. var workbook *XLSXWorkbook
  176. workbook = new(XLSXWorkbook)
  177. error = xml.Unmarshal(buf, workbook)
  178. if error != nil {
  179. t.Error(error.String())
  180. return
  181. }
  182. if workbook.FileVersion.AppName != "xl" {
  183. t.Error("Expected FileVersion.AppName == 'xl')")
  184. }
  185. if workbook.FileVersion.LastEdited != "4" {
  186. t.Error("Expected FileVersion.LastEdited == '4'")
  187. }
  188. if workbook.FileVersion.LowestEdited != "4" {
  189. t.Error("Expected FileVersion.LowestEdited == '4'")
  190. }
  191. if workbook.FileVersion.RupBuild != "4506" {
  192. t.Error("Expected FileVersion.RupBuild == '4506'")
  193. }
  194. if workbook.WorkbookPr.DefaultThemeVersion != "124226" {
  195. t.Error("Expected workbook.WorkbookPr.DefaultThemeVersion == '124226'")
  196. }
  197. if len(workbook.BookViews.WorkBookView) == 0 {
  198. t.Error("Expected len(workbook.BookViews.WorkBookView) == 0")
  199. }
  200. workBookView := workbook.BookViews.WorkBookView[0]
  201. if workBookView.XWindow != "120" {
  202. t.Error("Expected workBookView.XWindow == '120'")
  203. }
  204. if workBookView.YWindow != "75" {
  205. t.Error("Expected workBookView.YWindow == '75'")
  206. }
  207. if workBookView.WindowWidth != "15135" {
  208. t.Error("Expected workBookView.WindowWidth == '15135'")
  209. }
  210. if workBookView.WindowHeight != "7620" {
  211. t.Error("Expected workBookView.WindowHeight == '7620'")
  212. }
  213. if len(workbook.Sheets.Sheet) == 0 {
  214. t.Error("Expected len(workbook.Sheets.Sheet) == 0")
  215. }
  216. sheet := workbook.Sheets.Sheet[0]
  217. if sheet.Id != "rId1" {
  218. t.Error("Expected sheet.Id == 'rID1'")
  219. }
  220. if sheet.Name != "Sheet1" {
  221. t.Error("Expected sheet.Name == 'Sheet1'")
  222. }
  223. if sheet.SheetId != "1" {
  224. t.Error("Expected sheet.SheetId == '1'")
  225. }
  226. if len(workbook.DefinedNames.DefinedName) == 0 {
  227. t.Error("Expected len(workbook.DefinedNames.DefinedName) == 0")
  228. }
  229. dname := workbook.DefinedNames.DefinedName[0]
  230. if dname.Data != "Sheet1!$A$1533" {
  231. t.Error("dname.Data == 'Sheet1!$A$1533'")
  232. }
  233. if dname.LocalSheetID != "0" {
  234. t.Error("dname.LocalSheetID == '0'")
  235. }
  236. if dname.Name != "monitors" {
  237. t.Error("Expected dname.Name == 'monitors'")
  238. }
  239. if workbook.CalcPr.CalcId != "125725" {
  240. t.Error("workbook.CalcPr.CalcId != '125725'")
  241. }
  242. }