adjust.go 9.9 KB

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