adjust.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright 2016 - 2020 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.10 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. if xlsx.MergeCells != nil && len(xlsx.MergeCells.Cells) == 0 {
  53. xlsx.MergeCells = nil
  54. }
  55. return nil
  56. }
  57. // adjustColDimensions provides a function to update column dimensions when
  58. // inserting or deleting rows or columns.
  59. func (f *File) adjustColDimensions(xlsx *xlsxWorksheet, col, offset int) {
  60. for rowIdx := range xlsx.SheetData.Row {
  61. for colIdx, v := range xlsx.SheetData.Row[rowIdx].C {
  62. cellCol, cellRow, _ := CellNameToCoordinates(v.R)
  63. if col <= cellCol {
  64. if newCol := cellCol + offset; newCol > 0 {
  65. xlsx.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
  66. }
  67. }
  68. }
  69. }
  70. }
  71. // adjustRowDimensions provides a function to update row dimensions when
  72. // inserting or deleting rows or columns.
  73. func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, row, offset int) {
  74. for i, r := range xlsx.SheetData.Row {
  75. if newRow := r.R + offset; r.R >= row && newRow > 0 {
  76. f.ajustSingleRowDimensions(&xlsx.SheetData.Row[i], newRow)
  77. }
  78. }
  79. }
  80. // ajustSingleRowDimensions provides a function to ajust single row dimensions.
  81. func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
  82. r.R = num
  83. for i, col := range r.C {
  84. colName, _, _ := SplitCellName(col.R)
  85. r.C[i].R, _ = JoinCellName(colName, num)
  86. }
  87. }
  88. // adjustHyperlinks provides a function to update hyperlinks when inserting or
  89. // deleting rows or columns.
  90. func (f *File) adjustHyperlinks(xlsx *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
  91. // short path
  92. if xlsx.Hyperlinks == nil || len(xlsx.Hyperlinks.Hyperlink) == 0 {
  93. return
  94. }
  95. // order is important
  96. if offset < 0 {
  97. for rowIdx, linkData := range xlsx.Hyperlinks.Hyperlink {
  98. colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
  99. if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
  100. f.deleteSheetRelationships(sheet, linkData.RID)
  101. if len(xlsx.Hyperlinks.Hyperlink) > 1 {
  102. xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink[:rowIdx],
  103. xlsx.Hyperlinks.Hyperlink[rowIdx+1:]...)
  104. } else {
  105. xlsx.Hyperlinks = nil
  106. }
  107. }
  108. }
  109. }
  110. if xlsx.Hyperlinks == nil {
  111. return
  112. }
  113. for i := range xlsx.Hyperlinks.Hyperlink {
  114. link := &xlsx.Hyperlinks.Hyperlink[i] // get reference
  115. colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
  116. if dir == rows {
  117. if rowNum >= num {
  118. link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
  119. }
  120. } else {
  121. if colNum >= num {
  122. link.Ref, _ = CoordinatesToCellName(colNum+offset, rowNum)
  123. }
  124. }
  125. }
  126. }
  127. // adjustAutoFilter provides a function to update the auto filter when
  128. // inserting or deleting rows or columns.
  129. func (f *File) adjustAutoFilter(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
  130. if xlsx.AutoFilter == nil {
  131. return nil
  132. }
  133. coordinates, err := f.areaRefToCoordinates(xlsx.AutoFilter.Ref)
  134. if err != nil {
  135. return err
  136. }
  137. x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  138. if (dir == rows && y1 == num && offset < 0) || (dir == columns && x1 == num && x2 == num) {
  139. xlsx.AutoFilter = nil
  140. for rowIdx := range xlsx.SheetData.Row {
  141. rowData := &xlsx.SheetData.Row[rowIdx]
  142. if rowData.R > y1 && rowData.R <= y2 {
  143. rowData.Hidden = false
  144. }
  145. }
  146. return nil
  147. }
  148. coordinates = f.adjustAutoFilterHelper(dir, coordinates, num, offset)
  149. x1, y1, x2, y2 = coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  150. if xlsx.AutoFilter.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
  151. return err
  152. }
  153. return nil
  154. }
  155. // adjustAutoFilterHelper provides a function for adjusting auto filter to
  156. // compare and calculate cell axis by the given adjust direction, operation
  157. // axis and offset.
  158. func (f *File) adjustAutoFilterHelper(dir adjustDirection, coordinates []int, num, offset int) []int {
  159. if dir == rows {
  160. if coordinates[1] >= num {
  161. coordinates[1] += offset
  162. }
  163. if coordinates[3] >= num {
  164. coordinates[3] += offset
  165. }
  166. } else {
  167. if coordinates[2] >= num {
  168. coordinates[2] += offset
  169. }
  170. }
  171. return coordinates
  172. }
  173. // areaRefToCoordinates provides a function to convert area reference to a
  174. // pair of coordinates.
  175. func (f *File) areaRefToCoordinates(ref string) ([]int, error) {
  176. rng := strings.Split(ref, ":")
  177. return areaRangeToCoordinates(rng[0], rng[1])
  178. }
  179. // areaRangeToCoordinates provides a function to convert cell range to a
  180. // pair of coordinates.
  181. func areaRangeToCoordinates(firstCell, lastCell string) ([]int, error) {
  182. coordinates := make([]int, 4)
  183. var err error
  184. coordinates[0], coordinates[1], err = CellNameToCoordinates(firstCell)
  185. if err != nil {
  186. return coordinates, err
  187. }
  188. coordinates[2], coordinates[3], err = CellNameToCoordinates(lastCell)
  189. return coordinates, err
  190. }
  191. // sortCoordinates provides a function to correct the coordinate area, such
  192. // correct C1:B3 to B1:C3.
  193. func sortCoordinates(coordinates []int) error {
  194. if len(coordinates) != 4 {
  195. return errors.New("coordinates length must be 4")
  196. }
  197. if coordinates[2] < coordinates[0] {
  198. coordinates[2], coordinates[0] = coordinates[0], coordinates[2]
  199. }
  200. if coordinates[3] < coordinates[1] {
  201. coordinates[3], coordinates[1] = coordinates[1], coordinates[3]
  202. }
  203. return nil
  204. }
  205. // coordinatesToAreaRef provides a function to convert a pair of coordinates
  206. // to area reference.
  207. func (f *File) coordinatesToAreaRef(coordinates []int) (string, error) {
  208. if len(coordinates) != 4 {
  209. return "", errors.New("coordinates length must be 4")
  210. }
  211. firstCell, err := CoordinatesToCellName(coordinates[0], coordinates[1])
  212. if err != nil {
  213. return "", err
  214. }
  215. lastCell, err := CoordinatesToCellName(coordinates[2], coordinates[3])
  216. if err != nil {
  217. return "", err
  218. }
  219. return firstCell + ":" + lastCell, err
  220. }
  221. // adjustMergeCells provides a function to update merged cells when inserting
  222. // or deleting rows or columns.
  223. func (f *File) adjustMergeCells(xlsx *xlsxWorksheet, dir adjustDirection, num, offset int) error {
  224. if xlsx.MergeCells == nil {
  225. return nil
  226. }
  227. for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
  228. areaData := xlsx.MergeCells.Cells[i]
  229. coordinates, err := f.areaRefToCoordinates(areaData.Ref)
  230. if err != nil {
  231. return err
  232. }
  233. x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  234. if dir == rows {
  235. if y1 == num && y2 == num && offset < 0 {
  236. f.deleteMergeCell(xlsx, i)
  237. i--
  238. }
  239. y1 = f.adjustMergeCellsHelper(y1, num, offset)
  240. y2 = f.adjustMergeCellsHelper(y2, num, offset)
  241. } else {
  242. if x1 == num && x2 == num && offset < 0 {
  243. f.deleteMergeCell(xlsx, i)
  244. i--
  245. }
  246. x1 = f.adjustMergeCellsHelper(x1, num, offset)
  247. x2 = f.adjustMergeCellsHelper(x2, num, offset)
  248. }
  249. if x1 == x2 && y1 == y2 {
  250. f.deleteMergeCell(xlsx, i)
  251. i--
  252. }
  253. if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
  254. return err
  255. }
  256. }
  257. return nil
  258. }
  259. // adjustMergeCellsHelper provides a function for adjusting merge cells to
  260. // compare and calculate cell axis by the given pivot, operation axis and
  261. // offset.
  262. func (f *File) adjustMergeCellsHelper(pivot, num, offset int) int {
  263. if pivot >= num {
  264. pivot += offset
  265. if pivot < 1 {
  266. return 1
  267. }
  268. return pivot
  269. }
  270. return pivot
  271. }
  272. // deleteMergeCell provides a function to delete merged cell by given index.
  273. func (f *File) deleteMergeCell(sheet *xlsxWorksheet, idx int) {
  274. if len(sheet.MergeCells.Cells) > idx {
  275. sheet.MergeCells.Cells = append(sheet.MergeCells.Cells[:idx], sheet.MergeCells.Cells[idx+1:]...)
  276. sheet.MergeCells.Count = len(sheet.MergeCells.Cells)
  277. }
  278. }
  279. // adjustCalcChain provides a function to update the calculation chain when
  280. // inserting or deleting rows or columns.
  281. func (f *File) adjustCalcChain(dir adjustDirection, num, offset int) error {
  282. if f.CalcChain == nil {
  283. return nil
  284. }
  285. for index, c := range f.CalcChain.C {
  286. colNum, rowNum, err := CellNameToCoordinates(c.R)
  287. if err != nil {
  288. return err
  289. }
  290. if dir == rows && num <= rowNum {
  291. if newRow := rowNum + offset; newRow > 0 {
  292. f.CalcChain.C[index].R, _ = CoordinatesToCellName(colNum, newRow)
  293. }
  294. }
  295. if dir == columns && num <= colNum {
  296. if newCol := colNum + offset; newCol > 0 {
  297. f.CalcChain.C[index].R, _ = CoordinatesToCellName(newCol, rowNum)
  298. }
  299. }
  300. }
  301. return nil
  302. }