adjust.go 6.5 KB

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