stream_test.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. package xlsx
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. . "gopkg.in/check.v1"
  10. )
  11. const (
  12. TestsShouldMakeRealFiles = false
  13. )
  14. type StreamSuite struct{}
  15. var _ = Suite(&StreamSuite{})
  16. func (s *StreamSuite) TestTestsShouldMakeRealFilesShouldBeFalse(t *C) {
  17. if TestsShouldMakeRealFiles {
  18. t.Fatal("TestsShouldMakeRealFiles should only be true for local debugging. Don't forget to switch back before commiting.")
  19. }
  20. }
  21. func TestXlsxStreamWrite(t *testing.T) {
  22. // When shouldMakeRealFiles is set to true this test will make actual XLSX files in the file system.
  23. // This is useful to ensure files open in Excel, Numbers, Google Docs, etc.
  24. // In case of issues you can use "Open XML SDK 2.5" to diagnose issues in generated XLSX files:
  25. // https://www.microsoft.com/en-us/download/details.aspx?id=30425
  26. testCases := []struct {
  27. testName string
  28. sheetNames []string
  29. workbookData [][][]string
  30. headerTypes [][]*CellType
  31. expectedError error
  32. }{
  33. {
  34. testName: "One Sheet",
  35. sheetNames: []string{
  36. "Sheet1",
  37. },
  38. workbookData: [][][]string{
  39. {
  40. {"Token", "Name", "Price", "SKU"},
  41. {"123", "Taco", "300", "0000000123"},
  42. },
  43. },
  44. headerTypes: [][]*CellType{
  45. {nil, CellTypeString.Ptr(), nil, CellTypeString.Ptr()},
  46. },
  47. },
  48. {
  49. testName: "One Column",
  50. sheetNames: []string{
  51. "Sheet1",
  52. },
  53. workbookData: [][][]string{
  54. {
  55. {"Token"},
  56. {"123"},
  57. },
  58. },
  59. },
  60. {
  61. testName: "Several Sheets, with different numbers of columns and rows",
  62. sheetNames: []string{
  63. "Sheet 1", "Sheet 2", "Sheet3",
  64. },
  65. workbookData: [][][]string{
  66. {
  67. {"Token", "Name", "Price", "SKU"},
  68. {"123", "Taco", "300", "0000000123"},
  69. },
  70. {
  71. {"Token", "Name", "Price", "SKU", "Stock"},
  72. {"456", "Salsa", "200", "0346", "1"},
  73. {"789", "Burritos", "400", "754", "3"},
  74. },
  75. {
  76. {"Token", "Name", "Price"},
  77. {"9853", "Guacamole", "500"},
  78. {"2357", "Margarita", "700"},
  79. },
  80. },
  81. },
  82. {
  83. testName: "Two Sheets with same the name",
  84. sheetNames: []string{
  85. "Sheet 1", "Sheet 1",
  86. },
  87. workbookData: [][][]string{
  88. {
  89. {"Token", "Name", "Price", "SKU"},
  90. {"123", "Taco", "300", "0000000123"},
  91. },
  92. {
  93. {"Token", "Name", "Price", "SKU", "Stock"},
  94. {"456", "Salsa", "200", "0346", "1"},
  95. {"789", "Burritos", "400", "754", "3"},
  96. },
  97. },
  98. expectedError: fmt.Errorf("duplicate sheet name '%s'.", "Sheet 1"),
  99. },
  100. {
  101. testName: "One Sheet Registered, tries to write to two",
  102. sheetNames: []string{
  103. "Sheet 1",
  104. },
  105. workbookData: [][][]string{
  106. {
  107. {"Token", "Name", "Price", "SKU"},
  108. {"123", "Taco", "300", "0000000123"},
  109. },
  110. {
  111. {"Token", "Name", "Price", "SKU"},
  112. {"456", "Salsa", "200", "0346"},
  113. },
  114. },
  115. expectedError: AlreadyOnLastSheetError,
  116. },
  117. {
  118. testName: "One Sheet, too many columns in row 1",
  119. sheetNames: []string{
  120. "Sheet 1",
  121. },
  122. workbookData: [][][]string{
  123. {
  124. {"Token", "Name", "Price", "SKU"},
  125. {"123", "Taco", "300", "0000000123", "asdf"},
  126. },
  127. },
  128. expectedError: WrongNumberOfRowsError,
  129. },
  130. {
  131. testName: "One Sheet, too few columns in row 1",
  132. sheetNames: []string{
  133. "Sheet 1",
  134. },
  135. workbookData: [][][]string{
  136. {
  137. {"Token", "Name", "Price", "SKU"},
  138. {"123", "Taco", "300"},
  139. },
  140. },
  141. expectedError: WrongNumberOfRowsError,
  142. },
  143. {
  144. testName: "Lots of Sheets, only writes rows to one, only writes headers to one, should not error and should still create a valid file",
  145. sheetNames: []string{
  146. "Sheet 1", "Sheet 2", "Sheet 3", "Sheet 4", "Sheet 5", "Sheet 6",
  147. },
  148. workbookData: [][][]string{
  149. {
  150. {"Token", "Name", "Price", "SKU"},
  151. {"123", "Taco", "300", "0000000123"},
  152. },
  153. {{}},
  154. {{"Id", "Unit Cost"}},
  155. {{}},
  156. {{}},
  157. {{}},
  158. },
  159. },
  160. {
  161. testName: "Two Sheets, only writes to one, should not error and should still create a valid file",
  162. sheetNames: []string{
  163. "Sheet 1", "Sheet 2",
  164. },
  165. workbookData: [][][]string{
  166. {
  167. {"Token", "Name", "Price", "SKU"},
  168. {"123", "Taco", "300", "0000000123"},
  169. },
  170. {{}},
  171. },
  172. },
  173. {
  174. testName: "Larger Sheet",
  175. sheetNames: []string{
  176. "Sheet 1",
  177. },
  178. workbookData: [][][]string{
  179. {
  180. {"Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU"},
  181. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  182. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  183. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  184. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  185. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  186. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  187. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  188. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  189. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  190. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  191. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  192. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  193. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  194. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  195. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  196. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  197. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  198. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  199. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  200. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  201. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  202. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  203. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  204. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  205. },
  206. },
  207. },
  208. {
  209. testName: "UTF-8 Characters. This XLSX File loads correctly with Excel, Numbers, and Google Docs. It also passes Microsoft's Office File Format Validator.",
  210. sheetNames: []string{
  211. "Sheet1",
  212. },
  213. workbookData: [][][]string{
  214. {
  215. // String courtesy of https://github.com/minimaxir/big-list-of-naughty-strings/
  216. // Header row contains the tags that I am filtering on
  217. {"Token", endSheetDataTag, "Price", fmt.Sprintf(dimensionTag, "A1:D1")},
  218. // Japanese and emojis
  219. {"123", "パーティーへ行かないか", "300", "🍕🐵 🙈 🙉 🙊"},
  220. // XML encoder/parser test strings
  221. {"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>`},
  222. // Upside down text and Right to Left Arabic text
  223. {"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˥
  224. 00˙Ɩ$-`, "300", `ﷺ`},
  225. {"123", "Taco", "300", "0000000123"},
  226. },
  227. },
  228. },
  229. }
  230. for i, testCase := range testCases {
  231. t.Run(testCase.testName, func(t *testing.T) {
  232. var filePath string
  233. var buffer bytes.Buffer
  234. if TestsShouldMakeRealFiles {
  235. filePath = fmt.Sprintf("Workbook%d.xlsx", i)
  236. }
  237. err := writeStreamFile(filePath, &buffer, testCase.sheetNames, testCase.workbookData, testCase.headerTypes, TestsShouldMakeRealFiles)
  238. switch {
  239. case err != nil && testCase.expectedError == nil:
  240. t.Fatalf("Unexpected error: %v", err.Error())
  241. case err == nil && testCase.expectedError != nil:
  242. t.Fatalf("Error is nil, but expected error was: %v", testCase.expectedError)
  243. case err != nil && testCase.expectedError != nil && err.Error() != testCase.expectedError.Error():
  244. t.Fatalf("Error differs from expected error. Error: %v, Expected Error: %v ", err, testCase.expectedError)
  245. }
  246. if testCase.expectedError != nil {
  247. return
  248. }
  249. // read the file back with the xlsx package
  250. var bufReader *bytes.Reader
  251. var size int64
  252. if !TestsShouldMakeRealFiles {
  253. bufReader = bytes.NewReader(buffer.Bytes())
  254. size = bufReader.Size()
  255. }
  256. actualSheetNames, actualWorkbookData, _ := readXLSXFile(t, filePath, bufReader, size, TestsShouldMakeRealFiles)
  257. // check if data was able to be read correctly
  258. if !reflect.DeepEqual(actualSheetNames, testCase.sheetNames) {
  259. t.Fatal("Expected sheet names to be equal")
  260. }
  261. if !reflect.DeepEqual(actualWorkbookData, testCase.workbookData) {
  262. t.Fatal("Expected workbook data to be equal")
  263. }
  264. })
  265. }
  266. }
  267. func TestXlsxStreamWriteWithDefaultCellType(t *testing.T) {
  268. // When shouldMakeRealFiles is set to true this test will make actual XLSX files in the file system.
  269. // This is useful to ensure files open in Excel, Numbers, Google Docs, etc.
  270. // In case of issues you can use "Open XML SDK 2.5" to diagnose issues in generated XLSX files:
  271. // https://www.microsoft.com/en-us/download/details.aspx?id=30425
  272. testCases := []struct {
  273. testName string
  274. sheetNames []string
  275. workbookData [][][]string
  276. expectedWorkbookData [][][]string
  277. headerTypes [][]*StreamingCellMetadata
  278. expectedError error
  279. }{
  280. {
  281. testName: "One Sheet",
  282. sheetNames: []string{
  283. "Sheet1",
  284. },
  285. workbookData: [][][]string{
  286. {
  287. {"Token", "Name", "Price", "SKU"},
  288. {"123", "Taco", "300.0", "0000000123"},
  289. {"123", "Taco", "string", "0000000123"},
  290. },
  291. },
  292. expectedWorkbookData: [][][]string{
  293. {
  294. {"Token", "Name", "Price", "SKU"},
  295. {"123", "Taco", "300.00", "0000000123"},
  296. {"123", "Taco", "string", "0000000123"},
  297. },
  298. },
  299. headerTypes: [][]*StreamingCellMetadata{
  300. {DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultDecimalStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr()},
  301. },
  302. },
  303. {
  304. testName: "One Column",
  305. sheetNames: []string{
  306. "Sheet1",
  307. },
  308. workbookData: [][][]string{
  309. {
  310. {"Token"},
  311. {"1234"},
  312. },
  313. },
  314. expectedWorkbookData: [][][]string{
  315. {
  316. {"Token"},
  317. {"1234.00"},
  318. },
  319. },
  320. headerTypes: [][]*StreamingCellMetadata{
  321. {DefaultDecimalStreamingCellMetadata.Ptr()},
  322. },
  323. },
  324. {
  325. testName: "Several Sheets, with different numbers of columns and rows",
  326. sheetNames: []string{
  327. "Sheet 1", "Sheet 2", "Sheet3",
  328. },
  329. workbookData: [][][]string{
  330. {
  331. {"Token", "Name", "Price", "SKU"},
  332. {"123", "Taco", "300", "0000000123"},
  333. },
  334. {
  335. {"Token", "Name", "Price", "SKU", "Stock"},
  336. {"456", "Salsa", "200", "0346", "1"},
  337. {"789", "Burritos", "400", "754", "3"},
  338. },
  339. {
  340. {"Token", "Name", "Price"},
  341. {"9853", "Guacamole", "500"},
  342. {"2357", "Margarita", "700"},
  343. },
  344. },
  345. expectedWorkbookData: [][][]string{
  346. {
  347. {"Token", "Name", "Price", "SKU"},
  348. {"123", "Taco", "300.00", "0000000123"},
  349. },
  350. {
  351. {"Token", "Name", "Price", "SKU", "Stock"},
  352. {"456", "Salsa", "200.00", "0346", "1"},
  353. {"789", "Burritos", "400.00", "754", "3"},
  354. },
  355. {
  356. {"Token", "Name", "Price"},
  357. {"9853", "Guacamole", "500"},
  358. {"2357", "Margarita", "700"},
  359. },
  360. },
  361. headerTypes: [][]*StreamingCellMetadata{
  362. {DefaultIntegerStreamingCellMetadata.Ptr(), nil, DefaultDecimalStreamingCellMetadata.Ptr(), nil},
  363. {DefaultIntegerStreamingCellMetadata.Ptr(), nil, DefaultDecimalStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultIntegerStreamingCellMetadata.Ptr()},
  364. {nil, nil, nil},
  365. },
  366. },
  367. {
  368. testName: "Two Sheets with the same name",
  369. sheetNames: []string{
  370. "Sheet 1", "Sheet 1",
  371. },
  372. workbookData: [][][]string{
  373. {
  374. {"Token", "Name", "Price", "SKU"},
  375. {"123", "Taco", "300", "0000000123"},
  376. },
  377. {
  378. {"Token", "Name", "Price", "SKU", "Stock"},
  379. {"456", "Salsa", "200", "0346", "1"},
  380. {"789", "Burritos", "400", "754", "3"},
  381. },
  382. },
  383. expectedError: fmt.Errorf("duplicate sheet name '%s'.", "Sheet 1"),
  384. },
  385. {
  386. testName: "One Sheet Registered, tries to write to two",
  387. sheetNames: []string{
  388. "Sheet 1",
  389. },
  390. workbookData: [][][]string{
  391. {
  392. {"Token", "Name", "Price", "SKU"},
  393. {"123", "Taco", "300", "0000000123"},
  394. },
  395. {
  396. {"Token", "Name", "Price", "SKU"},
  397. {"456", "Salsa", "200", "0346"},
  398. },
  399. },
  400. expectedError: AlreadyOnLastSheetError,
  401. },
  402. {
  403. testName: "One Sheet, too many columns in row 1",
  404. sheetNames: []string{
  405. "Sheet 1",
  406. },
  407. workbookData: [][][]string{
  408. {
  409. {"Token", "Name", "Price", "SKU"},
  410. {"123", "Taco", "300", "0000000123", "asdf"},
  411. },
  412. },
  413. expectedError: WrongNumberOfRowsError,
  414. },
  415. {
  416. testName: "One Sheet, too few columns in row 1",
  417. sheetNames: []string{
  418. "Sheet 1",
  419. },
  420. workbookData: [][][]string{
  421. {
  422. {"Token", "Name", "Price", "SKU"},
  423. {"123", "Taco", "300"},
  424. },
  425. },
  426. expectedError: WrongNumberOfRowsError,
  427. },
  428. {
  429. testName: "Lots of Sheets, only writes rows to one, only writes headers to one, should not error and should still create a valid file",
  430. sheetNames: []string{
  431. "Sheet 1", "Sheet 2", "Sheet 3", "Sheet 4", "Sheet 5", "Sheet 6",
  432. },
  433. workbookData: [][][]string{
  434. {
  435. {"Token", "Name", "Price", "SKU"},
  436. {"123", "Taco", "300", "0000000123"},
  437. },
  438. {{}},
  439. {{"Id", "Unit Cost"}},
  440. {{}},
  441. {{}},
  442. {{}},
  443. },
  444. headerTypes: [][]*StreamingCellMetadata{
  445. {DefaultIntegerStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultIntegerStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr()},
  446. {nil},
  447. {nil, nil},
  448. {nil},
  449. {nil},
  450. {nil},
  451. },
  452. expectedWorkbookData: [][][]string{
  453. {
  454. {"Token", "Name", "Price", "SKU"},
  455. {"123", "Taco", "300", "0000000123"},
  456. },
  457. {{}},
  458. {{"Id", "Unit Cost"}},
  459. {{}},
  460. {{}},
  461. {{}},
  462. },
  463. },
  464. {
  465. testName: "Two Sheets, only writes to one, should not error and should still create a valid file",
  466. sheetNames: []string{
  467. "Sheet 1", "Sheet 2",
  468. },
  469. workbookData: [][][]string{
  470. {
  471. {"Token", "Name", "Price", "SKU"},
  472. {"123", "Taco", "300", "0000000123"},
  473. },
  474. {{}},
  475. },
  476. expectedWorkbookData: [][][]string{
  477. {
  478. {"Token", "Name", "Price", "SKU"},
  479. {"123", "Taco", "300", "0000000123"},
  480. },
  481. {{}},
  482. },
  483. headerTypes: [][]*StreamingCellMetadata{
  484. {DefaultDateStreamingCellMetadata.Ptr(), DefaultDateStreamingCellMetadata.Ptr(), DefaultDateStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr()},
  485. {nil},
  486. },
  487. },
  488. {
  489. testName: "Larger Sheet",
  490. sheetNames: []string{
  491. "Sheet 1",
  492. },
  493. workbookData: [][][]string{
  494. {
  495. {"Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU", "Token", "Name", "Price", "SKU"},
  496. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  497. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  498. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  499. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  500. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  501. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  502. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  503. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  504. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  505. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  506. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  507. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  508. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  509. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  510. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  511. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  512. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  513. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  514. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  515. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  516. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  517. {"123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123", "123", "Taco", "300", "0000000123"},
  518. {"456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346", "456", "Salsa", "200", "0346"},
  519. {"789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754", "789", "Burritos", "400", "754"},
  520. },
  521. },
  522. headerTypes: [][]*StreamingCellMetadata{
  523. {DefaultIntegerStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultIntegerStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr()},
  524. },
  525. },
  526. {
  527. testName: "UTF-8 Characters. This XLSX File loads correctly with Excel, Numbers, and Google Docs. It also passes Microsoft's Office File Format Validator.",
  528. sheetNames: []string{
  529. "Sheet1",
  530. },
  531. workbookData: [][][]string{
  532. {
  533. // String courtesy of https://github.com/minimaxir/big-list-of-naughty-strings/
  534. // Header row contains the tags that I am filtering on
  535. {"Token", endSheetDataTag, "Price", fmt.Sprintf(dimensionTag, "A1:D1")},
  536. // Japanese and emojis
  537. {"123", "パーティーへ行かないか", "300", "🍕🐵 🙈 🙉 🙊"},
  538. // XML encoder/parser test strings
  539. {"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>`},
  540. // Upside down text and Right to Left Arabic text
  541. {"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˥
  542. 00˙Ɩ$-`, "300", `ﷺ`},
  543. {"123", "Taco", "300", "0000000123"},
  544. },
  545. },
  546. headerTypes: [][]*StreamingCellMetadata{
  547. {DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr(), DefaultStringStreamingCellMetadata.Ptr()},
  548. },
  549. },
  550. }
  551. for i, testCase := range testCases {
  552. // if testCase.testName != "Lots of Sheets, only writes rows to one, only writes headers to one, should not error and should still create a valid file" {
  553. // continue
  554. // }
  555. t.Run(testCase.testName, func(t *testing.T) {
  556. var filePath string
  557. var buffer bytes.Buffer
  558. if TestsShouldMakeRealFiles {
  559. filePath = fmt.Sprintf("WorkbookTyped%d.xlsx", i)
  560. }
  561. err := writeStreamFileWithDefaultMetadata(filePath, &buffer, testCase.sheetNames, testCase.workbookData, testCase.headerTypes, TestsShouldMakeRealFiles)
  562. switch {
  563. case err == nil && testCase.expectedError != nil:
  564. t.Fatalf("Expected an error, but nil was returned\n")
  565. case err != nil && testCase.expectedError == nil:
  566. t.Fatalf("Unexpected error: %q", err.Error())
  567. case err != testCase.expectedError && err.Error() != testCase.expectedError.Error():
  568. t.Fatalf("Error differs from expected error. Error: %v, Expected Error: %v ", err, testCase.expectedError)
  569. case err != nil:
  570. // We got an error we expected
  571. return
  572. }
  573. // read the file back with the xlsx package
  574. var bufReader *bytes.Reader
  575. var size int64
  576. if !TestsShouldMakeRealFiles {
  577. bufReader = bytes.NewReader(buffer.Bytes())
  578. size = bufReader.Size()
  579. }
  580. actualSheetNames, actualWorkbookData, workbookCellTypes := readXLSXFile(t, filePath, bufReader, size, TestsShouldMakeRealFiles)
  581. verifyCellTypesInColumnMatchHeaderType(t, workbookCellTypes, testCase.headerTypes, testCase.workbookData)
  582. // check if data was able to be read correctly
  583. if !reflect.DeepEqual(actualSheetNames, testCase.sheetNames) {
  584. t.Fatal("Expected sheet names to be equal")
  585. }
  586. if testCase.expectedWorkbookData == nil {
  587. testCase.expectedWorkbookData = testCase.workbookData
  588. }
  589. if !reflect.DeepEqual(actualWorkbookData, testCase.expectedWorkbookData) {
  590. t.Log("expected: \n")
  591. t.Logf("%s\n", testCase.expectedWorkbookData)
  592. t.Log("\n")
  593. t.Log("result: \n")
  594. t.Logf("%s\n", actualWorkbookData)
  595. t.Log("\n")
  596. t.Fatal("Expected workbook data to be equal")
  597. }
  598. })
  599. }
  600. }
  601. // Ensures that the cell type of all cells in each column across all sheets matches the provided header types
  602. // in each corresponding sheet
  603. func verifyCellTypesInColumnMatchHeaderType(t *testing.T, workbookCellTypes [][][]CellType, headerMetadata [][]*StreamingCellMetadata, workbookData [][][]string) {
  604. numSheets := len(workbookCellTypes)
  605. numHeaders := len(headerMetadata)
  606. if numSheets != numHeaders {
  607. t.Fatalf("Number of sheets in workbook: %d not equal to number of sheet headers: %d", numSheets, numHeaders)
  608. }
  609. for sheetI, headers := range headerMetadata {
  610. var sanitizedHeaders []CellType
  611. for _, header := range headers {
  612. if header == (*StreamingCellMetadata)(nil) || header.cellType == CellTypeString {
  613. sanitizedHeaders = append(sanitizedHeaders, CellTypeInline)
  614. } else {
  615. sanitizedHeaders = append(sanitizedHeaders, header.cellType)
  616. }
  617. }
  618. sheet := workbookCellTypes[sheetI]
  619. // Skip header row
  620. for rowI, row := range sheet[1:] {
  621. if len(row) != len(headers) {
  622. t.Fatalf("Number of cells in row: %d not equal number of headers; %d", len(row), len(headers))
  623. }
  624. for colI, cellType := range row {
  625. headerTypeForCol := sanitizedHeaders[colI]
  626. if cellType != headerTypeForCol.fallbackTo(workbookData[sheetI][rowI+1][colI], CellTypeInline) {
  627. t.Fatalf("Cell type %d in row: %d and col: %d does not match header type: %d for this col in sheet: %d",
  628. cellType, rowI, colI, headerTypeForCol, sheetI)
  629. }
  630. }
  631. }
  632. }
  633. }
  634. // The purpose of TestXlsxStyleBehavior is to ensure that initMaxStyleId has the correct starting value
  635. // and that the logic in AddSheet() that predicts Style IDs is correct.
  636. func TestXlsxStyleBehavior(t *testing.T) {
  637. file := NewFile()
  638. sheet, err := file.AddSheet("Sheet 1")
  639. if err != nil {
  640. t.Fatal(err)
  641. }
  642. row := sheet.AddRow()
  643. rowData := []string{"testing", "1", "2", "3"}
  644. if count := row.WriteSlice(&rowData, -1); count != len(rowData) {
  645. t.Fatal("not enough cells written")
  646. }
  647. parts, err := file.MarshallParts()
  648. styleSheet, ok := parts["xl/styles.xml"]
  649. if !ok {
  650. t.Fatal("no style sheet")
  651. }
  652. // Created an XLSX file with only the default style.
  653. // This means the library adds a style by default, but no others are created
  654. if !strings.Contains(styleSheet, fmt.Sprintf(`<cellXfs count="%d">`, initMaxStyleId)) {
  655. t.Fatal("Expected sheet to have one style")
  656. }
  657. file = NewFile()
  658. sheet, err = file.AddSheet("Sheet 1")
  659. if err != nil {
  660. t.Fatal(err)
  661. }
  662. row = sheet.AddRow()
  663. rowData = []string{"testing", "1", "2", "3", "4"}
  664. if count := row.WriteSlice(&rowData, -1); count != len(rowData) {
  665. t.Fatal("not enough cells written")
  666. }
  667. sheet.SetType(0, 4, CellTypeString)
  668. sheet.SetType(3, 3, CellTypeNumeric)
  669. parts, err = file.MarshallParts()
  670. styleSheet, ok = parts["xl/styles.xml"]
  671. if !ok {
  672. t.Fatal("no style sheet")
  673. }
  674. // Created an XLSX file with two distinct cell types, which
  675. // should create two new styles. The same cell type was added
  676. // three times, this should be coalesced into the same style
  677. // rather than recreating the style. This XLSX stream library
  678. // depends on this behaviour when predicting the next style
  679. // id.
  680. if !strings.Contains(styleSheet, fmt.Sprintf(`<cellXfs count="%d">`, initMaxStyleId+2)) {
  681. t.Fatal("Expected sheet to have four styles")
  682. }
  683. }
  684. // writeStreamFile will write the file using this stream package
  685. func writeStreamFile(filePath string, fileBuffer io.Writer, sheetNames []string, workbookData [][][]string, headerTypes [][]*CellType, shouldMakeRealFiles bool) error {
  686. var file *StreamFileBuilder
  687. var err error
  688. if shouldMakeRealFiles {
  689. file, err = NewStreamFileBuilderForPath(filePath)
  690. if err != nil {
  691. return err
  692. }
  693. } else {
  694. file = NewStreamFileBuilder(fileBuffer)
  695. }
  696. for i, sheetName := range sheetNames {
  697. var sheetHeaderTypes []*CellType
  698. if i < len(headerTypes) {
  699. sheetHeaderTypes = headerTypes[i]
  700. }
  701. err := file.AddSheet(sheetName, sheetHeaderTypes)
  702. if err != nil {
  703. return err
  704. }
  705. }
  706. streamFile, err := file.Build()
  707. if err != nil {
  708. return err
  709. }
  710. for i, sheetData := range workbookData {
  711. if i != 0 {
  712. err = streamFile.NextSheet()
  713. if err != nil {
  714. return err
  715. }
  716. }
  717. for _, row := range sheetData {
  718. err = streamFile.Write(row)
  719. if err != nil {
  720. return err
  721. }
  722. }
  723. }
  724. err = streamFile.Close()
  725. if err != nil {
  726. return err
  727. }
  728. return nil
  729. }
  730. // writeStreamFileWithDefaultMetadata is the same thing as writeStreamFile but with headerMetadata instead of headerTypes
  731. func writeStreamFileWithDefaultMetadata(filePath string, fileBuffer io.Writer, sheetNames []string, workbookData [][][]string, headerMetadata [][]*StreamingCellMetadata, shouldMakeRealFiles bool) error {
  732. var file *StreamFileBuilder
  733. var err error
  734. if shouldMakeRealFiles {
  735. file, err = NewStreamFileBuilderForPath(filePath)
  736. if err != nil {
  737. return err
  738. }
  739. } else {
  740. file = NewStreamFileBuilder(fileBuffer)
  741. }
  742. for i, sheetName := range sheetNames {
  743. var sheetHeaderTypes []*StreamingCellMetadata
  744. if i < len(headerMetadata) {
  745. sheetHeaderTypes = headerMetadata[i]
  746. }
  747. err := file.AddSheetWithDefaultColumnMetadata(sheetName, sheetHeaderTypes)
  748. if err != nil {
  749. return err
  750. }
  751. }
  752. streamFile, err := file.Build()
  753. if err != nil {
  754. return err
  755. }
  756. for i, sheetData := range workbookData {
  757. if i != 0 {
  758. err = streamFile.NextSheet()
  759. if err != nil {
  760. return err
  761. }
  762. }
  763. for _, row := range sheetData {
  764. err = streamFile.WriteWithColumnDefaultMetadata(row)
  765. if err != nil {
  766. return err
  767. }
  768. }
  769. }
  770. err = streamFile.Close()
  771. if err != nil {
  772. return err
  773. }
  774. return nil
  775. }
  776. // readXLSXFile will read the file using the xlsx package.
  777. func readXLSXFile(t *testing.T, filePath string, fileBuffer io.ReaderAt, size int64, shouldMakeRealFiles bool) ([]string, [][][]string, [][][]CellType) {
  778. var readFile *File
  779. var err error
  780. if shouldMakeRealFiles {
  781. readFile, err = OpenFile(filePath)
  782. if err != nil {
  783. t.Fatal(err)
  784. }
  785. } else {
  786. readFile, err = OpenReaderAt(fileBuffer, size)
  787. if err != nil {
  788. t.Fatal(err)
  789. }
  790. }
  791. var actualWorkbookData [][][]string
  792. var workbookCellTypes [][][]CellType
  793. var sheetNames []string
  794. for _, sheet := range readFile.Sheets {
  795. sheetData := [][]string{}
  796. sheetCellTypes := [][]CellType{}
  797. for _, row := range sheet.Rows {
  798. data := []string{}
  799. cellTypes := []CellType{}
  800. for _, cell := range row.Cells {
  801. str, err := cell.FormattedValue()
  802. if err != nil {
  803. t.Fatal(err)
  804. }
  805. data = append(data, str)
  806. cellTypes = append(cellTypes, cell.Type())
  807. }
  808. sheetData = append(sheetData, data)
  809. sheetCellTypes = append(sheetCellTypes, cellTypes)
  810. }
  811. sheetNames = append(sheetNames, sheet.Name)
  812. actualWorkbookData = append(actualWorkbookData, sheetData)
  813. workbookCellTypes = append(workbookCellTypes, sheetCellTypes)
  814. }
  815. return sheetNames, actualWorkbookData, workbookCellTypes
  816. }
  817. func (s *StreamSuite) TestAddSheetErrorsAfterBuild(t *C) {
  818. file := NewStreamFileBuilder(bytes.NewBuffer(nil))
  819. err := file.AddSheet("Sheet1", nil)
  820. if err != nil {
  821. t.Fatal(err)
  822. }
  823. err = file.AddSheet("Sheet2", nil)
  824. if err != nil {
  825. t.Fatal(err)
  826. }
  827. _, err = file.Build()
  828. if err != nil {
  829. t.Fatal(err)
  830. }
  831. err = file.AddSheet("Sheet3", nil)
  832. if err != BuiltStreamFileBuilderError {
  833. t.Fatal(err)
  834. }
  835. }
  836. func (s *StreamSuite) TestBuildErrorsAfterBuild(t *C) {
  837. file := NewStreamFileBuilder(bytes.NewBuffer(nil))
  838. err := file.AddSheet("Sheet1", nil)
  839. if err != nil {
  840. t.Fatal(err)
  841. }
  842. err = file.AddSheet("Sheet2", nil)
  843. if err != nil {
  844. t.Fatal(err)
  845. }
  846. _, err = file.Build()
  847. if err != nil {
  848. t.Fatal(err)
  849. }
  850. _, err = file.Build()
  851. if err != BuiltStreamFileBuilderError {
  852. t.Fatal(err)
  853. }
  854. }
  855. func TestCloseWithNothingWrittenToSheets(t *testing.T) {
  856. buffer := bytes.NewBuffer(nil)
  857. file := NewStreamFileBuilder(buffer)
  858. sheetNames := []string{"Sheet1", "Sheet2"}
  859. expectedWorkbookData := [][][]string{{}, {}}
  860. err := file.AddSheet(sheetNames[0], nil)
  861. if err != nil {
  862. t.Fatal(err)
  863. }
  864. err = file.AddSheet(sheetNames[1], nil)
  865. if err != nil {
  866. t.Fatal(err)
  867. }
  868. stream, err := file.Build()
  869. if err != nil {
  870. t.Fatal(err)
  871. }
  872. err = stream.Close()
  873. if err != nil {
  874. t.Fatal(err)
  875. }
  876. bufReader := bytes.NewReader(buffer.Bytes())
  877. size := bufReader.Size()
  878. actualSheetNames, actualWorkbookData, _ := readXLSXFile(t, "", bufReader, size, false)
  879. // check if data was able to be read correctly
  880. if !reflect.DeepEqual(actualSheetNames, sheetNames) {
  881. t.Fatal("Expected sheet names to be equal")
  882. }
  883. if !reflect.DeepEqual(actualWorkbookData, expectedWorkbookData) {
  884. t.Logf("Expected:\n%s\n\n", expectedWorkbookData)
  885. t.Logf("Actual:\n%s\n\n", actualWorkbookData)
  886. t.Fatal("Expected workbook data to be equal")
  887. }
  888. }