adjust.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package excelize
  2. import (
  3. "strings"
  4. )
  5. type adjustDirection bool
  6. const (
  7. columns adjustDirection = false
  8. rows adjustDirection = true
  9. )
  10. // adjustHelper provides a function to adjust rows and columns dimensions,
  11. // hyperlinks, merged cells and auto filter when inserting or deleting rows or
  12. // columns.
  13. //
  14. // sheet: Worksheet name that we're editing
  15. // column: Index number of the column we're inserting/deleting before
  16. // row: Index number of the row we're inserting/deleting before
  17. // offset: Number of rows/column to insert/delete negative values indicate deletion
  18. //
  19. // TODO: adjustCalcChain, adjustPageBreaks, adjustComments,
  20. // adjustDataValidations, adjustProtectedCells
  21. //
  22. func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) {
  23. xlsx := f.workSheetReader(sheet)
  24. if dir == rows {
  25. f.adjustRowDimensions(xlsx, num, offset)
  26. } else {
  27. f.adjustColDimensions(xlsx, num, offset)
  28. }
  29. f.adjustHyperlinks(xlsx, sheet, dir, num, offset)
  30. f.adjustMergeCells(xlsx, dir, num, offset)
  31. f.adjustAutoFilter(xlsx, dir, num, offset)
  32. checkSheet(xlsx)
  33. checkRow(xlsx)
  34. }
  35. // adjustColDimensions provides a function to update column dimensions when
  36. // inserting or deleting rows or columns.
  37. func (f *File) adjustColDimensions(xlsx *xlsxWorksheet, col, offset int) {
  38. for rowIdx := range xlsx.SheetData.Row {
  39. for colIdx, v := range xlsx.SheetData.Row[rowIdx].C {
  40. cellCol, cellRow, _ := CellNameToCoordinates(v.R)
  41. if col <= cellCol {
  42. if newCol := cellCol + offset; newCol > 0 {
  43. xlsx.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
  44. }
  45. }
  46. }
  47. }
  48. }
  49. // adjustRowDimensions provides a function to update row dimensions when
  50. // inserting or deleting rows or columns.
  51. func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, row, offset int) {
  52. for i, r := range xlsx.SheetData.Row {
  53. if newRow := r.R + offset; r.R >= row && newRow > 0 {
  54. f.ajustSingleRowDimensions(&xlsx.SheetData.Row[i], newRow)
  55. }
  56. }
  57. }
  58. // ajustSingleRowDimensions provides a function to ajust single row dimensions.
  59. func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
  60. r.R = num
  61. for i, col := range r.C {
  62. colName, _, _ := SplitCellName(col.R)
  63. r.C[i].R, _ = JoinCellName(colName, num)
  64. }
  65. }
  66. // adjustHyperlinks provides a function to update hyperlinks when inserting or
  67. // deleting rows or columns.
  68. func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
  69. // short path
  70. if xlsx.Hyperlinks == nil || len(xlsx.Hyperlinks.Hyperlink) == 0 {
  71. return
  72. }
  73. // order is important
  74. if offset < 0 {
  75. for rowIdx, linkData := range xlsx.Hyperlinks.Hyperlink {
  76. colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
  77. if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
  78. f.deleteSheetRelationships(sheet, linkData.RID)
  79. if len(xlsx.Hyperlinks.Hyperlink) > 1 {
  80. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink[:rowIdx],
  81. xlsx.Hyperlinks.Hyperlink[rowIdx+1:]...)
  82. } else {
  83. xlsx.Hyperlinks = nil
  84. }
  85. }
  86. }
  87. }
  88. if xlsx.Hyperlinks == nil {
  89. return
  90. }
  91. for i := range xlsx.Hyperlinks.Hyperlink {
  92. link := &xlsx.Hyperlinks.Hyperlink[i] // get reference
  93. colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
  94. if dir == rows {
  95. if rowNum >= num {
  96. link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
  97. }
  98. } else {
  99. if colNum >= num {
  100. link.Ref, _ = CoordinatesToCellName(colNum+offset, rowNum)
  101. }
  102. }
  103. }
  104. }
  105. // adjustAutoFilter provides a function to update the auto filter when
  106. // inserting or deleting rows or columns.
  107. func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) {
  108. if xlsx.AutoFilter == nil {
  109. return
  110. }
  111. rng := strings.Split(xlsx.AutoFilter.Ref, ":")
  112. firstCell := rng[0]
  113. lastCell := rng[1]
  114. firstCol, firstRow, err := CellNameToCoordinates(firstCell)
  115. if err != nil {
  116. panic(err)
  117. }
  118. lastCol, lastRow, err := CellNameToCoordinates(lastCell)
  119. if err != nil {
  120. panic(err)
  121. }
  122. if (dir == rows && firstRow == num && offset < 0) || (dir == columns && firstCol == num && lastCol == num) {
  123. xlsx.AutoFilter = nil
  124. for rowIdx := range xlsx.SheetData.Row {
  125. rowData := &xlsx.SheetData.Row[rowIdx]
  126. if rowData.R > firstRow && rowData.R <= lastRow {
  127. rowData.Hidden = false
  128. }
  129. }
  130. return
  131. }
  132. if dir == rows {
  133. if firstRow >= num {
  134. firstCell, _ = CoordinatesToCellName(firstCol, firstRow+offset)
  135. }
  136. if lastRow >= num {
  137. lastCell, _ = CoordinatesToCellName(lastCol, lastRow+offset)
  138. }
  139. } else {
  140. if lastCol >= num {
  141. lastCell, _ = CoordinatesToCellName(lastCol+offset, lastRow)
  142. }
  143. }
  144. xlsx.AutoFilter.Ref = firstCell + ":" + lastCell
  145. }
  146. // adjustMergeCells provides a function to update merged cells when inserting
  147. // or deleting rows or columns.
  148. func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) {
  149. if xlsx.MergeCells == nil {
  150. return
  151. }
  152. for i, areaData := range xlsx.MergeCells.Cells {
  153. rng := strings.Split(areaData.Ref, ":")
  154. firstCell := rng[0]
  155. lastCell := rng[1]
  156. firstCol, firstRow, err := CellNameToCoordinates(firstCell)
  157. if err != nil {
  158. panic(err)
  159. }
  160. lastCol, lastRow, err := CellNameToCoordinates(lastCell)
  161. if err != nil {
  162. panic(err)
  163. }
  164. adjust := func(v int) int {
  165. if v >= num {
  166. v += offset
  167. if v < 1 {
  168. return 1
  169. }
  170. return v
  171. }
  172. return v
  173. }
  174. if dir == rows {
  175. firstRow = adjust(firstRow)
  176. lastRow = adjust(lastRow)
  177. } else {
  178. firstCol = adjust(firstCol)
  179. lastCol = adjust(lastCol)
  180. }
  181. if firstCol == lastCol && firstRow == lastRow {
  182. if len(xlsx.MergeCells.Cells) > 1 {
  183. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  184. xlsx.MergeCells.Count = len(xlsx.MergeCells.Cells)
  185. } else {
  186. xlsx.MergeCells = nil
  187. }
  188. }
  189. if firstCell, err = CoordinatesToCellName(firstCol, firstRow); err != nil {
  190. panic(err)
  191. }
  192. if lastCell, err = CoordinatesToCellName(lastCol, lastRow); err != nil {
  193. panic(err)
  194. }
  195. areaData.Ref = firstCell + ":" + lastCell
  196. }
  197. }