adjust.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 (
  11. "errors"
  12. "strings"
  13. )
  14. type adjustDirection bool
  15. const (
  16. columns adjustDirection = false
  17. rows adjustDirection = true
  18. )
  19. // adjustHelper provides a function to adjust rows and columns dimensions,
  20. // hyperlinks, merged cells and auto filter when inserting or deleting rows or
  21. // columns.
  22. //
  23. // sheet: Worksheet name that we're editing
  24. // column: Index number of the column we're inserting/deleting before
  25. // row: Index number of the row we're inserting/deleting before
  26. // offset: Number of rows/column to insert/delete negative values indicate deletion
  27. //
  28. // TODO: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells
  29. //
  30. func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
  31. xlsx, err := f.workSheetReader(sheet)
  32. if err != nil {
  33. return err
  34. }
  35. if dir == rows {
  36. f.adjustRowDimensions(xlsx, num, offset)
  37. } else {
  38. f.adjustColDimensions(xlsx, num, offset)
  39. }
  40. f.adjustHyperlinks(xlsx, sheet, dir, num, offset)
  41. if err = f.adjustMergeCells(xlsx, dir, num, offset); err != nil {
  42. return err
  43. }
  44. if err = f.adjustAutoFilter(xlsx, dir, num, offset); err != nil {
  45. return err
  46. }
  47. if err = f.adjustCalcChain(dir, num, offset); err != nil {
  48. return err
  49. }
  50. checkSheet(xlsx)
  51. checkRow(xlsx)
  52. return nil
  53. }
  54. // adjustColDimensions provides a function to update column dimensions when
  55. // inserting or deleting rows or columns.
  56. func (f *File) adjustColDimensions(xlsx *xlsxWorksheet, col, offset int) {
  57. for rowIdx := range xlsx.SheetData.Row {
  58. for colIdx, v := range xlsx.SheetData.Row[rowIdx].C {
  59. cellCol, cellRow, _ := CellNameToCoordinates(v.R)
  60. if col <= cellCol {
  61. if newCol := cellCol + offset; newCol > 0 {
  62. xlsx.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
  63. }
  64. }
  65. }
  66. }
  67. }
  68. // adjustRowDimensions provides a function to update row dimensions when
  69. // inserting or deleting rows or columns.
  70. func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, row, offset int) {
  71. for i, r := range xlsx.SheetData.Row {
  72. if newRow := r.R + offset; r.R >= row && newRow > 0 {
  73. f.ajustSingleRowDimensions(&xlsx.SheetData.Row[i], newRow)
  74. }
  75. }
  76. }
  77. // ajustSingleRowDimensions provides a function to ajust single row dimensions.
  78. func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
  79. r.R = num
  80. for i, col := range r.C {
  81. colName, _, _ := SplitCellName(col.R)
  82. r.C[i].R, _ = JoinCellName(colName, num)
  83. }
  84. }
  85. // adjustHyperlinks provides a function to update hyperlinks when inserting or
  86. // deleting rows or columns.
  87. func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
  88. // short path
  89. if xlsx.Hyperlinks == nil || len(xlsx.Hyperlinks.Hyperlink) == 0 {
  90. return
  91. }
  92. // order is important
  93. if offset < 0 {
  94. for rowIdx, linkData := range xlsx.Hyperlinks.Hyperlink {
  95. colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
  96. if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
  97. f.deleteSheetRelationships(sheet, linkData.RID)
  98. if len(xlsx.Hyperlinks.Hyperlink) > 1 {
  99. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink[:rowIdx],
  100. xlsx.Hyperlinks.Hyperlink[rowIdx+1:]...)
  101. } else {
  102. xlsx.Hyperlinks = nil
  103. }
  104. }
  105. }
  106. }
  107. if xlsx.Hyperlinks == nil {
  108. return
  109. }
  110. for i := range xlsx.Hyperlinks.Hyperlink {
  111. link := &xlsx.Hyperlinks.Hyperlink[i] // get reference
  112. colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
  113. if dir == rows {
  114. if rowNum >= num {
  115. link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
  116. }
  117. } else {
  118. if colNum >= num {
  119. link.Ref, _ = CoordinatesToCellName(colNum+offset, rowNum)
  120. }
  121. }
  122. }
  123. }
  124. // adjustAutoFilter provides a function to update the auto filter when
  125. // inserting or deleting rows or columns.
  126. func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
  127. if xlsx.AutoFilter == nil {
  128. return nil
  129. }
  130. coordinates, err := f.areaRefToCoordinates(xlsx.AutoFilter.Ref)
  131. if err != nil {
  132. return err
  133. }
  134. x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  135. if (dir == rows && y1 == num && offset < 0) || (dir == columns && x1 == num && x2 == num) {
  136. xlsx.AutoFilter = nil
  137. for rowIdx := range xlsx.SheetData.Row {
  138. rowData := &xlsx.SheetData.Row[rowIdx]
  139. if rowData.R > y1 && rowData.R <= y2 {
  140. rowData.Hidden = false
  141. }
  142. }
  143. return nil
  144. }
  145. coordinates = f.adjustAutoFilterHelper(dir, coordinates, num, offset)
  146. x1, y1, x2, y2 = coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  147. if xlsx.AutoFilter.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
  148. return err
  149. }
  150. return nil
  151. }
  152. // adjustAutoFilterHelper provides a function for adjusting auto filter to
  153. // compare and calculate cell axis by the given adjust direction, operation
  154. // axis and offset.
  155. func (f *File) adjustAutoFilterHelper(dir adjustDirection, coordinates []int, num, offset int) []int {
  156. if dir == rows {
  157. if coordinates[1] >= num {
  158. coordinates[1] += offset
  159. }
  160. if coordinates[3] >= num {
  161. coordinates[3] += offset
  162. }
  163. } else {
  164. if coordinates[2] >= num {
  165. coordinates[2] += offset
  166. }
  167. }
  168. return coordinates
  169. }
  170. // areaRefToCoordinates provides a function to convert area reference to a
  171. // pair of coordinates.
  172. func (f *File) areaRefToCoordinates(ref string) ([]int, error) {
  173. coordinates := make([]int, 4)
  174. rng := strings.Split(ref, ":")
  175. firstCell := rng[0]
  176. lastCell := rng[1]
  177. var err error
  178. coordinates[0], coordinates[1], err = CellNameToCoordinates(firstCell)
  179. if err != nil {
  180. return coordinates, err
  181. }
  182. coordinates[2], coordinates[3], err = CellNameToCoordinates(lastCell)
  183. if err != nil {
  184. return coordinates, err
  185. }
  186. return coordinates, err
  187. }
  188. // coordinatesToAreaRef provides a function to convert a pair of coordinates
  189. // to area reference.
  190. func (f *File) coordinatesToAreaRef(coordinates []int) (string, error) {
  191. if len(coordinates) != 4 {
  192. return "", errors.New("coordinates length must be 4")
  193. }
  194. firstCell, err := CoordinatesToCellName(coordinates[0], coordinates[1])
  195. if err != nil {
  196. return "", err
  197. }
  198. lastCell, err := CoordinatesToCellName(coordinates[2], coordinates[3])
  199. if err != nil {
  200. return "", err
  201. }
  202. return firstCell + ":" + lastCell, err
  203. }
  204. // adjustMergeCells provides a function to update merged cells when inserting
  205. // or deleting rows or columns.
  206. func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
  207. if xlsx.MergeCells == nil {
  208. return nil
  209. }
  210. for i, areaData := range xlsx.MergeCells.Cells {
  211. coordinates, err := f.areaRefToCoordinates(areaData.Ref)
  212. if err != nil {
  213. return err
  214. }
  215. x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  216. if dir == rows {
  217. if y1 == num && y2 == num && offset < 0 {
  218. f.deleteMergeCell(xlsx, i)
  219. }
  220. y1 = f.adjustMergeCellsHelper(y1, num, offset)
  221. y2 = f.adjustMergeCellsHelper(y2, num, offset)
  222. } else {
  223. if x1 == num && x2 == num && offset < 0 {
  224. f.deleteMergeCell(xlsx, i)
  225. }
  226. x1 = f.adjustMergeCellsHelper(x1, num, offset)
  227. x2 = f.adjustMergeCellsHelper(x2, num, offset)
  228. }
  229. if x1 == x2 && y1 == y2 {
  230. f.deleteMergeCell(xlsx, i)
  231. }
  232. if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
  233. return err
  234. }
  235. }
  236. return nil
  237. }
  238. // adjustMergeCellsHelper provides a function for adjusting merge cells to
  239. // compare and calculate cell axis by the given pivot, operation axis and
  240. // offset.
  241. func (f *File) adjustMergeCellsHelper(pivot, num, offset int) int {
  242. if pivot >= num {
  243. pivot += offset
  244. if pivot < 1 {
  245. return 1
  246. }
  247. return pivot
  248. }
  249. return pivot
  250. }
  251. // deleteMergeCell provides a function to delete merged cell by given index.
  252. func (f *File) deleteMergeCell(sheet *xlsxWorksheet, idx int) {
  253. if len(sheet.MergeCells.Cells) > 1 {
  254. sheet.MergeCells.Cells = append(sheet.MergeCells.Cells[:idx], sheet.MergeCells.Cells[idx+1:]...)
  255. sheet.MergeCells.Count = len(sheet.MergeCells.Cells)
  256. } else {
  257. sheet.MergeCells = nil
  258. }
  259. }
  260. // adjustCalcChain provides a function to update the calculation chain when
  261. // inserting or deleting rows or columns.
  262. func (f *File) adjustCalcChain(dir adjustDirection, num, offset int) error {
  263. if f.CalcChain == nil {
  264. return nil
  265. }
  266. for index, c := range f.CalcChain.C {
  267. colNum, rowNum, err := CellNameToCoordinates(c.R)
  268. if err != nil {
  269. return err
  270. }
  271. if dir == rows && num <= rowNum {
  272. if newRow := rowNum + offset; newRow > 0 {
  273. f.CalcChain.C[index].R, _ = CoordinatesToCellName(colNum, newRow)
  274. }
  275. }
  276. if dir == columns && num <= colNum {
  277. if newCol := colNum + offset; newCol > 0 {
  278. f.CalcChain.C[index].R, _ = CoordinatesToCellName(newCol, rowNum)
  279. }
  280. }
  281. }
  282. return nil
  283. }