From 7b7ca99f5d570c30f7eee92c38c5e632b7815239 Mon Sep 17 00:00:00 2001 From: Veniamin Albaev Date: Wed, 26 Dec 2018 08:33:40 +0300 Subject: Duplicate row (#317) * go mod tidy applied * File.DuplicateRow() method added --- rows.go | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'rows.go') diff --git a/rows.go b/rows.go index b633633..def150d 100644 --- a/rows.go +++ b/rows.go @@ -359,7 +359,7 @@ func (f *File) RemoveRow(sheet string, row int) { } } -// InsertRow provides a function to insert a new row before given row index. +// InsertRow provides a function to insert a new row after given row index. // For example, create a new row before row 3 in Sheet1: // // xlsx.InsertRow("Sheet1", 2) @@ -372,6 +372,46 @@ func (f *File) InsertRow(sheet string, row int) { f.adjustHelper(sheet, -1, row, 1) } +// DuplicateRow inserts a copy of specified row below specified +// +// xlsx.DuplicateRow("Sheet1", 2) +// +func (f *File) DuplicateRow(sheet string, row int) { + if row < 0 { + return + } + row2 := row + 1 + f.adjustHelper(sheet, -1, row2, 1) + + xlsx := f.workSheetReader(sheet) + idx := -1 + idx2 := -1 + + for i, r := range xlsx.SheetData.Row { + if r.R == row { + idx = i + } else if r.R == row2 { + idx2 = i + } + if idx != -1 && idx2 != -1 { + break + } + } + + if idx == -1 || (idx2 == -1 && len(xlsx.SheetData.Row) >= row2) { + return + } + rowData := xlsx.SheetData.Row[idx] + cols := make([]xlsxC, 0, len(rowData.C)) + rowData.C = append(cols, rowData.C...) + f.ajustSingleRowDimensions(&rowData, 1) + if idx2 != -1 { + xlsx.SheetData.Row[idx2] = rowData + } else { + xlsx.SheetData.Row = append(xlsx.SheetData.Row, rowData) + } +} + // checkRow provides a function to check and fill each column element for all // rows and make that is continuous in a worksheet of XML. For example: // -- cgit v1.2.1