Remove useless `.xlsx` tested files and go test updated.

This commit is contained in:
Ri Xu 2016-11-02 12:58:51 +08:00
parent b4f7e72353
commit 78eb982052
5 changed files with 53 additions and 29 deletions

View File

@ -11,6 +11,16 @@ func TestExcelize(t *testing.T) {
if err != nil { if err != nil {
t.Log(err) t.Log(err)
} }
// Test get all the rows in a not exists sheet.
rows := f1.GetRows("Sheet4")
// Test get all the rows in a sheet.
rows = f1.GetRows("Sheet2")
for _, row := range rows {
for _, cell := range row {
t.Log(cell, "\t")
}
t.Log("\r\n")
}
f1.UpdateLinkedValue() f1.UpdateLinkedValue()
f1.SetCellInt("SHEET2", "A1", 100) f1.SetCellInt("SHEET2", "A1", 100)
f1.SetCellStr("SHEET2", "C11", "Knowns") f1.SetCellStr("SHEET2", "C11", "Knowns")

72
rows.go
View File

@ -2,49 +2,63 @@ package excelize
import ( import (
"encoding/xml" "encoding/xml"
"strconv"
"strings" "strings"
"strconv"
) )
// GetRows return all the rows in a sheet, for example:
// GetRows return all the rows in a sheet //
func (f *File) GetRows(sheet string) ([]xlsxRow, error) { // rows := xlsx.GetRows("Sheet2")
var xlsx xlsxWorksheet // for _, row := range rows {
// for _, colCell := range row {
// fmt.Print(colCell, "\t")
// }
// fmt.Println()
// }
//
func (f *File) GetRows(sheet string) [][]string {
xlsx := xlsxWorksheet{}
r := [][]string{}
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
err := xml.Unmarshal([]byte(f.readXML(name)), &xlsx) err := xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
if ( err != nil ) { if err != nil {
return nil, err return r
} }
rows := xlsx.SheetData.Row rows := xlsx.SheetData.Row
for _, row := range rows {
return rows, nil c := []string{}
for _, colCell := range row.C {
val, _ := colCell.getValueFrom(f)
c = append(c, val)
}
r = append(r, c)
}
return r
} }
// readXMLSST read xmlSST simple function.
// readXMLSST read xmlSST simple function
func readXMLSST(f *File) (xlsxSST, error) { func readXMLSST(f *File) (xlsxSST, error) {
shardStrings := xlsxSST{} shardStrings := xlsxSST{}
err := xml.Unmarshal([]byte(f.readXML(`xl/sharedStrings.xml`)), &shardStrings) err := xml.Unmarshal([]byte(f.readXML(`xl/sharedStrings.xml`)), &shardStrings)
return shardStrings, err return shardStrings, err
} }
// GetValueFrom return a value from a column/row cell, // getValueFrom return a value from a column/row cell,
// this function is inteded to be used with for range on rows // this function is inteded to be used with for range on rows
// an argument with the xlsx opened file // an argument with the xlsx opened file.
func (self* xlsxC) GetValueFrom(f *File) (string, error) { func (xlsx *xlsxC) getValueFrom(f *File) (string, error) {
switch self.T { switch xlsx.T {
case "s": case "s":
xlsxSI := 0 xlsxSI := 0
xlsxSI, _ = strconv.Atoi(self.V) xlsxSI, _ = strconv.Atoi(xlsx.V)
d, err := readXMLSST(f) d, err := readXMLSST(f)
if ( err != nil ) { if err != nil {
return "", err return "", err
} }
return d.SI[xlsxSI].T, nil return d.SI[xlsxSI].T, nil
case "str": case "str":
return self.V, nil return xlsx.V, nil
default: default:
return self.V, nil return xlsx.V, nil
} // switch }
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.