提交 725c1a0c 编写于 作者: V Veniamin Albaev

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.
上级 b0ed4c12
......@@ -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)
}
}
......
此差异已折叠。
......@@ -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
)
......@@ -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))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册