stream_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package xlsx
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "strings"
  8. . "gopkg.in/check.v1"
  9. )
  10. const (
  11. TestsShouldMakeRealFiles = true
  12. )
  13. type StreamSuite struct{}
  14. var _ = Suite(&StreamSuite{})
  15. func (s *StreamSuite) TestTestsShouldMakeRealFilesShouldBeFalse(t *C) {
  16. if TestsShouldMakeRealFiles {
  17. t.Fatal("TestsShouldMakeRealFiles should only be true for local debugging. Don't forget to switch back before commiting.")
  18. }
  19. }
  20. func (s *StreamSuite) TestXlsxStreamWrite(t *C) {
  21. // When shouldMakeRealFiles is set to true this test will make actual XLSX files in the file system.
  22. // This is useful to ensure files open in Excel, Numbers, Google Docs, etc.
  23. // In case of issues you can use "Open XML SDK 2.5" to diagnose issues in generated XLSX files:
  24. // https://www.microsoft.com/en-us/download/details.aspx?id=30425
  25. testCases := []struct {
  26. testName string
  27. sheetNames []string
  28. workbookData [][][]string
  29. cellStyles [][][]int
  30. cellTypes [][][]*CellType
  31. expectedError error
  32. }{
  33. {
  34. testName: "Number Row",
  35. sheetNames: []string{
  36. "Sheet1",
  37. },
  38. workbookData: [][][]string{
  39. {
  40. {"1", "25", "A", "B"},
  41. {"1234", "98", "34", "34"},
  42. },
  43. },
  44. cellStyles: [][][]int{
  45. {
  46. {0,0,0,0},
  47. {0,2,3,0},
  48. },
  49. },
  50. cellTypes: [][][]*CellType{
  51. {
  52. {CellTypeString.Ptr(), CellTypeString.Ptr(), CellTypeString.Ptr(), CellTypeString.Ptr()},
  53. {CellTypeNumeric.Ptr(), CellTypeNumeric.Ptr(), CellTypeNumeric.Ptr(), CellTypeNumeric.Ptr()},
  54. },
  55. },
  56. },
  57. {
  58. testName: "One Sheet",
  59. sheetNames: []string{
  60. "Sheet1",
  61. },
  62. workbookData: [][][]string{
  63. {
  64. {"Token", "Name", "Price", "SKU"},
  65. {"123", "Taco", "300", "0000000123"},
  66. },
  67. },
  68. cellTypes: [][][]*CellType{
  69. {
  70. {CellTypeString.Ptr(), CellTypeString.Ptr(), CellTypeString.Ptr(), CellTypeString.Ptr()},
  71. {CellTypeNumeric.Ptr(), CellTypeString.Ptr(), nil, CellTypeString.Ptr()},
  72. },
  73. },
  74. },
  75. {
  76. testName: "One Column",
  77. sheetNames: []string{
  78. "Sheet1",
  79. },
  80. workbookData: [][][]string{
  81. {
  82. {"Token"},
  83. {"123"},
  84. },
  85. },
  86. },
  87. {
  88. testName: "Several Sheets, with different numbers of columns and rows",
  89. sheetNames: []string{
  90. "Sheet 1", "Sheet 2", "Sheet3",
  91. },
  92. workbookData: [][][]string{
  93. {
  94. {"Token", "Name", "Price", "SKU"},
  95. {"123", "Taco", "300", "0000000123"},
  96. },
  97. {
  98. {"Token", "Name", "Price", "SKU", "Stock"},
  99. {"456", "Salsa", "200", "0346", "1"},
  100. {"789", "Burritos", "400", "754", "3"},
  101. },
  102. {
  103. {"Token", "Name", "Price"},
  104. {"9853", "Guacamole", "500"},
  105. {"2357", "Margarita", "700"},
  106. },
  107. },
  108. },
  109. {
  110. testName: "Two Sheets with same the name",
  111. sheetNames: []string{
  112. "Sheet 1", "Sheet 1",
  113. },
  114. workbookData: [][][]string{
  115. {
  116. {"Token", "Name", "Price", "SKU"},
  117. {"123", "Taco", "300", "0000000123"},
  118. },
  119. {
  120. {"Token", "Name", "Price", "SKU", "Stock"},
  121. {"456", "Salsa", "200", "0346", "1"},
  122. {"789", "Burritos", "400", "754", "3"},
  123. },
  124. },
  125. expectedError: fmt.Errorf("duplicate sheet name '%s'.", "Sheet 1"),
  126. },
  127. {
  128. testName: "One Sheet Registered, tries to write to two",
  129. sheetNames: []string{
  130. "Sheet 1",
  131. },
  132. workbookData: [][][]string{
  133. {
  134. {"Token", "Name", "Price", "SKU"},
  135. {"123", "Taco", "300", "0000000123"},
  136. },
  137. {
  138. {"Token", "Name", "Price", "SKU"},
  139. {"456", "Salsa", "200", "0346"},
  140. },
  141. },
  142. expectedError: AlreadyOnLastSheetError,
  143. },
  144. {
  145. testName: "One Sheet, too many columns in row 1",
  146. sheetNames: []string{
  147. "Sheet 1",
  148. },
  149. workbookData: [][][]string{
  150. {
  151. {"Token", "Name", "Price", "SKU"},
  152. {"123", "Taco", "300", "0000000123", "asdf"},
  153. },
  154. },
  155. expectedError: WrongNumberOfRowsError,
  156. },
  157. {
  158. testName: "One Sheet, too few columns in row 1",
  159. sheetNames: []string{
  160. "Sheet 1",
  161. },
  162. workbookData: [][][]string{
  163. {
  164. {"Token", "Name", "Price", "SKU"},
  165. {"123", "Taco", "300"},
  166. },
  167. },
  168. expectedError: WrongNumberOfRowsError,
  169. },
  170. {
  171. testName: "Lots of Sheets, only writes rows to one, only writes headers to one, should not error and should still create a valid file",
  172. sheetNames: []string{
  173. "Sheet 1", "Sheet 2", "Sheet 3", "Sheet 4", "Sheet 5", "Sheet 6",
  174. },
  175. workbookData: [][][]string{
  176. {
  177. {"Token", "Name", "Price", "SKU"},
  178. {"123", "Taco", "300", "0000000123"},
  179. },
  180. {{}},
  181. {{"Id", "Unit Cost"}},
  182. {{}},
  183. {{}},
  184. {{}},
  185. },
  186. },
  187. {
  188. testName: "Two Sheets, only writes to one, should not error and should still create a valid file",
  189. sheetNames: []string{
  190. "Sheet 1", "Sheet 2",
  191. },
  192. workbookData: [][][]string{
  193. {
  194. {"Token", "Name", "Price", "SKU"},
  195. {"123", "Taco", "300", "0000000123"},
  196. },
  197. {{}},
  198. },
  199. },
  200. {
  201. testName: "Larger Sheet",
  202. sheetNames: []string{
  203. "Sheet 1",
  204. },
  205. workbookData: [][][]string{
  206. {
  207. {"Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU"},
  208. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  209. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  210. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  211. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  212. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  213. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  214. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  215. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  216. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  217. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  218. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  219. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  220. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  221. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  222. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  223. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  224. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  225. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  226. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  227. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  228. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  229. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  230. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  231. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  232. },
  233. },
  234. },
  235. {
  236. testName: "UTF-8 Characters. This XLSX File loads correctly with Excel, Numbers, and Google Docs. It also passes Microsoft's Office File Format Validator.",
  237. sheetNames: []string{
  238. "Sheet1",
  239. },
  240. workbookData: [][][]string{
  241. {
  242. // String courtesy of https://github.com/minimaxir/big-list-of-naughty-strings/
  243. // Header row contains the tags that I am filtering on
  244. {"Token", endSheetDataTag, "Price", fmt.Sprintf(dimensionTag, "A1:D1")},
  245. // Japanese and emojis
  246. {"123", "パーティーへ行かないか", "300", "🍕🐵 🙈 🙉 🙊"},
  247. // XML encoder/parser test strings
  248. {"123", `<?xml version="1.0" encoding="ISO-8859-1"?>`, "300", `<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE foo [ <!ELEMENT foo ANY ><!ENTITY xxe SYSTEM "file:///etc/passwd" >]><foo>&xxe;</foo>`},
  249. // Upside down text and Right to Left Arabic text
  250. {"123", `˙ɐnbᴉlɐ ɐuƃɐɯ ǝɹolop ʇǝ ǝɹoqɐl ʇn ʇunpᴉpᴉɔuᴉ ɹodɯǝʇ poɯsnᴉǝ op pǝs 'ʇᴉlǝ ƃuᴉɔsᴉdᴉpɐ ɹnʇǝʇɔǝsuoɔ 'ʇǝɯɐ ʇᴉs ɹolop ɯnsdᴉ ɯǝɹo˥
  251. 00˙Ɩ$-`, "300", `ﷺ`},
  252. {"123", "Taco", "300", "0000000123"},
  253. },
  254. },
  255. },
  256. }
  257. for i, testCase := range testCases {
  258. fmt.Print("Current Test case: ")
  259. fmt.Println(testCase.testName)
  260. var filePath string
  261. var buffer bytes.Buffer
  262. if TestsShouldMakeRealFiles {
  263. filePath = fmt.Sprintf("Workbook%d.xlsx", i)
  264. }
  265. if testCase.cellStyles == nil {
  266. testCase.cellStyles = [][][]int{}
  267. //testCase.cellStyles = append(testCase.cellStyles, [][]int{})
  268. for j,_ := range testCase.workbookData{
  269. testCase.cellStyles = append(testCase.cellStyles, [][]int{})
  270. for k,_ := range testCase.workbookData[j]{
  271. testCase.cellStyles[j] = append(testCase.cellStyles[j], []int{})
  272. for _,_ = range testCase.workbookData[j][k]{
  273. testCase.cellStyles[j][k] = append(testCase.cellStyles[j][k], 0)
  274. }
  275. }
  276. }
  277. }
  278. if testCase.cellTypes == nil {
  279. testCase.cellTypes = [][][]*CellType{}
  280. //testCase.cellTypes = append(testCase.cellTypes, [][]*CellType{})
  281. for j,_ := range testCase.workbookData{
  282. testCase.cellTypes = append(testCase.cellTypes, [][]*CellType{})
  283. for k,_ := range testCase.workbookData[j]{
  284. testCase.cellTypes[j] = append(testCase.cellTypes[j], []*CellType{})
  285. for _,_ = range testCase.workbookData[j][k]{
  286. testCase.cellTypes[j][k] = append(testCase.cellTypes[j][k], CellTypeString.Ptr())
  287. }
  288. }
  289. }
  290. }
  291. err := writeStreamFile(filePath, &buffer, testCase.sheetNames, testCase.workbookData, testCase.cellStyles, testCase.cellTypes, TestsShouldMakeRealFiles)
  292. if err != testCase.expectedError && err.Error() != testCase.expectedError.Error() {
  293. t.Fatalf("Error differs from expected error. Error: %v, Expected Error: %v ", err, testCase.expectedError)
  294. }
  295. if testCase.expectedError != nil {
  296. return
  297. }
  298. // read the file back with the xlsx package
  299. var bufReader *bytes.Reader
  300. var size int64
  301. if !TestsShouldMakeRealFiles {
  302. bufReader = bytes.NewReader(buffer.Bytes())
  303. size = bufReader.Size()
  304. }
  305. actualSheetNames, actualWorkbookData := readXLSXFile(t, filePath, bufReader, size, TestsShouldMakeRealFiles)
  306. // check if data was able to be read correctly
  307. if !reflect.DeepEqual(actualSheetNames, testCase.sheetNames) {
  308. t.Fatal("Expected sheet names to be equal")
  309. }
  310. if !reflect.DeepEqual(actualWorkbookData, testCase.workbookData) {
  311. t.Fatal("Expected workbook data to be equal")
  312. }
  313. }
  314. }
  315. // The purpose of TestXlsxStyleBehavior is to ensure that initMaxStyleId has the correct starting value
  316. // and that the logic in AddSheet() that predicts Style IDs is correct.
  317. func (s *StreamSuite) TestXlsxStyleBehavior(t *C) {
  318. file := NewFile()
  319. sheet, err := file.AddSheet("Sheet 1")
  320. if err != nil {
  321. t.Fatal(err)
  322. }
  323. row := sheet.AddRow()
  324. rowData := []string{"testing", "1", "2", "3"}
  325. if count := row.WriteSlice(&rowData, -1); count != len(rowData) {
  326. t.Fatal("not enough cells written")
  327. }
  328. parts, err := file.MarshallParts()
  329. styleSheet, ok := parts["xl/styles.xml"]
  330. if !ok {
  331. t.Fatal("no style sheet")
  332. }
  333. // Created an XLSX file with only the default style.
  334. // We expect that the number of styles is one more than our max index constant.
  335. // This means the library adds two styles by default.
  336. if !strings.Contains(styleSheet, fmt.Sprintf(`<cellXfs count="%d">`, initMaxStyleId+1)) {
  337. t.Fatal("Expected sheet to have two styles")
  338. }
  339. file = NewFile()
  340. sheet, err = file.AddSheet("Sheet 1")
  341. if err != nil {
  342. t.Fatal(err)
  343. }
  344. row = sheet.AddRow()
  345. rowData = []string{"testing", "1", "2", "3", "4"}
  346. if count := row.WriteSlice(&rowData, -1); count != len(rowData) {
  347. t.Fatal("not enough cells written")
  348. }
  349. sheet.Cols[0].SetType(CellTypeString)
  350. sheet.Cols[1].SetType(CellTypeString)
  351. sheet.Cols[3].SetType(CellTypeNumeric)
  352. sheet.Cols[4].SetType(CellTypeString)
  353. parts, err = file.MarshallParts()
  354. styleSheet, ok = parts["xl/styles.xml"]
  355. if !ok {
  356. t.Fatal("no style sheet")
  357. }
  358. // Created an XLSX file with two distinct cell types, which should create two new styles.
  359. // The same cell type was added three times, this should be coalesced into the same style rather than
  360. // recreating the style. This XLSX stream library depends on this behavior when predicting the next style id.
  361. if !strings.Contains(styleSheet, fmt.Sprintf(`<cellXfs count="%d">`, initMaxStyleId+1+2)) {
  362. t.Fatal("Expected sheet to have four styles")
  363. }
  364. }
  365. // writeStreamFile will write the file using this stream package
  366. func writeStreamFile(filePath string, fileBuffer io.Writer, sheetNames []string, workbookData [][][]string, cellStyles [][][]int, cellTypes [][][]*CellType, shouldMakeRealFiles bool) error {
  367. var file *StreamFileBuilder
  368. var err error
  369. if shouldMakeRealFiles {
  370. file, err = NewStreamFileBuilderForPath(filePath)
  371. if err != nil {
  372. return err
  373. }
  374. } else {
  375. file = NewStreamFileBuilder(fileBuffer)
  376. }
  377. for i, sheetName := range sheetNames {
  378. header := workbookData[i][0]
  379. headerCellStyles := cellStyles[i][0]
  380. var sheetHeaderTypes []*CellType
  381. if i < len(cellTypes) {
  382. sheetHeaderTypes = cellTypes[i][0]
  383. }
  384. err := file.AddSheet(sheetName, header, headerCellStyles, sheetHeaderTypes)
  385. if err != nil {
  386. return err
  387. }
  388. }
  389. streamFile, err := file.Build()
  390. if err != nil {
  391. return err
  392. }
  393. for i, sheetData := range workbookData {
  394. currentSheetCellTypes := cellTypes[i]
  395. currentSheetCellStyles := cellStyles[i]
  396. if i != 0 {
  397. err = streamFile.NextSheet()
  398. if err != nil {
  399. return err
  400. }
  401. }
  402. for i, row := range sheetData {
  403. if i == 0 {
  404. continue
  405. }
  406. currentCellStyles := currentSheetCellStyles[i]
  407. currentCellTypes := currentSheetCellTypes[i]
  408. err = streamFile.Write(row, currentCellTypes, currentCellStyles)
  409. if err != nil {
  410. return err
  411. }
  412. }
  413. }
  414. err = streamFile.Close()
  415. if err != nil {
  416. return err
  417. }
  418. return nil
  419. }
  420. // readXLSXFile will read the file using the xlsx package.
  421. func readXLSXFile(t *C, filePath string, fileBuffer io.ReaderAt, size int64, shouldMakeRealFiles bool) ([]string, [][][]string) {
  422. var readFile *File
  423. var err error
  424. if shouldMakeRealFiles {
  425. readFile, err = OpenFile(filePath)
  426. if err != nil {
  427. t.Fatal(err)
  428. }
  429. } else {
  430. readFile, err = OpenReaderAt(fileBuffer, size)
  431. if err != nil {
  432. t.Fatal(err)
  433. }
  434. }
  435. var actualWorkbookData [][][]string
  436. var sheetNames []string
  437. for _, sheet := range readFile.Sheets {
  438. sheetData := [][]string{}
  439. for _, row := range sheet.Rows {
  440. data := []string{}
  441. for _, cell := range row.Cells {
  442. str, err := cell.FormattedValue()
  443. if err != nil {
  444. t.Fatal(err)
  445. }
  446. data = append(data, str)
  447. }
  448. sheetData = append(sheetData, data)
  449. }
  450. sheetNames = append(sheetNames, sheet.Name)
  451. actualWorkbookData = append(actualWorkbookData, sheetData)
  452. }
  453. return sheetNames, actualWorkbookData
  454. }
  455. func (s *StreamSuite) TestAddSheetErrorsAfterBuild(t *C) {
  456. file := NewStreamFileBuilder(bytes.NewBuffer(nil))
  457. err := file.AddSheet("Sheet1", []string{"Header"}, []int{0}, nil)
  458. if err != nil {
  459. t.Fatal(err)
  460. }
  461. err = file.AddSheet("Sheet2", []string{"Header2"}, []int{0}, nil)
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. _, err = file.Build()
  466. if err != nil {
  467. t.Fatal(err)
  468. }
  469. err = file.AddSheet("Sheet3", []string{"Header3"}, []int{0}, nil)
  470. if err != BuiltStreamFileBuilderError {
  471. t.Fatal(err)
  472. }
  473. }
  474. func (s *StreamSuite) TestBuildErrorsAfterBuild(t *C) {
  475. file := NewStreamFileBuilder(bytes.NewBuffer(nil))
  476. err := file.AddSheet("Sheet1", []string{"Header"}, []int{0}, nil)
  477. if err != nil {
  478. t.Fatal(err)
  479. }
  480. err = file.AddSheet("Sheet2", []string{"Header2"}, []int{0}, nil)
  481. if err != nil {
  482. t.Fatal(err)
  483. }
  484. _, err = file.Build()
  485. if err != nil {
  486. t.Fatal(err)
  487. }
  488. _, err = file.Build()
  489. if err != BuiltStreamFileBuilderError {
  490. t.Fatal(err)
  491. }
  492. }
  493. func (s *StreamSuite) TestCloseWithNothingWrittenToSheets(t *C) {
  494. buffer := bytes.NewBuffer(nil)
  495. file := NewStreamFileBuilder(buffer)
  496. sheetNames := []string{"Sheet1", "Sheet2"}
  497. workbookData := [][][]string{
  498. {{"Header1", "Header2"}},
  499. {{"Header3", "Header4"}},
  500. }
  501. cellStyles := [][][]int{
  502. {{0, 0}},
  503. {{0, 0}},
  504. }
  505. cellTypes := [][][]*CellType{
  506. {{CellTypeString.Ptr(), CellTypeString.Ptr()}},
  507. {{CellTypeString.Ptr(), CellTypeString.Ptr()}},
  508. }
  509. err := file.AddSheet(sheetNames[0], workbookData[0][0], cellStyles[0][0], cellTypes[0][0])
  510. if err != nil {
  511. t.Fatal(err)
  512. }
  513. err = file.AddSheet(sheetNames[1], workbookData[1][0], cellStyles[1][0], cellTypes[1][0])
  514. if err != nil {
  515. t.Fatal(err)
  516. }
  517. stream, err := file.Build()
  518. if err != nil {
  519. t.Fatal(err)
  520. }
  521. err = stream.Close()
  522. if err != nil {
  523. t.Fatal(err)
  524. }
  525. bufReader := bytes.NewReader(buffer.Bytes())
  526. size := bufReader.Size()
  527. actualSheetNames, actualWorkbookData := readXLSXFile(t, "", bufReader, size, false)
  528. // check if data was able to be read correctly
  529. if !reflect.DeepEqual(actualSheetNames, sheetNames) {
  530. t.Fatal("Expected sheet names to be equal")
  531. }
  532. if !reflect.DeepEqual(actualWorkbookData, workbookData) {
  533. t.Fatal("Expected workbook data to be equal")
  534. }
  535. }