Browse Source

Merge pull request #334 from albenik/duplicate_row

Resolve #333 New feature: File.DuplicateRowTo() duplicate row to a specified row position
xuri 7 years ago
parent
commit
7f1323f7ac
6 changed files with 424 additions and 164 deletions
  1. 3 2
      datavalidation_test.go
  2. 5 7
      excelize.go
  3. 374 131
      excelize_test.go
  4. 1 2
      go.mod
  5. 4 2
      go.sum
  6. 37 20
      rows.go

+ 3 - 2
datavalidation_test.go

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

+ 5 - 7
excelize.go

@@ -238,18 +238,16 @@ func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, rowIndex, offset int) {
 	}
 	}
 	for i, r := range xlsx.SheetData.Row {
 	for i, r := range xlsx.SheetData.Row {
 		if r.R >= rowIndex {
 		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 {
 	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
+ 374 - 131
excelize_test.go


+ 1 - 2
go.mod

@@ -3,6 +3,5 @@ module github.com/360EntSecGroup-Skylar/excelize
 require (
 require (
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
 	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
 )
 )

+ 4 - 2
go.sum

@@ -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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 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 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
 github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 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=

+ 37 - 20
rows.go

@@ -376,38 +376,55 @@ func (f *File) InsertRow(sheet string, row int) {
 //    xlsx.DuplicateRow("Sheet1", 2)
 //    xlsx.DuplicateRow("Sheet1", 2)
 //
 //
 func (f *File) DuplicateRow(sheet string, row int) {
 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
 		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 {
 		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
 			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
 		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 {
 	if idx2 != -1 {
-		xlsx.SheetData.Row[idx2] = rowData
+		ws.SheetData.Row[idx2] = rowCopy
 	} else {
 	} 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 {
 			if lenCol < endCol {
 				oldRow := xlsx.SheetData.Row[k].C
 				oldRow := xlsx.SheetData.Row[k].C
 				xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0]
 				xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0]
-				tmp := []xlsxC{}
+				var tmp []xlsxC
 				for i := 0; i < endCol; i++ {
 				for i := 0; i < endCol; i++ {
 					buffer.WriteString(ToAlphaString(i))
 					buffer.WriteString(ToAlphaString(i))
 					buffer.WriteString(strconv.Itoa(endRow))
 					buffer.WriteString(strconv.Itoa(endRow))

Some files were not shown because too many files changed in this diff