提交 7b7ca99f 编写于 作者: V Veniamin Albaev 提交者: xurime

Duplicate row (#317)

* go mod tidy applied

* File.DuplicateRow() method added
上级 9b8baf75
......@@ -238,18 +238,19 @@ func (f *File) adjustRowDimensions(xlsx *xlsxWorksheet, rowIndex, offset int) {
}
for i, r := range xlsx.SheetData.Row {
if r.R >= rowIndex {
xlsx.SheetData.Row[i].R += offset
for k, v := range xlsx.SheetData.Row[i].C {
axis := v.R
col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row + offset
xlsx.SheetData.Row[i].C[k].R = col + strconv.Itoa(xAxis)
}
f.ajustSingleRowDimensions(&xlsx.SheetData.Row[i], offset)
}
}
}
func (f *File) ajustSingleRowDimensions(r *xlsxRow, offset int) {
r.R += offset
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)
}
}
// adjustHyperlinks provides a function to update hyperlinks when inserting or
// deleting rows or columns.
func (f *File) adjustHyperlinks(sheet string, column, rowIndex, offset int) {
......
......@@ -12,6 +12,8 @@ import (
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestOpenFile(t *testing.T) {
......@@ -1029,6 +1031,71 @@ func TestInsertRow(t *testing.T) {
}
}
func TestDuplicateRow(t *testing.T) {
const (
file = "./test/Book_DuplicateRow_%s.xlsx"
sheet = "Sheet1"
a1 = "A1"
b1 = "B1"
a2 = "A2"
b2 = "B2"
a3 = "A3"
b3 = "B3"
a4 = "A4"
b4 = "B4"
a1Value = "A1 value"
a2Value = "A2 value"
a3Value = "A3 value"
bnValue = "Bn value"
)
xlsx := NewFile()
xlsx.SetCellStr(sheet, a1, a1Value)
xlsx.SetCellStr(sheet, b1, bnValue)
t.Run("FromSingleRow", func(t *testing.T) {
xlsx.DuplicateRow(sheet, 1)
xlsx.DuplicateRow(sheet, 2)
if assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(file, "SignleRow"))) {
assert.Equal(t, a1Value, xlsx.GetCellValue(sheet, a1))
assert.Equal(t, a1Value, xlsx.GetCellValue(sheet, a2))
assert.Equal(t, a1Value, xlsx.GetCellValue(sheet, a3))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b1))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b2))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b3))
}
})
t.Run("UpdateDuplicatedRows", func(t *testing.T) {
xlsx.SetCellStr(sheet, a2, a2Value)
xlsx.SetCellStr(sheet, a3, a3Value)
if assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(file, "Updated"))) {
assert.Equal(t, a1Value, xlsx.GetCellValue(sheet, a1))
assert.Equal(t, a2Value, xlsx.GetCellValue(sheet, a2))
assert.Equal(t, a3Value, xlsx.GetCellValue(sheet, a3))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b1))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b2))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b3))
}
})
t.Run("FromFirstOfMultipleRows", func(t *testing.T) {
xlsx.DuplicateRow(sheet, 1)
if assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(file, "FirstOfMultipleRows"))) {
assert.Equal(t, a1Value, xlsx.GetCellValue(sheet, a1))
assert.Equal(t, a1Value, xlsx.GetCellValue(sheet, a2))
assert.Equal(t, a2Value, xlsx.GetCellValue(sheet, a3))
assert.Equal(t, a3Value, xlsx.GetCellValue(sheet, a4))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b1))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b2))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b3))
assert.Equal(t, bnValue, xlsx.GetCellValue(sheet, b4))
}
})
}
func TestSetPane(t *testing.T) {
xlsx := NewFile()
xlsx.SetPanes("Sheet1", `{"freeze":false,"split":false}`)
......
......@@ -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:
//
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册