Skip to content

Commit 88de2f8

Browse files
committed
Default row height compatibility with Apache OpenOffice and Kingsoft WPS, unit test update and typo fixed
1 parent 3c8c8c5 commit 88de2f8

File tree

10 files changed

+50
-37
lines changed

10 files changed

+50
-37
lines changed

cell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) error {
517517
// }
518518
// if err := f.SetCellRichText("Sheet1", "A1", []excelize.RichTextRun{
519519
// {
520-
// Text: "blod",
520+
// Text: "bold",
521521
// Font: &excelize.Font{
522522
// Bold: true,
523523
// Color: "2354e8",

cell_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func TestSetCellRichText(t *testing.T) {
190190
assert.NoError(t, f.SetColWidth("Sheet1", "A", "A", 44))
191191
richTextRun := []RichTextRun{
192192
{
193-
Text: "blod",
193+
Text: "bold",
194194
Font: &Font{
195195
Bold: true,
196196
Color: "2354e8",

col.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
// Define the default cell size and EMU unit of measurement.
2626
const (
2727
defaultColWidthPixels float64 = 64
28+
defaultRowHeight float64 = 15
2829
defaultRowHeightPixels float64 = 20
2930
EMU int = 9525
3031
)

drawing.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ func (f *File) prepareChartSheetDrawing(xlsx *xlsxChartsheet, drawingID int, she
5959
func (f *File) addChart(formatSet *formatChart, comboCharts []*formatChart) {
6060
count := f.countCharts()
6161
xlsxChartSpace := xlsxChartSpace{
62-
XMLNSc: NameSpaceDrawingMLChart,
63-
XMLNSa: NameSpaceDrawingML,
62+
XMLNSc: NameSpaceDrawingMLChart.Value,
63+
XMLNSa: NameSpaceDrawingML.Value,
6464
XMLNSr: SourceRelationship.Value,
65-
XMLNSc16r2: SourceRelationshipChart201506,
65+
XMLNSc16r2: SourceRelationshipChart201506.Value,
6666
Date1904: &attrValBool{Val: boolPtr(false)},
6767
Lang: &attrValString{Val: stringPtr("en-US")},
6868
RoundedCorners: &attrValBool{Val: boolPtr(false)},
@@ -1143,8 +1143,8 @@ func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
11431143

11441144
if f.Drawings[path] == nil {
11451145
content := xlsxWsDr{}
1146-
content.A = NameSpaceDrawingML
1147-
content.Xdr = NameSpaceDrawingMLSpreadSheet
1146+
content.A = NameSpaceDrawingML.Value
1147+
content.Xdr = NameSpaceDrawingMLSpreadSheet.Value
11481148
if _, ok = f.XLSX[path]; ok { // Append Model
11491149
decodeWsDr := decodeWsDr{}
11501150
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
12101210
},
12111211
Graphic: &xlsxGraphic{
12121212
GraphicData: &xlsxGraphicData{
1213-
URI: NameSpaceDrawingMLChart,
1213+
URI: NameSpaceDrawingMLChart.Value,
12141214
Chart: &xlsxChart{
1215-
C: NameSpaceDrawingMLChart,
1215+
C: NameSpaceDrawingMLChart.Value,
12161216
R: SourceRelationship.Value,
12171217
RID: "rId" + strconv.Itoa(rID),
12181218
},
@@ -1250,9 +1250,9 @@ func (f *File) addSheetDrawingChart(drawingXML string, rID int, formatSet *forma
12501250
},
12511251
Graphic: &xlsxGraphic{
12521252
GraphicData: &xlsxGraphicData{
1253-
URI: NameSpaceDrawingMLChart,
1253+
URI: NameSpaceDrawingMLChart.Value,
12541254
Chart: &xlsxChart{
1255-
C: NameSpaceDrawingMLChart,
1255+
C: NameSpaceDrawingMLChart.Value,
12561256
R: SourceRelationship.Value,
12571257
RID: "rId" + strconv.Itoa(rID),
12581258
},

merge.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func (f *File) MergeCell(sheet, hcell, vcell string) error {
9797
} else {
9898
xlsx.MergeCells = &xlsxMergeCells{Cells: []*xlsxMergeCell{{Ref: ref}}}
9999
}
100+
xlsx.MergeCells.Count = len(xlsx.MergeCells.Cells)
100101
return err
101102
}
102103

@@ -146,6 +147,7 @@ func (f *File) UnmergeCell(sheet string, hcell, vcell string) error {
146147
i++
147148
}
148149
xlsx.MergeCells.Cells = xlsx.MergeCells.Cells[:i]
150+
xlsx.MergeCells.Count = len(xlsx.MergeCells.Cells)
149151
return nil
150152
}
151153

rows.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,21 +262,24 @@ func (f *File) GetRowHeight(sheet string, row int) (float64, error) {
262262
if row < 1 {
263263
return defaultRowHeightPixels, newInvalidRowNumberError(row)
264264
}
265-
266-
xlsx, err := f.workSheetReader(sheet)
265+
var ht = defaultRowHeight
266+
ws, err := f.workSheetReader(sheet)
267267
if err != nil {
268-
return defaultRowHeightPixels, err
268+
return ht, err
269269
}
270-
if row > len(xlsx.SheetData.Row) {
271-
return defaultRowHeightPixels, nil // it will be better to use 0, but we take care with BC
270+
if ws.SheetFormatPr != nil {
271+
ht = ws.SheetFormatPr.DefaultRowHeight
272+
}
273+
if row > len(ws.SheetData.Row) {
274+
return ht, nil // it will be better to use 0, but we take care with BC
272275
}
273-
for _, v := range xlsx.SheetData.Row {
276+
for _, v := range ws.SheetData.Row {
274277
if v.R == row && v.Ht != 0 {
275278
return v.Ht, nil
276279
}
277280
}
278281
// Optimisation for when the row heights haven't changed.
279-
return defaultRowHeightPixels, nil
282+
return ht, nil
280283
}
281284

282285
// sharedStringsReader provides a function to get the pointer to the structure

rows_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ func TestRowHeight(t *testing.T) {
112112
// Test get row height that rows index over exists rows.
113113
height, err = xlsx.GetRowHeight(sheet1, 5)
114114
assert.NoError(t, err)
115-
assert.Equal(t, defaultRowHeightPixels, height)
115+
assert.Equal(t, defaultRowHeight, height)
116116

117117
// Test get row height that rows heights haven't changed.
118118
height, err = xlsx.GetRowHeight(sheet1, 3)
119119
assert.NoError(t, err)
120-
assert.Equal(t, defaultRowHeightPixels, height)
120+
assert.Equal(t, defaultRowHeight, height)
121121

122122
// Test set and get row height on not exists worksheet.
123123
assert.EqualError(t, xlsx.SetRowHeight("SheetN", 1, 111.0), "sheet SheetN is not exist")

sheet.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,13 +1630,19 @@ func (f *File) relsReader(path string) *xlsxRelationships {
16301630
func prepareSheetXML(xlsx *xlsxWorksheet, col int, row int) {
16311631
rowCount := len(xlsx.SheetData.Row)
16321632
sizeHint := 0
1633+
var ht float64
1634+
var customHeight bool
1635+
if xlsx.SheetFormatPr != nil {
1636+
ht = xlsx.SheetFormatPr.DefaultRowHeight
1637+
customHeight = true
1638+
}
16331639
if rowCount > 0 {
16341640
sizeHint = len(xlsx.SheetData.Row[rowCount-1].C)
16351641
}
16361642
if rowCount < row {
16371643
// append missing rows
16381644
for rowIdx := rowCount; rowIdx < row; rowIdx++ {
1639-
xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1, C: make([]xlsxC, 0, sizeHint)})
1645+
xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{R: rowIdx + 1, CustomHeight: customHeight, Ht: ht, C: make([]xlsxC, 0, sizeHint)})
16401646
}
16411647
}
16421648
rowData := &xlsx.SheetData.Row[row-1]

sparkline.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func (f *File) AddSparkline(sheet string, opt *SparklineOption) (err error) {
441441
}
442442
} else {
443443
groups = &xlsxX14SparklineGroups{
444-
XMLNSXM: NameSpaceSpreadSheetExcel2006Main,
444+
XMLNSXM: NameSpaceSpreadSheetExcel2006Main.Value,
445445
SparklineGroups: []*xlsxX14SparklineGroup{group},
446446
}
447447
if sparklineGroupsBytes, err = xml.Marshal(groups); err != nil {
@@ -525,7 +525,7 @@ func (f *File) appendSparkline(ws *xlsxWorksheet, group *xlsxX14SparklineGroup,
525525
return
526526
}
527527
groups = &xlsxX14SparklineGroups{
528-
XMLNSXM: NameSpaceSpreadSheetExcel2006Main,
528+
XMLNSXM: NameSpaceSpreadSheetExcel2006Main.Value,
529529
Content: decodeSparklineGroups.Content + string(sparklineGroupBytes),
530530
}
531531
if sparklineGroupsBytes, err = xml.Marshal(groups); err != nil {

xmlDrawing.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@ package excelize
1313

1414
import "encoding/xml"
1515

16-
// Source relationship and namespace.
16+
// Source relationship and namespace list, associated prefixes and schema in which it was
17+
// introduced.
1718
var (
18-
SourceRelationship = xml.Attr{Name: xml.Name{Local: "r", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/officeDocument/2006/relationships"}
19-
SourceRelationshipCompatibility = xml.Attr{Name: xml.Name{Local: "mc", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/markup-compatibility/2006"}
20-
NameSpaceSpreadSheet = xml.Attr{Name: xml.Name{Local: "xmlns"}, Value: "http://schemas.openxmlformats.org/spreadsheetml/2006/main"}
21-
NameSpaceSpreadSheetX14 = xml.Attr{Name: xml.Name{Local: "x14", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"}
19+
SourceRelationship = xml.Attr{Name: xml.Name{Local: "r", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/officeDocument/2006/relationships"}
20+
SourceRelationshipCompatibility = xml.Attr{Name: xml.Name{Local: "mc", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/markup-compatibility/2006"}
21+
SourceRelationshipChart20070802 = xml.Attr{Name: xml.Name{Local: "c14", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2007/8/2/chart"}
22+
SourceRelationshipChart2014 = xml.Attr{Name: xml.Name{Local: "c16", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2014/chart"}
23+
SourceRelationshipChart201506 = xml.Attr{Name: xml.Name{Local: "c16r2", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/drawing/2015/06/chart"}
24+
NameSpaceSpreadSheet = xml.Attr{Name: xml.Name{Local: "xmlns"}, Value: "http://schemas.openxmlformats.org/spreadsheetml/2006/main"}
25+
NameSpaceSpreadSheetX14 = xml.Attr{Name: xml.Name{Local: "x14", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"}
26+
NameSpaceDrawingML = xml.Attr{Name: xml.Name{Local: "a", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/drawingml/2006/main"}
27+
NameSpaceDrawingMLChart = xml.Attr{Name: xml.Name{Local: "c", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/drawingml/2006/chart"}
28+
NameSpaceDrawingMLSpreadSheet = xml.Attr{Name: xml.Name{Local: "xdr", Space: "xmlns"}, Value: "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"}
29+
NameSpaceSpreadSheetX15 = xml.Attr{Name: xml.Name{Local: "x15", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"}
30+
NameSpaceSpreadSheetExcel2006Main = xml.Attr{Name: xml.Name{Local: "xne", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/excel/2006/main"}
31+
NameSpaceMacExcel2008Main = xml.Attr{Name: xml.Name{Local: "mx", Space: "xmlns"}, Value: "http://schemas.microsoft.com/office/mac/excel/2008/main"}
2232
)
2333

2434
// Source relationship and namespace.
@@ -37,15 +47,6 @@ const (
3747
SourceRelationshipPivotCache = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition"
3848
SourceRelationshipSharedStrings = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"
3949
SourceRelationshipVBAProject = "http://schemas.microsoft.com/office/2006/relationships/vbaProject"
40-
SourceRelationshipChart201506 = "http://schemas.microsoft.com/office/drawing/2015/06/chart"
41-
SourceRelationshipChart20070802 = "http://schemas.microsoft.com/office/drawing/2007/8/2/chart"
42-
SourceRelationshipChart2014 = "http://schemas.microsoft.com/office/drawing/2014/chart"
43-
NameSpaceDrawingML = "http://schemas.openxmlformats.org/drawingml/2006/main"
44-
NameSpaceDrawingMLChart = "http://schemas.openxmlformats.org/drawingml/2006/chart"
45-
NameSpaceDrawingMLSpreadSheet = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
46-
NameSpaceSpreadSheetX15 = "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"
47-
NameSpaceSpreadSheetExcel2006Main = "http://schemas.microsoft.com/office/excel/2006/main"
48-
NameSpaceMacExcel2008Main = "http://schemas.microsoft.com/office/mac/excel/2008/main"
4950
NameSpaceXML = "http://www.w3.org/XML/1998/namespace"
5051
NameSpaceXMLSchemaInstance = "http://www.w3.org/2001/XMLSchema-instance"
5152
StrictSourceRelationship = "http://purl.oclc.org/ooxml/officeDocument/relationships"

0 commit comments

Comments
 (0)