Compatibility improvement

This commit is contained in:
xuri 2019-09-24 21:53:19 +08:00
parent 75d66a03f3
commit a34d3b8c86
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
10 changed files with 39 additions and 51 deletions

View File

@ -1845,7 +1845,7 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI
graphicFrame := xlsxGraphicFrame{ graphicFrame := xlsxGraphicFrame{
NvGraphicFramePr: xlsxNvGraphicFramePr{ NvGraphicFramePr: xlsxNvGraphicFramePr{
CNvPr: &xlsxCNvPr{ CNvPr: &xlsxCNvPr{
ID: f.countCharts() + f.countMedia() + 1, ID: len(content.OneCellAnchor) + len(content.TwoCellAnchor) + 2,
Name: "Chart " + strconv.Itoa(cNvPrID), Name: "Chart " + strconv.Itoa(cNvPrID),
}, },
}, },

10
col.go
View File

@ -10,6 +10,7 @@
package excelize package excelize
import ( import (
"errors"
"math" "math"
"strings" "strings"
) )
@ -112,19 +113,22 @@ func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
for c := range xlsx.Cols.Col { for c := range xlsx.Cols.Col {
colData := &xlsx.Cols.Col[c] colData := &xlsx.Cols.Col[c]
if colData.Min <= colNum && colNum <= colData.Max { if colData.Min <= colNum && colNum <= colData.Max {
level = colData.OutlineLevel level = colData.OutlineLevel + 1
} }
} }
return level, err return level, err
} }
// SetColOutlineLevel provides a function to set outline level of a single // SetColOutlineLevel provides a function to set outline level of a single
// column by given worksheet name and column name. For example, set outline // column by given worksheet name and column name. The value of parameter
// level of column D in Sheet1 to 2: // 'level' is 1-7. For example, set outline level of column D in Sheet1 to 2:
// //
// err := f.SetColOutlineLevel("Sheet1", "D", 2) // err := f.SetColOutlineLevel("Sheet1", "D", 2)
// //
func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error { func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
if level > 7 || level < 1 {
return errors.New("invalid outline level")
}
colNum, err := ColumnNameToNumber(col) colNum, err := ColumnNameToNumber(col)
if err != nil { if err != nil {
return err return err

View File

@ -10,9 +10,7 @@ import (
func TestColumnVisibility(t *testing.T) { func TestColumnVisibility(t *testing.T) {
t.Run("TestBook1", func(t *testing.T) { t.Run("TestBook1", func(t *testing.T) {
f, err := prepareTestBook1() f, err := prepareTestBook1()
if !assert.NoError(t, err) { assert.NoError(t, err)
t.FailNow()
}
assert.NoError(t, f.SetColVisible("Sheet1", "F", false)) assert.NoError(t, f.SetColVisible("Sheet1", "F", false))
assert.NoError(t, f.SetColVisible("Sheet1", "F", true)) assert.NoError(t, f.SetColVisible("Sheet1", "F", true))
@ -38,9 +36,7 @@ func TestColumnVisibility(t *testing.T) {
t.Run("TestBook3", func(t *testing.T) { t.Run("TestBook3", func(t *testing.T) {
f, err := prepareTestBook3() f, err := prepareTestBook3()
if !assert.NoError(t, err) { assert.NoError(t, err)
t.FailNow()
}
f.GetColVisible("Sheet1", "B") f.GetColVisible("Sheet1", "B")
}) })
} }
@ -49,12 +45,14 @@ func TestOutlineLevel(t *testing.T) {
f := NewFile() f := NewFile()
f.GetColOutlineLevel("Sheet1", "D") f.GetColOutlineLevel("Sheet1", "D")
f.NewSheet("Sheet2") f.NewSheet("Sheet2")
f.SetColOutlineLevel("Sheet1", "D", 4) assert.NoError(t, f.SetColOutlineLevel("Sheet1", "D", 4))
f.GetColOutlineLevel("Sheet1", "D") f.GetColOutlineLevel("Sheet1", "D")
f.GetColOutlineLevel("Shee2", "A") f.GetColOutlineLevel("Shee2", "A")
f.SetColWidth("Sheet2", "A", "D", 13) assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13))
f.SetColOutlineLevel("Sheet2", "B", 2) assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
f.SetRowOutlineLevel("Sheet1", 2, 250) assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7))
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level")
assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 2, 8), "invalid outline level")
// Test set and get column outline level with illegal cell coordinates. // Test set and get column outline level with illegal cell coordinates.
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "*", 1), `invalid column name "*"`) assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "*", 1), `invalid column name "*"`)
@ -67,7 +65,7 @@ func TestOutlineLevel(t *testing.T) {
assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 0, 1), "invalid row number 0") assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 0, 1), "invalid row number 0")
level, err := f.GetRowOutlineLevel("Sheet1", 2) level, err := f.GetRowOutlineLevel("Sheet1", 2)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, uint8(250), level) assert.Equal(t, uint8(7), level)
_, err = f.GetRowOutlineLevel("Sheet1", 0) _, err = f.GetRowOutlineLevel("Sheet1", 0)
assert.EqualError(t, err, `invalid row number 0`) assert.EqualError(t, err, `invalid row number 0`)
@ -76,15 +74,10 @@ func TestOutlineLevel(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, uint8(0), level) assert.Equal(t, uint8(0), level)
err = f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx")) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx")))
if !assert.NoError(t, err) {
t.FailNow()
}
f, err = OpenFile(filepath.Join("test", "Book1.xlsx")) f, err = OpenFile(filepath.Join("test", "Book1.xlsx"))
if !assert.NoError(t, err) { assert.NoError(t, err)
t.FailNow()
}
f.SetColOutlineLevel("Sheet2", "B", 2) f.SetColOutlineLevel("Sheet2", "B", 2)
} }
@ -138,11 +131,7 @@ func TestInsertCol(t *testing.T) {
f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External") f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
f.MergeCell(sheet1, "A1", "C3") f.MergeCell(sheet1, "A1", "C3")
err := f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`) assert.NoError(t, f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`))
if !assert.NoError(t, err) {
t.FailNow()
}
assert.NoError(t, f.InsertCol(sheet1, "A")) assert.NoError(t, f.InsertCol(sheet1, "A"))
// Test insert column with illegal cell coordinates. // Test insert column with illegal cell coordinates.

