cell_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package excelize
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strconv"
  6. "sync"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestConcurrency(t *testing.T) {
  12. f := NewFile()
  13. wg := new(sync.WaitGroup)
  14. for i := 1; i <= 5; i++ {
  15. wg.Add(1)
  16. go func(val int, t *testing.T) {
  17. assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("A%d", val), val))
  18. assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("B%d", val), strconv.Itoa(val)))
  19. _, err := f.GetCellValue("Sheet1", fmt.Sprintf("A%d", val))
  20. assert.NoError(t, err)
  21. wg.Done()
  22. }(i, t)
  23. }
  24. wg.Wait()
  25. val, err := f.GetCellValue("Sheet1", "A1")
  26. if err != nil {
  27. t.Error(err)
  28. }
  29. assert.Equal(t, "1", val)
  30. }
  31. func TestCheckCellInArea(t *testing.T) {
  32. f := NewFile()
  33. expectedTrueCellInAreaList := [][2]string{
  34. {"c2", "A1:AAZ32"},
  35. {"B9", "A1:B9"},
  36. {"C2", "C2:C2"},
  37. }
  38. for _, expectedTrueCellInArea := range expectedTrueCellInAreaList {
  39. cell := expectedTrueCellInArea[0]
  40. area := expectedTrueCellInArea[1]
  41. ok, err := f.checkCellInArea(cell, area)
  42. assert.NoError(t, err)
  43. assert.Truef(t, ok,
  44. "Expected cell %v to be in area %v, got false\n", cell, area)
  45. }
  46. expectedFalseCellInAreaList := [][2]string{
  47. {"c2", "A4:AAZ32"},
  48. {"C4", "D6:A1"}, // weird case, but you never know
  49. {"AEF42", "BZ40:AEF41"},
  50. }
  51. for _, expectedFalseCellInArea := range expectedFalseCellInAreaList {
  52. cell := expectedFalseCellInArea[0]
  53. area := expectedFalseCellInArea[1]
  54. ok, err := f.checkCellInArea(cell, area)
  55. assert.NoError(t, err)
  56. assert.Falsef(t, ok,
  57. "Expected cell %v not to be inside of area %v, but got true\n", cell, area)
  58. }
  59. ok, err := f.checkCellInArea("A1", "A:B")
  60. assert.EqualError(t, err, `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  61. assert.False(t, ok)
  62. ok, err = f.checkCellInArea("AA0", "Z0:AB1")
  63. assert.EqualError(t, err, `cannot convert cell "AA0" to coordinates: invalid cell name "AA0"`)
  64. assert.False(t, ok)
  65. }
  66. func TestSetCellFloat(t *testing.T) {
  67. sheet := "Sheet1"
  68. t.Run("with no decimal", func(t *testing.T) {
  69. f := NewFile()
  70. assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.0, -1, 64))
  71. assert.NoError(t, f.SetCellFloat(sheet, "A2", 123.0, 1, 64))
  72. val, err := f.GetCellValue(sheet, "A1")
  73. assert.NoError(t, err)
  74. assert.Equal(t, "123", val, "A1 should be 123")
  75. val, err = f.GetCellValue(sheet, "A2")
  76. assert.NoError(t, err)
  77. assert.Equal(t, "123.0", val, "A2 should be 123.0")
  78. })
  79. t.Run("with a decimal and precision limit", func(t *testing.T) {
  80. f := NewFile()
  81. assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.42, 1, 64))
  82. val, err := f.GetCellValue(sheet, "A1")
  83. assert.NoError(t, err)
  84. assert.Equal(t, "123.4", val, "A1 should be 123.4")
  85. })
  86. t.Run("with a decimal and no limit", func(t *testing.T) {
  87. f := NewFile()
  88. assert.NoError(t, f.SetCellFloat(sheet, "A1", 123.42, -1, 64))
  89. val, err := f.GetCellValue(sheet, "A1")
  90. assert.NoError(t, err)
  91. assert.Equal(t, "123.42", val, "A1 should be 123.42")
  92. })
  93. f := NewFile()
  94. assert.EqualError(t, f.SetCellFloat(sheet, "A", 123.42, -1, 64), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  95. }
  96. func TestSetCellValue(t *testing.T) {
  97. f := NewFile()
  98. assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Now().UTC()), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  99. assert.EqualError(t, f.SetCellValue("Sheet1", "A", time.Duration(1e13)), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  100. }
  101. func TestSetCellValues(t *testing.T) {
  102. f := NewFile()
  103. err := f.SetCellValue("Sheet1", "A1", time.Date(2010, time.December, 31, 0, 0, 0, 0, time.UTC))
  104. assert.NoError(t, err)
  105. v, err := f.GetCellValue("Sheet1", "A1")
  106. assert.NoError(t, err)
  107. assert.Equal(t, v, "12/31/10 12:00")
  108. // test date value lower than min date supported by Excel
  109. err = f.SetCellValue("Sheet1", "A1", time.Date(1600, time.December, 31, 0, 0, 0, 0, time.UTC))
  110. assert.NoError(t, err)
  111. v, err = f.GetCellValue("Sheet1", "A1")
  112. assert.NoError(t, err)
  113. assert.Equal(t, v, "1600-12-31T00:00:00Z")
  114. }
  115. func TestSetCellBool(t *testing.T) {
  116. f := NewFile()
  117. assert.EqualError(t, f.SetCellBool("Sheet1", "A", true), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  118. }
  119. func TestGetCellValue(t *testing.T) {
  120. // Test get cell value without r attribute of the row.
  121. f := NewFile()
  122. sheetData := `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData>%s</sheetData></worksheet>`
  123. delete(f.Sheet, "xl/worksheets/sheet1.xml")
  124. f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `<row r="3"><c t="str"><v>A3</v></c></row><row><c t="str"><v>A4</v></c><c t="str"><v>B4</v></c></row><row r="7"><c t="str"><v>A7</v></c><c t="str"><v>B7</v></c></row><row><c t="str"><v>A8</v></c><c t="str"><v>B8</v></c></row>`))
  125. f.checked = nil
  126. cells := []string{"A3", "A4", "B4", "A7", "B7"}
  127. rows, err := f.GetRows("Sheet1")
  128. assert.Equal(t, [][]string{nil, nil, {"A3"}, {"A4", "B4"}, nil, nil, {"A7", "B7"}, {"A8", "B8"}}, rows)
  129. assert.NoError(t, err)
  130. for _, cell := range cells {
  131. value, err := f.GetCellValue("Sheet1", cell)
  132. assert.Equal(t, cell, value)
  133. assert.NoError(t, err)
  134. }
  135. cols, err := f.GetCols("Sheet1")
  136. assert.Equal(t, [][]string{{"", "", "A3", "A4", "", "", "A7", "A8"}, {"", "", "", "B4", "", "", "B7", "B8"}}, cols)
  137. assert.NoError(t, err)
  138. delete(f.Sheet, "xl/worksheets/sheet1.xml")
  139. f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `<row r="2"><c r="A2" t="str"><v>A2</v></c></row><row r="2"><c r="B2" t="str"><v>B2</v></c></row>`))
  140. f.checked = nil
  141. cell, err := f.GetCellValue("Sheet1", "A2")
  142. assert.Equal(t, "A2", cell)
  143. assert.NoError(t, err)
  144. delete(f.Sheet, "xl/worksheets/sheet1.xml")
  145. f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `<row r="2"><c r="A2" t="str"><v>A2</v></c></row><row r="2"><c r="B2" t="str"><v>B2</v></c></row>`))
  146. f.checked = nil
  147. rows, err = f.GetRows("Sheet1")
  148. assert.Equal(t, [][]string{nil, {"A2", "B2"}}, rows)
  149. assert.NoError(t, err)
  150. delete(f.Sheet, "xl/worksheets/sheet1.xml")
  151. f.XLSX["xl/worksheets/sheet1.xml"] = []byte(fmt.Sprintf(sheetData, `<row r="1"><c r="A1" t="str"><v>A1</v></c></row><row r="1"><c r="B1" t="str"><v>B1</v></c></row>`))
  152. f.checked = nil
  153. rows, err = f.GetRows("Sheet1")
  154. assert.Equal(t, [][]string{{"A1", "B1"}}, rows)
  155. assert.NoError(t, err)
  156. }
  157. func TestGetCellFormula(t *testing.T) {
  158. // Test get cell formula on not exist worksheet.
  159. f := NewFile()
  160. _, err := f.GetCellFormula("SheetN", "A1")
  161. assert.EqualError(t, err, "sheet SheetN is not exist")
  162. // Test get cell formula on no formula cell.
  163. assert.NoError(t, f.SetCellValue("Sheet1", "A1", true))
  164. _, err = f.GetCellFormula("Sheet1", "A1")
  165. assert.NoError(t, err)
  166. }
  167. func ExampleFile_SetCellFloat() {
  168. f := NewFile()
  169. var x = 3.14159265
  170. if err := f.SetCellFloat("Sheet1", "A1", x, 2, 64); err != nil {
  171. fmt.Println(err)
  172. }
  173. val, _ := f.GetCellValue("Sheet1", "A1")
  174. fmt.Println(val)
  175. // Output: 3.14
  176. }
  177. func BenchmarkSetCellValue(b *testing.B) {
  178. values := []string{"First", "Second", "Third", "Fourth", "Fifth", "Sixth"}
  179. cols := []string{"A", "B", "C", "D", "E", "F"}
  180. f := NewFile()
  181. b.ResetTimer()
  182. for i := 1; i <= b.N; i++ {
  183. for j := 0; j < len(values); j++ {
  184. if err := f.SetCellValue("Sheet1", cols[j]+strconv.Itoa(i), values[j]); err != nil {
  185. b.Error(err)
  186. }
  187. }
  188. }
  189. }
  190. func TestOverflowNumericCell(t *testing.T) {
  191. f, err := OpenFile(filepath.Join("test", "OverflowNumericCell.xlsx"))
  192. if !assert.NoError(t, err) {
  193. t.FailNow()
  194. }
  195. val, err := f.GetCellValue("Sheet1", "A1")
  196. assert.NoError(t, err)
  197. // GOARCH=amd64 - all ok; GOARCH=386 - actual: "-2147483648"
  198. assert.Equal(t, "8595602512225", val, "A1 should be 8595602512225")
  199. }
  200. func TestSetCellRichText(t *testing.T) {
  201. f := NewFile()
  202. assert.NoError(t, f.SetRowHeight("Sheet1", 1, 35))
  203. assert.NoError(t, f.SetColWidth("Sheet1", "A", "A", 44))
  204. richTextRun := []RichTextRun{
  205. {
  206. Text: "bold",
  207. Font: &Font{
  208. Bold: true,
  209. Color: "2354e8",
  210. Family: "Times New Roman",
  211. },
  212. },
  213. {
  214. Text: " and ",
  215. Font: &Font{
  216. Family: "Times New Roman",
  217. },
  218. },
  219. {
  220. Text: "italic ",
  221. Font: &Font{
  222. Bold: true,
  223. Color: "e83723",
  224. Italic: true,
  225. Family: "Times New Roman",
  226. },
  227. },
  228. {
  229. Text: "text with color and font-family,",
  230. Font: &Font{
  231. Bold: true,
  232. Color: "2354e8",
  233. Family: "Times New Roman",
  234. },
  235. },
  236. {
  237. Text: "\r\nlarge text with ",
  238. Font: &Font{
  239. Size: 14,
  240. Color: "ad23e8",
  241. },
  242. },
  243. {
  244. Text: "strike",
  245. Font: &Font{
  246. Color: "e89923",
  247. Strike: true,
  248. },
  249. },
  250. {
  251. Text: " and ",
  252. Font: &Font{
  253. Size: 14,
  254. Color: "ad23e8",
  255. },
  256. },
  257. {
  258. Text: "underline.",
  259. Font: &Font{
  260. Color: "23e833",
  261. Underline: "single",
  262. },
  263. },
  264. }
  265. assert.NoError(t, f.SetCellRichText("Sheet1", "A1", richTextRun))
  266. assert.NoError(t, f.SetCellRichText("Sheet1", "A2", richTextRun))
  267. style, err := f.NewStyle(&Style{
  268. Alignment: &Alignment{
  269. WrapText: true,
  270. },
  271. })
  272. assert.NoError(t, err)
  273. assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style))
  274. assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetCellRichText.xlsx")))
  275. // Test set cell rich text on not exists worksheet
  276. assert.EqualError(t, f.SetCellRichText("SheetN", "A1", richTextRun), "sheet SheetN is not exist")
  277. // Test set cell rich text with illegal cell coordinates
  278. assert.EqualError(t, f.SetCellRichText("Sheet1", "A", richTextRun), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
  279. }
  280. func TestFormattedValue2(t *testing.T) {
  281. f := NewFile()
  282. v := f.formattedValue(0, "43528")
  283. assert.Equal(t, "43528", v)
  284. v = f.formattedValue(15, "43528")
  285. assert.Equal(t, "43528", v)
  286. v = f.formattedValue(1, "43528")
  287. assert.Equal(t, "43528", v)
  288. customNumFmt := "[$-409]MM/DD/YYYY"
  289. _, err := f.NewStyle(&Style{
  290. CustomNumFmt: &customNumFmt,
  291. })
  292. assert.NoError(t, err)
  293. v = f.formattedValue(1, "43528")
  294. assert.Equal(t, "03/04/2019", v)
  295. // formatted value with no built-in number format ID
  296. numFmtID := 5
  297. f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{
  298. NumFmtID: &numFmtID,
  299. })
  300. v = f.formattedValue(2, "43528")
  301. assert.Equal(t, "43528", v)
  302. // formatted value with invalid number format ID
  303. f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{
  304. NumFmtID: nil,
  305. })
  306. _ = f.formattedValue(3, "43528")
  307. // formatted value with empty number format
  308. f.Styles.NumFmts = nil
  309. f.Styles.CellXfs.Xf = append(f.Styles.CellXfs.Xf, xlsxXf{
  310. NumFmtID: &numFmtID,
  311. })
  312. v = f.formattedValue(1, "43528")
  313. assert.Equal(t, "43528", v)
  314. }