cell.go 7.1 KB

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