Просмотр исходного кода

Return NumberFormat string from Cell.

Geoffrey J. Teale 11 лет назад
Родитель
Сommit
e73bceb44d
2 измененных файлов с 47 добавлено и 2 удалено
  1. 24 2
      lib.go
  2. 23 0
      lib_test.go

+ 24 - 2
lib.go

@@ -28,6 +28,7 @@ type Cell struct {
 	Value      string
 	styleIndex int
 	styles     *xlsxStyles
+	numFmtRefTable map[int]xlsxNumFmt
 }
 
 // CellInterface defines the public API of the Cell.
@@ -73,6 +74,17 @@ func (c *Cell) GetStyle() *Style {
 	return style
 }
 
+// The number format string is returnable from a cell.
+func (c *Cell) GetNumberFormat() string {
+	var numberFormat string = ""
+	if c.styleIndex > 0 && c.styleIndex <= len(c.styles.CellXfs) {
+		xf := c.styles.CellXfs[c.styleIndex-1]
+		numFmt := c.numFmtRefTable[xf.NumFmtId]
+		numberFormat = numFmt.FormatCode
+	}
+	return numberFormat
+}
+
 // Row is a high level structure indended to provide user access to a
 // row within a xlsx.Sheet.  An xlsx.Row contains a slice of xlsx.Cell.
 type Row struct {
@@ -88,6 +100,7 @@ type Sheet struct {
 	MaxCol int
 }
 
+
 // Style is a high level structure intended to provide user access to
 // the contents of Style within an XLSX file.
 type Style struct {
@@ -124,6 +137,7 @@ type Font struct {
 // to the user.
 type File struct {
 	worksheets     map[string]*zip.File
+	numFmtRefTable map[int]xlsxNumFmt
 	referenceTable []string
 	styles         *xlsxStyles
 	Sheets         []*Sheet          // sheet access by index
@@ -330,8 +344,6 @@ func getValueFromCellData(rawcell xlsxC, reftable []string) string {
 				panic(error)
 			}
 			value = reftable[ref]
-		case "n": // Number
-			
 		default:
 			value = vval
 		}
@@ -397,6 +409,7 @@ func readRowsFromSheet(Worksheet *xlsxWorksheet, file *File) ([]*Row, int, int)
 			row.Cells[cellX].Value = getValueFromCellData(rawcell, reftable)
 			row.Cells[cellX].styleIndex = rawcell.S
 			row.Cells[cellX].styles = file.styles
+			row.Cells[cellX].numFmtRefTable = file.numFmtRefTable
 			insertColIndex++
 		}
 		rows[insertRowIndex-minRow] = row
@@ -509,6 +522,14 @@ func readStylesFromZipFile(f *zip.File) (*xlsxStyles, error) {
 	return style, nil
 }
 
+func buildNumFmtRefTable(style *xlsxStyles) map[int]xlsxNumFmt {
+	refTable := make(map[int]xlsxNumFmt)
+	for _, numFmt := range style.NumFmts {
+		refTable[numFmt.NumFmtId] = numFmt
+	}
+	return refTable
+}
+
 // readWorkbookRelationsFromZipFile is an internal helper function to
 // extract a map of relationship ID strings to the name of the
 // worksheet.xml file they refer to.  The resulting map can be used to
@@ -614,6 +635,7 @@ func ReadZipReader(r *zip.Reader) (*File, error) {
 		return nil, err
 	}
 	file.styles = style
+	file.numFmtRefTable = buildNumFmtRefTable(style)
 	sheets, err = readSheetsFromZipFile(workbook, file, sheetXMLMap)
 	if err != nil {
 		return nil, err

+ 23 - 0
lib_test.go

@@ -55,6 +55,29 @@ func (l *LibSuite) TestCreateSheet(c *C) {
 	c.Assert(cellstring, Equals, "Foo")
 }
 
+func (l *LibSuite) TestGetNumberFormat(c *C) {
+	var cell *Cell
+	var cellXfs []xlsxXf
+	var numFmt xlsxNumFmt
+	var numFmts []xlsxNumFmt
+	var xStyles *xlsxStyles
+	var numFmtRefTable map[int]xlsxNumFmt
+
+	cellXfs = make([]xlsxXf, 1)
+	cellXfs[0] = xlsxXf{NumFmtId: 1}
+
+	numFmt = xlsxNumFmt{NumFmtId: 1, FormatCode: "DD/MM/YY"}
+	numFmts = make([]xlsxNumFmt, 1)
+	numFmts[0] = numFmt
+	numFmtRefTable = make(map[int]xlsxNumFmt)
+	numFmtRefTable[1] = numFmt
+
+	xStyles = &xlsxStyles{NumFmts: numFmts, CellXfs: cellXfs}
+
+	cell = &Cell{Value: "123", numFmtRefTable: numFmtRefTable, styleIndex: 1, styles: xStyles}
+	c.Assert(cell.GetNumberFormat(), Equals, "DD/MM/YY")
+}
+
 // Test that GetStyle correctly converts the xlsxStyle.Fonts.
 func (l *LibSuite) TestGetStyleWithFonts(c *C) {
 	var cell *Cell