View File

@ -272,7 +272,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
twoCellAnchor.To = &to twoCellAnchor.To = &to
pic := xlsxPic{} pic := xlsxPic{}
pic.NvPicPr.CNvPicPr.PicLocks.NoChangeAspect = formatSet.NoChangeAspect pic.NvPicPr.CNvPicPr.PicLocks.NoChangeAspect = formatSet.NoChangeAspect
pic.NvPicPr.CNvPr.ID = f.countCharts() + f.countMedia() + 1 pic.NvPicPr.CNvPr.ID = len(content.OneCellAnchor) + len(content.TwoCellAnchor) + 2
pic.NvPicPr.CNvPr.Descr = file pic.NvPicPr.CNvPr.Descr = file
pic.NvPicPr.CNvPr.Name = "Picture " + strconv.Itoa(cNvPrID) pic.NvPicPr.CNvPr.Name = "Picture " + strconv.Itoa(cNvPrID)
if hyperlinkRID != 0 { if hyperlinkRID != 0 {

View File

@ -11,6 +11,7 @@ package excelize
import ( import (
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"math" "math"
"strconv" "strconv"
@ -257,8 +258,8 @@ func (f *File) GetRowVisible(sheet string, row int) (bool, error) {
} }
// SetRowOutlineLevel provides a function to set outline level number of a // SetRowOutlineLevel provides a function to set outline level number of a
// single row by given worksheet name and Excel row number. For example, // single row by given worksheet name and Excel row number. The value of
// outline row 2 in Sheet1 to level 1: // parameter 'level' is 1-7. For example, outline row 2 in Sheet1 to level 1:
// //
// err := f.SetRowOutlineLevel("Sheet1", 2, 1) // err := f.SetRowOutlineLevel("Sheet1", 2, 1)
// //
@ -266,6 +267,9 @@ func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error {
if row < 1 { if row < 1 {
return newInvalidRowNumberError(row) return newInvalidRowNumberError(row)
} }
if level > 7 || level < 1 {
return errors.New("invalid outline level")
}
xlsx, err := f.workSheetReader(sheet) xlsx, err := f.workSheetReader(sheet)
if err != nil { if err != nil {
return err return err

View File

@ -429,11 +429,6 @@ func (f *File) AddSparkline(sheet string, opt *SparklineOption) error {
return err return err
} }
for idx, ext := range decodeExtLst.Ext { for idx, ext := range decodeExtLst.Ext {
// hack: add back missing namespace
decodeExtLst.Ext[idx].XMLNSX14 = decodeExtLst.Ext[idx].X14
decodeExtLst.Ext[idx].XMLNSX15 = decodeExtLst.Ext[idx].X15
decodeExtLst.Ext[idx].XMLNSX14 = ""
decodeExtLst.Ext[idx].XMLNSX15 = ""
if ext.URI == ExtURISparklineGroups { if ext.URI == ExtURISparklineGroups {
decodeSparklineGroups := decodeX14SparklineGroups{} decodeSparklineGroups := decodeX14SparklineGroups{}
_ = xml.Unmarshal([]byte(ext.Content), &decodeSparklineGroups) _ = xml.Unmarshal([]byte(ext.Content), &decodeSparklineGroups)
@ -458,9 +453,8 @@ func (f *File) AddSparkline(sheet string, opt *SparklineOption) error {
} }
sparklineGroupsBytes, _ := xml.Marshal(groups) sparklineGroupsBytes, _ := xml.Marshal(groups)
extLst := xlsxWorksheetExt{ extLst := xlsxWorksheetExt{
XMLNSX14: NameSpaceSpreadSheetX14, URI: ExtURISparklineGroups,
URI: ExtURISparklineGroups, Content: string(sparklineGroupsBytes),
Content: string(sparklineGroupsBytes),
} }
extBytes, _ := xml.Marshal(extLst) extBytes, _ := xml.Marshal(extLst)
ws.ExtLst.Ext = string(extBytes) ws.ExtLst.Ext = string(extBytes)

Binary file not shown.

View File

@ -293,9 +293,10 @@ type cAutoTitleDeleted struct {
type cView3D struct { type cView3D struct {
RotX *attrValInt `xml:"rotX"` RotX *attrValInt `xml:"rotX"`
RotY *attrValInt `xml:"rotY"` RotY *attrValInt `xml:"rotY"`
RAngAx *attrValInt `xml:"rAngAx"`
DepthPercent *attrValInt `xml:"depthPercent"` DepthPercent *attrValInt `xml:"depthPercent"`
Perspective *attrValInt `xml:"perspective"` Perspective *attrValInt `xml:"perspective"`
RAngAx *attrValInt `xml:"rAngAx"` ExtLst *xlsxExtLst `xml:"extLst"`
} }
// cPlotArea directly maps the plotArea element. This element specifies the // cPlotArea directly maps the plotArea element. This element specifies the

View File

@ -85,9 +85,6 @@ type xlsxFonts struct {
// xlsxFont directly maps the font element. This element defines the // xlsxFont directly maps the font element. This element defines the
// properties for one of the fonts used in this workbook. // properties for one of the fonts used in this workbook.
type xlsxFont struct { type xlsxFont struct {
Name *attrValString `xml:"name"`
Charset *attrValInt `xml:"charset"`
Family *attrValInt `xml:"family"`
B *bool `xml:"b,omitempty"` B *bool `xml:"b,omitempty"`
I *bool `xml:"i,omitempty"` I *bool `xml:"i,omitempty"`
Strike *bool `xml:"strike,omitempty"` Strike *bool `xml:"strike,omitempty"`
@ -95,9 +92,12 @@ type xlsxFont struct {
Shadow *bool `xml:"shadow,omitempty"` Shadow *bool `xml:"shadow,omitempty"`
Condense *bool `xml:"condense,omitempty"` Condense *bool `xml:"condense,omitempty"`
Extend *bool `xml:"extend,omitempty"` Extend *bool `xml:"extend,omitempty"`
Color *xlsxColor `xml:"color"`
Sz *attrValFloat `xml:"sz"`
U *attrValString `xml:"u"` U *attrValString `xml:"u"`
Sz *attrValFloat `xml:"sz"`
Color *xlsxColor `xml:"color"`
Name *attrValString `xml:"name"`
Family *attrValInt `xml:"family"`
Charset *attrValInt `xml:"charset"`
Scheme *attrValString `xml:"scheme"` Scheme *attrValString `xml:"scheme"`
} }

View File

@ -629,13 +629,9 @@ type xlsxLegacyDrawing struct {
// xlsxWorksheetExt directly maps the ext element in the worksheet. // xlsxWorksheetExt directly maps the ext element in the worksheet.
type xlsxWorksheetExt struct { type xlsxWorksheetExt struct {
XMLName xml.Name `xml:"ext"` XMLName xml.Name `xml:"ext"`
XMLNSX14 string `xml:"xmlns:x14,attr,omitempty"` URI string `xml:"uri,attr"`
XMLNSX15 string `xml:"xmlns:x15,attr,omitempty"` Content string `xml:",innerxml"`
X14 string `xml:"x14,attr,omitempty"`
X15 string `xml:"x15,attr,omitempty"`
URI string `xml:"uri,attr"`
Content string `xml:",innerxml"`
} }
// decodeWorksheetExt directly maps the ext element. // decodeWorksheetExt directly maps the ext element.