lib_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package xlsx
  2. import (
  3. "bytes"
  4. // "archive/zip"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "xml"
  9. )
  10. func TestOpenXLSXFile(t *testing.T) {
  11. var xlsxFile *XLSXFile
  12. var error os.Error
  13. xlsxFile, error = OpenXLSXFile("testfile.xlsx")
  14. if error != nil {
  15. t.Error(error.String())
  16. return
  17. }
  18. if xlsxFile == nil {
  19. t.Error("OpenXLSXFile returned nil XLSXFileInterface without generating an os.Error")
  20. return
  21. }
  22. }
  23. func TestExtractSheets(t *testing.T) {
  24. var xlsxFile *XLSXFile
  25. var smap map[string]*XLSXSheetStruct
  26. xlsxFile, _ = OpenXLSXFile("testfile.xlsx")
  27. smap = xlsxFile.Sheets
  28. if len(smap) == 0 {
  29. t.Error("No sheets read from XLSX file")
  30. return
  31. }
  32. fmt.Printf("%v\n", len(smap))
  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 TestUnmarshallSharedStrings(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. if sst.Count != "4" {
  65. t.Error(`sst.Count != "4"`)
  66. }
  67. if sst.UniqueCount != "4" {
  68. t.Error(`sst.UniqueCount != 4`)
  69. }
  70. if len(sst.SI) == 0 {
  71. t.Error("Expected 4 sst.SI but found none")
  72. }
  73. si := sst.SI[0]
  74. if si.T.Data != "Foo" {
  75. t.Error("Expected s.T.Data == 'Foo'")
  76. }
  77. }
  78. func TestUnmarshallSheet(t *testing.T) {
  79. var sheetxml = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  80. <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>`)
  81. worksheet := new(XLSXWorksheet)
  82. error := xml.Unmarshal(sheetxml, worksheet)
  83. if error != nil {
  84. t.Error(error.String())
  85. return
  86. }
  87. if worksheet.Dimension.Ref != "A1:B2" {
  88. t.Error("Expected worksheet.Dimension.Ref == 'A1:B2'")
  89. }
  90. if len(worksheet.SheetViews.SheetView) == 0 {
  91. t.Error("Expected len(worksheet.SheetViews.SheetView) == 1")
  92. }
  93. sheetview := worksheet.SheetViews.SheetView[0]
  94. if sheetview.TabSelected != "1" {
  95. t.Error("Expected sheetview.TabSelected == '1'")
  96. }
  97. if sheetview.WorkbookViewID != "0" {
  98. t.Error("Expected sheetview.WorkbookViewID == '0'")
  99. }
  100. if sheetview.Selection.ActiveCell != "C2" {
  101. t.Error("Expeceted sheetview.Selection.ActiveCell == 'C2'")
  102. }
  103. if sheetview.Selection.SQRef != "C2" {
  104. t.Error("Expected sheetview.Selection.SQRef == 'C2'")
  105. }
  106. if worksheet.SheetFormatPr.BaseColWidth != "10" {
  107. t.Error("Expected worksheet.SheetFormatPr.BaseColWidth == '10'")
  108. }
  109. if worksheet.SheetFormatPr.DefaultRowHeight != "15" {
  110. t.Error("Expected worksheet.SheetFormatPr.DefaultRowHeight == '15'")
  111. }
  112. if len(worksheet.SheetData.Row) == 0 {
  113. t.Error("Expected len(worksheet.SheetData.Row) == '2'")
  114. }
  115. row := worksheet.SheetData.Row[0]
  116. if row.R != "1" {
  117. t.Error("Expected row.r == '1'")
  118. }
  119. if row.Spans != "1:2" {
  120. t.Error("Expected row.Spans == '1:2'")
  121. }
  122. if len(row.C) != 2 {
  123. t.Error("Expected len(row.C) == 2")
  124. }
  125. c := row.C[0]
  126. if c.R != "A1" {
  127. t.Error("Expected c.R == 'A1'")
  128. }
  129. if c.T != "s" {
  130. t.Error("Expected c.T == 's'")
  131. }
  132. if c.V.Data != "0" {
  133. t.Error("Expected c.V.Data == '0'")
  134. }
  135. }
  136. func TestUnmarshallXML(t *testing.T) {
  137. var error os.Error
  138. 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>`)
  139. var workbook *XLSXWorkbook
  140. workbook = new(XLSXWorkbook)
  141. error = xml.Unmarshal(buf, workbook)
  142. if error != nil {
  143. t.Error(error.String())
  144. return
  145. }
  146. if workbook.FileVersion.AppName != "xl" {
  147. t.Error("Expected FileVersion.AppName == 'xl')")
  148. }
  149. if workbook.FileVersion.LastEdited != "4" {
  150. t.Error("Expected FileVersion.LastEdited == '4'")
  151. }
  152. if workbook.FileVersion.LowestEdited != "4" {
  153. t.Error("Expected FileVersion.LowestEdited == '4'")
  154. }
  155. if workbook.FileVersion.RupBuild != "4506" {
  156. t.Error("Expected FileVersion.RupBuild == '4506'")
  157. }
  158. if workbook.WorkbookPr.DefaultThemeVersion != "124226" {
  159. t.Error("Expected workbook.WorkbookPr.DefaultThemeVersion == '124226'")
  160. }
  161. if len(workbook.BookViews.WorkBookView) == 0 {
  162. t.Error("Expected len(workbook.BookViews.WorkBookView) == 0")
  163. }
  164. workBookView := workbook.BookViews.WorkBookView[0]
  165. if workBookView.XWindow != "120" {
  166. t.Error("Expected workBookView.XWindow == '120'")
  167. }
  168. if workBookView.YWindow != "75" {
  169. t.Error("Expected workBookView.YWindow == '75'")
  170. }
  171. if workBookView.WindowWidth != "15135" {
  172. t.Error("Expected workBookView.WindowWidth == '15135'")
  173. }
  174. if workBookView.WindowHeight != "7620" {
  175. t.Error("Expected workBookView.WindowHeight == '7620'")
  176. }
  177. if len(workbook.Sheets.Sheet) == 0 {
  178. t.Error("Expected len(workbook.Sheets.Sheet) == 0")
  179. }
  180. sheet := workbook.Sheets.Sheet[0]
  181. if sheet.Id != "rId1" {
  182. t.Error("Expected sheet.Id == 'rID1'")
  183. }
  184. if sheet.Name != "Sheet1" {
  185. t.Error("Expected sheet.Name == 'Sheet1'")
  186. }
  187. if sheet.SheetId != "1" {
  188. t.Error("Expected sheet.SheetId == '1'")
  189. }
  190. if len(workbook.DefinedNames.DefinedName) == 0 {
  191. t.Error("Expected len(workbook.DefinedNames.DefinedName) == 0")
  192. }
  193. dname := workbook.DefinedNames.DefinedName[0]
  194. if dname.Data != "Sheet1!$A$1533" {
  195. t.Error("dname.Data == 'Sheet1!$A$1533'")
  196. }
  197. if dname.LocalSheetID != "0" {
  198. t.Error("dname.LocalSheetID == '0'")
  199. }
  200. if dname.Name != "monitors" {
  201. t.Error("Expected dname.Name == 'monitors'")
  202. }
  203. if workbook.CalcPr.CalcId != "125725" {
  204. t.Error("workbook.CalcPr.CalcId != '125725'")
  205. }
  206. }