2019-03-06 21:40:45 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-18 14:57:35 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-06 21:40:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRows(t *testing.T) {
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
const sheet2 = "Sheet2"
|
|
|
|
|
2019-03-06 21:40:45 +08:00
|
|
|
xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
rows, err := xlsx.Rows(sheet2)
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
collectedRows := make([][]string, 0)
|
2019-03-06 21:40:45 +08:00
|
|
|
for rows.Next() {
|
2019-03-23 20:08:06 +08:00
|
|
|
columns, err := rows.Columns()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
collectedRows = append(collectedRows, trimSliceSpace(columns))
|
2019-03-06 21:40:45 +08:00
|
|
|
}
|
|
|
|
if !assert.NoError(t, rows.Error()) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
returnedRows, err := xlsx.GetRows(sheet2)
|
|
|
|
assert.NoError(t, err)
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
for i := range returnedRows {
|
|
|
|
returnedRows[i] = trimSliceSpace(returnedRows[i])
|
2019-03-06 21:40:45 +08:00
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
if !assert.Equal(t, collectedRows, returnedRows) {
|
|
|
|
t.FailNow()
|
2019-03-06 21:40:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 14:57:35 +08:00
|
|
|
// test bug https://github.com/360EntSecGroup-Skylar/excelize/issues/502
|
|
|
|
func TestRowsIterator(t *testing.T) {
|
|
|
|
const (
|
|
|
|
sheet2 = "Sheet2"
|
|
|
|
expectedNumRow = 11
|
|
|
|
)
|
|
|
|
xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rows, err := xlsx.Rows(sheet2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
var rowCount int
|
|
|
|
for rows.Next() {
|
|
|
|
rowCount++
|
|
|
|
require.True(t, rowCount <= expectedNumRow, "rowCount is greater than expected")
|
|
|
|
}
|
|
|
|
assert.Equal(t, expectedNumRow, rowCount)
|
|
|
|
}
|
|
|
|
|
2019-03-06 21:40:45 +08:00
|
|
|
func TestRowsError(t *testing.T) {
|
|
|
|
xlsx, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
_, err = xlsx.Rows("SheetN")
|
2019-04-16 10:57:21 +08:00
|
|
|
assert.EqualError(t, err, "sheet SheetN is not exist")
|
2019-03-06 21:40:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRowHeight(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
sheet1 := xlsx.GetSheetName(1)
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, xlsx.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0), "invalid row number 0")
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2019-04-16 10:57:21 +08:00
|
|
|
_, err := xlsx.GetRowHeight("Sheet1", 0)
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, err, "invalid row number 0")
|
2019-03-06 21:40:45 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.SetRowHeight(sheet1, 1, 111.0))
|
2019-04-16 10:57:21 +08:00
|
|
|
height, err := xlsx.GetRowHeight(sheet1, 1)
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 111.0, height)
|
2019-03-06 21:40:45 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.SetRowHeight(sheet1, 4, 444.0))
|
|
|
|
height, err = xlsx.GetRowHeight(sheet1, 4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 444.0, height)
|
2019-03-06 21:40:45 +08:00
|
|
|
|
2019-03-24 13:08:32 +08:00
|
|
|
// Test get row height that rows index over exists rows.
|
|
|
|
height, err = xlsx.GetRowHeight(sheet1, 5)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, defaultRowHeightPixels, height)
|
|
|
|
|
|
|
|
// Test get row height that rows heights haven't changed.
|
|
|
|
height, err = xlsx.GetRowHeight(sheet1, 3)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, defaultRowHeightPixels, height)
|
|
|
|
|
2019-04-16 10:57:21 +08:00
|
|
|
// Test set and get row height on not exists worksheet.
|
|
|
|
assert.EqualError(t, xlsx.SetRowHeight("SheetN", 1, 111.0), "sheet SheetN is not exist")
|
|
|
|
_, err = xlsx.GetRowHeight("SheetN", 3)
|
|
|
|
assert.EqualError(t, err, "sheet SheetN is not exist")
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
err = xlsx.SaveAs(filepath.Join("test", "TestRowHeight.xlsx"))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
convertColWidthToPixels(0)
|
|
|
|
}
|
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
func TestRowVisibility(t *testing.T) {
|
|
|
|
xlsx, err := prepareTestBook1()
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2019-04-16 10:57:21 +08:00
|
|
|
xlsx.NewSheet("Sheet3")
|
|
|
|
assert.NoError(t, xlsx.SetRowVisible("Sheet3", 2, false))
|
|
|
|
assert.NoError(t, xlsx.SetRowVisible("Sheet3", 2, true))
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
xlsx.GetRowVisible("Sheet3", 2)
|
2019-03-24 13:08:32 +08:00
|
|
|
xlsx.GetRowVisible("Sheet3", 25)
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, xlsx.SetRowVisible("Sheet3", 0, true), "invalid row number 0")
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
visible, err := xlsx.GetRowVisible("Sheet3", 0)
|
|
|
|
assert.Equal(t, false, visible)
|
|
|
|
assert.EqualError(t, err, "invalid row number 0")
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
|
|
|
assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestRowVisibility.xlsx")))
|
|
|
|
}
|
|
|
|
|
2019-03-06 21:40:45 +08:00
|
|
|
func TestRemoveRow(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
sheet1 := xlsx.GetSheetName(1)
|
2019-04-15 11:22:57 +08:00
|
|
|
r, err := xlsx.workSheetReader(sheet1)
|
|
|
|
assert.NoError(t, err)
|
2019-03-06 21:40:45 +08:00
|
|
|
const (
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
colCount = 10
|
|
|
|
rowCount = 10
|
2019-03-06 21:40:45 +08:00
|
|
|
)
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
fillCells(xlsx, sheet1, colCount, rowCount)
|
|
|
|
|
2019-03-06 21:40:45 +08:00
|
|
|
xlsx.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, xlsx.RemoveRow(sheet1, -1), "invalid row number -1")
|
2019-03-06 21:40:45 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, xlsx.RemoveRow(sheet1, 0), "invalid row number 0")
|
2019-03-06 21:40:45 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.RemoveRow(sheet1, 4))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.Len(t, r.SheetData.Row, rowCount-1) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
xlsx.MergeCell(sheet1, "B3", "B5")
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.RemoveRow(sheet1, 2))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.Len(t, r.SheetData.Row, rowCount-2) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.RemoveRow(sheet1, 4))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.Len(t, r.SheetData.Row, rowCount-3) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-04-15 11:22:57 +08:00
|
|
|
err = xlsx.AutoFilter(sheet1, "A2", "A2", `{"column":"A","expression":"x != blanks"}`)
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.RemoveRow(sheet1, 1))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.Len(t, r.SheetData.Row, rowCount-4) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.RemoveRow(sheet1, 2))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.Len(t, r.SheetData.Row, rowCount-5) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.RemoveRow(sheet1, 1))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.Len(t, r.SheetData.Row, rowCount-6) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-03-24 13:08:32 +08:00
|
|
|
assert.NoError(t, xlsx.RemoveRow(sheet1, 10))
|
2019-03-06 21:40:45 +08:00
|
|
|
assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestRemoveRow.xlsx")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsertRow(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
sheet1 := xlsx.GetSheetName(1)
|
2019-04-15 11:22:57 +08:00
|
|
|
r, err := xlsx.workSheetReader(sheet1)
|
|
|
|
assert.NoError(t, err)
|
2019-03-06 21:40:45 +08:00
|
|
|
const (
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
colCount = 10
|
|
|
|
rowCount = 10
|
2019-03-06 21:40:45 +08:00
|
|
|
)
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
fillCells(xlsx, sheet1, colCount, rowCount)
|
|
|
|
|
2019-03-06 21:40:45 +08:00
|
|
|
xlsx.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, xlsx.InsertRow(sheet1, -1), "invalid row number -1")
|
2019-03-06 21:40:45 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, xlsx.InsertRow(sheet1, 0), "invalid row number 0")
|
2019-03-06 21:40:45 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.InsertRow(sheet1, 1))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.Len(t, r.SheetData.Row, rowCount+1) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.InsertRow(sheet1, 4))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.Len(t, r.SheetData.Row, rowCount+2) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestInsertRow.xlsx")))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Testing internal sructure state after insert operations.
|
|
|
|
// It is important for insert workflow to be constant to avoid side effect with functions related to internal structure.
|
|
|
|
func TestInsertRowInEmptyFile(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
sheet1 := xlsx.GetSheetName(1)
|
2019-04-15 11:22:57 +08:00
|
|
|
r, err := xlsx.workSheetReader(sheet1)
|
|
|
|
assert.NoError(t, err)
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.InsertRow(sheet1, 1))
|
2019-03-06 21:40:45 +08:00
|
|
|
assert.Len(t, r.SheetData.Row, 0)
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.InsertRow(sheet1, 2))
|
2019-03-06 21:40:45 +08:00
|
|
|
assert.Len(t, r.SheetData.Row, 0)
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.InsertRow(sheet1, 99))
|
2019-03-06 21:40:45 +08:00
|
|
|
assert.Len(t, r.SheetData.Row, 0)
|
|
|
|
assert.NoError(t, xlsx.SaveAs(filepath.Join("test", "TestInsertRowInEmptyFile.xlsx")))
|
|
|
|
}
|
|
|
|
|
2019-04-16 10:57:21 +08:00
|
|
|
func TestDuplicateRowFromSingleRow(t *testing.T) {
|
2019-03-06 21:40:45 +08:00
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
|
|
|
|
|
|
|
|
cells := map[string]string{
|
|
|
|
"A1": "A1 Value",
|
|
|
|
"A2": "A2 Value",
|
|
|
|
"A3": "A3 Value",
|
|
|
|
"B1": "B1 Value",
|
|
|
|
"B2": "B2 Value",
|
|
|
|
"B3": "B3 Value",
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("FromSingleRow", func(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
xlsx.SetCellStr(sheet, "A1", cells["A1"])
|
|
|
|
xlsx.SetCellStr(sheet, "B1", cells["B1"])
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.DuplicateRow(sheet, 1))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.FromSingleRow_1"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
expect := map[string]string{
|
|
|
|
"A1": cells["A1"], "B1": cells["B1"],
|
|
|
|
"A2": cells["A1"], "B2": cells["B1"],
|
|
|
|
}
|
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v, cell) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.DuplicateRow(sheet, 2))
|
2019-03-06 21:40:45 +08:00
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.FromSingleRow_2"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
expect = map[string]string{
|
|
|
|
"A1": cells["A1"], "B1": cells["B1"],
|
|
|
|
"A2": cells["A1"], "B2": cells["B1"],
|
|
|
|
"A3": cells["A1"], "B3": cells["B1"],
|
|
|
|
}
|
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v, cell) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateRowUpdateDuplicatedRows(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
|
|
|
|
|
|
|
|
cells := map[string]string{
|
|
|
|
"A1": "A1 Value",
|
|
|
|
"A2": "A2 Value",
|
|
|
|
"A3": "A3 Value",
|
|
|
|
"B1": "B1 Value",
|
|
|
|
"B2": "B2 Value",
|
|
|
|
"B3": "B3 Value",
|
|
|
|
}
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
t.Run("UpdateDuplicatedRows", func(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
xlsx.SetCellStr(sheet, "A1", cells["A1"])
|
|
|
|
xlsx.SetCellStr(sheet, "B1", cells["B1"])
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.DuplicateRow(sheet, 1))
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
xlsx.SetCellStr(sheet, "A2", cells["A2"])
|
|
|
|
xlsx.SetCellStr(sheet, "B2", cells["B2"])
|
|
|
|
|
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.UpdateDuplicatedRows"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
expect := map[string]string{
|
|
|
|
"A1": cells["A1"], "B1": cells["B1"],
|
|
|
|
"A2": cells["A2"], "B2": cells["B2"],
|
|
|
|
}
|
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v, cell) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateRowFirstOfMultipleRows(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
|
|
|
|
|
|
|
|
cells := map[string]string{
|
|
|
|
"A1": "A1 Value",
|
|
|
|
"A2": "A2 Value",
|
|
|
|
"A3": "A3 Value",
|
|
|
|
"B1": "B1 Value",
|
|
|
|
"B2": "B2 Value",
|
|
|
|
"B3": "B3 Value",
|
|
|
|
}
|
|
|
|
|
|
|
|
newFileWithDefaults := func() *File {
|
|
|
|
f := NewFile()
|
|
|
|
for cell, val := range cells {
|
|
|
|
f.SetCellStr(sheet, cell, val)
|
|
|
|
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
t.Run("FirstOfMultipleRows", func(t *testing.T) {
|
|
|
|
xlsx := newFileWithDefaults()
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.DuplicateRow(sheet, 1))
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.FirstOfMultipleRows"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
expect := map[string]string{
|
|
|
|
"A1": cells["A1"], "B1": cells["B1"],
|
|
|
|
"A2": cells["A1"], "B2": cells["B1"],
|
|
|
|
"A3": cells["A2"], "B3": cells["B2"],
|
|
|
|
"A4": cells["A3"], "B4": cells["B3"],
|
|
|
|
}
|
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v, cell) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateRowZeroWithNoRows(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
t.Run("ZeroWithNoRows", func(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, xlsx.DuplicateRow(sheet, 0), "invalid row number 0")
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.ZeroWithNoRows"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
val, err := xlsx.GetCellValue(sheet, "A1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "", val)
|
|
|
|
val, err = xlsx.GetCellValue(sheet, "B1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "", val)
|
|
|
|
val, err = xlsx.GetCellValue(sheet, "A2")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "", val)
|
|
|
|
val, err = xlsx.GetCellValue(sheet, "B2")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "", val)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2019-03-06 21:40:45 +08:00
|
|
|
expect := map[string]string{
|
|
|
|
"A1": "", "B1": "",
|
|
|
|
"A2": "", "B2": "",
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2019-03-06 21:40:45 +08:00
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v, cell) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateRowMiddleRowOfEmptyFile(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
t.Run("MiddleRowOfEmptyFile", func(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.DuplicateRow(sheet, 99))
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.MiddleRowOfEmptyFile"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
expect := map[string]string{
|
|
|
|
"A98": "",
|
|
|
|
"A99": "",
|
|
|
|
"A100": "",
|
|
|
|
}
|
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v, cell) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateRowWithLargeOffsetToMiddleOfData(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
|
|
|
|
|
|
|
|
cells := map[string]string{
|
|
|
|
"A1": "A1 Value",
|
|
|
|
"A2": "A2 Value",
|
|
|
|
"A3": "A3 Value",
|
|
|
|
"B1": "B1 Value",
|
|
|
|
"B2": "B2 Value",
|
|
|
|
"B3": "B3 Value",
|
|
|
|
}
|
|
|
|
|
|
|
|
newFileWithDefaults := func() *File {
|
|
|
|
f := NewFile()
|
|
|
|
for cell, val := range cells {
|
|
|
|
f.SetCellStr(sheet, cell, val)
|
|
|
|
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
t.Run("WithLargeOffsetToMiddleOfData", func(t *testing.T) {
|
|
|
|
xlsx := newFileWithDefaults()
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.DuplicateRowTo(sheet, 1, 3))
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.WithLargeOffsetToMiddleOfData"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
expect := map[string]string{
|
|
|
|
"A1": cells["A1"], "B1": cells["B1"],
|
|
|
|
"A2": cells["A2"], "B2": cells["B2"],
|
|
|
|
"A3": cells["A1"], "B3": cells["B1"],
|
|
|
|
"A4": cells["A3"], "B4": cells["B3"],
|
|
|
|
}
|
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v, cell) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateRowWithLargeOffsetToEmptyRows(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
|
|
|
|
|
|
|
|
cells := map[string]string{
|
|
|
|
"A1": "A1 Value",
|
|
|
|
"A2": "A2 Value",
|
|
|
|
"A3": "A3 Value",
|
|
|
|
"B1": "B1 Value",
|
|
|
|
"B2": "B2 Value",
|
|
|
|
"B3": "B3 Value",
|
|
|
|
}
|
|
|
|
|
|
|
|
newFileWithDefaults := func() *File {
|
|
|
|
f := NewFile()
|
|
|
|
for cell, val := range cells {
|
|
|
|
f.SetCellStr(sheet, cell, val)
|
|
|
|
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
t.Run("WithLargeOffsetToEmptyRows", func(t *testing.T) {
|
|
|
|
xlsx := newFileWithDefaults()
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.DuplicateRowTo(sheet, 1, 7))
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.WithLargeOffsetToEmptyRows"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
expect := map[string]string{
|
|
|
|
"A1": cells["A1"], "B1": cells["B1"],
|
|
|
|
"A2": cells["A2"], "B2": cells["B2"],
|
|
|
|
"A3": cells["A3"], "B3": cells["B3"],
|
|
|
|
"A7": cells["A1"], "B7": cells["B1"],
|
|
|
|
}
|
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v, cell) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateRowInsertBefore(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
|
|
|
|
|
|
|
|
cells := map[string]string{
|
|
|
|
"A1": "A1 Value",
|
|
|
|
"A2": "A2 Value",
|
|
|
|
"A3": "A3 Value",
|
|
|
|
"B1": "B1 Value",
|
|
|
|
"B2": "B2 Value",
|
|
|
|
"B3": "B3 Value",
|
|
|
|
}
|
|
|
|
|
|
|
|
newFileWithDefaults := func() *File {
|
|
|
|
f := NewFile()
|
|
|
|
for cell, val := range cells {
|
|
|
|
f.SetCellStr(sheet, cell, val)
|
|
|
|
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
t.Run("InsertBefore", func(t *testing.T) {
|
|
|
|
xlsx := newFileWithDefaults()
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.DuplicateRowTo(sheet, 2, 1))
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.InsertBefore"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := map[string]string{
|
|
|
|
"A1": cells["A2"], "B1": cells["B2"],
|
|
|
|
"A2": cells["A1"], "B2": cells["B1"],
|
|
|
|
"A3": cells["A2"], "B3": cells["B2"],
|
|
|
|
"A4": cells["A3"], "B4": cells["B3"],
|
|
|
|
}
|
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v, cell) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-04-16 10:57:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateRowInsertBeforeWithLargeOffset(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRow.%s.xlsx")
|
|
|
|
|
|
|
|
cells := map[string]string{
|
|
|
|
"A1": "A1 Value",
|
|
|
|
"A2": "A2 Value",
|
|
|
|
"A3": "A3 Value",
|
|
|
|
"B1": "B1 Value",
|
|
|
|
"B2": "B2 Value",
|
|
|
|
"B3": "B3 Value",
|
|
|
|
}
|
|
|
|
|
|
|
|
newFileWithDefaults := func() *File {
|
|
|
|
f := NewFile()
|
|
|
|
for cell, val := range cells {
|
|
|
|
f.SetCellStr(sheet, cell, val)
|
|
|
|
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
t.Run("InsertBeforeWithLargeOffset", func(t *testing.T) {
|
|
|
|
xlsx := newFileWithDefaults()
|
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.NoError(t, xlsx.DuplicateRowTo(sheet, 3, 1))
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
if !assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, "TestDuplicateRow.InsertBeforeWithLargeOffset"))) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := map[string]string{
|
|
|
|
"A1": cells["A3"], "B1": cells["B3"],
|
|
|
|
"A2": cells["A1"], "B2": cells["B1"],
|
|
|
|
"A3": cells["A2"], "B3": cells["B2"],
|
|
|
|
"A4": cells["A3"], "B4": cells["B3"],
|
|
|
|
}
|
|
|
|
for cell, val := range expect {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, cell)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateRowInvalidRownum(t *testing.T) {
|
|
|
|
const sheet = "Sheet1"
|
|
|
|
outFile := filepath.Join("test", "TestDuplicateRowInvalidRownum.%s.xlsx")
|
|
|
|
|
|
|
|
cells := map[string]string{
|
|
|
|
"A1": "A1 Value",
|
|
|
|
"A2": "A2 Value",
|
|
|
|
"A3": "A3 Value",
|
|
|
|
"B1": "B1 Value",
|
|
|
|
"B2": "B2 Value",
|
|
|
|
"B3": "B3 Value",
|
|
|
|
}
|
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
invalidIndexes := []int{-100, -2, -1, 0}
|
2019-03-06 21:40:45 +08:00
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
for _, row := range invalidIndexes {
|
|
|
|
name := fmt.Sprintf("%d", row)
|
2019-03-06 21:40:45 +08:00
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
for col, val := range cells {
|
|
|
|
xlsx.SetCellStr(sheet, col, val)
|
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, xlsx.DuplicateRow(sheet, row), fmt.Sprintf("invalid row number %d", row))
|
2019-03-06 21:40:45 +08:00
|
|
|
|
|
|
|
for col, val := range cells {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, col)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v) {
|
2019-03-06 21:40:45 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, name)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
for _, row1 := range invalidIndexes {
|
|
|
|
for _, row2 := range invalidIndexes {
|
|
|
|
name := fmt.Sprintf("[%d,%d]", row1, row2)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
xlsx := NewFile()
|
|
|
|
for col, val := range cells {
|
|
|
|
xlsx.SetCellStr(sheet, col, val)
|
|
|
|
}
|
2019-03-06 21:40:45 +08:00
|
|
|
|
2019-03-23 20:08:06 +08:00
|
|
|
assert.EqualError(t, xlsx.DuplicateRowTo(sheet, row1, row2), fmt.Sprintf("invalid row number %d", row1))
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
|
|
|
|
for col, val := range cells {
|
2019-03-23 20:08:06 +08:00
|
|
|
v, err := xlsx.GetCellValue(sheet, col)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.Equal(t, val, v) {
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
t.FailNow()
|
|
|
|
}
|
2019-03-06 21:40:45 +08:00
|
|
|
}
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
assert.NoError(t, xlsx.SaveAs(fmt.Sprintf(outFile, name)))
|
|
|
|
})
|
|
|
|
}
|
2019-03-06 21:40:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 01:03:29 +08:00
|
|
|
func TestErrSheetNotExistError(t *testing.T) {
|
|
|
|
err := ErrSheetNotExist{SheetName: "Sheet1"}
|
|
|
|
assert.EqualValues(t, err.Error(), "Sheet Sheet1 is not exist")
|
|
|
|
}
|
|
|
|
|
2019-08-05 06:23:42 +08:00
|
|
|
func BenchmarkRows(b *testing.B) {
|
2019-10-24 22:14:33 +08:00
|
|
|
f, _ := OpenFile(filepath.Join("test", "Book1.xlsx"))
|
2019-08-05 06:23:42 +08:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
rows, _ := f.Rows("Sheet2")
|
|
|
|
for rows.Next() {
|
|
|
|
row, _ := rows.Columns()
|
|
|
|
for i := range row {
|
|
|
|
if i >= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 21:40:45 +08:00
|
|
|
func trimSliceSpace(s []string) []string {
|
|
|
|
for {
|
|
|
|
if len(s) > 0 && s[len(s)-1] == "" {
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|