handle the cell without r attribute in a row element

formula
xuri 5 years ago
parent 1fe660df64
commit 2285d4dc71
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7

@ -593,6 +593,22 @@ func checkRow(xlsx *xlsxWorksheet) error {
if colCount == 0 { if colCount == 0 {
continue continue
} }
// check and fill the cell without r attribute in a row element
rCount := 0
for idx, cell := range rowData.C {
rCount++
if cell.R != "" {
lastR, _, err := CellNameToCoordinates(cell.R)
if err != nil {
return err
}
if lastR > rCount {
rCount = lastR
}
continue
}
rowData.C[idx].R, _ = CoordinatesToCellName(rCount, rowIdx+1)
}
lastCol, _, err := CellNameToCoordinates(rowData.C[colCount-1].R) lastCol, _, err := CellNameToCoordinates(rowData.C[colCount-1].R)
if err != nil { if err != nil {
return err return err

@ -831,6 +831,17 @@ func TestErrSheetNotExistError(t *testing.T) {
assert.EqualValues(t, err.Error(), "sheet Sheet1 is not exist") assert.EqualValues(t, err.Error(), "sheet Sheet1 is not exist")
} }
func TestCheckRow(t *testing.T) {
f := NewFile()
f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" ><sheetData><row r="2"><c><v>1</v></c><c r="F2"><v>2</v></c><c><v>3</v></c><c><v>4</v></c><c r="M2"><v>5</v></c></row></sheetData></worksheet>`)
_, err := f.GetRows("Sheet1")
assert.NoError(t, err)
assert.NoError(t, f.SetCellValue("Sheet1", "A1", false))
f = NewFile()
f.XLSX["xl/worksheets/sheet1.xml"] = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" ><sheetData><row r="2"><c><v>1</v></c><c r="-"><v>2</v></c><c><v>3</v></c><c><v>4</v></c><c r="M2"><v>5</v></c></row></sheetData></worksheet>`)
assert.EqualError(t, f.SetCellValue("Sheet1", "A1", false), `cannot convert cell "-" to coordinates: invalid cell name "-"`)
}
func BenchmarkRows(b *testing.B) { func BenchmarkRows(b *testing.B) {
f, _ := OpenFile(filepath.Join("test", "Book1.xlsx")) f, _ := OpenFile(filepath.Join("test", "Book1.xlsx"))
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {

@ -237,7 +237,7 @@ func (f *File) SetActiveSheet(index int) {
for idx, name := range f.GetSheetList() { for idx, name := range f.GetSheetList() {
xlsx, err := f.workSheetReader(name) xlsx, err := f.workSheetReader(name)
if err != nil { if err != nil {
// Chartsheet // Chartsheet or dialogsheet
return return
} }
if xlsx.SheetViews == nil { if xlsx.SheetViews == nil {
@ -365,8 +365,8 @@ func (f *File) GetSheetIndex(name string) int {
return idx return idx
} }
// GetSheetMap provides a function to get worksheet and chartsheet name and // GetSheetMap provides a function to get worksheet, chartsheet and
// ID map of XLSX. For example: // dialogsheet ID and name map of XLSX. For example:
// //
// f, err := excelize.OpenFile("Book1.xlsx") // f, err := excelize.OpenFile("Book1.xlsx")
// if err != nil { // if err != nil {
@ -387,8 +387,8 @@ func (f *File) GetSheetMap() map[int]string {
return sheetMap return sheetMap
} }
// GetSheetList provides a function to get worksheet and chartsheet name list // GetSheetList provides a function to get worksheet, chartsheet and
// of workbook. // dialogsheet name list of workbook.
func (f *File) GetSheetList() (list []string) { func (f *File) GetSheetList() (list []string) {
wb := f.workbookReader() wb := f.workbookReader()
if wb != nil { if wb != nil {

@ -23,6 +23,7 @@ const (
SourceRelationshipHyperLink = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" SourceRelationshipHyperLink = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
SourceRelationshipWorkSheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" SourceRelationshipWorkSheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
SourceRelationshipChartsheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" SourceRelationshipChartsheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet"
SourceRelationshipDialogsheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/dialogsheet"
SourceRelationshipPivotTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable" SourceRelationshipPivotTable = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotTable"
SourceRelationshipPivotCache = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition" SourceRelationshipPivotCache = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition"
SourceRelationshipSharedStrings = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" SourceRelationshipSharedStrings = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"

Loading…
Cancel
Save