Jelajahi Sumber

Moving stream style list to streamFileBuilder

DamianSzkuat 6 tahun lalu
induk
melakukan
dae88388a2
4 mengubah file dengan 60 tambahan dan 32 penghapusan
  1. 5 1
      stream_file.go
  2. 31 21
      stream_file_builder.go
  3. 11 10
      stream_style.go
  4. 13 0
      stream_test.go

+ 5 - 1
stream_file.go

@@ -104,7 +104,11 @@ func (sf *StreamFile) write(cells []StreamCell) error {
 		// Build the XML cell opening
 		cellOpen := `<c r="` + cellCoordinate + `" t="` + cellType + `"`
 		// Add in the style id if the cell isn't using the default style
-		cellOpen += ` s="` + strconv.Itoa(sf.styleIdMap[cell.cellStyle]) + `"`
+		if idx, ok := sf.styleIdMap[cell.cellStyle]; ok {
+			cellOpen += ` s="` + strconv.Itoa(idx) + `"`
+		} else {
+			return errors.New("Trying to make use of a style that has not been added!")
+		}
 
 		cellOpen += `>`
 

+ 31 - 21
stream_file_builder.go

@@ -33,13 +33,14 @@ import (
 )
 
 type StreamFileBuilder struct {
-	built              bool
-	xlsxFile           *File
-	zipWriter          *zip.Writer
+	built              	bool
+	xlsxFile           	*File
+	zipWriter          	*zip.Writer
 	// cellTypeToStyleIds map[CellType]int
-	maxStyleId         int
-	styleIds           [][]int
-	styleIdMap		   map[StreamStyle]int
+	maxStyleId         	int
+	styleIds           	[][]int
+	styleIdMap		   	map[StreamStyle]int
+	streamStyles		[]StreamStyle
 }
 
 const (
@@ -109,9 +110,12 @@ func (sb *StreamFileBuilder) AddSheet(name string, cells []StreamCell) error {
 	}
 
 	for i, cell := range cells {
-		cellStyleIndex := sb.styleIdMap[cell.cellStyle]
-		sheet.Cols[i].SetType(cell.cellType)
-		sb.styleIds[len(sb.styleIds)-1] = append(sb.styleIds[len(sb.styleIds)-1], cellStyleIndex)
+		if cellStyleIndex, ok := sb.styleIdMap[cell.cellStyle]; ok {
+			sheet.Cols[i].SetType(cell.cellType)
+			sb.styleIds[len(sb.styleIds)-1] = append(sb.styleIds[len(sb.styleIds)-1], cellStyleIndex)
+		} else {
+			return errors.New("Trying to make use of a style that has not been added!")
+		}
 	}
 	return nil
 }
@@ -128,7 +132,7 @@ func (sb *StreamFileBuilder) Build() (*StreamFile, error) {
 	if err != nil {
 		return nil, err
 	}
-	parts, err = sb.addDefaultStyles(parts)
+	parts["xl/styles.xml"], err = sb.masrshalStyles()
 	if err != nil {
 		return nil, err
 	}
@@ -166,24 +170,30 @@ func (sb *StreamFileBuilder) Build() (*StreamFile, error) {
 	return es, nil
 }
 
-func (sb *StreamFileBuilder) addDefaultStyles(parts map[string]string) (map[string]string, error) {
-	var err error
-
-	for _,streamStyle := range DefaultStyles{
-		//if streamStyle != nil{
-			XfId := handleStyleForXLSX(streamStyle.style, streamStyle.xNumFmtId, sb.xlsxFile.styles)
-			sb.styleIdMap[streamStyle] = XfId
-		//}
+func (sb *StreamFileBuilder) masrshalStyles() (string, error) {
+	for _,streamStyle := range sb.streamStyles{
+		XfId := handleStyleForXLSX(streamStyle.style, streamStyle.xNumFmtId, sb.xlsxFile.styles)
+		sb.styleIdMap[streamStyle] = XfId
 	}
 
-	parts["xl/styles.xml"], err = sb.xlsxFile.styles.Marshal()
+	styleSheetXMLString, err := sb.xlsxFile.styles.Marshal()
 	if err!=nil {
-		return nil, err
+		return "", err
 	}
 
-	return parts, nil
+	return styleSheetXMLString, nil
 }
 
+// AddStreamStyle adds a StreamStyle to the list of styles that can be used in the file
+// This function will only work before the file is built and will throw an error
+// if it is called after building the file.
+func (sb *StreamFileBuilder) AddStreamStyle(streamStyle StreamStyle) error {
+	if sb.built {
+		return errors.New("The style file has been built, cannot add new styles anymore.")
+	}
+	sb.streamStyles = append(sb.streamStyles, streamStyle)
+	return nil
+}
 
 
 // processEmptySheetXML will take in the path and XML data of an empty sheet, and will save the beginning and end of the

+ 11 - 10
stream_style.go

@@ -25,7 +25,8 @@ var BoldIntegers StreamStyle
 var ItalicIntegers StreamStyle
 var UnderlinedIntegers StreamStyle
 
-var DefaultStyles []StreamStyle
+// var DefaultStyles []StreamStyle
+
 
 var Bold *Font
 var Italic *Font
@@ -57,10 +58,10 @@ func init(){
 	ItalicStrings = MakeStringStyle(Italic, DefaultFill(), DefaultAlignment(), DefaultBorder())
 	UnderlinedStrings = MakeStringStyle(Underlined, DefaultFill(), DefaultAlignment(), DefaultBorder())
 
-	DefaultStyles = append(DefaultStyles, Strings)
-	DefaultStyles = append(DefaultStyles, BoldStrings)
-	DefaultStyles = append(DefaultStyles, ItalicStrings)
-	DefaultStyles = append(DefaultStyles, UnderlinedStrings)
+	//DefaultStyles = append(DefaultStyles, Strings)
+	//DefaultStyles = append(DefaultStyles, BoldStrings)
+	//DefaultStyles = append(DefaultStyles, ItalicStrings)
+	//DefaultStyles = append(DefaultStyles, UnderlinedStrings)
 
 	// Init default Integer styles
 	Integers = MakeIntegerStyle(DefaultFont(), DefaultFill(), DefaultAlignment(), DefaultBorder())
@@ -68,10 +69,10 @@ func init(){
 	ItalicIntegers = MakeIntegerStyle(Italic, DefaultFill(), DefaultAlignment(), DefaultBorder())
 	UnderlinedIntegers = MakeIntegerStyle(Underlined, DefaultFill(), DefaultAlignment(), DefaultBorder())
 
-	DefaultStyles = append(DefaultStyles, Integers)
-	DefaultStyles = append(DefaultStyles, BoldIntegers)
-	DefaultStyles = append(DefaultStyles, ItalicIntegers)
-	DefaultStyles = append(DefaultStyles, UnderlinedIntegers)
+	//DefaultStyles = append(DefaultStyles, Integers)
+	//DefaultStyles = append(DefaultStyles, BoldIntegers)
+	//DefaultStyles = append(DefaultStyles, ItalicIntegers)
+	//DefaultStyles = append(DefaultStyles, UnderlinedIntegers)
 }
 
 // MakeStyle creates a new StreamStyle and add it to the styles that will be streamed
@@ -94,7 +95,7 @@ func MakeStyle(formatStyleId int, font *Font, fill *Fill, alignment *Alignment,
 		style: 		newStyle,
 	}
 
-	DefaultStyles = append(DefaultStyles, newStreamStyle)
+	// DefaultStyles = append(DefaultStyles, newStreamStyle)
 	return newStreamStyle
 }
 

+ 13 - 0
stream_test.go

@@ -570,6 +570,7 @@ func (s *StreamSuite) TestXlsxStreamWrite(t *C) {
 			},
 		},
 	}
+
 	for i, testCase := range testCases {
 		var filePath string
 		var buffer bytes.Buffer
@@ -679,6 +680,18 @@ func writeStreamFile(filePath string, fileBuffer io.Writer, sheetNames []string,
 		file = NewStreamFileBuilder(fileBuffer)
 	}
 
+	err = file.AddStreamStyle(Strings)
+	err = file.AddStreamStyle(BoldStrings)
+	err = file.AddStreamStyle(ItalicStrings)
+	err = file.AddStreamStyle(UnderlinedStrings)
+	err = file.AddStreamStyle(Integers)
+	err = file.AddStreamStyle(BoldIntegers)
+	err = file.AddStreamStyle(ItalicIntegers)
+	err = file.AddStreamStyle(UnderlinedIntegers)
+	if err != nil { // TODO handle all errors not just one
+		return err
+	}
+
 	for i, sheetName := range sheetNames {
 		header := workbookData[i][0]
 		err := file.AddSheet(sheetName, header)