adjust.go 9.7 KB

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