Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open rows totalRow property #1054

Merged
merged 2 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,22 @@ const (

// Cols defines an iterator to a sheet
type Cols struct {
err error
curCol, totalCol, stashCol, totalRow int
rawCellValue bool
sheet string
f *File
sheetXML []byte
err error
curCol, totalCols, totalRows, stashCol int
rawCellValue bool
sheet string
f *File
sheetXML []byte
}

// CurrentCol returns the column number that represents the current column.
func (cols *Cols) CurrentCol() int {
return cols.curCol
}

// TotalCols returns the total columns count in the worksheet.
func (cols *Cols) TotalCols() int {
return cols.totalCols
}

// GetCols return all the columns in a sheet by given worksheet name (case
Expand Down Expand Up @@ -71,7 +81,7 @@ func (f *File) GetCols(sheet string, opts ...Options) ([][]string, error) {
// Next will return true if the next column is found.
func (cols *Cols) Next() bool {
cols.curCol++
return cols.curCol <= cols.totalCol
return cols.curCol <= cols.totalCols
}

// Error will return an error when the error occurs.
Expand Down Expand Up @@ -159,7 +169,7 @@ func columnXMLHandler(colIterator *columnXMLIterator, xmlElement *xml.StartEleme
colIterator.row = colIterator.curRow
}
}
colIterator.cols.totalRow = colIterator.row
colIterator.cols.totalRows = colIterator.row
colIterator.cellCol = 0
}
if inElement == "c" {
Expand All @@ -171,8 +181,8 @@ func columnXMLHandler(colIterator *columnXMLIterator, xmlElement *xml.StartEleme
}
}
}
if colIterator.cellCol > colIterator.cols.totalCol {
colIterator.cols.totalCol = colIterator.cellCol
if colIterator.cellCol > colIterator.cols.totalCols {
colIterator.cols.totalCols = colIterator.cellCol
}
}
}
Expand Down
26 changes: 12 additions & 14 deletions col_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,37 @@ func TestCols(t *testing.T) {
}

func TestColumnsIterator(t *testing.T) {
const (
sheet2 = "Sheet2"
expectedNumCol = 9
)

sheetName, colCount, expectedNumCol := "Sheet2", 0, 9
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
require.NoError(t, err)

cols, err := f.Cols(sheet2)
cols, err := f.Cols(sheetName)
require.NoError(t, err)

var colCount int
for cols.Next() {
colCount++
assert.Equal(t, colCount, cols.CurrentCol())
assert.Equal(t, expectedNumCol, cols.TotalCols())
require.True(t, colCount <= expectedNumCol, "colCount is greater than expected")
}
assert.Equal(t, expectedNumCol, colCount)
assert.NoError(t, f.Close())

f = NewFile()
f, sheetName, colCount, expectedNumCol = NewFile(), "Sheet1", 0, 4
cells := []string{"C2", "C3", "C4", "D2", "D3", "D4"}
for _, cell := range cells {
assert.NoError(t, f.SetCellValue("Sheet1", cell, 1))
assert.NoError(t, f.SetCellValue(sheetName, cell, 1))
}
cols, err = f.Cols("Sheet1")
cols, err = f.Cols(sheetName)
require.NoError(t, err)

colCount = 0
for cols.Next() {
colCount++
assert.Equal(t, colCount, cols.CurrentCol())
assert.Equal(t, expectedNumCol, cols.TotalCols())
require.True(t, colCount <= 4, "colCount is greater than expected")
}
assert.Equal(t, 4, colCount)
assert.Equal(t, expectedNumCol, colCount)
}

func TestColsError(t *testing.T) {
Expand Down Expand Up @@ -130,8 +128,8 @@ func TestGetColsError(t *testing.T) {
f = NewFile()
cols, err := f.Cols("Sheet1")
assert.NoError(t, err)
cols.totalRow = 2
cols.totalCol = 2
cols.totalRows = 2
cols.totalCols = 2
cols.curCol = 1
cols.sheetXML = []byte(`<worksheet><sheetData><row r="1"><c r="A" t="str"><v>A</v></c></row></sheetData></worksheet>`)
_, err = cols.Rows()
Expand Down
28 changes: 19 additions & 9 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,29 @@ func (f *File) GetRows(sheet string, opts ...Options) ([][]string, error) {

// Rows defines an iterator to a sheet.
type Rows struct {
err error
curRow, totalRow, stashRow int
rawCellValue bool
sheet string
f *File
tempFile *os.File
decoder *xml.Decoder
err error
curRow, totalRows, stashRow int
rawCellValue bool
sheet string
f *File
tempFile *os.File
decoder *xml.Decoder
}

// CurrentRow returns the row number that represents the current row.
func (rows *Rows) CurrentRow() int {
return rows.curRow
}

// TotalRows returns the total rows count in the worksheet.
func (rows *Rows) TotalRows() int {
return rows.totalRows
}

// Next will return true if find the next row element.
func (rows *Rows) Next() bool {
rows.curRow++
return rows.curRow <= rows.totalRow
return rows.curRow <= rows.totalRows
}

// Error will return the error when the error occurs.
Expand Down Expand Up @@ -255,7 +265,7 @@ func (f *File) Rows(sheet string) (*Rows, error) {
}
}
}
rows.totalRow = row
rows.totalRows = row
}
case xml.EndElement:
if xmlElement.Name.Local == "sheetData" {
Expand Down
22 changes: 10 additions & 12 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,38 +65,36 @@ func TestRows(t *testing.T) {
}

func TestRowsIterator(t *testing.T) {
const (
sheet2 = "Sheet2"
expectedNumRow = 11
)
sheetName, rowCount, expectedNumRow := "Sheet2", 0, 11
f, err := OpenFile(filepath.Join("test", "Book1.xlsx"))
require.NoError(t, err)

rows, err := f.Rows(sheet2)
rows, err := f.Rows(sheetName)
require.NoError(t, err)
var rowCount int

for rows.Next() {
rowCount++
assert.Equal(t, rowCount, rows.CurrentRow())
assert.Equal(t, expectedNumRow, rows.TotalRows())
require.True(t, rowCount <= expectedNumRow, "rowCount is greater than expected")
}
assert.Equal(t, expectedNumRow, rowCount)
assert.NoError(t, rows.Close())
assert.NoError(t, f.Close())

// Valued cell sparse distribution test
f = NewFile()
f, sheetName, rowCount, expectedNumRow = NewFile(), "Sheet1", 0, 3
cells := []string{"C1", "E1", "A3", "B3", "C3", "D3", "E3"}
for _, cell := range cells {
assert.NoError(t, f.SetCellValue("Sheet1", cell, 1))
assert.NoError(t, f.SetCellValue(sheetName, cell, 1))
}
rows, err = f.Rows("Sheet1")
rows, err = f.Rows(sheetName)
require.NoError(t, err)
rowCount = 0
for rows.Next() {
rowCount++
require.True(t, rowCount <= 3, "rowCount is greater than expected")
require.True(t, rowCount <= expectedNumRow, "rowCount is greater than expected")
}
assert.Equal(t, 3, rowCount)
assert.Equal(t, expectedNumRow, rowCount)
}

func TestRowsError(t *testing.T) {
Expand Down