adjust.go 9.9 KB

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