cell.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // SetCellFormula provides function to set cell formula by given string and
  86. // sheet index.
  87. func (f *File) SetCellFormula(sheet, axis, formula string) {
  88. axis = strings.ToUpper(axis)
  89. var xlsx xlsxWorksheet
  90. col := string(strings.Map(letterOnlyMapF, axis))
  91. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  92. xAxis := row - 1
  93. yAxis := titleToNumber(col)
  94. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  95. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  96. rows := xAxis + 1
  97. cell := yAxis + 1
  98. xlsx = completeRow(xlsx, rows, cell)
  99. xlsx = completeCol(xlsx, rows, cell)
  100. if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
  101. xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
  102. } else {
  103. f := xlsxF{
  104. Content: formula,
  105. }
  106. xlsx.SheetData.Row[xAxis].C[yAxis].F = &f
  107. }
  108. output, _ := xml.Marshal(xlsx)
  109. f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
  110. }
  111. // SetCellHyperLink provides function to set cell hyperlink by given sheet index
  112. // and link URL address. Only support external link currently.
  113. func (f *File) SetCellHyperLink(sheet, axis, link string) {
  114. axis = strings.ToUpper(axis)
  115. var xlsx xlsxWorksheet
  116. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  117. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  118. rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
  119. hyperlink := xlsxHyperlink{
  120. Ref: axis,
  121. RID: "rId" + strconv.Itoa(rID),
  122. }
  123. if xlsx.Hyperlinks != nil {
  124. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
  125. } else {
  126. hyperlinks := xlsxHyperlinks{}
  127. hyperlinks.Hyperlink = append(hyperlinks.Hyperlink, hyperlink)
  128. xlsx.Hyperlinks = &hyperlinks
  129. }
  130. output, _ := xml.Marshal(xlsx)
  131. f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
  132. }
  133. // MergeCell provides function to merge cells by given axis and sheet name.
  134. // For example create a merged cell of A1:B2 on Sheet1:
  135. //
  136. // xlsx.MergeCell("sheet1", "D9", "E9")
  137. //
  138. // If you create a merged cell that overlaps with another existing merged cell,
  139. // those merged cells that already exist will be removed.
  140. func (f *File) MergeCell(sheet, hcell, vcell string) {
  141. if hcell == vcell {
  142. return
  143. }
  144. hcell = strings.ToUpper(hcell)
  145. vcell = strings.ToUpper(vcell)
  146. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  147. hcol := string(strings.Map(letterOnlyMapF, hcell))
  148. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  149. hyAxis := hrow - 1
  150. hxAxis := titleToNumber(hcol)
  151. vcol := string(strings.Map(letterOnlyMapF, vcell))
  152. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  153. vyAxis := vrow - 1
  154. vxAxis := titleToNumber(vcol)
  155. if vxAxis < hxAxis {
  156. hcell, vcell = vcell, hcell
  157. vxAxis, hxAxis = hxAxis, vxAxis
  158. }
  159. if vyAxis < hyAxis {
  160. hcell, vcell = vcell, hcell
  161. vyAxis, hyAxis = hyAxis, vyAxis
  162. }
  163. var xlsx xlsxWorksheet
  164. name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
  165. xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
  166. if xlsx.MergeCells != nil {
  167. mergeCell := xlsxMergeCell{}
  168. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  169. mergeCell.Ref = toAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + toAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
  170. // Delete the merged cells of the overlapping area.
  171. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  172. if checkCellInArea(hcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0], mergeCell.Ref) {
  173. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  174. } else if checkCellInArea(vcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[1], mergeCell.Ref) {
  175. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  176. }
  177. }
  178. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells, &mergeCell)
  179. } else {
  180. mergeCell := xlsxMergeCell{}
  181. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  182. mergeCell.Ref = toAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + toAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
  183. mergeCells := xlsxMergeCells{}
  184. mergeCells.Cells = append(mergeCells.Cells, &mergeCell)
  185. xlsx.MergeCells = &mergeCells
  186. }
  187. output, _ := xml.Marshal(xlsx)
  188. f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
  189. }
  190. // checkCellInArea provides function to determine if a given coordinate is
  191. // within an area.
  192. func checkCellInArea(cell, area string) bool {
  193. result := false
  194. cell = strings.ToUpper(cell)
  195. col := string(strings.Map(letterOnlyMapF, cell))
  196. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
  197. xAxis := row - 1
  198. yAxis := titleToNumber(col)
  199. ref := strings.Split(area, ":")
  200. hCol := string(strings.Map(letterOnlyMapF, ref[0]))
  201. hRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[0]))
  202. hyAxis := hRow - 1
  203. hxAxis := titleToNumber(hCol)
  204. vCol := string(strings.Map(letterOnlyMapF, ref[1]))
  205. vRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[1]))
  206. vyAxis := vRow - 1
  207. vxAxis := titleToNumber(vCol)
  208. if hxAxis <= yAxis && yAxis <= vxAxis && hyAxis <= xAxis && xAxis <= vyAxis {
  209. result = true
  210. }
  211. return result
  212. }