- Complete the element `sheetFormatPr` struct definition;

- Partial logic performance optimization, use pointer reference instead of a pass the variable value;
- Add comments for content types struct definition;
- Update go test `TestSetBorder` section
This commit is contained in:
Ri Xu 2017-03-10 23:10:15 +08:00
parent 1f73f08185
commit 5384756d64
9 changed files with 64 additions and 41 deletions

10
cell.go
View File

@ -111,7 +111,7 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
} }
ok := f.checked[name] ok := f.checked[name]
if !ok { if !ok {
xlsx = checkRow(xlsx) checkRow(&xlsx)
f.checked[name] = true f.checked[name] = true
} }
if xlsx.MergeCells != nil { if xlsx.MergeCells != nil {
@ -129,8 +129,8 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
rows := xAxis + 1 rows := xAxis + 1
cell := yAxis + 1 cell := yAxis + 1
xlsx = completeRow(xlsx, rows, cell) completeRow(&xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell) completeCol(&xlsx, rows, cell)
if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil { if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
@ -156,7 +156,7 @@ func (f *File) SetCellHyperLink(sheet, axis, link string) {
} }
ok := f.checked[name] ok := f.checked[name]
if !ok { if !ok {
xlsx = checkRow(xlsx) checkRow(&xlsx)
f.checked[name] = true f.checked[name] = true
} }
if xlsx.MergeCells != nil { if xlsx.MergeCells != nil {
@ -226,7 +226,7 @@ func (f *File) MergeCell(sheet, hcell, vcell string) {
} }
ok := f.checked[name] ok := f.checked[name]
if !ok { if !ok {
xlsx = checkRow(xlsx) checkRow(&xlsx)
f.checked[name] = true f.checked[name] = true
} }
if xlsx.MergeCells != nil { if xlsx.MergeCells != nil {

View File

@ -96,7 +96,7 @@ func (f *File) SetCellInt(sheet, axis string, value int) {
} }
ok := f.checked[name] ok := f.checked[name]
if !ok { if !ok {
xlsx = checkRow(xlsx) checkRow(&xlsx)
f.checked[name] = true f.checked[name] = true
} }
@ -115,8 +115,8 @@ func (f *File) SetCellInt(sheet, axis string, value int) {
rows := xAxis + 1 rows := xAxis + 1
cell := yAxis + 1 cell := yAxis + 1
xlsx = completeRow(xlsx, rows, cell) completeRow(&xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell) completeCol(&xlsx, rows, cell)
xlsx.SheetData.Row[xAxis].C[yAxis].T = "" xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value) xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value)
@ -137,7 +137,7 @@ func (f *File) SetCellStr(sheet, axis, value string) {
} }
ok := f.checked[name] ok := f.checked[name]
if !ok { if !ok {
xlsx = checkRow(xlsx) checkRow(&xlsx)
f.checked[name] = true f.checked[name] = true
} }
if xlsx.MergeCells != nil { if xlsx.MergeCells != nil {
@ -158,8 +158,8 @@ func (f *File) SetCellStr(sheet, axis, value string) {
rows := xAxis + 1 rows := xAxis + 1
cell := yAxis + 1 cell := yAxis + 1
xlsx = completeRow(xlsx, rows, cell) completeRow(&xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell) completeCol(&xlsx, rows, cell)
xlsx.SheetData.Row[xAxis].C[yAxis].T = "str" xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
xlsx.SheetData.Row[xAxis].C[yAxis].V = value xlsx.SheetData.Row[xAxis].C[yAxis].V = value
@ -180,7 +180,7 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
} }
ok := f.checked[name] ok := f.checked[name]
if !ok { if !ok {
xlsx = checkRow(xlsx) checkRow(&xlsx)
f.checked[name] = true f.checked[name] = true
} }
if xlsx.MergeCells != nil { if xlsx.MergeCells != nil {
@ -198,8 +198,8 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
rows := xAxis + 1 rows := xAxis + 1
cell := yAxis + 1 cell := yAxis + 1
xlsx = completeRow(xlsx, rows, cell) completeRow(&xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell) completeCol(&xlsx, rows, cell)
xlsx.SheetData.Row[xAxis].C[yAxis].T = "" xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
xlsx.SheetData.Row[xAxis].C[yAxis].V = value xlsx.SheetData.Row[xAxis].C[yAxis].V = value
@ -209,7 +209,7 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
} }
// Completion column element tags of XML in a sheet. // Completion column element tags of XML in a sheet.
func completeCol(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet { func completeCol(xlsx *xlsxWorksheet, row int, cell int) {
if len(xlsx.SheetData.Row) < cell { if len(xlsx.SheetData.Row) < cell {
for i := len(xlsx.SheetData.Row); i < cell; i++ { for i := len(xlsx.SheetData.Row); i < cell; i++ {
xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{ xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{
@ -231,11 +231,10 @@ func completeCol(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet {
} }
} }
} }
return xlsx
} }
// Completion row element tags of XML in a sheet. // Completion row element tags of XML in a sheet.
func completeRow(xlsx xlsxWorksheet, row, cell int) xlsxWorksheet { func completeRow(xlsx *xlsxWorksheet, row, cell int) {
currentRows := len(xlsx.SheetData.Row) currentRows := len(xlsx.SheetData.Row)
if currentRows > 1 { if currentRows > 1 {
lastRow := xlsx.SheetData.Row[currentRows-1].R lastRow := xlsx.SheetData.Row[currentRows-1].R
@ -273,7 +272,6 @@ func completeRow(xlsx xlsxWorksheet, row, cell int) xlsxWorksheet {
} }
} }
xlsx.SheetData = sheetData xlsx.SheetData = sheetData
return xlsx
} }
// Replace xl/worksheets/sheet%d.xml XML tags to self-closing for compatible // Replace xl/worksheets/sheet%d.xml XML tags to self-closing for compatible
@ -308,7 +306,7 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
// //
// Noteice: this method could be very slow for large spreadsheets (more than // Noteice: this method could be very slow for large spreadsheets (more than
// 3000 rows one sheet). // 3000 rows one sheet).
func checkRow(xlsx xlsxWorksheet) xlsxWorksheet { func checkRow(xlsx *xlsxWorksheet) {
buffer := bytes.Buffer{} buffer := bytes.Buffer{}
for k, v := range xlsx.SheetData.Row { for k, v := range xlsx.SheetData.Row {
lenCol := len(v.C) lenCol := len(v.C)
@ -337,7 +335,6 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
} }
} }
} }
return xlsx
} }
// UpdateLinkedValue fix linked values within a spreadsheet are not updating in // UpdateLinkedValue fix linked values within a spreadsheet are not updating in

View File

@ -283,12 +283,15 @@ func TestSetBorder(t *testing.T) {
t.Log(err) t.Log(err)
} }
// Test set border with invalid style index number. // Test set border with invalid style index number.
err = xlsx.SetBorder("Sheet1", "J21", "L25", "") err = xlsx.SetBorder("Sheet1", "J21", "L25", `{"border":[{"type":"left","color":"0000FF","style":-1},{"type":"top","color":"00FF00","style":14},{"type":"bottom","color":"FFFF00","style":5},{"type":"right","color":"FF0000","style":6},{"type":"diagonalDown","color":"A020F0","style":9},{"type":"diagonalUp","color":"A020F0","style":8}]}`)
if err != nil {
t.Log(err)
}
if err != nil { if err != nil {
t.Log(err) t.Log(err)
} }
// Test set border on overlapping area. // Test set border on overlapping area.
err = xlsx.SetBorder("Sheet1", "J21", "L25", `{"border":[{"type":"left","color":"0000FF","style":-1},{"type":"top","color":"00FF00","style":14},{"type":"bottom","color":"FFFF00","style":5},{"type":"right","color":"FF0000","style":6},{"type":"diagonalDown","color":"A020F0","style":9},{"type":"diagonalUp","color":"A020F0","style":8}]}`) err = xlsx.SetBorder("Sheet1", "J21", "L25", `{"border":[{"type":"left","color":"0000FF","style":2},{"type":"top","color":"00FF00","style":12},{"type":"bottom","color":"FFFF00","style":5},{"type":"right","color":"FF0000","style":6},{"type":"diagonalDown","color":"A020F0","style":9},{"type":"diagonalUp","color":"A020F0","style":8}]}`)
if err != nil { if err != nil {
t.Log(err) t.Log(err)
} }

