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

Cell with empty StreamStyle will get the column style

DamianSzkuat 6 лет назад
Родитель
Сommit
31d335f084
2 измененных файлов с 59 добавлено и 35 удалено
  1. 53 29
      stream_file.go
  2. 6 6
      stream_style_test.go

+ 53 - 29
stream_file.go

@@ -163,56 +163,34 @@ func (sf *StreamFile) writeS(cells []StreamCell) error {
 
 	// Add cells one by one
 	for colIndex, cell := range cells {
-		// Get the cell reference (location)
-		cellCoordinate := GetCellIDStringFromCoords(colIndex, sf.currentSheet.rowCount-1)
-
-		// Get the cell type string
-		cellType, err := getCellTypeAsString(cell.cellType)
-		if err != nil {
-			return err
-		}
-
-		// Build the XML cell opening
-		cellOpen := `<c r="` + cellCoordinate + `" t="` + cellType + `"`
-		// Add in the style id of the stream cell.
-		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 += `>`
 
-		// The XML cell contents
-		cellContentsOpen, cellContentsClose, err := getCellContentOpenAncCloseTags(cell.cellType)
-		if err != nil {
+		cellParts, err := sf.marshallCell(cell, colIndex)
+		if err != nil{
 			return err
 		}
 
-		// The XMl cell ending
-		cellClose := `</c>`
-
 		// Write the cell opening
-		if err := sf.currentSheet.write(cellOpen); err != nil {
+		if err := sf.currentSheet.write(cellParts["cellOpen"]); err != nil {
 			return err
 		}
 
 		// Write the cell contents opening
-		if err := sf.currentSheet.write(cellContentsOpen); err != nil {
+		if err := sf.currentSheet.write(cellParts["cellContentsOpen"]); err != nil {
 			return err
 		}
 
 		// Write cell contents
-		if err := xml.EscapeText(sf.currentSheet.writer, []byte(cell.cellData)); err != nil {
+		if err := xml.EscapeText(sf.currentSheet.writer, []byte(cellParts["cellContents"])); err != nil {
 			return err
 		}
 
 		// Write cell contents ending
-		if err := sf.currentSheet.write(cellContentsClose); err != nil {
+		if err := sf.currentSheet.write(cellParts["cellContentsClose"]); err != nil {
 			return err
 		}
 
 		// Write the cell ending
-		if err := sf.currentSheet.write(cellClose); err != nil {
+		if err := sf.currentSheet.write(cellParts["cellClose"]); err != nil {
 			return err
 		}
 	}
@@ -223,6 +201,52 @@ func (sf *StreamFile) writeS(cells []StreamCell) error {
 	return sf.zipWriter.Flush()
 }
 
+func (sf *StreamFile) marshallCell(cell StreamCell, colIndex int) (map[string]string, error) {
+	// Get the cell reference (location)
+	cellCoordinate := GetCellIDStringFromCoords(colIndex, sf.currentSheet.rowCount-1)
+
+	// Get the cell type string
+	cellType, err := getCellTypeAsString(cell.cellType)
+	if err != nil {
+		return nil, err
+	}
+
+	cellParts := make(map[string]string)
+
+	// Build the XML cell opening
+	cellOpen := `<c r="` + cellCoordinate + `" t="` + cellType + `"`
+	// Add in the style id of the stream cell. If the streamStyle is empty, don't add a style,
+	// default column style will be used
+	if cell.cellStyle != (StreamStyle{}){
+		if idx, ok := sf.styleIdMap[cell.cellStyle]; ok {
+			cellOpen += ` s="` + strconv.Itoa(idx) + `"`
+		} else {
+			return nil, errors.New("trying to make use of a style that has not been added")
+		}
+	}
+	cellOpen += `>`
+
+	cellParts["cellOpen"] = cellOpen
+
+	// The XML cell contents
+	cellContentsOpen, cellContentsClose, err := getCellContentOpenAncCloseTags(cell.cellType)
+	if err != nil {
+		return nil, err
+	}
+
+	cellParts["cellContentsOpen"] = cellContentsOpen
+	cellParts["cellContentsClose"] = cellContentsClose
+
+	cellParts["cellContents"] = cell.cellData
+
+	// The XMl cell ending
+	cellClose := `</c>`
+
+	cellParts["cellClose"] = cellClose
+
+	return cellParts, nil
+}
+
 func getCellTypeAsString(cellType CellType) (string, error) {
 	// documentation for the c.t (cell.Type) attribute:
 	// b (Boolean): Cell containing a boolean.

+ 6 - 6
stream_style_test.go

@@ -329,8 +329,8 @@ func writeStreamFileWithStyle(filePath string, fileBuffer io.Writer, sheetNames
 
 	for i, sheetName := range sheetNames {
 		colStyles := []StreamStyle{}
-		for _, cell := range workbookData[i][0] {
-			colStyles = append(colStyles, cell.cellStyle)
+		for range workbookData[i][0] {
+			colStyles = append(colStyles, Strings)
 		}
 
 		err := file.AddSheetS(sheetName, colStyles)
@@ -468,13 +468,13 @@ func (s *StreamSuite) TestCloseWithNothingWrittenToSheetsWithStyle(t *C) {
 	}
 
 	colStyles0 := []StreamStyle{}
-	for _, cell := range workbookData[0][0] {
-		colStyles0 = append(colStyles0, cell.cellStyle)
+	for range workbookData[0][0] {
+		colStyles0 = append(colStyles0, Strings)
 	}
 
 	colStyles1 := []StreamStyle{}
-	for _, cell := range workbookData[1][0] {
-		colStyles1 = append(colStyles1, cell.cellStyle)
+	for range workbookData[1][0] {
+		colStyles1 = append(colStyles1, Strings)
 	}
 
 	err = file.AddSheetS(sheetNames[0], colStyles0)