cell.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package excelize
  2. import (
  3. "encoding/xml"
  4. "strconv"
  5. "strings"
  6. )
  7. // GetCellValue provides function to get value from cell by given sheet index
  8. // and axis in XLSX file. The value of the merged cell is not available
  9. // currently.
  10. func (f *File) GetCellValue(sheet string, axis string) string {
  11. axis = strings.ToUpper(axis)
  12. var xlsx xlsxWorksheet
  13. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  14. xAxis := row - 1
  15. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  16. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  17. rows := len(xlsx.SheetData.Row)
  18. if rows > 1 {
  19. lastRow := xlsx.SheetData.Row[rows-1].R
  20. if lastRow >= rows {
  21. rows = lastRow
  22. }
  23. }
  24. if rows <= xAxis {
  25. return ""
  26. }
  27. for _, v := range xlsx.SheetData.Row {
  28. if v.R != row {
  29. continue
  30. }
  31. for _, r := range v.C {
  32. if axis != r.R {
  33. continue
  34. }
  35. switch r.T {
  36. case "s":
  37. shardStrings := xlsxSST{}
  38. xlsxSI := 0
  39. xlsxSI, _ = strconv.Atoi(r.V)
  40. xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
  41. return shardStrings.SI[xlsxSI].T
  42. case "str":
  43. return r.V
  44. default:
  45. return r.V
  46. }
  47. }
  48. }
  49. return ""
  50. }
  51. // GetCellFormula provides function to get formula from cell by given sheet
  52. // index and axis in XLSX file.
  53. func (f *File) GetCellFormula(sheet string, axis string) string {
  54. axis = strings.ToUpper(axis)
  55. var xlsx xlsxWorksheet
  56. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  57. xAxis := row - 1
  58. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  59. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  60. rows := len(xlsx.SheetData.Row)
  61. if rows > 1 {
  62. lastRow := xlsx.SheetData.Row[rows-1].R
  63. if lastRow >= rows {
  64. rows = lastRow
  65. }
  66. }
  67. if rows <= xAxis {
  68. return ""
  69. }
  70. for _, v := range xlsx.SheetData.Row {
  71. if v.R != row {
  72. continue
  73. }
  74. for _, f := range v.C {
  75. if axis != f.R {
  76. continue
  77. }
  78. if f.F != nil {
  79. return f.F.Content
  80. }
  81. }
  82. }
  83. return ""
  84. }
  85. // SetCellHyperLink provides function to set cell hyperlink by given sheet index
  86. // and link URL address. Only support external link currently.
  87. func (f *File) SetCellHyperLink(sheet, axis, link string) {
  88. axis = strings.ToUpper(axis)
  89. var xlsx xlsxWorksheet
  90. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  91. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  92. rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
  93. hyperlink := xlsxHyperlink{
  94. Ref: axis,
  95. RID: "rId" + strconv.Itoa(rID),
  96. }
  97. if xlsx.Hyperlinks != nil {
  98. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
  99. } else {
  100. hyperlinks := xlsxHyperlinks{}
  101. hyperlinks.Hyperlink = append(hyperlinks.Hyperlink, hyperlink)
  102. xlsx.Hyperlinks = &hyperlinks
  103. }
  104. output, _ := xml.Marshal(xlsx)
  105. f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
  106. }
  107. // SetCellFormula provides function to set cell formula by given string and
  108. // sheet index.
  109. func (f *File) SetCellFormula(sheet, axis, formula string) {
  110. axis = strings.ToUpper(axis)
  111. var xlsx xlsxWorksheet
  112. col := string(strings.Map(letterOnlyMapF, axis))
  113. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  114. xAxis := row - 1
  115. yAxis := titleToNumber(col)
  116. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  117. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  118. rows := xAxis + 1
  119. cell := yAxis + 1
  120. xlsx = completeRow(xlsx, rows, cell)
  121. xlsx = completeCol(xlsx, rows, cell)
  122. if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
  123. xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
  124. } else {
  125. f := xlsxF{
  126. Content: formula,
  127. }
  128. xlsx.SheetData.Row[xAxis].C[yAxis].F = &f
  129. }
  130. output, _ := xml.Marshal(xlsx)
  131. f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
  132. }