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
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.
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)
42 sheetID := f.getSheetID(sheet)
44 f.adjustRowDimensions(ws, num, offset)
46 f.adjustColDimensions(ws, num, offset)
48 f.adjustHyperlinks(ws, sheet, dir, num, offset)
49 if err = f.adjustMergeCells(ws, dir, num, offset); err != nil {
52 if err = f.adjustAutoFilter(ws, dir, num, offset); err != nil {
55 if err = f.adjustCalcChain(dir, num, offset, sheetID); err != nil {
61 if ws.MergeCells != nil && len(ws.MergeCells.Cells) == 0 {
68 // adjustColDimensions provides a function to update column dimensions when
69 // inserting or deleting rows or columns.
70 func (f *File) adjustColDimensions(ws *xlsxWorksheet, col, offset int) {
71 for rowIdx := range ws.SheetData.Row {
72 for colIdx, v := range ws.SheetData.Row[rowIdx].C {
73 cellCol, cellRow, _ := CellNameToCoordinates(v.R)
75 if newCol := cellCol + offset; newCol > 0 {
76 ws.SheetData.Row[rowIdx].C[colIdx].R, _ = CoordinatesToCellName(newCol, cellRow)
83 // adjustRowDimensions provides a function to update row dimensions when
84 // inserting or deleting rows or columns.
85 func (f *File) adjustRowDimensions(ws *xlsxWorksheet, row, offset int) {
86 for i := range ws.SheetData.Row {
87 r := &ws.SheetData.Row[i]
88 if newRow := r.R + offset; r.R >= row && newRow > 0 {
89 f.ajustSingleRowDimensions(r, newRow)
94 // ajustSingleRowDimensions provides a function to ajust single row dimensions.
95 func (f *File) ajustSingleRowDimensions(r *xlsxRow, num int) {
97 for i, col := range r.C {
98 colName, _, _ := SplitCellName(col.R)
99 r.C[i].R, _ = JoinCellName(colName, num)
103 // adjustHyperlinks provides a function to update hyperlinks when inserting or
104 // deleting rows or columns.
105 func (f *File) adjustHyperlinks(ws *xlsxWorksheet, sheet string, dir adjustDirection, num, offset int) {
107 if ws.Hyperlinks == nil || len(ws.Hyperlinks.Hyperlink) == 0 {
111 // order is important
113 for i := len(ws.Hyperlinks.Hyperlink) - 1; i >= 0; i-- {
114 linkData := ws.Hyperlinks.Hyperlink[i]
115 colNum, rowNum, _ := CellNameToCoordinates(linkData.Ref)
117 if (dir == rows && num == rowNum) || (dir == columns && num == colNum) {
118 f.deleteSheetRelationships(sheet, linkData.RID)
119 if len(ws.Hyperlinks.Hyperlink) > 1 {
120 ws.Hyperlinks.Hyperlink = append(ws.Hyperlinks.Hyperlink[:i],
121 ws.Hyperlinks.Hyperlink[i+1:]...)
128 if ws.Hyperlinks == nil {
131 for i := range ws.Hyperlinks.Hyperlink {
132 link := &ws.Hyperlinks.Hyperlink[i] // get reference
133 colNum, rowNum, _ := CellNameToCoordinates(link.Ref)
136 link.Ref, _ = CoordinatesToCellName(colNum, rowNum+offset)
140 link.Ref, _ = CoordinatesToCellName(colNum+offset, rowNum)
146 // adjustAutoFilter provides a function to update the auto filter when
147 // inserting or deleting rows or columns.
148 func (f *File) adjustAutoFilter(ws *xlsxWorksheet, dir adjustDirection, num, offset int) error {
149 if ws.AutoFilter == nil {
153 coordinates, err := f.areaRefToCoordinates(ws.AutoFilter.Ref)
157 x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
159 if (dir == rows && y1 == num && offset < 0) || (dir == columns && x1 == num && x2 == num) {
161 for rowIdx := range ws.SheetData.Row {
162 rowData := &ws.SheetData.Row[rowIdx]
163 if rowData.R > y1 && rowData.R <= y2 {
164 rowData.Hidden = false
170 coordinates = f.adjustAutoFilterHelper(dir, coordinates, num, offset)
171 x1, y1, x2, y2 = coordinates[0], coordinates[1], coordinates[2], coordinates[3]
173 if ws.AutoFilter.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
179 // adjustAutoFilterHelper provides a function for adjusting auto filter to
180 // compare and calculate cell axis by the given adjust direction, operation
182 func (f *File) adjustAutoFilterHelper(dir adjustDirection, coordinates []int, num, offset int) []int {
184 if coordinates[1] >= num {
185 coordinates[1] += offset
187 if coordinates[3] >= num {
188 coordinates[3] += offset
191 if coordinates[2] >= num {
192 coordinates[2] += offset
198 // areaRefToCoordinates provides a function to convert area reference to a
199 // pair of coordinates.
200 func (f *File) areaRefToCoordinates(ref string) ([]int, error) {
201 rng := strings.Split(strings.Replace(ref, "$", "", -1), ":")
202 return areaRangeToCoordinates(rng[0], rng[1])
205 // areaRangeToCoordinates provides a function to convert cell range to a
206 // pair of coordinates.
207 func areaRangeToCoordinates(firstCell, lastCell string) ([]int, error) {
208 coordinates := make([]int, 4)
210 coordinates[0], coordinates[1], err = CellNameToCoordinates(firstCell)
212 return coordinates, err
214 coordinates[2], coordinates[3], err = CellNameToCoordinates(lastCell)
215 return coordinates, err
218 // sortCoordinates provides a function to correct the coordinate area, such
219 // correct C1:B3 to B1:C3.
220 func sortCoordinates(coordinates []int) error {
221 if len(coordinates) != 4 {
222 return errors.New("coordinates length must be 4")
224 if coordinates[2] < coordinates[0] {
225 coordinates[2], coordinates[0] = coordinates[0], coordinates[2]
227 if coordinates[3] < coordinates[1] {
228 coordinates[3], coordinates[1] = coordinates[1], coordinates[3]
233 // coordinatesToAreaRef provides a function to convert a pair of coordinates
234 // to area reference.
235 func (f *File) coordinatesToAreaRef(coordinates []int) (string, error) {
236 if len(coordinates) != 4 {
237 return "", errors.New("coordinates length must be 4")
239 firstCell, err := CoordinatesToCellName(coordinates[0], coordinates[1])
243 lastCell, err := CoordinatesToCellName(coordinates[2], coordinates[3])
247 return firstCell + ":" + lastCell, err
250 // adjustMergeCells provides a function to update merged cells when inserting
251 // or deleting rows or columns.
252 func (f *File) adjustMergeCells(ws *xlsxWorksheet, dir adjustDirection, num, offset int) error {
253 if ws.MergeCells == nil {
257 for i := 0; i < len(ws.MergeCells.Cells); i++ {
258 areaData := ws.MergeCells.Cells[i]
259 coordinates, err := f.areaRefToCoordinates(areaData.Ref)
263 x1, y1, x2, y2 := coordinates[0], coordinates[1], coordinates[2], coordinates[3]
265 if y1 == num && y2 == num && offset < 0 {
266 f.deleteMergeCell(ws, i)
269 y1 = f.adjustMergeCellsHelper(y1, num, offset)
270 y2 = f.adjustMergeCellsHelper(y2, num, offset)
272 if x1 == num && x2 == num && offset < 0 {
273 f.deleteMergeCell(ws, i)
276 x1 = f.adjustMergeCellsHelper(x1, num, offset)
277 x2 = f.adjustMergeCellsHelper(x2, num, offset)
279 if x1 == x2 && y1 == y2 {
280 f.deleteMergeCell(ws, i)
283 if areaData.Ref, err = f.coordinatesToAreaRef([]int{x1, y1, x2, y2}); err != nil {
290 // adjustMergeCellsHelper provides a function for adjusting merge cells to
291 // compare and calculate cell axis by the given pivot, operation axis and
293 func (f *File) adjustMergeCellsHelper(pivot, num, offset int) int {
304 // deleteMergeCell provides a function to delete merged cell by given index.
305 func (f *File) deleteMergeCell(ws *xlsxWorksheet, idx int) {
306 if len(ws.MergeCells.Cells) > idx {
307 ws.MergeCells.Cells = append(ws.MergeCells.Cells[:idx], ws.MergeCells.Cells[idx+1:]...)
308 ws.MergeCells.Count = len(ws.MergeCells.Cells)
312 // adjustCalcChain provides a function to update the calculation chain when
313 // inserting or deleting rows or columns.
314 func (f *File) adjustCalcChain(dir adjustDirection, num, offset, sheetID int) error {
315 if f.CalcChain == nil {
318 for index, c := range f.CalcChain.C {
322 colNum, rowNum, err := CellNameToCoordinates(c.R)
326 if dir == rows && num <= rowNum {
327 if newRow := rowNum + offset; newRow > 0 {
328 f.CalcChain.C[index].R, _ = CoordinatesToCellName(colNum, newRow)
331 if dir == columns && num <= colNum {
332 if newCol := colNum + offset; newCol > 0 {
333 f.CalcChain.C[index].R, _ = CoordinatesToCellName(newCol, rowNum)