worksheet_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package xlsx
  2. import (
  3. "bytes"
  4. "testing"
  5. "xml"
  6. )
  7. // Test we can succesfully unmarshal the sheetN.xml files within and
  8. // XLSX file into an XLSXWorksheet struct (and it's related children).
  9. func TestUnmarshallWorksheet(t *testing.T) {
  10. var sheetxml = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  11. <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>`)
  12. worksheet := new(XLSXWorksheet)
  13. error := xml.Unmarshal(sheetxml, worksheet)
  14. if error != nil {
  15. t.Error(error.String())
  16. return
  17. }
  18. if worksheet.Dimension.Ref != "A1:B2" {
  19. t.Error("Expected worksheet.Dimension.Ref == 'A1:B2'")
  20. }
  21. if len(worksheet.SheetViews.SheetView) == 0 {
  22. t.Error("Expected len(worksheet.SheetViews.SheetView) == 1")
  23. }
  24. sheetview := worksheet.SheetViews.SheetView[0]
  25. if sheetview.TabSelected != "1" {
  26. t.Error("Expected sheetview.TabSelected == '1'")
  27. }
  28. if sheetview.WorkbookViewID != "0" {
  29. t.Error("Expected sheetview.WorkbookViewID == '0'")
  30. }
  31. if sheetview.Selection.ActiveCell != "C2" {
  32. t.Error("Expeceted sheetview.Selection.ActiveCell == 'C2'")
  33. }
  34. if sheetview.Selection.SQRef != "C2" {
  35. t.Error("Expected sheetview.Selection.SQRef == 'C2'")
  36. }
  37. if worksheet.SheetFormatPr.BaseColWidth != "10" {
  38. t.Error("Expected worksheet.SheetFormatPr.BaseColWidth == '10'")
  39. }
  40. if worksheet.SheetFormatPr.DefaultRowHeight != "15" {
  41. t.Error("Expected worksheet.SheetFormatPr.DefaultRowHeight == '15'")
  42. }
  43. if len(worksheet.SheetData.Row) == 0 {
  44. t.Error("Expected len(worksheet.SheetData.Row) == '2'")
  45. }
  46. row := worksheet.SheetData.Row[0]
  47. if row.R != "1" {
  48. t.Error("Expected row.r == '1'")
  49. }
  50. if row.Spans != "1:2" {
  51. t.Error("Expected row.Spans == '1:2'")
  52. }
  53. if len(row.C) != 2 {
  54. t.Error("Expected len(row.C) == 2")
  55. }
  56. c := row.C[0]
  57. if c.R != "A1" {
  58. t.Error("Expected c.R == 'A1'")
  59. }
  60. if c.T != "s" {
  61. t.Error("Expected c.T == 's'")
  62. }
  63. if c.V.Data != "0" {
  64. t.Error("Expected c.V.Data == '0'")
  65. }
  66. }