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.
formula
Veniamin Albaev 6 years ago
parent b0ed4c12d2
commit 725c1a0c40

@ -10,13 +10,14 @@
package excelize
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDataValidation(t *testing.T) {
const resultFile = "./test/TestDataValidation.xlsx"
resultFile := filepath.Join("test", "TestDataValidation.xlsx")
xlsx := NewFile()
@ -50,7 +51,7 @@ func TestDataValidation(t *testing.T) {
}
func TestDataValidationError(t *testing.T) {
const resultFile = "./test/TestDataValidationError.xlsx"
resultFile := filepath.Join("test", "TestDataValidationError.xlsx")
xlsx := NewFile()
xlsx.SetCellStr("Sheet1", "E1", "E1")

@ -238,18 +238,16 @@ func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, rowIndex, offset int) {
}
for i, r := range xlsx.SheetData.Row {
if r.R >= rowIndex {
f.ajustSingleRowDimensions(&xlsx.SheetData.Row[i], offset)
f.ajustSingleRowDimensions(&xlsx.SheetData.Row[i], r.R+offset)
}
}
}
// ajustSingleRowDimensions provides a function to ajust single row
// dimensions.
func (f *File) ajustSingleRowDimensions(r *xlsxRow, offset int) {
r.R += offset
// ajustSingleRowDimensions provides a function to ajust single row dimensions.
func (f *File) ajustSingleRowDimensions(r *xlsxRow, row int) {
r.R = row
for i, col := range r.C {
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, col.R))
r.C[i].R = string(strings.Map(letterOnlyMapF, col.R)) + strconv.Itoa(row+offset)
r.C[i].R = string(strings.Map(letterOnlyMapF, col.R)) + strconv.Itoa(r.R)
}
}

File diff suppressed because it is too large Load Diff

@ -3,6 +3,5 @@ module github.com/360EntSecGroup-Skylar/excelize
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb
github.com/stretchr/testify v1.3.0
)

@ -1,8 +1,10 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb h1:cRItZejS4Ok67vfCdrbGIaqk86wmtQNOjVD7jSyS2aw=
github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

@ -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))

Loading…
Cancel
Save