worksheet_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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">
  13. <sheetPr filterMode="false">
  14. <pageSetUpPr fitToPage="false"/>
  15. </sheetPr>
  16. <dimension ref="A1:B2"/>
  17. <sheetViews>
  18. <sheetView colorId="64" defaultGridColor="true" rightToLeft="false" showFormulas="false" showGridLines="true" showOutlineSymbols="true" showRowColHeaders="true" showZeros="true" tabSelected="true" topLeftCell="A1" view="normal" windowProtection="false" workbookViewId="0" zoomScale="100" zoomScaleNormal="100" zoomScalePageLayoutView="100">
  19. <selection activeCell="B2" activeCellId="0" pane="topLeft" sqref="B2"/>
  20. </sheetView>
  21. </sheetViews>
  22. <sheetFormatPr defaultRowHeight="15">
  23. </sheetFormatPr>
  24. <cols>
  25. <col collapsed="false" hidden="false" max="1025" min="1" style="0" width="10.5748987854251"/>
  26. </cols>
  27. <sheetData>
  28. <row collapsed="false" customFormat="false" customHeight="false" hidden="false" ht="14.9" outlineLevel="0" r="1">
  29. <c r="A1" s="1" t="s">
  30. <v>0</v>
  31. </c>
  32. <c r="B1" s="0" t="s">
  33. <v>1</v>
  34. </c>
  35. </row>
  36. <row collapsed="false" customFormat="false" customHeight="false" hidden="false" ht="14.9" outlineLevel="0" r="2">
  37. <c r="A2" s="0" t="s">
  38. <v>2</v>
  39. </c>
  40. <c r="B2" s="2" t="s">
  41. <v>3</v>
  42. </c>
  43. </row>
  44. </sheetData>
  45. <printOptions headings="false" gridLines="false" gridLinesSet="true" horizontalCentered="false" verticalCentered="false"/>
  46. <pageMargins left="0.7" right="0.7" top="0.7875" bottom="0.7875" header="0.511805555555555" footer="0.511805555555555"/>
  47. <pageSetup blackAndWhite="false" cellComments="none" copies="1" draft="false" firstPageNumber="0" fitToHeight="1" fitToWidth="1" horizontalDpi="300" orientation="portrait" pageOrder="downThenOver" paperSize="9" scale="100" useFirstPageNumber="false" usePrinterDefaults="false" verticalDpi="300"/>
  48. <headerFooter differentFirst="false" differentOddEven="false">
  49. <oddHeader>
  50. </oddHeader>
  51. <oddFooter>
  52. </oddFooter>
  53. </headerFooter>
  54. </worksheet>`)
  55. worksheet := new(xlsxWorksheet)
  56. error := xml.NewDecoder(sheetxml).Decode(worksheet)
  57. if error != nil {
  58. t.Error(error.Error())
  59. return
  60. }
  61. if worksheet.Dimension.Ref != "A1:B2" {
  62. t.Error(fmt.Sprintf("Expected worksheet.Dimension.Ref == 'A1:B2', got %s", worksheet.Dimension.Ref))
  63. }
  64. if len(worksheet.SheetData.Row) == 0 {
  65. t.Error(fmt.Sprintf("Expected len(worksheet.SheetData.Row) == '2', got %d", worksheet.SheetData.Row))
  66. }
  67. row := worksheet.SheetData.Row[0]
  68. if row.R != 1 {
  69. t.Error(fmt.Sprintf("Expected row.r == 1, got %d", row.R))
  70. }
  71. if len(row.C) != 2 {
  72. t.Error(fmt.Sprintf("Expected len(row.C) == 2, got %s", row.C))
  73. }
  74. c := row.C[0]
  75. if c.R != "A1" {
  76. t.Error(fmt.Sprintf("Expected c.R == 'A1' got %s", c.R))
  77. }
  78. if c.T != "s" {
  79. t.Error(fmt.Sprintf("Expected c.T == 's' got %s", c.T))
  80. }
  81. if c.V != "0" {
  82. t.Error(fmt.Sprintf("Expected c.V.Data == '0', got %s", c.V))
  83. }
  84. }