cell.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package excelize
  2. import (
  3. "encoding/xml"
  4. "strconv"
  5. "strings"
  6. )
  7. // GetCellValue provides function to get formatted value from cell by given
  8. // sheet index and axis in XLSX file. If it is possible to apply a format to the
  9. // cell value, it will do so, if not then an error will be returned, along with
  10. // the raw value of the cell.
  11. func (f *File) GetCellValue(sheet, axis string) string {
  12. xlsx := f.workSheetReader(sheet)
  13. axis = strings.ToUpper(axis)
  14. if xlsx.MergeCells != nil {
  15. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  16. if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
  17. axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
  18. }
  19. }
  20. }
  21. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  22. xAxis := row - 1
  23. rows := len(xlsx.SheetData.Row)
  24. if rows > 1 {
  25. lastRow := xlsx.SheetData.Row[rows-1].R
  26. if lastRow >= rows {
  27. rows = lastRow
  28. }
  29. }
  30. if rows < xAxis {
  31. return ""
  32. }
  33. for _, v := range xlsx.SheetData.Row {
  34. if v.R != row {
  35. continue
  36. }
  37. for _, r := range v.C {
  38. if axis != r.R {
  39. continue
  40. }
  41. switch r.T {
  42. case "s":
  43. shardStrings := xlsxSST{}
  44. xlsxSI := 0
  45. xlsxSI, _ = strconv.Atoi(r.V)
  46. xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
  47. return f.formattedValue(r.S, shardStrings.SI[xlsxSI].T)
  48. case "str":
  49. return f.formattedValue(r.S, r.V)
  50. default:
  51. return f.formattedValue(r.S, r.V)
  52. }
  53. }
  54. }
  55. return ""
  56. }
  57. // formattedValue provides function to returns a value after formatted. If it is
  58. // possible to apply a format to the cell value, it will do so, if not then an
  59. // error will be returned, along with the raw value of the cell.
  60. func (f *File) formattedValue(s int, v string) string {
  61. if s == 0 {
  62. return v
  63. }
  64. var styleSheet xlsxStyleSheet
  65. xml.Unmarshal([]byte(f.readXML("xl/styles.xml")), &styleSheet)
  66. return builtInNumFmtFunc[styleSheet.CellXfs.Xf[s].NumFmtID](styleSheet.CellXfs.Xf[s].NumFmtID, v)
  67. }
  68. // GetCellFormula provides function to get formula from cell by given sheet
  69. // index and axis in XLSX file.
  70. func (f *File) GetCellFormula(sheet, axis string) string {
  71. xlsx := f.workSheetReader(sheet)
  72. axis = strings.ToUpper(axis)
  73. if xlsx.MergeCells != nil {
  74. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  75. if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
  76. axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
  77. }
  78. }
  79. }
  80. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  81. xAxis := row - 1
  82. rows := len(xlsx.SheetData.Row)
  83. if rows > 1 {
  84. lastRow := xlsx.SheetData.Row[rows-1].R
  85. if lastRow >= rows {
  86. rows = lastRow
  87. }
  88. }
  89. if rows < xAxis {
  90. return ""
  91. }
  92. for _, v := range xlsx.SheetData.Row {
  93. if v.R != row {
  94. continue
  95. }
  96. for _, f := range v.C {
  97. if axis != f.R {
  98. continue
  99. }
  100. if f.F != nil {
  101. return f.F.Content
  102. }
  103. }
  104. }
  105. return ""
  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. xlsx := f.workSheetReader(sheet)
  111. axis = strings.ToUpper(axis)
  112. if xlsx.MergeCells != nil {
  113. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  114. if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
  115. axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
  116. }
  117. }
  118. }
  119. col := string(strings.Map(letterOnlyMapF, axis))
  120. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
  121. xAxis := row - 1
  122. yAxis := titleToNumber(col)
  123. rows := xAxis + 1
  124. cell := yAxis + 1
  125. completeRow(xlsx, rows, cell)
  126. completeCol(xlsx, rows, cell)
  127. if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
  128. xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
  129. } else {
  130. f := xlsxF{
  131. Content: formula,
  132. }
  133. xlsx.SheetData.Row[xAxis].C[yAxis].F = &f
  134. }
  135. }
  136. // SetCellHyperLink provides function to set cell hyperlink by given sheet index
  137. // and link URL address. Only support external link currently.
  138. func (f *File) SetCellHyperLink(sheet, axis, link string) {
  139. xlsx := f.workSheetReader(sheet)
  140. axis = strings.ToUpper(axis)
  141. if xlsx.MergeCells != nil {
  142. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  143. if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
  144. axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
  145. }
  146. }
  147. }
  148. rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
  149. hyperlink := xlsxHyperlink{
  150. Ref: axis,
  151. RID: "rId" + strconv.Itoa(rID),
  152. }
  153. if xlsx.Hyperlinks != nil {
  154. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
  155. } else {
  156. hyperlinks := xlsxHyperlinks{}
  157. hyperlinks.Hyperlink = append(hyperlinks.Hyperlink, hyperlink)
  158. xlsx.Hyperlinks = &hyperlinks
  159. }
  160. }
  161. // MergeCell provides function to merge cells by given coordinate area and sheet
  162. // name. For example create a merged cell of D3:E9 on Sheet1:
  163. //
  164. // xlsx.MergeCell("sheet1", "D3", "E9")
  165. //
  166. // If you create a merged cell that overlaps with another existing merged cell,
  167. // those merged cells that already exist will be removed.
  168. func (f *File) MergeCell(sheet, hcell, vcell string) {
  169. if hcell == vcell {
  170. return
  171. }
  172. hcell = strings.ToUpper(hcell)
  173. vcell = strings.ToUpper(vcell)
  174. // Coordinate conversion, convert C1:B3 to 2,0,1,2.
  175. hcol := string(strings.Map(letterOnlyMapF, hcell))
  176. hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
  177. hyAxis := hrow - 1
  178. hxAxis := titleToNumber(hcol)
  179. vcol := string(strings.Map(letterOnlyMapF, vcell))
  180. vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
  181. vyAxis := vrow - 1
  182. vxAxis := titleToNumber(vcol)
  183. if vxAxis < hxAxis {
  184. hcell, vcell = vcell, hcell
  185. vxAxis, hxAxis = hxAxis, vxAxis
  186. }
  187. if vyAxis < hyAxis {
  188. hcell, vcell = vcell, hcell
  189. vyAxis, hyAxis = hyAxis, vyAxis
  190. }
  191. xlsx := f.workSheetReader(sheet)
  192. if xlsx.MergeCells != nil {
  193. mergeCell := xlsxMergeCell{}
  194. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  195. mergeCell.Ref = toAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + toAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
  196. // Delete the merged cells of the overlapping area.
  197. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  198. if checkCellInArea(hcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0], mergeCell.Ref) {
  199. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  200. } else if checkCellInArea(vcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[1], mergeCell.Ref) {
  201. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  202. }
  203. }
  204. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells, &mergeCell)
  205. } else {
  206. mergeCell := xlsxMergeCell{}
  207. // Correct the coordinate area, such correct C1:B3 to B1:C3.
  208. mergeCell.Ref = toAlphaString(hxAxis+1) + strconv.Itoa(hyAxis+1) + ":" + toAlphaString(vxAxis+1) + strconv.Itoa(vyAxis+1)
  209. mergeCells := xlsxMergeCells{}
  210. mergeCells.Cells = append(mergeCells.Cells, &mergeCell)
  211. xlsx.MergeCells = &mergeCells
  212. }
  213. }
  214. // checkCellInArea provides function to determine if a given coordinate is
  215. // within an area.
  216. func checkCellInArea(cell, area string) bool {
  217. result := false
  218. cell = strings.ToUpper(cell)
  219. col := string(strings.Map(letterOnlyMapF, cell))
  220. row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
  221. xAxis := row - 1
  222. yAxis := titleToNumber(col)
  223. ref := strings.Split(area, ":")
  224. hCol := string(strings.Map(letterOnlyMapF, ref[0]))
  225. hRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[0]))
  226. hyAxis := hRow - 1
  227. hxAxis := titleToNumber(hCol)
  228. vCol := string(strings.Map(letterOnlyMapF, ref[1]))
  229. vRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[1]))
  230. vyAxis := vRow - 1
  231. vxAxis := titleToNumber(vCol)
  232. if hxAxis <= yAxis && yAxis <= vxAxis && hyAxis <= xAxis && xAxis <= vyAxis {
  233. result = true
  234. }
  235. return result
  236. }