Browse Source

get max col

get max col
frogs 13 years ago
parent
commit
7f09bfb98c
4 changed files with 27 additions and 8 deletions
  1. BIN
      .DS_Store
  2. 2 0
      .gitignore
  3. 16 8
      lib.go
  4. 9 0
      worksheet.go

BIN
.DS_Store


+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+
+.DS_Store

+ 16 - 8
lib.go

@@ -26,7 +26,7 @@ func (e *XLSXReaderError) Error() string {
 // Cell is a high level structure intended to provide user access to
 // the contents of Cell within an xlsx.Row.
 type Cell struct {
-	data string
+	Value string
 }
 
 // CellInterface defines the public API of the Cell.
@@ -35,7 +35,7 @@ type CellInterface interface {
 }
 
 func (c *Cell) String() string {
-	return c.data
+	return c.Value
 }
 
 // Row is a high level structure indended to provide user access to a
@@ -48,6 +48,8 @@ type Row struct {
 // the contents of a particular sheet within an XLSX file.
 type Sheet struct {
 	Rows []*Row
+	MaxRow int
+	MaxCol int
 }
 
 // File is a high level structure providing a slice of Sheet structs
@@ -194,7 +196,7 @@ func makeRowFromSpan(spans string) *Row {
 	row.Cells = make([]*Cell, upper)
 	for i := 0; i < upper; i++ {
 		cell = new(Cell)
-		cell.data = ""
+		cell.Value = ""
 		row.Cells[i] = cell
 	}
 	return row
@@ -223,7 +225,7 @@ func makeRowFromRaw(rawrow xlsxRow) *Row {
 	row.Cells = make([]*Cell, upper)
 	for i := 0; i < upper; i++ {
 		cell = new(Cell)
-		cell.data = ""
+		cell.Value = ""
 		row.Cells[i] = cell
 	}
 	return row
@@ -252,11 +254,13 @@ func getValueFromCellData(rawcell xlsxC, reftable []string) string {
 // readRowsFromSheet is an internal helper function that extracts the
 // 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 {
+func readRowsFromSheet(Worksheet *xlsxWorksheet, reftable []string) ([]*Row ,int) {
 	var rows []*Row
 	var row *Row
+	var maxCol int
 
 	rows = make([]*Row, len(Worksheet.SheetData.Row))
+	maxCol = 0
 	for i, rawrow := range Worksheet.SheetData.Row {
 		// range is not empty
 		if len(rawrow.Spans) != 0 {
@@ -269,11 +273,14 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, reftable []string) []*Row {
 			if error != nil {
 				panic(fmt.Sprintf("Invalid Cell Coord, %s\n", rawcell.R))
 			}
-			row.Cells[x].data = getValueFromCellData(rawcell, reftable)
+			if x > maxCol {
+				maxCol = x
+			}
+			row.Cells[x].Value = getValueFromCellData(rawcell, reftable)
 		}
 		rows[i] = row
 	}
-	return rows
+	return rows,maxCol
 }
 
 // readSheetsFromZipFile is an internal helper function that loops
@@ -302,8 +309,9 @@ func readSheetsFromZipFile(f *zip.File, file *File) ([]*Sheet, []string, error)
 			return nil, nil, error
 		}
 		sheet := new(Sheet)
-		sheet.Rows = readRowsFromSheet(worksheet, file.referenceTable)
+		sheet.Rows,sheet.MaxCol = readRowsFromSheet(worksheet, file.referenceTable)
 		sheets[i] = sheet
+		sheet.MaxRow = len(sheet.Rows)
 		names[i] = rawsheet.Name
 	}
 	return sheets, names, nil

+ 9 - 0
worksheet.go

@@ -94,3 +94,12 @@ type xlsxC struct {
 // 	Data string `xml:"chardata"`
 // }
 
+// get cell
+func (sh *Sheet) Cell(row,col int) *Cell {
+
+	if len(sh.Rows) > row && len(sh.Rows[row].Cells) > col {
+		return sh.Rows[row].Cells[col]
+	}
+	return new(Cell)
+}
+