adjust.go 9.9 KB

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