stream_test.go 37 KB

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