diff options
author | Veniamin Albaev <albenik@gmail.com> | 2018-12-27 17:28:28 +0300 |
---|---|---|
committer | Veniamin Albaev <albenik@gmail.com> | 2019-01-10 14:29:19 +0300 |
commit | 725c1a0c40971282ff924f4802a57e4c17431691 (patch) | |
tree | a2ca4ea8afe8f48ddb11ad3dc7744b59a497ddc0 /rows.go | |
parent | b0ed4c12d2cced29d82e8a4af47f7b42f555ca8c (diff) |
New feature: File.DuplicateRowTo() duplicate row to specified row position.
DuplicateRowTo() is similar to DuplicateRow() but copies specified row not just after specified source row
but to any other specified position below or above source row.
Also I made minor modifications of tests: using filepath.Join() instead of direct unix-way paths strings
to avoid possible tests fails on other OS.
Diffstat (limited to 'rows.go')
-rw-r--r-- | rows.go | 57 |
1 files changed, 37 insertions, 20 deletions
@@ -376,38 +376,55 @@ func (f *File) InsertRow(sheet string, row int) { // xlsx.DuplicateRow("Sheet1", 2) // func (f *File) DuplicateRow(sheet string, row int) { - if row < 0 { + f.DuplicateRowTo(sheet, row, row+1) +} + +// DuplicateRowTo inserts a copy of specified row at specified row position +// movig down exists rows aftet target position +// +// xlsx.DuplicateRowTo("Sheet1", 2, 7) +// +func (f *File) DuplicateRowTo(sheet string, row, row2 int) { + if row <= 0 || row2 <= 0 || row == row2 { return } - row2 := row + 1 - f.adjustHelper(sheet, -1, row2, 1) - xlsx := f.workSheetReader(sheet) - idx := -1 - idx2 := -1 + ws := f.workSheetReader(sheet) - for i, r := range xlsx.SheetData.Row { + var ok bool + var rowCopy xlsxRow + + for i, r := range ws.SheetData.Row { if r.R == row { - idx = i - } else if r.R == row2 { - idx2 = i - } - if idx != -1 && idx2 != -1 { + rowCopy = ws.SheetData.Row[i] + ok = true break } } + if !ok { + return + } - if idx == -1 || (idx2 == -1 && len(xlsx.SheetData.Row) >= row2) { + f.adjustHelper(sheet, -1, row2, 1) + + idx2 := -1 + for i, r := range ws.SheetData.Row { + if r.R == row2 { + idx2 = i + break + } + } + if idx2 == -1 && len(ws.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) + + rowCopy.C = append(make([]xlsxC, 0, len(rowCopy.C)), rowCopy.C...) + f.ajustSingleRowDimensions(&rowCopy, row2) + if idx2 != -1 { - xlsx.SheetData.Row[idx2] = rowData + ws.SheetData.Row[idx2] = rowCopy } else { - xlsx.SheetData.Row = append(xlsx.SheetData.Row, rowData) + ws.SheetData.Row = append(ws.SheetData.Row, rowCopy) } } @@ -446,7 +463,7 @@ func checkRow(xlsx *xlsxWorksheet) { if lenCol < endCol { oldRow := xlsx.SheetData.Row[k].C xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0] - tmp := []xlsxC{} + var tmp []xlsxC for i := 0; i < endCol; i++ { buffer.WriteString(ToAlphaString(i)) buffer.WriteString(strconv.Itoa(endRow)) |