View File

@ -112,7 +112,7 @@ func (f *File) SetRowHeight(sheet string, rowIndex int, height float64) {
rows := rowIndex + 1 rows := rowIndex + 1
cells := 0 cells := 0
xlsx = completeRow(xlsx, rows, cells) completeRow(&xlsx, rows, cells)
xlsx.SheetData.Row[rowIndex].Ht = strconv.FormatFloat(height, 'f', -1, 64) xlsx.SheetData.Row[rowIndex].Ht = strconv.FormatFloat(height, 'f', -1, 64)
xlsx.SheetData.Row[rowIndex].CustomHeight = true xlsx.SheetData.Row[rowIndex].CustomHeight = true

View File

@ -204,12 +204,12 @@ func (f *File) setCellStyle(sheet, hcell, vcell string, styleID int) {
} }
ok := f.checked[name] ok := f.checked[name]
if !ok { if !ok {
xlsx = checkRow(xlsx) checkRow(&xlsx)
f.checked[name] = true f.checked[name] = true
} }
xlsx = completeRow(xlsx, vxAxis+1, vyAxis+1) completeRow(&xlsx, vxAxis+1, vyAxis+1)
xlsx = completeCol(xlsx, vxAxis+1, vyAxis+1) completeCol(&xlsx, vxAxis+1, vyAxis+1)
for r, row := range xlsx.SheetData.Row { for r, row := range xlsx.SheetData.Row {
for k, c := range row.C { for k, c := range row.C {

View File

@ -2,17 +2,24 @@ package excelize
import "encoding/xml" import "encoding/xml"
// xlsxTypes directly maps the types elemen of content types for relationship
// parts, it takes a Multipurpose Internet Mail Extension (MIME) media type as a
// value.
type xlsxTypes struct { type xlsxTypes struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/content-types Types"` XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/content-types Types"`
Overrides []xlsxOverride `xml:"Override"` Overrides []xlsxOverride `xml:"Override"`
Defaults []xlsxDefault `xml:"Default"` Defaults []xlsxDefault `xml:"Default"`
} }
// xlsxOverride directly maps the override element in the namespace
// http://schemas.openxmlformats.org/package/2006/content-types
type xlsxOverride struct { type xlsxOverride struct {
PartName string `xml:",attr"` PartName string `xml:",attr"`
ContentType string `xml:",attr"` ContentType string `xml:",attr"`
} }
// xlsxDefault directly maps the default element in the namespace
// http://schemas.openxmlformats.org/package/2006/content-types
type xlsxDefault struct { type xlsxDefault struct {
Extension string `xml:",attr"` Extension string `xml:",attr"`
ContentType string `xml:",attr"` ContentType string `xml:",attr"`

View File

@ -39,10 +39,10 @@ type xlsxPicLocks struct {
NoSelect bool `xml:"noSelect,attr,omitempty"` NoSelect bool `xml:"noSelect,attr,omitempty"`
} }
// xlsxBlip directly maps the blip element in the namespace http://purl.oclc.or // xlsxBlip directly maps the blip element in the namespace
// g/ooxml/officeDoc ument/relationships - This element specifies the existence // http://purl.oclc.org/ooxml/officeDoc ument/relationships - This element
// of an image (binary large image or picture) and contains a reference to the // specifies the existence of an image (binary large image or picture) and
// image data. // contains a reference to the image data.
type xlsxBlip struct { type xlsxBlip struct {
Embed string `xml:"r:embed,attr"` Embed string `xml:"r:embed,attr"`
Cstate string `xml:"cstate,attr,omitempty"` Cstate string `xml:"cstate,attr,omitempty"`

View File

@ -47,9 +47,10 @@ type xlsxLine struct {
// in the namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main - // in the namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much as I need. // currently I have not checked it for completeness - it does as much as I need.
type xlsxColor struct { type xlsxColor struct {
RGB string `xml:"rgb,attr,omitempty"` RGB string `xml:"rgb,attr,omitempty"`
Theme *int `xml:"theme,attr,omitempty"` Indexed *int `xml:"indexed,attr,omitempty"`
Tint float64 `xml:"tint,attr,omitempty"` Theme *int `xml:"theme,attr,omitempty"`
Tint float64 `xml:"tint,attr,omitempty"`
} }
// xlsxFonts directly maps the fonts element. This element contains all font // xlsxFonts directly maps the fonts element. This element contains all font
@ -77,7 +78,20 @@ type xlsxFills struct {
// xlsxFill directly maps the fill element. This element specifies fill // xlsxFill directly maps the fill element. This element specifies fill
// formatting. // formatting.
type xlsxFill struct { type xlsxFill struct {
Fill string `xml:",innerxml"` PatternFill xlsxPatternFill `xml:"patternFill,omitempty"`
}
// xlsxPatternFill directly maps the patternFill element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have
// not checked it for completeness - it does as much as I need. This element is
// used to specify cell fill information for pattern and solid color cell fills.
// For solid cell fills (no pattern), fgColor is used. For cell fills with
// patterns specified, then the cell fill color is specified by the bgColor
// element.
type xlsxPatternFill struct {
PatternType string `xml:"patternType,attr,omitempty"`
FgColor xlsxColor `xml:"fgColor,omitempty"`
BgColor xlsxColor `xml:"bgColor,omitempty"`
} }
// xlsxBorders directly maps the borders element. This element contains borders // xlsxBorders directly maps the borders element. This element contains borders
@ -118,8 +132,8 @@ type xlsxCellStyles struct {
// workbook. // workbook.
type xlsxCellStyle struct { type xlsxCellStyle struct {
XMLName xml.Name `xml:"cellStyle"` XMLName xml.Name `xml:"cellStyle"`
BuiltInID *int `xml:"builtInId,attr,omitempty"` BuiltInID *int `xml:"builtinId,attr,omitempty"`
CustomBuiltIn *bool `xml:"customBuiltIn,attr,omitempty"` CustomBuiltIn *bool `xml:"customBuiltin,attr,omitempty"`
Hidden *bool `xml:"hidden,attr,omitempty"` Hidden *bool `xml:"hidden,attr,omitempty"`
ILevel *bool `xml:"iLevel,attr,omitempty"` ILevel *bool `xml:"iLevel,attr,omitempty"`
Name string `xml:"name,attr"` Name string `xml:"name,attr"`
@ -202,7 +216,7 @@ type xlsxTableStyles struct {
// should format and display a table. // should format and display a table.
type xlsxTableStyle struct { type xlsxTableStyle struct {
Name string `xml:"name,attr,omitempty"` Name string `xml:"name,attr,omitempty"`
Pivot int `xml:"pivot,attr,omitempty"` Pivot int `xml:"pivot,attr"`
Count int `xml:"count,attr,omitempty"` Count int `xml:"count,attr,omitempty"`
Table bool `xml:"table,attr,omitempty"` Table bool `xml:"table,attr,omitempty"`
TableStyleElement string `xml:",innerxml"` TableStyleElement string `xml:",innerxml"`

View File

@ -112,15 +112,17 @@ type xlsxPageMargins struct {
} }
// xlsxSheetFormatPr directly maps the sheetFormatPr element in the namespace // xlsxSheetFormatPr directly maps the sheetFormatPr element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main - currently I have // http://schemas.openxmlformats.org/spreadsheetml/2006/main. This element
// not checked it for completeness - it does as much as I need. // specifies the sheet formatting properties.
type xlsxSheetFormatPr struct { type xlsxSheetFormatPr struct {
BaseColWidth uint8 `xml:"baseColWidth,attr,omitempty"`
CustomHeight float64 `xml:"customHeight,attr,omitempty"`
DefaultColWidth float64 `xml:"defaultColWidth,attr,omitempty"` DefaultColWidth float64 `xml:"defaultColWidth,attr,omitempty"`
DefaultRowHeight float64 `xml:"defaultRowHeight,attr"` DefaultRowHeight float64 `xml:"defaultRowHeight,attr"`
CustomHeight float64 `xml:"customHeight,attr,omitempty"` ThickTop bool `xml:"thickTop,attr,omitempty"`
ZeroHeight float64 `xml:"zeroHeight,attr,omitempty"`
OutlineLevelCol uint8 `xml:"outlineLevelCol,attr,omitempty"` OutlineLevelCol uint8 `xml:"outlineLevelCol,attr,omitempty"`
OutlineLevelRow uint8 `xml:"outlineLevelRow,attr,omitempty"` OutlineLevelRow uint8 `xml:"outlineLevelRow,attr,omitempty"`
ZeroHeight float64 `xml:"zeroHeight,attr,omitempty"`
} }
// xlsxSheetViews directly maps the sheetViews element in the namespace // xlsxSheetViews directly maps the sheetViews element in the namespace