Geoffrey J. Teale пре 13 година
родитељ
комит
d41901f070
8 измењених фајлова са 76 додато и 73 уклоњено
  1. 1 1
      lib.go
  2. 6 4
      lib_test.go
  3. 12 12
      sharedstrings.go
  4. 4 4
      sharedstrings_test.go
  5. 25 25
      workbook.go
  6. 1 1
      workbook_test.go
  7. 24 23
      worksheet.go
  8. 3 3
      worksheet_test.go

+ 1 - 1
lib.go

@@ -203,7 +203,7 @@ func makeRowFromSpan(spans string) *Row {
 // Note - this is not actually general enough - we should support retaining tabs and newlines. 
 func getValueFromCellData(rawcell XLSXC, reftable []string) string {
 	var value string = ""
-	var data string = rawcell.V.Data
+	var data string = rawcell.V
 	if len(data) > 0 {
 		vval := strings.Trim(data, " \t\n\r")
 		if rawcell.T == "s" {

+ 6 - 4
lib_test.go

@@ -42,13 +42,15 @@ func TestCreateSheet(t *testing.T) {
 		t.Error("OpenFile returned a nil File pointer but did not generate an error.")
 		return
 	}
-	if len(xlsxFile.Sheets) == 0 {
-		t.Error("Expected len(xlsxFile.Sheets) > 0")
+	sheetLen := len(xlsxFile.Sheets)
+	if sheetLen == 0 {
+		t.Error("Expected len(xlsxFile.Sheets) > 0, but got ", sheetLen)
 		return
 	}
 	sheet = xlsxFile.Sheets[0]
-	if len(sheet.Rows) != 2 {
-		t.Error("Expected len(sheet.Rows) == 2")
+	rowLen := len(sheet.Rows)
+	if rowLen != 2 {
+		t.Error("Expected len(sheet.Rows) == 2, but got ", rowLen)
 		return
 	}
 	row = sheet.Rows[0]

+ 12 - 12
sharedstrings.go

@@ -5,9 +5,9 @@ package xlsx
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main currently
 // I have not checked this for completeness - it does as much as need.
 type XLSXSST struct {
-	Count       string `xml:"attr"`
-	UniqueCount string `xml:"attr"`
-	SI          []XLSXSI
+	Count       string    `xml:"count,attr"`
+	UniqueCount string    `xml:"uniqueCount,attr"`
+	SI          []XLSXSI  `xml:"si"`
 }
 
 
@@ -16,16 +16,16 @@ type XLSXSST struct {
 // currently I have not checked this for completeness - it does as
 // much as I need.
 type XLSXSI struct {
-	T XLSXT
+	T string `xml:"t"`
 }
 
-// XLSXT directly maps the t element from the namespace
-// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
-// currently I have not checked this for completeness - it does as
-// much as I need.
-type XLSXT struct {
-	Data string `xml:"chardata"`
-}
+// // XLSXT directly maps the t element from the namespace
+// // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
+// // currently I have not checked this for completeness - it does as
+// // much as I need.
+// type XLSXT struct {
+// 	Data string `xml:"chardata"`
+// }
 
 
 // MakeSharedStringRefTable() takes an XLSXSST struct and converts
@@ -35,7 +35,7 @@ type XLSXT struct {
 func MakeSharedStringRefTable(source *XLSXSST) []string {
 	reftable := make([]string, len(source.SI))
 	for i, si := range source.SI {
-		reftable[i] = si.T.Data
+		reftable[i] = si.T
 	}
 	return reftable
 }

+ 4 - 4
sharedstrings_test.go

@@ -13,7 +13,7 @@ func TestMakeSharedStringRefTable(t *testing.T) {
 	sst := new(XLSXSST)
 	error := xml.NewDecoder(sharedstringsXML).Decode(sst)
 	if error != nil {
-		t.Error(error.String())
+		t.Error(error.Error())
 		return
 	}
 	reftable := MakeSharedStringRefTable(sst)
@@ -37,7 +37,7 @@ func TestResolveSharedString(t *testing.T) {
 	sst := new(XLSXSST)
 	error := xml.NewDecoder(sharedstringsXML).Decode(sst)
 	if error != nil {
-		t.Error(error.String())
+		t.Error(error.Error())
 		return
 	}
 	reftable := MakeSharedStringRefTable(sst)
@@ -54,7 +54,7 @@ func TestUnmarshallSharedStrings(t *testing.T) {
 	sst := new(XLSXSST)
 	error := xml.NewDecoder(sharedstringsXML).Decode(sst)
 	if error != nil {
-		t.Error(error.String())
+		t.Error(error.Error())
 		return
 	}
 	if sst.Count != "4" {
@@ -67,7 +67,7 @@ func TestUnmarshallSharedStrings(t *testing.T) {
 		t.Error("Expected 4 sst.SI but found none")
 	}
 	si := sst.SI[0]
-	if si.T.Data != "Foo" {
+	if si.T != "Foo" {
 		t.Error("Expected s.T.Data == 'Foo'")
 	}
 

+ 25 - 25
workbook.go

@@ -12,12 +12,12 @@ import (
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXWorkbook struct {
-	FileVersion  XLSXFileVersion
-	WorkbookPr   XLSXWorkbookPr
-	BookViews    XLSXBookViews
-	Sheets       XLSXSheets
-	DefinedNames XLSXDefinedNames
-	CalcPr       XLSXCalcPr
+	FileVersion  XLSXFileVersion  `xml:"fileVersion"`
+	WorkbookPr   XLSXWorkbookPr   `xml:"workbookPr"`
+	BookViews    XLSXBookViews    `xml:"bookViews"`
+	Sheets       XLSXSheets       `xml:"sheets"`
+	DefinedNames XLSXDefinedNames `xml:"definedNames"`
+	CalcPr       XLSXCalcPr       `xml:"calcPr"` 
 }
 
 // XLSXFileVersion directly maps the fileVersion element from the
@@ -25,10 +25,10 @@ type XLSXWorkbook struct {
 // - currently I have not checked it for completeness - it does as
 // much as I need.
 type XLSXFileVersion struct {
-	AppName      string `xml:"attr"`
-	LastEdited   string `xml:"attr"`
-	LowestEdited string `xml:"attr"`
-	RupBuild     string `xml:"attr"`
+	AppName      string `xml:"appName,attr"`
+	LastEdited   string `xml:"lastEdited,attr"`
+	LowestEdited string `xml:"lowestEdited,attr"`
+	RupBuild     string `xml:"rupBuild,attr"`
 }
 
 // XLSXWorkbookPr directly maps the workbookPr element from the
@@ -36,7 +36,7 @@ type XLSXFileVersion struct {
 // - currently I have not checked it for completeness - it does as
 // much as I need.
 type XLSXWorkbookPr struct {
-	DefaultThemeVersion string `xml:"attr"`
+	DefaultThemeVersion string `xml:"defaultThemeVersion,attr"`
 }
 
 // XLSXBookViews directly maps the bookViews element from the
@@ -44,7 +44,7 @@ type XLSXWorkbookPr struct {
 // - currently I have not checked it for completeness - it does as
 // much as I need.
 type XLSXBookViews struct {
-	WorkBookView []XLSXWorkBookView
+	WorkBookView []XLSXWorkBookView `xml:"workbookView"`
 }
 
 // XLSXWorkBookView directly maps the workbookView element from the
@@ -52,10 +52,10 @@ type XLSXBookViews struct {
 // - currently I have not checked it for completeness - it does as
 // much as I need.
 type XLSXWorkBookView struct {
-	XWindow      string `xml:"attr"`
-	YWindow      string `xml:"attr"`
-	WindowWidth  string `xml:"attr"`
-	WindowHeight string `xml:"attr"`
+	XWindow      string `xml:"xWindow,attr"`
+	YWindow      string `xml:"yWindow,attr"`
+	WindowWidth  string `xml:"windowWidth,attr"`
+	WindowHeight string `xml:"windowHeight,attr"`
 }
 
 // XLSXSheets directly maps the sheets element from the namespace
@@ -63,7 +63,7 @@ type XLSXWorkBookView struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXSheets struct {
-	Sheet []XLSXSheet
+	Sheet []XLSXSheet `xml:"sheet"`
 }
 
 // XLSXSheet directly maps the sheet element from the namespace
@@ -71,9 +71,9 @@ type XLSXSheets struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXSheet struct {
-	Name    string `xml:"attr"`
-	SheetId string `xml:"attr"`
-	Id      string `xml:"attr"`
+	Name    string `xml:"name,attr"`
+	SheetId string `xml:"sheetId,attr"`
+	Id      string `xml:"id,attr"`
 }
 
 // XLSXDefinedNames directly maps the definedNames element from the
@@ -81,7 +81,7 @@ type XLSXSheet struct {
 // - currently I have not checked it for completeness - it does as
 // much as I need.
 type XLSXDefinedNames struct {
-	DefinedName []XLSXDefinedName
+	DefinedName []XLSXDefinedName `xml:"definedName"`
 }
 
 // XLSXDefinedName directly maps the definedName element from the
@@ -89,9 +89,9 @@ type XLSXDefinedNames struct {
 // - currently I have not checked it for completeness - it does as
 // much as I need.
 type XLSXDefinedName struct {
-	Data         string `xml:"chardata"`
-	Name         string `xml:"attr"`
-	LocalSheetID string `xml:"attr"`
+	Data         string `xml:",chardata"`
+	Name         string `xml:"name,attr"`
+	LocalSheetID string `xml:"localSheetId,attr"`
 }
 
 // XLSXCalcPr directly maps the calcPr element from the namespace
@@ -99,7 +99,7 @@ type XLSXDefinedName struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXCalcPr struct {
-	CalcId string `xml:"attr"`
+	CalcId string `xml:"calcId,attr"`
 }
 
 // getWorksheetFromSheet() is an internal helper function to open a sheetN.xml file, refered to by an xlsx.XLSXSheet struct, from the XLSX file and unmarshal it an xlsx.XLSXWorksheet struct 

+ 1 - 1
workbook_test.go

@@ -68,7 +68,7 @@ func TestUnmarshallWorkbookXML(t *testing.T) {
 	}
 	dname := workbook.DefinedNames.DefinedName[0]
 	if dname.Data != "Sheet1!$A$1533" {
-		t.Error("dname.Data == 'Sheet1!$A$1533'")
+		t.Error("dname.Data == 'Sheet1!$A$1533', but got '", dname.Data, "'")
 	}
 	if dname.LocalSheetID != "0" {
 		t.Error("dname.LocalSheetID == '0'")

+ 24 - 23
worksheet.go

@@ -5,10 +5,10 @@ package xlsx
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXWorksheet struct {
-	Dimension     XLSXDimension
-	SheetViews    XLSXSheetViews
-	SheetFormatPr XLSXSheetFormatPr
-	SheetData     XLSXSheetData
+	Dimension     XLSXDimension      `xml:"dimension"`
+	SheetViews    XLSXSheetViews     `xml:"sheetViews"`
+	SheetFormatPr XLSXSheetFormatPr  `xml:"sheetFormatPr"`
+	SheetData     XLSXSheetData      `xml:"sheetData"`
 }
 
 // XLSXDimension directly maps the dimension element in the namespace
@@ -16,7 +16,7 @@ type XLSXWorksheet struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXDimension struct {
-	Ref string `xml:"attr"`
+	Ref string `xml:"ref,attr"`
 }
 
 // XLSXSheetViews directly maps the sheetViews element in the namespace
@@ -24,7 +24,7 @@ type XLSXDimension struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXSheetViews struct {
-	SheetView []XLSXSheetView
+	SheetView []XLSXSheetView `xml:"sheetView"`
 }
 
 // XLSXSheetView directly maps the sheetView element in the namespace
@@ -32,9 +32,9 @@ type XLSXSheetViews struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXSheetView struct {
-	TabSelected    string `xml:"attr"`
-	WorkbookViewID string `xml:"attr"`
-	Selection      XLSXSelection
+	TabSelected    string `xml:"tabSelected,attr"`
+	WorkbookViewID string `xml:"workbookViewId,attr"`
+	Selection      XLSXSelection `xml:"selection"`
 }
 
 
@@ -42,9 +42,10 @@ type XLSXSheetView struct {
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
 // currently I have not checked it for completeness - it does as much
 // as I need.
+
 type XLSXSelection struct {
-	ActiveCell string `xml:"attr"`
-	SQRef      string `xml:"attr"`
+	ActiveCell string `xml:"activeCell,attr"`
+	SQRef      string `xml:"sqref,attr"`
 }
 
 // XLSXSheetFormatPr directly maps the sheetFormatPr element in the namespace
@@ -52,8 +53,8 @@ type XLSXSelection struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXSheetFormatPr struct {
-	BaseColWidth     string `xml:"attr"`
-	DefaultRowHeight string `xml:"attr"`
+	BaseColWidth     string `xml:"baseColWidth,attr"`
+	DefaultRowHeight string `xml:"defaultRowHeight,attr"`
 }
 
 // XLSXSheetData directly maps the sheetData element in the namespace
@@ -61,7 +62,7 @@ type XLSXSheetFormatPr struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXSheetData struct {
-	Row []XLSXRow
+	Row []XLSXRow  `xml:"row"`
 }
 
 // XLSXRow directly maps the row element in the namespace
@@ -69,9 +70,9 @@ type XLSXSheetData struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXRow struct {
-	R     string `xml:"attr"`
-	Spans string `xml:"attr"`
-	C     []XLSXC
+	R     string  `xml:"r,attr"`
+	Spans string  `xml:"spans,attr"`
+	C     []XLSXC `xml:"c"`
 }
 
 // XLSXC directly maps the c element in the namespace
@@ -79,9 +80,9 @@ type XLSXRow struct {
 // currently I have not checked it for completeness - it does as much
 // as I need.
 type XLSXC struct {
-	R string `xml:"attr"`
-	T string `xml:"attr"`
-	V XLSXV
+	R string `xml:"r,attr"`
+	T string `xml:"t,attr"`
+	V string  `xml:"v"`
 }
 
 
@@ -89,7 +90,7 @@ type XLSXC struct {
 // http://schemas.openxmlformats.org/spreadsheetml/2006/main -
 // currently I have not checked it for completeness - it does as much
 // as I need.
-type XLSXV struct {
-	Data string `xml:"chardata"`
-}
+// type XLSXV struct {
+// 	Data string `xml:"chardata"`
+// }
 

+ 3 - 3
worksheet_test.go

@@ -15,7 +15,7 @@ func TestUnmarshallWorksheet(t *testing.T) {
 	worksheet := new(XLSXWorksheet)
 	error := xml.NewDecoder(sheetxml).Decode(worksheet)
 	if error != nil {
-		t.Error(error.String())
+		t.Error(error.Error())
 		return
 	}
 	if worksheet.Dimension.Ref != "A1:B2" {
@@ -63,8 +63,8 @@ func TestUnmarshallWorksheet(t *testing.T) {
 	if c.T != "s" {
 		t.Error(fmt.Sprintf("Expected c.T == 's' got %s", c.T))
 	}
-	if c.V.Data != "0" {
-		t.Error(fmt.Sprintf("Expected c.V.Data == '0', got %s", c.V.Data))
+	if c.V != "0" {
+		t.Error(fmt.Sprintf("Expected c.V.Data == '0', got %s", c.V))
 	}
 
 }