Browse Source

Merge pull request #207 from Joshuabaker2/master

Fixed DefinedNames.DefinedName pointer issue.
Geoffrey J. Teale 9 years ago
parent
commit
af354c7272
3 changed files with 24 additions and 2 deletions
  1. 11 0
      cell.go
  2. 10 0
      cell_test.go
  3. 3 2
      lib.go

+ 11 - 0
cell.go

@@ -356,14 +356,23 @@ func parseTime(c *Cell) (string, error) {
 	}
 	val := TimeFromExcelTime(f, c.date1904)
 	format := c.GetNumberFormat()
+
+
 	// Replace Excel placeholders with Go time placeholders.
 	// For example, replace yyyy with 2006. These are in a specific order,
 	// due to the fact that m is used in month, minute, and am/pm. It would
 	// be easier to fix that with regular expressions, but if it's possible
 	// to keep this simple it would be easier to maintain.
+	// Full-length month and days (e.g. March, Tuesday) have letters in them that would be replaced
+	// by other characters below (such as the 'h' in March, or the 'd' in Tuesday) below.
+	// First we convert them to arbitrary characters unused in Excel Date formats, and then at the end,
+	// turn them to what they should actually be.
+	// Based off: http://www.ozgrid.com/Excel/CustomFormats.htm
 	replacements := []struct{ xltime, gotime string }{
 		{"yyyy", "2006"},
 		{"yy", "06"},
+		{"mmmm", "%%%%"},
+		{"dddd", "&&&&"},
 		{"dd", "02"},
 		{"d", "2"},
 		{"mmm", "Jan"},
@@ -377,6 +386,8 @@ func parseTime(c *Cell) (string, error) {
 		{"am/pm", "pm"},
 		{"m/", "1/"},
 		{".0", ".9999"},
+		{"%%%%", "January"},
+		{"&&&&", "Monday"},
 	}
 	for _, repl := range replacements {
 		format = strings.Replace(format, repl.xltime, repl.gotime, 1)

+ 10 - 0
cell_test.go

@@ -347,6 +347,16 @@ func (l *CellSuite) TestFormattedValue(c *C) {
 	fvc.Equals(cell, "2003-11-22 18:00:00")
 	smallCell.NumFmt = "yyyy-mm-dd hh:mm:ss"
 	fvc.Equals(smallCell, "1899-12-30 00:14:47")
+
+	cell.NumFmt = "mmmm d, yyyy"
+	fvc.Equals(cell, "November 22, 2003")
+	smallCell.NumFmt = "mmmm d, yyyy"
+	fvc.Equals(smallCell, "December 30, 1899")
+
+	cell.NumFmt = "dddd, mmmm dd, yyyy"
+	fvc.Equals(cell, "Saturday, November 22, 2003")
+	smallCell.NumFmt = "dddd, mmmm dd, yyyy"
+	fvc.Equals(smallCell, "Saturday, December 30, 1899")
 }
 
 // test setters and getters

+ 3 - 2
lib.go

@@ -684,8 +684,9 @@ func readSheetsFromZipFile(f *zip.File, file *File, sheetXMLMap map[string]strin
 	}
 	file.Date1904 = workbook.WorkbookPr.Date1904
 
-	for _, entry := range workbook.DefinedNames.DefinedName {
-		file.DefinedNames = append(file.DefinedNames, &entry)
+
+	for entryNum, _ := range workbook.DefinedNames.DefinedName {
+		file.DefinedNames = append(file.DefinedNames, &workbook.DefinedNames.DefinedName[entryNum])
 	}
 
 	// Only try and read sheets that have corresponding files.