worksheet_test.go 3.1 KB

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