file_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package xlsx
  2. import (
  3. . "gopkg.in/check.v1"
  4. )
  5. type FileSuite struct {}
  6. var _ = Suite(&FileSuite{})
  7. // Test we can correctly open a XSLX file and return a xlsx.File
  8. // struct.
  9. func (l *FileSuite) TestOpenFile(c *C) {
  10. var xlsxFile *File
  11. var error error
  12. xlsxFile, error = OpenFile("testfile.xlsx")
  13. c.Assert(error, IsNil)
  14. c.Assert(xlsxFile, NotNil)
  15. }
  16. // Test we can create a File object from scratch
  17. func (l *FileSuite) TestCreateFile(c *C) {
  18. var xlsxFile *File
  19. xlsxFile = NewFile()
  20. c.Assert(xlsxFile, NotNil)
  21. }
  22. // Test that when we open a real XLSX file we create xlsx.Sheet
  23. // objects for the sheets inside the file and that these sheets are
  24. // themselves correct.
  25. func (l *FileSuite) TestCreateSheet(c *C) {
  26. var xlsxFile *File
  27. var err error
  28. var sheet *Sheet
  29. var row *Row
  30. xlsxFile, err = OpenFile("testfile.xlsx")
  31. c.Assert(err, IsNil)
  32. c.Assert(xlsxFile, NotNil)
  33. sheetLen := len(xlsxFile.Sheets)
  34. c.Assert(sheetLen, Equals, 3)
  35. sheet = xlsxFile.Sheets["Tabelle1"]
  36. rowLen := len(sheet.Rows)
  37. c.Assert(rowLen, Equals, 2)
  38. row = sheet.Rows[0]
  39. c.Assert(len(row.Cells), Equals, 2)
  40. cell := row.Cells[0]
  41. cellstring := cell.String()
  42. c.Assert(cellstring, Equals, "Foo")
  43. }
  44. // Test that we can add a sheet to a File
  45. func (l *FileSuite) TestAddSheet(c *C) {
  46. var f *File
  47. f = NewFile()
  48. sheet := f.AddSheet("MySheet")
  49. c.Assert(sheet, NotNil)
  50. c.Assert(len(f.Sheets), Equals, 1)
  51. c.Assert(f.Sheets["MySheet"], Equals, sheet)
  52. }
  53. // Test that we can marshall a File to a collection of xml files
  54. func (l *FileSuite) TestMarshalFile(c *C) {
  55. var f *File
  56. f = NewFile()
  57. sheet1 := f.AddSheet("MySheet")
  58. row1 := sheet1.AddRow()
  59. cell1 := row1.AddCell()
  60. cell1.Value = "A cell!"
  61. sheet2 := f.AddSheet("AnotherSheet")
  62. row2 := sheet2.AddRow()
  63. cell2 := row2.AddCell()
  64. cell2.Value = "A cell!"
  65. parts, err := f.MarshallParts()
  66. c.Assert(err, IsNil)
  67. c.Assert(len(parts), Equals, 7)
  68. expectedSheet := `<?xml version="1.0" encoding="UTF-8"?>
  69. <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  70. <dimension ref="A1:A1"></dimension>
  71. <sheetData>
  72. <row r="1">
  73. <c r="A1" t="s">
  74. <v>0</v>
  75. </c>
  76. </row>
  77. </sheetData>
  78. </worksheet>`
  79. c.Assert(parts["worksheets/sheet1.xml"], Equals, expectedSheet)
  80. c.Assert(parts["worksheets/sheet2.xml"], Equals, expectedSheet)
  81. expectedRels := `<?xml version="1.0" encoding="UTF-8"?>
  82. <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  83. <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
  84. <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
  85. <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
  86. </Relationships>`
  87. c.Assert(parts[".rels"], Equals, expectedRels)
  88. expectedApp := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  89. <Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
  90. <TotalTime>0</TotalTime>
  91. <Application>Go XLSX</Application>
  92. </Properties>`
  93. c.Assert(parts["docProps/app.xml"], Equals, expectedApp)
  94. expectedCore := `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  95. <cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></cp:coreProperties>`
  96. c.Assert(parts["docProps/core.xml"], Equals, expectedCore)
  97. expectedXLSXSST := `<?xml version="1.0" encoding="UTF-8"?>
  98. <sst count="1" uniqueCount="1">
  99. <si>
  100. <t>A cell!</t>
  101. </si>
  102. </sst>`
  103. c.Assert(parts["xl/sharedStrings.xml"], Equals, expectedXLSXSST)
  104. expectedXLSXWorkbookRels := `<?xml version="1.0" encoding="UTF-8"?>
  105. <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  106. <Relationship Id="rId1" Target="worksheets/sheet1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"></Relationship>
  107. <Relationship Id="rId2" Target="worksheets/sheet2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"></Relationship>
  108. <Relationship Id="rId3" Target="sharedStrings.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"></Relationship>
  109. </Relationships>`
  110. c.Assert(parts["xl/_rels/workbook.xml.rels"], Equals, expectedXLSXWorkbookRels)
  111. }