adjust.go 6.4 KB

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