adjust.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. return areaRangeToCoordinates(rng[0], rng[1])
  182. }
  183. // areaRangeToCoordinates provides a function to convert cell range to a
  184. // pair of coordinates.
  185. func areaRangeToCoordinates(firstCell, lastCell string) ([]int, error) {
  186. coordinates := make([]int, 4)
  187. var err error
  188. coordinates[0], coordinates[1], err = CellNameToCoordinates(firstCell)
  189. if err != nil {
  190. return coordinates, err
  191. }
  192. coordinates[2], coordinates[3], err = CellNameToCoordinates(lastCell)
  193. return coordinates, err
  194. }
  195. // sortCoordinates provides a function to correct the coordinate area, such
  196. // correct C1:B3 to B1:C3.
  197. func sortCoordinates(coordinates []int) error {
  198. if len(coordinates) != 4 {
  199. return ErrCoordinates
  200. }
  201. if coordinates[2] < coordinates[0] {
  202. coordinates[2], coordinates[0] = coordinates[0], coordinates[2]
  203. }
  204. if coordinates[3] < coordinates[1] {
  205. coordinates[3], coordinates[1] = coordinates[1], coordinates[3]
  206. }
  207. return nil
  208. }
  209. // coordinatesToAreaRef provides a function to convert a pair of coordinates
  210. // to area reference.
  211. func (f *File) coordinatesToAreaRef(coordinates []int) (string, error) {
  212. if len(coordinates) != 4 {
  213. return "", ErrCoordinates
  214. }
  215. firstCell, err := CoordinatesToCellName(coordinates[0], coordinates[1])
  216. if err != nil {
  217. return "", err
  218. }
  219. lastCell, err := CoordinatesToCellName(coordinates[2], coordinates[3])
  220. if err != nil {
  221. return "", err
  222. }
  223. return firstCell + ":" + lastCell, err
  224. }
  225. // adjustMergeCells provides a function to update merged cells when inserting
  226. // or deleting rows or columns.
  227. func (f *File) adjustMergeCells(ws *xlsxWorksheet, dir adjustDirection, num, offset int) error {
  228. if ws.MergeCells == nil {
  229. return nil
  230. }
  231. for i := 0; i < len(ws.MergeCells.Cells); i++ {
  232. areaData := ws.MergeCells.Cells[i]
  233. coordinates, err := f.areaRefToCoordinates(areaData.Ref)
  234. if err != nil {
  235. return err
  236. }
  237. x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
  238. if dir == rows {
  239. if y1 == num && y2 == num && offset < 0 {
  240. f.deleteMergeCell(ws, i)
  241. i--
  242. }
  243. y1 = f.adjustMergeCellsHelper(y1, num, offset)
  244. y2 = f.adjustMergeCellsHelper(y2, num, offset)
  245. } else {
  246. if x1 == num && x2 == num && offset < 0 {
  247. f.deleteMergeCell(ws, i)
  248. i--
  249. }
  250. x1 = f.adjustMergeCellsHelper(x1, num, offset)
  251. x2 = f.adjustMergeCellsHelper(x2, num, offset)
  252. }
  253. if x1 == x2 && y1 == y2 {
  254. f.deleteMergeCell(ws, i)
  255. i--
  256. }
  257. if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
  258. return err
  259. }
  260. }
  261. return nil
  262. }
  263. // adjustMergeCellsHelper provides a function for adjusting merge cells to
  264. // compare and calculate cell axis by the given pivot, operation axis and
  265. // offset.
  266. func (f *File) adjustMergeCellsHelper(pivot, num, offset int) int {
  267. if pivot >= num {
  268. pivot += offset
  269. if pivot < 1 {
  270. return 1
  271. }
  272. return pivot
  273. }
  274. return pivot
  275. }
  276. // deleteMergeCell provides a function to delete merged cell by given index.
  277. func (f *File) deleteMergeCell(ws *xlsxWorksheet, idx int) {
  278. if len(ws.MergeCells.Cells) > idx {
  279. ws.MergeCells.Cells = append(ws.MergeCells.Cells[:idx], ws.MergeCells.Cells[idx+1:]...)
  280. ws.MergeCells.Count = len(ws.MergeCells.Cells)
  281. }
  282. }
  283. // adjustCalcChain provides a function to update the calculation chain when
  284. // inserting or deleting rows or columns.
  285. func (f *File) adjustCalcChain(dir adjustDirection, num, offset, sheetID int) error {
  286. if f.CalcChain == nil {
  287. return nil
  288. }
  289. for index, c := range f.CalcChain.C {
  290. if c.I != sheetID {
  291. continue
  292. }
  293. colNum, rowNum, err := CellNameToCoordinates(c.R)
  294. if err != nil {
  295. return err
  296. }
  297. if dir == rows && num <= rowNum {
  298. if newRow := rowNum + offset; newRow > 0 {
  299. f.CalcChain.C[index].R, _ = CoordinatesToCellName(colNum, newRow)
  300. }
  301. }
  302. if dir == columns && num <= colNum {
  303. if newCol := colNum + offset; newCol > 0 {
  304. f.CalcChain.C[index].R, _ = CoordinatesToCellName(newCol, rowNum)
  305. }
  306. }
  307. }
  308. return nil
  309. }