cell.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package excelize
  2. import (
  3. "encoding/xml"
  4. "strconv"
  5. "strings"
  6. )
  7. // GetCellValue provide function get value from cell by given sheet index and
  8. // axis in XLSX file. The value of the merged cell is not available currently.
  9. func (f *File) GetCellValue(sheet string, axis string) string {
  10. axis = strings.ToUpper(axis)
  11. var xlsx xlsxWorksheet
  12. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  13. xAxis := row - 1
  14. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  15. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  16. rows := len(xlsx.SheetData.Row)
  17. if rows > 1 {
  18. lastRow := xlsx.SheetData.Row[rows-1].R
  19. if lastRow >= rows {
  20. rows = lastRow
  21. }
  22. }
  23. if rows <= xAxis {
  24. return ""
  25. }
  26. for _, v := range xlsx.SheetData.Row {
  27. if v.R != row {
  28. continue
  29. }
  30. for _, r := range v.C {
  31. if axis != r.R {
  32. continue
  33. }
  34. switch r.T {
  35. case "s":
  36. shardStrings := xlsxSST{}
  37. xlsxSI := 0
  38. xlsxSI, _ = strconv.Atoi(r.V)
  39. xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
  40. return shardStrings.SI[xlsxSI].T
  41. case "str":
  42. return r.V
  43. default:
  44. return r.V
  45. }
  46. }
  47. }
  48. return ""
  49. }
  50. // GetCellFormula provide function get formula from cell by given sheet index
  51. // and axis in XLSX file.
  52. func (f *File) GetCellFormula(sheet string, axis string) string {
  53. axis = strings.ToUpper(axis)
  54. var xlsx xlsxWorksheet
  55. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  56. xAxis := row - 1
  57. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  58. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  59. rows := len(xlsx.SheetData.Row)
  60. if rows > 1 {
  61. lastRow := xlsx.SheetData.Row[rows-1].R
  62. if lastRow >= rows {
  63. rows = lastRow
  64. }
  65. }
  66. if rows <= xAxis {
  67. return ""
  68. }
  69. for _, v := range xlsx.SheetData.Row {
  70. if v.R != row {
  71. continue
  72. }
  73. for _, f := range v.C {
  74. if axis != f.R {
  75. continue
  76. }
  77. if f.F != nil {
  78. return f.F.Content
  79. }
  80. }
  81. }
  82. return ""
  83. }