Browse Source

Tested trailing space rows and fixed handling of integer fields

Geoffrey J. Teale 14 years ago
parent
commit
8814fbe6ce
2 changed files with 200 additions and 87 deletions
  1. 35 11
      lib.go
  2. 165 76
      lib_test.go

+ 35 - 11
lib.go

@@ -185,27 +185,51 @@ func getCoordsFromCellIDString(cellIDString string) (x, y int, error os.Error) {
 // rows from a XSLXWorksheet, poulates them with Cells and resolves
 // the value references from the reference table and stores them in
 func readRowsFromSheet(worksheet *XLSXWorksheet, reftable []string) []*Row {
+
+	// Note, this function needs tidying up!
 	var rows []*Row
+	var error os.Error
+	var upper int
+	var row *Row
+	var cell *Cell
+
 	rows = make([]*Row, len(worksheet.SheetData.Row))
 	for i, rawrow := range worksheet.SheetData.Row {
-		row := new(Row)
-		lower, upper, error := getRangeFromString(rawrow.Spans)
+		row = new(Row)
+		_, upper, error = getRangeFromString(rawrow.Spans)
 		if error != nil {
 			panic(error)
 		}
-		size := (upper - lower) + 1
-		row.Cells = make([]*Cell, size)
-		for j, rawcell := range rawrow.C {
-			cell := new(Cell)
+		error = nil
+		row.Cells = make([]*Cell, upper)
+		for i := 0; i < upper; i++ {
+			cell = new(Cell)
+			cell.data = ""
+			row.Cells[i] = cell
+		}
+		for _, rawcell := range rawrow.C {
+			x, _, error := getCoordsFromCellIDString(rawcell.R)
+			if error != nil {
+				panic(fmt.Sprintf("Invalid Cell Coord, %s\n", rawcell.R))
+			}
+			error = nil
+			cell = new(Cell)
 			cell.data = ""
 			if len(rawcell.V.Data) > 0 {
-				ref, error := strconv.Atoi(rawcell.V.Data)
-				if error != nil {
-					panic(fmt.Sprintf("Invalid reference in Excel Cell (not found in sharedStrings.xml) - the reference was %v\n", rawcell.V.Data))
+				vval := strings.Trim(rawcell.V.Data, " \t\n\r")
+				if rawcell.T == "s" {
+					ref, error := strconv.Atoi(vval)
+					if error != nil {
+						panic(error)
+						panic(fmt.Sprintf("Invalid reference in Excel Cell (not found in sharedStrings.xml) - the reference was %v\n", rawcell.V.Data))
+					}
+					cell.data = reftable[ref]
+				} else {
+					cell.data = vval
 				}
-				cell.data = reftable[ref]
+				
 			}
-			row.Cells[j] = cell
+			row.Cells[x] = cell
 		}
 		rows[i] = row
 	}

+ 165 - 76
lib_test.go

@@ -287,81 +287,170 @@ func TestGetRangeFromString(t *testing.T) {
 }
 
 
-// func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
-// 	var sharedstringsXML = bytes.NewBufferString(`
-// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-// <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="8" uniqueCount="5"><si><t>Bob</t></si><si><t>Alice</t></si><si><t>Sue</t></si><si><t>Yes</t></si><si><t>No</t></si></sst>`)
-// 	var sheetxml = bytes.NewBufferString(`
-// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-// <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:C3"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="D3" sqref="D3"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
-// <sheetData>
-//   <row r="1" spans="1:3">
-//     <c r="A1" t="s">
-//       <v>
-//         0
-//       </v>
-//     </c>
-//     <c r="B1" t="s">
-//       <v>
-//         1
-//       </v>
-//     </c>
-//     <c r="C1" t="s">
-//       <v>
-//         2
-//       </v>
-//     </c>
-//   </row>
-//   <row r="2" spans="1:3">
-//     <c r="A2" t="s">
-//       <v>
-//         3
-//       </v>
-//     </c>
-//     <c r="B2" t="s">
-//       <v>
-//         4
-//       </v>
-//     </c>
-//     <c r="C2" t="s">
-//       <v>
-//         3
-//       </v>
-//     </c>
-//   </row>
-//   <row r="3" spans="1:3">
-//     <c r="A3" t="s">
-//       <v>
-//         4
-//       </v>
-//     </c>
-//     <c r="C3" t="s">
-//       <v>
-//         3
-//       </v>
-//     </c>
-//   </row>
-// </sheetData>
-// <pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
-// </worksheet>
+func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
+	var sharedstringsXML = bytes.NewBufferString(`
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="8" uniqueCount="5"><si><t>Bob</t></si><si><t>Alice</t></si><si><t>Sue</t></si><si><t>Yes</t></si><si><t>No</t></si></sst>`)
+	var sheetxml = bytes.NewBufferString(`
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:C3"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="D3" sqref="D3"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
+<sheetData>
+  <row r="1" spans="1:3">
+    <c r="A1" t="s">
+      <v>
+        0
+      </v>
+    </c>
+    <c r="B1" t="s">
+      <v>
+        1
+      </v>
+    </c>
+    <c r="C1" t="s">
+      <v>
+        2
+      </v>
+    </c>
+  </row>
+  <row r="2" spans="1:3">
+    <c r="A2" t="s">
+      <v>
+        3
+      </v>
+    </c>
+    <c r="B2" t="s">
+      <v>
+        4
+      </v>
+    </c>
+    <c r="C2" t="s">
+      <v>
+        3
+      </v>
+    </c>
+  </row>
+  <row r="3" spans="1:3">
+    <c r="A3" t="s">
+      <v>
+        4
+      </v>
+    </c>
+    <c r="C3" t="s">
+      <v>
+        3
+      </v>
+    </c>
+  </row>
+</sheetData>
+<pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
+</worksheet>
+
+`)
+	worksheet := new(XLSXWorksheet)
+	error := xml.Unmarshal(sheetxml, worksheet)
+	if error != nil {
+		t.Error(error.String())
+		return
+	}
+	sst := new(XLSXSST)
+	error = xml.Unmarshal(sharedstringsXML, sst)
+	if error != nil {
+		t.Error(error.String())
+		return
+	}
+	reftable := MakeSharedStringRefTable(sst)
+	rows := readRowsFromSheet(worksheet, reftable)
+	if len(rows) != 3 {
+		t.Error("Expected len(rows) == 3, got ", strconv.Itoa(len(rows)))
+	}
+	row := rows[2]
+	if len(row.Cells) != 3 {
+		t.Error("Expected len(row.Cells) == 3, got ", strconv.Itoa(len(row.Cells)))
+	}
+	cell1 := row.Cells[0]
+	if cell1.String() != "No" {
+		t.Error("Expected cell1.String() == 'No', got ", cell1.String())
+	}
+	cell2 := row.Cells[1]
+	if cell2.String() != "" {
+		t.Error("Expected cell2.String() == '', got ", cell2.String())
+	}
+	cell3 := row.Cells[2]
+	if cell3.String() != "Yes" {
+		t.Error("Expected cell3.String() == 'Yes', got ", cell3.String())
+	}
+
+}
+
 
-// `)
-// 	worksheet := new(XLSXWorksheet)
-// 	error := xml.Unmarshal(sheetxml, worksheet)
-// 	if error != nil {
-// 		t.Error(error.String())
-// 		return
-// 	}
-// 	sst := new(XLSXSST)
-// 	error = xml.Unmarshal(sharedstringsXML, sst)
-// 	if error != nil {
-// 		t.Error(error.String())
-// 		return
-// 	}
-// 	reftable := MakeSharedStringRefTable(sst)
-// 	rows := readRowsFromSheet(worksheet, reftable)
-// 	if len(rows) != 3 {
-// 		t.Error("Expected len(rows) == 3")
-// 	}
+
+func TestReadRowsFromSheetWithTrailingEmptyCells(t *testing.T) {
+	var row *Row
+	var cell1, cell2, cell3, cell4 *Cell
+	var sharedstringsXML = bytes.NewBufferString(`
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4"><si><t>A</t></si><si><t>B</t></si><si><t>C</t></si><si><t>D</t></si></sst>`)
+	var sheetxml = bytes.NewBufferString(`
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:D8"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="A7" sqref="A7"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/><sheetData><row r="1" spans="1:4"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="s"><v>2</v></c><c r="D1" t="s"><v>3</v></c></row><row r="2" spans="1:4"><c r="A2"><v>1</v></c></row><row r="3" spans="1:4"><c r="B3"><v>1</v></c></row><row r="4" spans="1:4"><c r="C4"><v>1</v></c></row><row r="5" spans="1:4"><c r="D5"><v>1</v></c></row><row r="6" spans="1:4"><c r="C6"><v>1</v></c></row><row r="7" spans="1:4"><c r="B7"><v>1</v></c></row><row r="8" spans="1:4"><c r="A8"><v>1</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/></worksheet>
+`)
+	worksheet := new(XLSXWorksheet)
+	error := xml.Unmarshal(sheetxml, worksheet)
+	if error != nil {
+		t.Error(error.String())
+		return
+	}
+	sst := new(XLSXSST)
+	error = xml.Unmarshal(sharedstringsXML, sst)
+	if error != nil {
+		t.Error(error.String())
+		return
+	}
+	reftable := MakeSharedStringRefTable(sst)
+	rows := readRowsFromSheet(worksheet, reftable)
+	if len(rows) != 8 {
+		t.Error("Expected len(rows) == 8, got ", strconv.Itoa(len(rows)))
+	}
+	row = rows[0]
+	if len(row.Cells) != 4 {
+		t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
+	}
+	cell1 = row.Cells[0]
+	if cell1.String() != "A" {
+		t.Error("Expected cell1.String() == 'A', got ", cell1.String())
+	}
+	cell2 = row.Cells[1]
+	if cell2.String() != "B" {
+		t.Error("Expected cell2.String() == 'B', got ", cell2.String())
+	}
+	cell3 = row.Cells[2]
+	if cell3.String() != "C" {
+		t.Error("Expected cell3.String() == 'C', got ", cell3.String())
+	}
+	cell4 = row.Cells[3]
+	if cell4.String() != "D" {
+		t.Error("Expected cell4.String() == 'D', got ", cell4.String())
+	}
 	
-// }
+	row = rows[1]
+	if len(row.Cells) != 4 {
+		t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
+	}
+	cell1 = row.Cells[0]
+	if cell1.String() != "1" {
+		t.Error("Expected cell1.String() == '1', got ", cell1.String())
+	}
+	cell2 = row.Cells[1]
+	if cell2.String() != "" {
+		t.Error("Expected cell2.String() == '', got ", cell2.String())
+	}
+	cell3 = row.Cells[2]
+	if cell3.String() != "" {
+		t.Error("Expected cell3.String() == '', got ", cell3.String())
+	}
+	cell4 = row.Cells[3]
+	if cell4.String() != "" {
+		t.Error("Expected cell4.String() == '', got ", cell4.String())
+	}
+
+}