Ver Fonte

Some cleanup and tests

DamianSzkuat há 6 anos atrás
pai
commit
2228ed7bbc
5 ficheiros alterados com 90 adições e 34 exclusões
  1. BIN
      WorkbookWithStyle0.xlsx
  2. 12 3
      stream_cell.go
  3. 2 24
      stream_file_builder.go
  4. 6 1
      stream_style.go
  5. 70 6
      stream_style_test.go

BIN
WorkbookWithStyle0.xlsx


+ 12 - 3
stream_cell.go

@@ -1,6 +1,8 @@
 package xlsx
 
-import "strconv"
+import (
+	"strconv"
+)
 
 // StreamCell holds the data, style and type of cell for streaming
 type StreamCell struct {
@@ -34,8 +36,15 @@ func MakeIntegerStreamCell(cellData int) StreamCell {
 	return NewStreamCell(strconv.Itoa(cellData), Integers, CellTypeNumeric)
 }
 
-// MakeStyledIntegerStreamCell created a new cell that holds an integer value (represented as string)
+// MakeStyledIntegerStreamCell creates a new cell that holds an integer value (represented as string)
 // and is styled according to the given style.
 func MakeStyledIntegerStreamCell(cellData int, cellStyle StreamStyle) StreamCell {
 	return NewStreamCell(strconv.Itoa(cellData), cellStyle, CellTypeNumeric)
-}
+}
+
+// MakeDateStreamCell creates a new cell that holds a date value and is formatted as dd-mm-yyyy and
+// and is of type numeric
+//func MakeDateStreamCell(t time.Time) StreamCell {
+//	excelTime := TimeToExcelTime(t, false)
+//	return NewStreamCell(fmt.Sprintf("%f", excelTime), Dates, CellTypeNumeric)
+//}

+ 2 - 24
stream_file_builder.go

@@ -195,16 +195,6 @@ func (sb *StreamFileBuilder) Build() (*StreamFile, error) {
 		parts["xl/styles.xml"] = xmlStylesSheetString
 	}
 
