浏览代码

Fixes #195: Make GetRows return value avoid empty cell

* #195: proposed resolution to the issue

* Make GetRows return value avoid empty cell

* Update test file to fix broken testing.
Rafael Barros 7 年之前
父节点
当前提交
dea7ba0ec4
共有 2 个文件被更改,包括 9 次插入10 次删除
  1. 9 10
      rows.go
  2. 二进制
      test/Book1.xlsx

+ 9 - 10
rows.go

@@ -31,10 +31,9 @@ import (
 //
 func (f *File) GetRows(sheet string) [][]string {
 	xlsx := f.workSheetReader(sheet)
-	rows := [][]string{}
 	name, ok := f.sheetMap[trimSheetName(sheet)]
 	if !ok {
-		return rows
+		return [][]string{}
 	}
 	if xlsx != nil {
 		output, _ := xml.Marshal(f.Sheet[name])
@@ -44,15 +43,12 @@ func (f *File) GetRows(sheet string) [][]string {
 	d := f.sharedStringsReader()
 	var inElement string
 	var r xlsxRow
-	var row []string
 	tr, tc := f.getTotalRowsCols(name)
-	for i := 0; i < tr; i++ {
-		row = []string{}
-		for j := 0; j <= tc; j++ {
-			row = append(row, "")
-		}
-		rows = append(rows, row)
+	rows := make([][]string, tr)
+	for i := range rows {
+		rows[i] = make([]string, tc+1)
 	}
+	var row int
 	decoder := xml.NewDecoder(bytes.NewReader(f.readXML(name)))
 	for {
 		token, _ := decoder.Token()
@@ -70,12 +66,15 @@ func (f *File) GetRows(sheet string) [][]string {
 					c := TitleToNumber(strings.Map(letterOnlyMapF, colCell.R))
 					val, _ := colCell.getValueFrom(f, d)
 					rows[cr][c] = val
+					if val != "" {
+						row = r.R
+					}
 				}
 			}
 		default:
 		}
 	}
-	return rows
+	return rows[:row]
 }
 
 // Rows defines an iterator to a sheet

二进制
test/Book1.xlsx