lib_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 TestUnmarshallSharedStrings(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. if sst.Count != "4" {
  44. t.Error(`sst.Count != "4"`)
  45. }
  46. if sst.UniqueCount != "4" {
  47. t.Error(`sst.UniqueCount != 4`)
  48. }
  49. if len(sst.SI) == 0 {
  50. t.Error("Expected 4 sst.SI but found none")
  51. }
  52. si := sst.SI[0]
  53. if si.T.Data != "Foo" {
  54. t.Error("Expected s.T.Data == 'Foo'")
  55. }
  56. }
  57. func TestUnmarshallSheet(t *testing.T) {
  58. var sheetxml = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  59. <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>`)
  60. worksheet := new(XLSXWorksheet)
  61. error := xml.Unmarshal(sheetxml, worksheet)
  62. if error != nil {
  63. t.Error(error.String())
  64. return
  65. }
  66. if worksheet.Dimension.Ref != "A1:B2" {
  67. t.Error("Expected worksheet.Dimension.Ref == 'A1:B2'")
  68. }
  69. if len(worksheet.SheetViews.SheetView) == 0 {
  70. t.Error("Expected len(worksheet.SheetViews.SheetView) == 1")
  71. }
  72. sheetview := worksheet.SheetViews.SheetView[0]
  73. if sheetview.TabSelected != "1" {
  74. t.Error("Expected sheetview.TabSelected == '1'")
  75. }
  76. if sheetview.WorkbookViewID != "0" {
  77. t.Error("Expected sheetview.WorkbookViewID == '0'")
  78. }
  79. if sheetview.Selection.ActiveCell != "C2" {
  80. t.Error("Expeceted sheetview.Selection.ActiveCell == 'C2'")
  81. }
  82. if sheetview.Selection.SQRef != "C2" {
  83. t.Error("Expected sheetview.Selection.SQRef == 'C2'")
  84. }
  85. if worksheet.SheetFormatPr.BaseColWidth != "10" {
  86. t.Error("Expected worksheet.SheetFormatPr.BaseColWidth == '10'")
  87. }
  88. if worksheet.SheetFormatPr.DefaultRowHeight != "15" {
  89. t.Error("Expected worksheet.SheetFormatPr.DefaultRowHeight == '15'")
  90. }
  91. if len(worksheet.SheetData.Row) == 0 {
  92. t.Error("Expected len(worksheet.SheetData.Row) == '2'")
  93. }
  94. row := worksheet.SheetData.Row[0]
  95. if row.R != "1" {
  96. t.Error("Expected row.r == '1'")
  97. }
  98. if row.Spans != "1:2" {
  99. t.Error("Expected row.Spans == '1:2'")
  100. }
  101. if len(row.C) != 2 {
  102. t.Error("Expected len(row.C) == 2")
  103. }
  104. c := row.C[0]
  105. if c.R != "A1" {
  106. t.Error("Expected c.R == 'A1'")
  107. }
  108. if c.T != "s" {
  109. t.Error("Expected c.T == 's'")
  110. }
  111. if c.V.Data != "0" {
  112. t.Error("Expected c.V.Data == '0'")
  113. }
  114. }
  115. func TestUnmarshallXML(t *testing.T) {
  116. var error os.Error
  117. 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>`)
  118. var workbook *XLSXWorkbook
  119. workbook = new(XLSXWorkbook)
  120. error = xml.Unmarshal(buf, workbook)
  121. if error != nil {
  122. t.Error(error.String())
  123. return
  124. }
  125. if workbook.FileVersion.AppName != "xl" {
  126. t.Error("Expected FileVersion.AppName == 'xl')")
  127. }
  128. if workbook.FileVersion.LastEdited != "4" {
  129. t.Error("Expected FileVersion.LastEdited == '4'")
  130. }
  131. if workbook.FileVersion.LowestEdited != "4" {
  132. t.Error("Expected FileVersion.LowestEdited == '4'")
  133. }
  134. if workbook.FileVersion.RupBuild != "4506" {
  135. t.Error("Expected FileVersion.RupBuild == '4506'")
  136. }
  137. if workbook.WorkbookPr.DefaultThemeVersion != "124226" {
  138. t.Error("Expected workbook.WorkbookPr.DefaultThemeVersion == '124226'")
  139. }
  140. if len(workbook.BookViews.WorkBookView) == 0 {
  141. t.Error("Expected len(workbook.BookViews.WorkBookView) == 0")
  142. }
  143. workBookView := workbook.BookViews.WorkBookView[0]
  144. if workBookView.XWindow != "120" {
  145. t.Error("Expected workBookView.XWindow == '120'")
  146. }
  147. if workBookView.YWindow != "75" {
  148. t.Error("Expected workBookView.YWindow == '75'")
  149. }
  150. if workBookView.WindowWidth != "15135" {
  151. t.Error("Expected workBookView.WindowWidth == '15135'")
  152. }
  153. if workBookView.WindowHeight != "7620" {
  154. t.Error("Expected workBookView.WindowHeight == '7620'")
  155. }
  156. if len(workbook.Sheets.Sheet) == 0 {
  157. t.Error("Expected len(workbook.Sheets.Sheet) == 0")
  158. }
  159. sheet := workbook.Sheets.Sheet[0]
  160. if sheet.Id != "rId1" {
  161. t.Error("Expected sheet.Id == 'rID1'")
  162. }
  163. if sheet.Name != "Sheet1" {
  164. t.Error("Expected sheet.Name == 'Sheet1'")
  165. }
  166. if sheet.SheetId != "1" {
  167. t.Error("Expected sheet.SheetId == '1'")
  168. }
  169. if len(workbook.DefinedNames.DefinedName) == 0 {
  170. t.Error("Expected len(workbook.DefinedNames.DefinedName) == 0")
  171. }
  172. dname := workbook.DefinedNames.DefinedName[0]
  173. if dname.Data != "Sheet1!$A$1533" {
  174. t.Error("dname.Data == 'Sheet1!$A$1533'")
  175. }
  176. if dname.LocalSheetID != "0" {
  177. t.Error("dname.LocalSheetID == '0'")
  178. }
  179. if dname.Name != "monitors" {
  180. t.Error("Expected dname.Name == 'monitors'")
  181. }
  182. if workbook.CalcPr.CalcId != "125725" {
  183. t.Error("workbook.CalcPr.CalcId != '125725'")
  184. }
  185. }