-	//styleIdMap := make(map[StreamStyle]int)
-	//if len(sb.streamStyles) > 0 {
-	//	// Add the created styles to the XLSX file and marshal the new style sheet
-	//	styleIdMap = sb.addStylesToXlsxFile()
-	//	parts["xl/styles.xml"], err = sb.marshalStyles()
-	//	if err != nil {
-	//		return nil, err
-	//	}
-	//}
-
 	es := &StreamFile{
 		zipWriter:      sb.zipWriter,
 		xlsxFile:       sb.xlsxFile,
@@ -238,18 +228,6 @@ func (sb *StreamFileBuilder) Build() (*StreamFile, error) {
 	return es, nil
 }
 
-//func (sb *StreamFileBuilder) addStylesToXlsxFile() map[StreamStyle]int {
-//	styleIdMap := make(map[StreamStyle]int)
-//	// TODO test
-//	sb.xlsxFile.styles = newXlsxStyleSheet(sb.xlsxFile.theme)
-//	// TODO test
-//	for streamStyle,_ := range sb.streamStyles{
-//		XfId := handleStyleForXLSX(streamStyle.style, streamStyle.xNumFmtId, sb.xlsxFile.styles)
-//		styleIdMap[streamStyle] = XfId
-//	}
-//	return styleIdMap
-//}
-
 func (sb *StreamFileBuilder) marshalStyles() (string, error) {
 	styleSheetXMLString, err := sb.xlsxFile.styles.Marshal()
 	if err!=nil {
@@ -335,11 +313,11 @@ func getSheetIndex(sf *StreamFile, path string) (int, error) {
 	indexString := path[len(sheetFilePathPrefix) : len(path)-len(sheetFilePathSuffix)]
 	sheetXLSXIndex, err := strconv.Atoi(indexString)
 	if err != nil {
-		return -1, errors.New("Unexpected sheet file name from xlsx package")
+		return -1, errors.New("unexpected sheet file name from xlsx package")
 	}
 	if sheetXLSXIndex < 1 || len(sf.sheetXmlPrefix) < sheetXLSXIndex ||
 		len(sf.sheetXmlSuffix) < sheetXLSXIndex || len(sf.xlsxFile.Sheets) < sheetXLSXIndex {
-		return -1, errors.New("Unexpected sheet index")
+		return -1, errors.New("unexpected sheet index")
 	}
 	sheetArrayIndex := sheetXLSXIndex - 1
 	return sheetArrayIndex, nil

+ 6 - 1
stream_style.go

@@ -25,8 +25,9 @@ var BoldIntegers StreamStyle
 var ItalicIntegers StreamStyle
 var UnderlinedIntegers StreamStyle
 
-// var DefaultStyles []StreamStyle
+var Dates StreamStyle
 
+var Decimals StreamStyle
 
 var Bold *Font
 var Italic *Font
@@ -63,6 +64,10 @@ func init(){
 	BoldIntegers = MakeIntegerStyle(Bold, DefaultFill(), DefaultAlignment(), DefaultBorder())
 	ItalicIntegers = MakeIntegerStyle(Italic, DefaultFill(), DefaultAlignment(), DefaultBorder())
 	UnderlinedIntegers = MakeIntegerStyle(Underlined, DefaultFill(), DefaultAlignment(), DefaultBorder())
+
+	Dates = MakeDateStyle(DefaultFont(), DefaultFill(), DefaultAlignment(), DefaultBorder())
+
+	Decimals = MakeDecimalStyle(DefaultFont(), DefaultFill(), DefaultAlignment(), DefaultBorder())
 }
 
 // MakeStyle creates a new StreamStyle and add it to the styles that will be streamed

+ 70 - 6
stream_style_test.go

@@ -34,14 +34,14 @@ func (s *StreamSuite) TestXlsxStreamWriteWithStyle(t *C) {
 		expectedError error
 	}{
 		{
-			testName: "Number Row",
+			testName: "Style Test",
 			sheetNames: []string{
 				"Sheet1",
 			},
 			workbookData: [][][]StreamCell{
 				{
 					{MakeStringStreamCell("1"), MakeStringStreamCell("25"),
-						MakeStringStreamCell("A"), MakeStringStreamCell("B")},
+						MakeStyledStringStreamCell("A", BoldStrings), MakeStringStreamCell("B")},
 					{MakeIntegerStreamCell(1234), MakeStyledIntegerStreamCell(98, BoldIntegers),
 						MakeStyledIntegerStreamCell(34, ItalicIntegers), MakeStyledIntegerStreamCell(26, UnderlinedIntegers)},
 				},
@@ -266,7 +266,7 @@ func (s *StreamSuite) TestXlsxStreamWriteWithStyle(t *C) {
 			filePath = fmt.Sprintf("WorkbookWithStyle%d.xlsx", i)
 		}
 
-		err := writeStreamFileWithStyle(filePath, &buffer, testCase.sheetNames, testCase.workbookData, StreamTestsShouldMakeRealFiles)
+		err := writeStreamFileWithStyle(filePath, &buffer, testCase.sheetNames, testCase.workbookData, StreamTestsShouldMakeRealFiles, []StreamStyle{})
 		if err != testCase.expectedError && err.Error() != testCase.expectedError.Error() {
 			t.Fatalf("Error differs from expected error. Error: %v, Expected Error: %v ", err, testCase.expectedError)
 		}
@@ -304,7 +304,8 @@ func (s *StreamSuite) TestXlsxStreamWriteWithStyle(t *C) {
 }
 
 // writeStreamFile will write the file using this stream package
-func writeStreamFileWithStyle(filePath string, fileBuffer io.Writer, sheetNames []string, workbookData [][][]StreamCell, shouldMakeRealFiles bool) error {
+func writeStreamFileWithStyle(filePath string, fileBuffer io.Writer, sheetNames []string, workbookData [][][]StreamCell,
+								shouldMakeRealFiles bool, customStyles []StreamStyle) error {
 	var file *StreamFileBuilder
 	var err error
 	if shouldMakeRealFiles {
@@ -316,8 +317,11 @@ func writeStreamFileWithStyle(filePath string, fileBuffer io.Writer, sheetNames
 		file = NewStreamFileBuilder(fileBuffer)
 	}
 
-	err = file.AddStreamStyleList([]StreamStyle{Strings,BoldStrings,ItalicIntegers,UnderlinedStrings,
-												Integers, BoldIntegers, ItalicIntegers, UnderlinedIntegers})
+	defaultStyles := []StreamStyle{Strings,BoldStrings,ItalicIntegers,UnderlinedStrings,
+						Integers, BoldIntegers, ItalicIntegers, UnderlinedIntegers,
+						Dates}
+	allStylesToBeAdded := append(defaultStyles, customStyles...)
+	err = file.AddStreamStyleList(allStylesToBeAdded)
 	if err != nil {
 		return err
 	}
@@ -358,8 +362,68 @@ func writeStreamFileWithStyle(filePath string, fileBuffer io.Writer, sheetNames
 	return nil
 }
 
+func (s *StreamSuite) TestMakeNewStylesAndUseIt(t *C) {
+	var filePath string
+	var buffer bytes.Buffer
+	if TestsShouldMakeRealFiles {
+		filePath = fmt.Sprintf("Workbook_newStyle.xlsx")
+	}
+
+	timesNewRoman12 := NewFont(12, TimesNewRoman)
+	timesNewRoman12.Color = RGB_Dard_Green
+	courier20 := NewFont(12, Courier)
+	courier20.Color = RGB_Dark_Red
+
+	greenFill := NewFill(Solid_Cell_Fill, RGB_Light_Green, RGB_White)
+	redFill := NewFill(Solid_Cell_Fill, RGB_Light_Red, RGB_White)
 
+	greenStyle := MakeStyle(0, timesNewRoman12, greenFill, DefaultAlignment(), DefaultBorder())
+	redStyle := MakeStyle(0, courier20, redFill, DefaultAlignment(), DefaultBorder())
 
+	// decimalStyle := MakeDecimalStyle(DefaultFont(), DefaultFill(), DefaultAlignment(), DefaultBorder())
+
+	sheetNames := []string{"Sheet1"}
+	workbookData := [][][]StreamCell{
+		{
+			{MakeStringStreamCell("Header1"), MakeStringStreamCell("Header2")},
+			{MakeStyledStringStreamCell("Good", greenStyle), MakeStyledStringStreamCell("Bad", redStyle)},
+		},
+	}
+
+	err := writeStreamFileWithStyle(filePath, &buffer, sheetNames, workbookData, TestsShouldMakeRealFiles, []StreamStyle{greenStyle, redStyle})
+
+	if err != nil {
+		t.Fatal("Error during writing")
+	}
+
+	// read the file back with the xlsx package
+	var bufReader *bytes.Reader
+	var size int64
+	if !TestsShouldMakeRealFiles {
+		bufReader = bytes.NewReader(buffer.Bytes())
+		size = bufReader.Size()
+	}
+	actualSheetNames, actualWorkbookData := readXLSXFile(t, filePath, bufReader, size, TestsShouldMakeRealFiles)
+	// check if data was able to be read correctly
+	if !reflect.DeepEqual(actualSheetNames, sheetNames) {
+		t.Fatal("Expected sheet names to be equal")
+	}
+
+	expectedWorkbookDataStrings := [][][]string{}
+	for j, _ := range workbookData {
+		expectedWorkbookDataStrings = append(expectedWorkbookDataStrings, [][]string{})
+		for k, _ := range workbookData[j] {
+			expectedWorkbookDataStrings[j] = append(expectedWorkbookDataStrings[j], []string{})
+			for _, cell := range workbookData[j][k] {
+				expectedWorkbookDataStrings[j][k] = append(expectedWorkbookDataStrings[j][k], cell.cellData)
+			}
+		}
+
+	}
+	if !reflect.DeepEqual(actualWorkbookData, expectedWorkbookDataStrings) {
+		t.Fatal("Expected workbook data to be equal")
+	}
+}