cell.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 axis in XLSX file.
  8. // 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 and axis in XLSX file.
  51. func (f *File) GetCellFormula(sheet string, axis string) string {
  52. axis = strings.ToUpper(axis)
  53. var xlsx xlsxWorksheet
  54. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  55. xAxis := row - 1
  56. name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
  57. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  58. rows := len(xlsx.SheetData.Row)
  59. if rows > 1 {
  60. lastRow := xlsx.SheetData.Row[rows-1].R
  61. if lastRow >= rows {
  62. rows = lastRow
  63. }
  64. }
  65. if rows <= xAxis {
  66. return ``
  67. }
  68. for _, v := range xlsx.SheetData.Row {
  69. if v.R != row {
  70. continue
  71. }
  72. for _, f := range v.C {
  73. if axis != f.R {
  74. continue
  75. }
  76. if f.F != nil {
  77. return f.F.Content
  78. }
  79. }
  80. }
  81. return ``
  82. }