|
|
@@ -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")
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
|
|
|
|