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
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.
19 type adjustDirection bool
22 columns adjustDirection = false
23 rows adjustDirection = true
26 // adjustHelper provides a function to adjust rows and columns dimensions,
27 // hyperlinks, merged cells and auto filter when inserting or deleting rows or
30 // sheet: Worksheet name that we're editing
31 // column: Index number of the column we're inserting/deleting before
32 // row: Index number of the row we're inserting/deleting before
33 // offset: Number of rows/column to insert/delete negative values indicate deletion
35 // TODO: adjustPageBreaks, adjustComments, adjustDataValidations, adjustProtectedCells
37 func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
38 ws, err := f.workSheetReader(sheet)
43 f.adjustRowDimensions(ws, num, offset)
45 f.adjustColDimensions(ws, num, offset)
47 f.adjustHyperlinks(ws, sheet, dir, num, offset)
48 if err = f.adjustMergeCells(ws, dir, num, offset); err != nil {
51 if err = f.adjustAutoFilter(ws, dir, num, offset); err != nil {
54 if err = f.adjustCalcChain(dir, num, offset); err != nil {
60 if ws.MergeCells != nil && len(ws.MergeCells.Cells) == 0 {
67 // adjustColDimensions provides a function to update column dimensions when
68 // inserting or deleting rows or columns.
69 func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) {
70 for rowIdx := range ws.SheetData.Row {
71 for colIdx, v := range ws.SheetData.Row[rowIdx].C {
72 cellCol, cellRow, _ := CellNameToCoordinates(v.R)
74 if newCol := cellCol + offset; newCol > 0 {
75 ws.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
82 // adjustRowDimensions provides a function to update row dimensions when
83 // inserting or deleting rows or columns.
84 func (f *File) adjustRowDimensions(ws *xlsxWorksheet, row, offset int) {
85 for i := range ws.SheetData.Row {
86 r := &ws.SheetData.Row[i]
87 if newRow := r.R + offset; r.R >= row && newRow > 0 {
88 f.ajustSingleRowDimensions(r, newRow)
93 // ajustSingleRowDimensions provides a function to ajust single row dimensions.
94 func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
96 for i, col := range r.C {
97 colName, _, _ := SplitCellName(col.R)
98 r.C[i].R, _ = JoinCellName(colName, num)
102 // adjustHyperlinks provides a function to update hyperlinks when inserting or
103 // deleting rows or columns.
104 func (f *File) adjustHyperlinks(ws *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
106 if ws.Hyperlinks == nil || len(ws.Hyperlinks.Hyperlink) == 0 {
110 // order is important
112 for i := len(ws.Hyperlinks.Hyperlink) - 1; i >= 0; i-- {
113 linkData := ws.Hyperlinks.Hyperlink[i]
114 colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
116 if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
117 f.deleteSheetRelationships(sheet, linkData.RID)
118 if len(ws.Hyperlinks.Hyperlink) > 1 {
119 ws.Hyperlinks.Hyperlink = append(ws.Hyperlinks.Hyperlink[:i],
120 ws.Hyperlinks.Hyperlink[i+1:]...)
127 if ws.Hyperlinks == nil {
130 for i := range ws.Hyperlinks.Hyperlink {
131 link := &ws.Hyperlinks.Hyperlink[i] // get reference
132 colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
135 link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
139 link.Ref, _ = CoordinatesToCellName(colNum+offset, rowNum)
145 // adjustAutoFilter provides a function to update the auto filter when
146 // inserting or deleting rows or columns.
147 func (f *File) adjustAutoFilter(ws *xlsxWorksheet, dir adjustDirection, num, offset int) error {
148 if ws.AutoFilter == nil {
152 coordinates, err := f.areaRefToCoordinates(ws.AutoFilter.Ref)
156 x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
158 if (dir == rows && y1 == num && offset < 0) || (dir == columns && x1 == num && x2 == num) {
160 for rowIdx := range ws.SheetData.Row {
161 rowData := &ws.SheetData.Row[rowIdx]
162 if rowData.R > y1 && rowData.R <= y2 {
163 rowData.Hidden = false
169 coordinates = f.adjustAutoFilterHelper(dir, coordinates, num, offset)
170 x1, y1, x2, y2 = coordinates[0], coordinates[1], coordinates[2], coordinates[3]
172 if ws.AutoFilter.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
178 // adjustAutoFilterHelper provides a function for adjusting auto filter to
179 // compare and calculate cell axis by the given adjust direction, operation
181 func (f *File) adjustAutoFilterHelper(dir adjustDirection, coordinates []int, num, offset int) []int {
183 if coordinates[1] >= num {
184 coordinates[1] += offset
186 if coordinates[3] >= num {
187 coordinates[3] += offset
190 if coordinates[2] >= num {
191 coordinates[2] += offset
197 // areaRefToCoordinates provides a function to convert area reference to a
198 // pair of coordinates.
199 func (f *File) areaRefToCoordinates(ref string) ([]int, error) {
200 rng := strings.Split(ref, ":")
201 return areaRangeToCoordinates(rng[0], rng[1])
204 // areaRangeToCoordinates provides a function to convert cell range to a
205 // pair of coordinates.
206 func areaRangeToCoordinates(firstCell, lastCell string) ([]int, error) {
207 coordinates := make([]int, 4)
209 coordinates[0], coordinates[1], err = CellNameToCoordinates(firstCell)
211 return coordinates, err
213 coordinates[2], coordinates[3], err = CellNameToCoordinates(lastCell)
214 return coordinates, err
217 // sortCoordinates provides a function to correct the coordinate area, such
218 // correct C1:B3 to B1:C3.
219 func sortCoordinates(coordinates []int) error {
220 if len(coordinates) != 4 {
221 return errors.New("coordinates length must be 4")
223 if coordinates[2] < coordinates[0] {
224 coordinates[2], coordinates[0] = coordinates[0], coordinates[2]
226 if coordinates[3] < coordinates[1] {
227 coordinates[3], coordinates[1] = coordinates[1], coordinates[3]
232 // coordinatesToAreaRef provides a function to convert a pair of coordinates
233 // to area reference.
234 func (f *File) coordinatesToAreaRef(coordinates []int) (string, error) {
235 if len(coordinates) != 4 {
236 return "", errors.New("coordinates length must be 4")
238 firstCell, err := CoordinatesToCellName(coordinates[0], coordinates[1])
242 lastCell, err := CoordinatesToCellName(coordinates[2], coordinates[3])
246 return firstCell + ":" + lastCell, err
249 // adjustMergeCells provides a function to update merged cells when inserting
250 // or deleting rows or columns.
251 func (f *File) adjustMergeCells(ws *xlsxWorksheet, dir adjustDirection, num, offset int) error {
252 if ws.MergeCells == nil {
256 for i := 0; i < len(ws.MergeCells.Cells); i++ {
257 areaData := ws.MergeCells.Cells[i]
258 coordinates, err := f.areaRefToCoordinates(areaData.Ref)
262 x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
264 if y1 == num && y2 == num && offset < 0 {
265 f.deleteMergeCell(ws, i)
268 y1 = f.adjustMergeCellsHelper(y1, num, offset)
269 y2 = f.adjustMergeCellsHelper(y2, num, offset)
271 if x1 == num && x2 == num && offset < 0 {
272 f.deleteMergeCell(ws, i)
275 x1 = f.adjustMergeCellsHelper(x1, num, offset)
276 x2 = f.adjustMergeCellsHelper(x2, num, offset)
278 if x1 == x2 && y1 == y2 {
279 f.deleteMergeCell(ws, i)
282 if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
289 // adjustMergeCellsHelper provides a function for adjusting merge cells to
290 // compare and calculate cell axis by the given pivot, operation axis and
292 func (f *File) adjustMergeCellsHelper(pivot, num, offset int) int {
303 // deleteMergeCell provides a function to delete merged cell by given index.
304 func (f *File) deleteMergeCell(ws *xlsxWorksheet, idx int) {
305 if len(ws.MergeCells.Cells) > idx {
306 ws.MergeCells.Cells = append(ws.MergeCells.Cells[:idx], ws.MergeCells.Cells[idx+1:]...)
307 ws.MergeCells.Count = len(ws.MergeCells.Cells)
311 // adjustCalcChain provides a function to update the calculation chain when
312 // inserting or deleting rows or columns.
313 func (f *File) adjustCalcChain(dir adjustDirection, num, offset int) error {
314 if f.CalcChain == nil {
317 for index, c := range f.CalcChain.C {
318 colNum, rowNum, err := CellNameToCoordinates(c.R)
322 if dir == rows && num <= rowNum {
323 if newRow := rowNum + offset; newRow > 0 {
324 f.CalcChain.C[index].R, _ = CoordinatesToCellName(colNum, newRow)
327 if dir == columns && num <= colNum {
328 if newCol := colNum + offset; newCol > 0 {
329 f.CalcChain.C[index].R, _ = CoordinatesToCellName(newCol, rowNum)