cell.go 7.2 KB

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