From 88de2f8d510b0959bbb672b80656d207bd0bc927 Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 22 Aug 2020 18:58:43 +0800 Subject: [PATCH] Default row height compatibility with Apache OpenOffice and Kingsoft WPS, unit test update and typo fixed --- cell.go | 2 +- cell_test.go | 2 +- col.go | 1 + drawing.go | 18 +++++++++--------- merge.go | 2 ++ rows.go | 17 ++++++++++------- rows_test.go | 4 ++-- sheet.go | 8 +++++++- sparkline.go | 4 ++-- xmlDrawing.go | 29 +++++++++++++++-------------- 10 files changed, 50 insertions(+), 37 deletions(-) diff --git a/cell.go b/cell.go index 383c02c..5fe2157 100644 --- a/cell.go +++ b/cell.go @@ -517,7 +517,7 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) error { // } // if err := f.SetCellRichText("Sheet1", "A1", []excelize.RichTextRun{ // { -// Text: "blod", +// Text: "bold", // Font: &excelize.Font{ // Bold: true, // Color: "2354e8", diff --git a/cell_test.go b/cell_test.go index ba4cd83..441a694 100644 --- a/cell_test.go +++ b/cell_test.go @@ -190,7 +190,7 @@ func TestSetCellRichText(t *testing.T) { assert.NoError(t, f.SetColWidth("Sheet1", "A", "A", 44)) richTextRun := []RichTextRun{ { - Text: "blod", + Text: "bold", Font: &Font{ Bold: true, Color: "2354e8", diff --git a/col.go b/col.go index 5a8299e..72db4be 100644 --- a/col.go +++ b/col.go @@ -25,6 +25,7 @@ import ( // Define the default cell size and EMU unit of measurement. const ( defaultColWidthPixels float64 = 64 + defaultRowHeight float64 = 15 defaultRowHeightPixels float64 = 20 EMU int = 9525 ) diff --git a/drawing.go b/drawing.go index ced747d..806c1b7 100644 --- a/drawing.go +++ b/drawing.go @@ -59,10 +59,10 @@ func (f *File) prepareChartSheetDrawing(xlsx *xlsxChartsheet, drawingID int, she func (f *File) addChart(formatSet *formatChart, comboCharts []*formatChart) { count := f.countCharts() xlsxChartSpace := xlsxChartSpace{ - XMLNSc: NameSpaceDrawingMLChart, - XMLNSa: NameSpaceDrawingML, + XMLNSc: NameSpaceDrawingMLChart.Value, + XMLNSa: NameSpaceDrawingML.Value, XMLNSr: SourceRelationship.Value, - XMLNSc16r2: SourceRelationshipChart201506, + XMLNSc16r2: SourceRelationshipChart201506.Value, Date1904: &attrValBool{Val: boolPtr(false)}, Lang: &attrValString{Val: stringPtr("en-US")}, RoundedCorners: &attrValBool{Val: boolPtr(false)}, @@ -1143,8 +1143,8 @@ func (f *File) drawingParser(path string) (*xlsxWsDr, int) { if f.Drawings[path] == nil { content := xlsxWsDr{} - content.A = NameSpaceDrawingML - content.Xdr = NameSpaceDrawingMLSpreadSheet + content.A = NameSpaceDrawingML.Value + content.Xdr = NameSpaceDrawingMLSpreadSheet.Value if _, ok = f.XLSX[path]; ok { // Append Model decodeWsDr := decodeWsDr{} if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path)))). @@ -1210,9 +1210,9 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI }, Graphic: &xlsxGraphic{ GraphicData: &xlsxGraphicData{ - URI: NameSpaceDrawingMLChart, + URI: NameSpaceDrawingMLChart.Value, Chart: &xlsxChart{ - C: NameSpaceDrawingMLChart, + C: NameSpaceDrawingMLChart.Value, R: SourceRelationship.Value, RID: "rId" + strconv.Itoa(rID), }, @@ -1250,9 +1250,9 @@ func (f *File) addSheetDrawingChart(drawingXML string, rID int, formatSet *forma }, Graphic: &xlsxGraphic{ GraphicData: &xlsxGraphicData{ - URI: NameSpaceDrawingMLChart, + URI: NameSpaceDrawingMLChart.Value, Chart: &xlsxChart{ - C: NameSpaceDrawingMLChart, + C: NameSpaceDrawingMLChart.Value, R: SourceRelationship.Value, RID: "rId" + strconv.Itoa(rID), }, diff --git a/merge.go b/merge.go index b233335..7bb6d42 100644 --- a/merge.go +++ b/merge.go @@ -97,6 +97,7 @@ func (f *File) MergeCell(sheet, hcell, vcell string) error { } else { xlsx.MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: ref}}} } + xlsx.MergeCells.Count = len(xlsx.MergeCells.Cells) return err } @@ -146,6 +147,7 @@ func (f *File) UnmergeCell(sheet string, hcell, vcell string) error { i++ } xlsx.MergeCells.Cells = xlsx.MergeCells.Cells[:i] + xlsx.MergeCells.Count = len(xlsx.MergeCells.Cells) return nil } diff --git a/rows.go b/rows.go index 66dd16b..c6098e6 100644 --- a/rows.go +++ b/rows.go @@ -262,21 +262,24 @@ func (f *File) GetRowHeight(sheet string, row int) (float64, error) { if row < 1 { return defaultRowHeightPixels, newInvalidRowNumberError(row) } - - xlsx, err := f.workSheetReader(sheet) + var ht = defaultRowHeight + ws, err := f.workSheetReader(sheet) if err != nil { - return defaultRowHeightPixels, err + return ht, err } - if row > len(xlsx.SheetData.Row) { - return defaultRowHeightPixels, nil // it will be better to use 0, but we take care with BC + if ws.SheetFormatPr != nil { + ht = ws.SheetFormatPr.DefaultRowHeight + } + if row > len(ws.SheetData.Row) { + return ht, nil // it will be better to use 0, but we take care with BC } - for _, v := range xlsx.SheetData.Row { + for _, v := range ws.SheetData.Row { if v.R == row && v.Ht != 0 { return v.Ht, nil } } // Optimisation for when the row heights haven't changed. - return defaultRowHeightPixels, nil + return ht, nil } // sharedStringsReader provides a function to get the pointer to the structure diff --git a/rows_test.go b/rows_test.go index 14537eb..e3ce9ee 100644 --- a/rows_test.go +++ b/rows_test.go @@ -112,12 +112,12 @@ func TestRowHeight(t *testing.T) { // Test get row height that rows index over exists rows. height, err = xlsx.GetRowHeight(sheet1, 5) assert.NoError(t, err) - assert.Equal(t, defaultRowHeightPixels, height) + assert.Equal(t, defaultRowHeight, height) // Test get row height that rows heights haven't changed. height, err = xlsx.GetRowHeight(sheet1, 3) assert.NoError(t, err) - assert.Equal(t, defaultRowHeightPixels, height) + assert.Equal(t, defaultRowHeight, height) // Test set and get row height on not exists worksheet. assert.EqualError(t, xlsx.SetRowHeight("SheetN", 1, 111.0), "sheet SheetN is not exist") diff --git a/sheet.go b/sheet.go index a92221d..dedd2d9 100644 --- a/sheet.go +++ b/sheet.go @@ -1630,13 +1630,19 @@ func (f *File) relsReader(path string) *xlsxRelationships { func prepareSheetXML(xlsx *xlsxWorksheet, col int, row int) { rowCount := len(xlsx.SheetData.Row) sizeHint := 0 + var ht float64 + var customHeight bool + if xlsx.SheetFormatPr != nil { + ht = xlsx.SheetFormatPr.DefaultRowHeight + customHeight = true + } if rowCount > 0 { sizeHint = len(xlsx.SheetData.Row[rowCount-1].C) } if rowCount < row { // append missing rows for rowIdx := rowCount; rowIdx < row; rowIdx++ { - xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1, C: make([]xlsxC, 0, sizeHint)}) + xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1, CustomHeight: customHeight, Ht: ht, C: make([]xlsxC, 0, sizeHint)}) } } rowData := &xlsx.SheetData.Row[row-1] diff --git a/sparkline.go b/sparkline.go index 4004878..b42207c 100644 --- a/sparkline.go +++ b/sparkline.go @@ -441,7 +441,7 @@ func (f *File) AddSparkline(sheet string, opt *SparklineOption) (err error) { } } else { groups = &xlsxX14SparklineGroups{ - XMLNSXM: NameSpaceSpreadSheetExcel2006Main, + XMLNSXM: NameSpaceSpreadSheetExcel2006Main.Value, SparklineGroups: []*xlsxX14SparklineGroup{group}, } if sparklineGroupsBytes, err = xml.Marshal(groups); err != nil { @@ -525,7 +525,7 @@ func (f *File) appendSparkline(ws *xlsxWorksheet, group *xlsxX14SparklineGroup, return } groups = &xlsxX14SparklineGroups{ - XMLNSXM: NameSpaceSpreadSheetExcel2006Main, + XMLNSXM: NameSpaceSpreadSheetExcel2006Main.Value, Content: decodeSparklineGroups.Content + string(sparklineGroupBytes), } if sparklineGroupsBytes, err = xml.Marshal(groups); err != nil { diff --git a/xmlDrawing.go b/xmlDrawing.go index 64d2bc5..e3e496a 100644 --- a/xmlDrawing.go +++ b/xmlDrawing.go @@ -13,12 +13,22 @@ package excelize import "encoding/xml" -// Source relationship and namespace. +// Source relationship and namespace list, associated prefixes and schema in which it was +// introduced. var ( - SourceRelationship = xml.Attr{Name: xml.Name{Local: "r", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/officeDocument/2006/relationships"} - SourceRelationshipCompatibility = xml.Attr{Name: xml.Name{Local: "mc", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/markup-compatibility/2006"} - NameSpaceSpreadSheet = xml.Attr{Name: xml.Name{Local: "xmlns"}, Value: "http://schemas.openxmlformats.org/spreadsheetml/2006/main"} - NameSpaceSpreadSheetX14 = xml.Attr{Name: xml.Name{Local: "x14", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"} + SourceRelationship = xml.Attr{Name: xml.Name{Local: "r", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/officeDocument/2006/relationships"} + SourceRelationshipCompatibility = xml.Attr{Name: xml.Name{Local: "mc", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/markup-compatibility/2006"} + SourceRelationshipChart20070802 = xml.Attr{Name: xml.Name{Local: "c14", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2007/8/2/chart"} + SourceRelationshipChart2014 = xml.Attr{Name: xml.Name{Local: "c16", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2014/chart"} + SourceRelationshipChart201506 = xml.Attr{Name: xml.Name{Local: "c16r2", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2015/06/chart"} + NameSpaceSpreadSheet = xml.Attr{Name: xml.Name{Local: "xmlns"}, Value: "http://schemas.openxmlformats.org/spreadsheetml/2006/main"} + NameSpaceSpreadSheetX14 = xml.Attr{Name: xml.Name{Local: "x14", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"} + NameSpaceDrawingML = xml.Attr{Name: xml.Name{Local: "a", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/drawingml/2006/main"} + NameSpaceDrawingMLChart = xml.Attr{Name: xml.Name{Local: "c", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/drawingml/2006/chart"} + NameSpaceDrawingMLSpreadSheet = xml.Attr{Name: xml.Name{Local: "xdr", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"} + NameSpaceSpreadSheetX15 = xml.Attr{Name: xml.Name{Local: "x15", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"} + NameSpaceSpreadSheetExcel2006Main = xml.Attr{Name: xml.Name{Local: "xne", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/excel/2006/main"} + NameSpaceMacExcel2008Main = xml.Attr{Name: xml.Name{Local: "mx", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/mac/excel/2008/main"} ) // Source relationship and namespace. @@ -37,15 +47,6 @@ const ( SourceRelationshipPivotCache = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition" SourceRelationshipSharedStrings = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" SourceRelationshipVBAProject = "http://schemas.microsoft.com/office/2006/relationships/vbaProject" - SourceRelationshipChart201506 = "http://schemas.microsoft.com/office/drawing/2015/06/chart" - SourceRelationshipChart20070802 = "http://schemas.microsoft.com/office/drawing/2007/8/2/chart" - SourceRelationshipChart2014 = "http://schemas.microsoft.com/office/drawing/2014/chart" - NameSpaceDrawingML = "http://schemas.openxmlformats.org/drawingml/2006/main" - NameSpaceDrawingMLChart = "http://schemas.openxmlformats.org/drawingml/2006/chart" - NameSpaceDrawingMLSpreadSheet = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" - NameSpaceSpreadSheetX15 = "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" - NameSpaceSpreadSheetExcel2006Main = "http://schemas.microsoft.com/office/excel/2006/main" - NameSpaceMacExcel2008Main = "http://schemas.microsoft.com/office/mac/excel/2008/main" NameSpaceXML = "http://www.w3.org/XML/1998/namespace" NameSpaceXMLSchemaInstance = "http://www.w3.org/2001/XMLSchema-instance" StrictSourceRelationship = "http://purl.oclc.org/ooxml/officeDocument/relationships"