xmlWorkbook_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package xlsx
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. . "gopkg.in/check.v1"
  6. )
  7. type WorkbookSuite struct{}
  8. var _ = Suite(&WorkbookSuite{})
  9. // Test we can succesfully unmarshal the workbook.xml file from within
  10. // an XLSX file and return a xlsxWorkbook struct (and associated
  11. // children).
  12. func (w *WorkbookSuite) TestUnmarshallWorkbookXML(c *C) {
  13. var buf = bytes.NewBufferString(
  14. `<?xml version="1.0"
  15. encoding="UTF-8"
  16. standalone="yes"?>
  17. <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
  18. xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
  19. <fileVersion appName="xl"
  20. lastEdited="4"
  21. lowestEdited="4"
  22. rupBuild="4506"/>
  23. <workbookPr defaultThemeVersion="124226" date1904="true"/>
  24. <bookViews>
  25. <workbookView xWindow="120"
  26. yWindow="75"
  27. windowWidth="15135"
  28. windowHeight="7620"/>
  29. </bookViews>
  30. <sheets>
  31. <sheet name="Sheet1"
  32. sheetId="1"
  33. r:id="rId1"
  34. state="visible"/>
  35. <sheet name="Sheet2"
  36. sheetId="2"
  37. r:id="rId2"
  38. state="hidden"/>
  39. <sheet name="Sheet3"
  40. sheetId="3"
  41. r:id="rId3"
  42. state="veryHidden"/>
  43. </sheets>
  44. <definedNames>
  45. <definedName name="monitors" comment="this is the comment"
  46. description="give cells a name"
  47. localSheetId="0">Sheet1!$A$1533</definedName>
  48. </definedNames>
  49. <calcPr calcId="125725"/>
  50. </workbook>`)
  51. var workbook *xlsxWorkbook
  52. workbook = new(xlsxWorkbook)
  53. err := xml.NewDecoder(buf).Decode(workbook)
  54. c.Assert(err, IsNil)
  55. c.Assert(workbook.FileVersion.AppName, Equals, "xl")
  56. c.Assert(workbook.FileVersion.LastEdited, Equals, "4")
  57. c.Assert(workbook.FileVersion.LowestEdited, Equals, "4")
  58. c.Assert(workbook.FileVersion.RupBuild, Equals, "4506")
  59. c.Assert(workbook.WorkbookPr.DefaultThemeVersion, Equals, "124226")
  60. c.Assert(workbook.WorkbookPr.Date1904, Equals, true)
  61. c.Assert(workbook.BookViews.WorkBookView, HasLen, 1)
  62. workBookView := workbook.BookViews.WorkBookView[0]
  63. c.Assert(workBookView.XWindow, Equals, "120")
  64. c.Assert(workBookView.YWindow, Equals, "75")
  65. c.Assert(workBookView.WindowWidth, Equals, 15135)
  66. c.Assert(workBookView.WindowHeight, Equals, 7620)
  67. c.Assert(workbook.Sheets.Sheet, HasLen, 3)
  68. sheet := workbook.Sheets.Sheet[0]
  69. c.Assert(sheet.Id, Equals, "rId1")
  70. c.Assert(sheet.Name, Equals, "Sheet1")
  71. c.Assert(sheet.SheetId, Equals, "1")
  72. c.Assert(sheet.State, Equals, "visible")
  73. c.Assert(workbook.DefinedNames.DefinedName, HasLen, 1)
  74. dname := workbook.DefinedNames.DefinedName[0]
  75. c.Assert(dname.Data, Equals, "Sheet1!$A$1533")
  76. c.Assert(dname.LocalSheetID, Equals, 0)
  77. c.Assert(dname.Name, Equals, "monitors")
  78. c.Assert(dname.Comment, Equals, "this is the comment")
  79. c.Assert(dname.Description, Equals, "give cells a name")
  80. c.Assert(workbook.CalcPr.CalcId, Equals, "125725")
  81. }
  82. // Test we can marshall a Workbook to xml
  83. func (w *WorkbookSuite) TestMarshallWorkbook(c *C) {
  84. var workbook *xlsxWorkbook
  85. workbook = new(xlsxWorkbook)
  86. workbook.FileVersion = xlsxFileVersion{}
  87. workbook.FileVersion.AppName = "xlsx"
  88. workbook.WorkbookPr = xlsxWorkbookPr{BackupFile: false}
  89. workbook.BookViews = xlsxBookViews{}
  90. workbook.BookViews.WorkBookView = make([]xlsxWorkBookView, 1)
  91. workbook.BookViews.WorkBookView[0] = xlsxWorkBookView{}
  92. workbook.Sheets = xlsxSheets{}
  93. workbook.Sheets.Sheet = make([]xlsxSheet, 1)
  94. workbook.Sheets.Sheet[0] = xlsxSheet{Name: "sheet1", SheetId: "1", Id: "rId2"}
  95. body, err := xml.Marshal(workbook)
  96. c.Assert(err, IsNil)
  97. expectedWorkbook := `<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fileVersion appName="xlsx"></fileVersion><workbookPr date1904="false"></workbookPr><workbookProtection></workbookProtection><bookViews><workbookView></workbookView></bookViews><sheets><sheet name="sheet1" sheetId="1" xmlns:relationships="http://schemas.openxmlformats.org/officeDocument/2006/relationships" relationships:id="rId2"></sheet></sheets><definedNames></definedNames><calcPr></calcPr></workbook>`
  98. c.Assert(string(body), Equals, expectedWorkbook)
  99. }