adjust.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells
  26. //
  27. func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
  28. xlsx, err := f.workSheetReader(sheet)
  29. if err != nil {
  30. return err
  31. }
  32. if dir == rows {
  33. f.adjustRowDimensions(xlsx, num, offset)
  34. } else {
  35. f.adjustColDimensions(xlsx, num, offset)
  36. }
  37. f.adjustHyperlinks(xlsx, sheet, dir, num, offset)
  38. if err = f.adjustMergeCells(xlsx, dir, num, offset); err != nil {
  39. return err
  40. }
  41. if err = f.adjustAutoFilter(xlsx, dir, num, offset); err != nil {
  42. return err
  43. }
  44. if err = f.adjustCalcChain(dir, num, offset); err != nil {
  45. return err
  46. }
  47. checkSheet(xlsx)
  48. checkRow(xlsx)
  49. return nil
  50. }
  51. // adjustColDimensions provides a function to update column dimensions when
  52. // inserting or deleting rows or columns.
  53. func (f *File) adjustColDimensions(xlsx *xlsxWorksheet, col, offset int) {
  54. for rowIdx := range xlsx.SheetData.Row {
  55. for colIdx, v := range xlsx.SheetData.Row[rowIdx].C {
  56. cellCol, cellRow, _ := CellNameToCoordinates(v.R)
  57. if col <= cellCol {
  58. if newCol := cellCol + offset; newCol > 0 {
  59. xlsx.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
  60. }
  61. }
  62. }
  63. }
  64. }
  65. // adjustRowDimensions provides a function to update row dimensions when
  66. // inserting or deleting rows or columns.
  67. func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, row, offset int) {
  68. for i, r := range xlsx.SheetData.Row {
  69. if newRow := r.R + offset; r.R >= row && newRow > 0 {
  70. f.ajustSingleRowDimensions(&xlsx.SheetData.Row[i], newRow)
  71. }
  72. }
  73. }
  74. // ajustSingleRowDimensions provides a function to ajust single row dimensions.
  75. func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
  76. r.R = num
  77. for i, col := range r.C {
  78. colName, _, _ := SplitCellName(col.R)
  79. r.C[i].R, _ = JoinCellName(colName, num)
  80. }
  81. }
  82. // adjustHyperlinks provides a function to update hyperlinks when inserting or
  83. // deleting rows or columns.
  84. func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
  85. // short path
  86. if xlsx.Hyperlinks == nil || len(xlsx.Hyperlinks.Hyperlink) == 0 {
  87. return
  88. }
  89. // order is important
  90. if offset < 0 {
  91. for rowIdx, linkData := range xlsx.Hyperlinks.Hyperlink {
  92. colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
  93. if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
  94. f.deleteSheetRelationships(sheet, linkData.RID)
  95. if len(xlsx.Hyperlinks.Hyperlink) > 1 {
  96. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink[:rowIdx],
  97. xlsx.Hyperlinks.Hyperlink[rowIdx+1:]...)
  98. } else {
  99. xlsx.Hyperlinks = nil
  100. }
  101. }
  102. }
  103. }
  104. if xlsx.Hyperlinks == nil {
  105. return
  106. }
  107. for i := range xlsx.Hyperlinks.Hyperlink {
  108. link := &xlsx.Hyperlinks.Hyperlink[i] // get reference
  109. colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
  110. if dir == rows {
  111. if rowNum >= num {
  112. link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
  113. }
  114. } else {
  115. if colNum >= num {
  116. link.Ref, _ = CoordinatesToCellName(colNum+offset, rowNum)
  117. }
  118. }
  119. }
  120. }
  121. // adjustAutoFilter provides a function to update the auto filter when
  122. // inserting or deleting rows or columns.
  123. func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
  124. if xlsx.AutoFilter == nil {
  125. return nil
  126. }
  127. rng := strings.Split(xlsx.AutoFilter.Ref, ":")
  128. firstCell := rng[0]
  129. lastCell := rng[1]
  130. firstCol, firstRow, err := CellNameToCoordinates(firstCell)
  131. if err != nil {
  132. return err
  133. }
  134. lastCol, lastRow, err := CellNameToCoordinates(lastCell)
  135. if err != nil {
  136. return err
  137. }
  138. if (dir == rows && firstRow == num && offset < 0) || (dir == columns && firstCol == num && lastCol == num) {
  139. xlsx.AutoFilter = nil
  140. for rowIdx := range xlsx.SheetData.Row {
  141. rowData := &xlsx.SheetData.Row[rowIdx]
  142. if rowData.R > firstRow && rowData.R <= lastRow {
  143. rowData.Hidden = false
  144. }
  145. }
  146. return nil
  147. }
  148. if dir == rows {
  149. if firstRow >= num {
  150. firstCell, _ = CoordinatesToCellName(firstCol, firstRow+offset)
  151. }
  152. if lastRow >= num {
  153. lastCell, _ = CoordinatesToCellName(lastCol, lastRow+offset)
  154. }
  155. } else {
  156. if lastCol >= num {
  157. lastCell, _ = CoordinatesToCellName(lastCol+offset, lastRow)
  158. }
  159. }
  160. xlsx.AutoFilter.Ref = firstCell + ":" + lastCell
  161. return nil
  162. }
  163. // adjustMergeCells provides a function to update merged cells when inserting
  164. // or deleting rows or columns.
  165. func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
  166. if xlsx.MergeCells == nil {
  167. return nil
  168. }
  169. for i, areaData := range xlsx.MergeCells.Cells {
  170. rng := strings.Split(areaData.Ref, ":")
  171. firstCell := rng[0]
  172. lastCell := rng[1]
  173. firstCol, firstRow, err := CellNameToCoordinates(firstCell)
  174. if err != nil {
  175. return err
  176. }
  177. lastCol, lastRow, err := CellNameToCoordinates(lastCell)
  178. if err != nil {
  179. return err
  180. }
  181. adjust := func(v int) int {
  182. if v >= num {
  183. v += offset
  184. if v < 1 {
  185. return 1
  186. }
  187. return v
  188. }
  189. return v
  190. }
  191. if dir == rows {
  192. firstRow = adjust(firstRow)
  193. lastRow = adjust(lastRow)
  194. } else {
  195. firstCol = adjust(firstCol)
  196. lastCol = adjust(lastCol)
  197. }
  198. if firstCol == lastCol && firstRow == lastRow {
  199. if len(xlsx.MergeCells.Cells) > 1 {
  200. xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
  201. xlsx.MergeCells.Count = len(xlsx.MergeCells.Cells)
  202. } else {
  203. xlsx.MergeCells = nil
  204. }
  205. }
  206. if firstCell, err = CoordinatesToCellName(firstCol, firstRow); err != nil {
  207. return err
  208. }
  209. if lastCell, err = CoordinatesToCellName(lastCol, lastRow); err != nil {
  210. return err
  211. }
  212. areaData.Ref = firstCell + ":" + lastCell
  213. }
  214. return nil
  215. }
  216. // adjustCalcChain provides a function to update the calculation chain when
  217. // inserting or deleting rows or columns.
  218. func (f *File) adjustCalcChain(dir adjustDirection, num, offset int) error {
  219. if f.CalcChain == nil {
  220. return nil
  221. }
  222. for index, c := range f.CalcChain.C {
  223. colNum, rowNum, err := CellNameToCoordinates(c.R)
  224. if err != nil {
  225. return err
  226. }
  227. if dir == rows && num <= rowNum {
  228. if newRow := rowNum + offset; newRow > 0 {
  229. f.CalcChain.C[index].R, _ = CoordinatesToCellName(colNum, newRow)
  230. }
  231. }
  232. if dir == columns && num <= colNum {
  233. if newCol := colNum + offset; newCol > 0 {
  234. f.CalcChain.C[index].R, _ = CoordinatesToCellName(newCol, rowNum)
  235. }
  236. }
  237. }
  238. return nil
  239. }