Selaa lähdekoodia

Copy old styles across, when restyling a car.

Geoffrey J. Teale 11 vuotta sitten
vanhempi
commit
94ed93183b
3 muutettua tiedostoa jossa 15 lisäystä ja 11 poistoa
  1. 5 3
      file.go
  2. 1 4
      sheet.go
  3. 9 4
      xmlStyle.go

+ 5 - 3
file.go

@@ -160,9 +160,11 @@ func (f *File) MarshallParts() (map[string]string, error) {
 	workbook = f.makeWorkbook()
 	sheetIndex := 1
 
-	styles := &xlsxStyleSheet{}
+	if f.styles == nil {
+		f.styles = &xlsxStyleSheet{}
+	}
 	for _, sheet := range f.Sheets {
-		xSheet := sheet.makeXLSXSheet(refTable, styles)
+		xSheet := sheet.makeXLSXSheet(refTable, f.styles)
 		rId := fmt.Sprintf("rId%d", sheetIndex)
 		sheetId := strconv.Itoa(sheetIndex)
 		sheetPath := fmt.Sprintf("worksheets/sheet%d.xml", sheetIndex)
@@ -214,7 +216,7 @@ func (f *File) MarshallParts() (map[string]string, error) {
 		return parts, err
 	}
 
-	parts["xl/styles.xml"], err = styles.Marshal()
+	parts["xl/styles.xml"], err = f.styles.Marshal()
 	if err != nil {
 		return parts, err
 	}

+ 1 - 4
sheet.go

@@ -86,11 +86,8 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 			xCellXf.FillId = fillId
 			xCellXf.BorderId = borderId
 			xCellXf.NumFmtId = xNumFmt.NumFmtId
-			styleXfId := styles.addCellStyleXf(xCellStyleXf)
+			styles.addCellStyleXf(xCellStyleXf)
 			XfId := styles.addCellXf(xCellXf)
-			if styleXfId != XfId {
-				panic("StyleXFId != XfId, this should never happen.")
-			}
 			if c > maxCell {
 				maxCell = c
 			}

+ 9 - 4
xmlStyle.go

@@ -65,10 +65,12 @@ func (styles *xlsxStyleSheet) getStyle(styleIndex int) (style Style) {
 		style.ApplyFont = xf.ApplyFont || styleXf.ApplyFont
 
 		if xf.BorderId > -1 && xf.BorderId < styles.Borders.Count {
-			style.Border.Left = styles.Borders.Border[xf.BorderId].Left.Style
-			style.Border.Right = styles.Borders.Border[xf.BorderId].Right.Style
-			style.Border.Top = styles.Borders.Border[xf.BorderId].Top.Style
-			style.Border.Bottom = styles.Borders.Border[xf.BorderId].Bottom.Style
+			var border xlsxBorder
+			border = styles.Borders.Border[xf.BorderId]
+			style.Border.Left = border.Left.Style
+			style.Border.Right = border.Right.Style
+			style.Border.Top = border.Top.Style
+			style.Border.Bottom = border.Bottom.Style
 		}
 
 		if xf.FillId > -1 && xf.FillId < styles.Fills.Count {
@@ -105,6 +107,9 @@ func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int) string {
 
 func (styles *xlsxStyleSheet) addFont(xFont xlsxFont) (index int) {
 	var font xlsxFont
+	if xFont.Name.Val == "" {
+		return 0
+	}
 	for index, font = range styles.Fonts.Font {
 		if font.Equals(xFont) {
 			return index