Skip to content

Commit

Permalink
Default row height compatibility with Apache OpenOffice and Kingsoft …
Browse files Browse the repository at this point in the history
…WPS, unit test update and typo fixed
  • Loading branch information
xuri committed Aug 22, 2020
1 parent 3c8c8c5 commit 88de2f8
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
18 changes: 9 additions & 9 deletions drawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down Expand Up @@ -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)))).
Expand Down Expand Up @@ -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),
},
Expand Down Expand Up @@ -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),
},
Expand Down
2 changes: 2 additions & 0 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 10 additions & 7 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 7 additions & 1 deletion sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions sparkline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 15 additions & 14 deletions xmlDrawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"
Expand Down

0 comments on commit 88de2f8

Please sign in to comment.