summaryrefslogtreecommitdiff
path: root/rows.go
diff options
context:
space:
mode:
authorVeniamin Albaev <albenik@gmail.com>2018-12-26 08:33:40 +0300
committerxuri <xuri.me@gmail.com>2018-12-26 13:33:40 +0800
commit7b7ca99f5d570c30f7eee92c38c5e632b7815239 (patch)
tree280ea5f79cc5875232044c81dd57bc3f344f3de1 /rows.go
parent9b8baf75ad7613dba24635b4c66791e404c6b7d5 (diff)
Duplicate row (#317)
* go mod tidy applied * File.DuplicateRow() method added
Diffstat (limited to 'rows.go')
-rw-r--r--rows.go42
1 files changed, 41 insertions, 1 deletions
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:
//