Skip to content

Commit

Permalink
- Complete the element sheetFormatPr struct definition;
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
xuri committed Mar 10, 2017
1 parent 1f73f08 commit 5384756
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 41 deletions.
10 changes: 5 additions & 5 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
}
ok := f.checked[name]
if !ok {
xlsx = checkRow(xlsx)
checkRow(&xlsx)
f.checked[name] = true
}
if xlsx.MergeCells != nil {
Expand All @@ -129,8 +129,8 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
rows := xAxis + 1
cell := yAxis + 1

xlsx = completeRow(xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell)
completeRow(&xlsx, rows, cell)
completeCol(&xlsx, rows, cell)

if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
Expand All @@ -156,7 +156,7 @@ func (f *File) SetCellHyperLink(sheet, axis, link string) {
}
ok := f.checked[name]
if !ok {
xlsx = checkRow(xlsx)
checkRow(&xlsx)
f.checked[name] = true
}
if xlsx.MergeCells != nil {
Expand Down Expand Up @@ -226,7 +226,7 @@ func (f *File) MergeCell(sheet, hcell, vcell string) {
}
ok := f.checked[name]
if !ok {
xlsx = checkRow(xlsx)
checkRow(&xlsx)
f.checked[name] = true
}
if xlsx.MergeCells != nil {
Expand Down
27 changes: 12 additions & 15 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (f *File) SetCellInt(sheet, axis string, value int) {
}
ok := f.checked[name]
if !ok {
xlsx = checkRow(xlsx)
checkRow(&xlsx)
f.checked[name] = true
}

Expand All @@ -115,8 +115,8 @@ func (f *File) SetCellInt(sheet, axis string, value int) {
rows := xAxis + 1
cell := yAxis + 1

xlsx = completeRow(xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell)
completeRow(&xlsx, rows, cell)
completeCol(&xlsx, rows, cell)

xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value)
Expand All @@ -137,7 +137,7 @@ func (f *File) SetCellStr(sheet, axis, value string) {
}
ok := f.checked[name]
if !ok {
xlsx = checkRow(xlsx)
checkRow(&xlsx)
f.checked[name] = true
}
if xlsx.MergeCells != nil {
Expand All @@ -158,8 +158,8 @@ func (f *File) SetCellStr(sheet, axis, value string) {
rows := xAxis + 1
cell := yAxis + 1

xlsx = completeRow(xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell)
completeRow(&xlsx, rows, cell)
completeCol(&xlsx, rows, cell)

xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
xlsx.SheetData.Row[xAxis].C[yAxis].V = value
Expand All @@ -180,7 +180,7 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
}
ok := f.checked[name]
if !ok {
xlsx = checkRow(xlsx)
checkRow(&xlsx)
f.checked[name] = true
}
if xlsx.MergeCells != nil {
Expand All @@ -198,8 +198,8 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
rows := xAxis + 1
cell := yAxis + 1

xlsx = completeRow(xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell)
completeRow(&xlsx, rows, cell)
completeCol(&xlsx, rows, cell)

xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
xlsx.SheetData.Row[xAxis].C[yAxis].V = value
Expand All @@ -209,7 +209,7 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
}

// 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 {
for i := len(xlsx.SheetData.Row); i < cell; i++ {
xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{
Expand All @@ -231,11 +231,10 @@ func completeCol(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet {
}
}
}
return xlsx
}

// 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)
if currentRows > 1 {
lastRow := xlsx.SheetData.Row[currentRows-1].R
Expand Down Expand Up @@ -273,7 +272,6 @@ func completeRow(xlsx xlsxWorksheet, row, cell int) xlsxWorksheet {
}
}
xlsx.SheetData = sheetData
return xlsx
}

// Replace xl/worksheets/sheet%d.xml XML tags to self-closing for compatible
Expand Down Expand Up @@ -308,7 +306,7 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
//
// Noteice: this method could be very slow for large spreadsheets (more than
// 3000 rows one sheet).
func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
func checkRow(xlsx *xlsxWorksheet) {
buffer := bytes.Buffer{}
for k, v := range xlsx.SheetData.Row {
lenCol := len(v.C)
Expand Down Expand Up @@ -337,7 +335,6 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
}
}
}
return xlsx
}

// UpdateLinkedValue fix linked values within a spreadsheet are not updating in
Expand Down
7 changes: 5 additions & 2 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,15 @@ func TestSetBorder(t *testing.T) {
t.Log(err)
}
// 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 {
t.Log(err)
}
// 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 {
t.Log(err)
}
Expand Down
2 changes: 1 addition & 1 deletion rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (f *File) SetRowHeight(sheet string, rowIndex int, height float64) {
rows := rowIndex + 1
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].CustomHeight = true
Expand Down
6 changes: 3 additions & 3 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ func (f *File) setCellStyle(sheet, hcell, vcell string, styleID int) {
}
ok := f.checked[name]
if !ok {
xlsx = checkRow(xlsx)
checkRow(&xlsx)
f.checked[name] = true
}

xlsx = completeRow(xlsx, vxAxis+1, vyAxis+1)
xlsx = completeCol(xlsx, vxAxis+1, vyAxis+1)
completeRow(&xlsx, vxAxis+1, vyAxis+1)
completeCol(&xlsx, vxAxis+1, vyAxis+1)

for r, row := range xlsx.SheetData.Row {
for k, c := range row.C {
Expand Down
7 changes: 7 additions & 0 deletions xmlContentTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ package excelize

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 {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/package/2006/content-types Types"`
Overrides []xlsxOverride `xml:"Override"`
Defaults []xlsxDefault `xml:"Default"`
}

// xlsxOverride directly maps the override element in the namespace
// http://schemas.openxmlformats.org/package/2006/content-types
type xlsxOverride struct {
PartName 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 {
Extension string `xml:",attr"`
ContentType string `xml:",attr"`
Expand Down
8 changes: 4 additions & 4 deletions xmlDrawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ type xlsxPicLocks struct {
NoSelect bool `xml:"noSelect,attr,omitempty"`
}

// xlsxBlip directly maps the blip element in the namespace http://purl.oclc.or
// g/ooxml/officeDoc ument/relationships - This element specifies the existence
// of an image (binary large image or picture) and contains a reference to the
// image data.
// xlsxBlip directly maps the blip element in the namespace
// http://purl.oclc.org/ooxml/officeDoc ument/relationships - This element
// specifies the existence of an image (binary large image or picture) and
// contains a reference to the image data.
type xlsxBlip struct {
Embed string `xml:"r:embed,attr"`
Cstate string `xml:"cstate,attr,omitempty"`
Expand Down
28 changes: 21 additions & 7 deletions xmlStyles.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ type xlsxLine struct {
// 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.
type xlsxColor struct {
RGB string `xml:"rgb,attr,omitempty"`
Theme *int `xml:"theme,attr,omitempty"`
Tint float64 `xml:"tint,attr,omitempty"`
RGB string `xml:"rgb,attr,omitempty"`
Indexed *int `xml:"indexed,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
Expand Down Expand Up @@ -77,7 +78,20 @@ type xlsxFills struct {
// xlsxFill directly maps the fill element. This element specifies fill
// formatting.
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
Expand Down Expand Up @@ -118,8 +132,8 @@ type xlsxCellStyles struct {
// workbook.
type xlsxCellStyle struct {
XMLName xml.Name `xml:"cellStyle"`
BuiltInID *int `xml:"builtInId,attr,omitempty"`
CustomBuiltIn *bool `xml:"customBuiltIn,attr,omitempty"`
BuiltInID *int `xml:"builtinId,attr,omitempty"`
CustomBuiltIn *bool `xml:"customBuiltin,attr,omitempty"`
Hidden *bool `xml:"hidden,attr,omitempty"`
ILevel *bool `xml:"iLevel,attr,omitempty"`
Name string `xml:"name,attr"`
Expand Down Expand Up @@ -202,7 +216,7 @@ type xlsxTableStyles struct {
// should format and display a table.
type xlsxTableStyle struct {
Name string `xml:"name,attr,omitempty"`
Pivot int `xml:"pivot,attr,omitempty"`
Pivot int `xml:"pivot,attr"`
Count int `xml:"count,attr,omitempty"`
Table bool `xml:"table,attr,omitempty"`
TableStyleElement string `xml:",innerxml"`
Expand Down
10 changes: 6 additions & 4 deletions xmlWorksheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,17 @@ type xlsxPageMargins struct {
}

// xlsxSheetFormatPr directly maps the sheetFormatPr 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.
// http://schemas.openxmlformats.org/spreadsheetml/2006/main. This element
// specifies the sheet formatting properties.
type xlsxSheetFormatPr struct {
BaseColWidth uint8 `xml:"baseColWidth,attr,omitempty"`
CustomHeight float64 `xml:"customHeight,attr,omitempty"`
DefaultColWidth float64 `xml:"defaultColWidth,attr,omitempty"`
DefaultRowHeight float64 `xml:"defaultRowHeight,attr"`
CustomHeight float64 `xml:"customHeight,attr,omitempty"`
ZeroHeight float64 `xml:"zeroHeight,attr,omitempty"`
ThickTop bool `xml:"thickTop,attr,omitempty"`
OutlineLevelCol uint8 `xml:"outlineLevelCol,attr,omitempty"`
OutlineLevelRow uint8 `xml:"outlineLevelRow,attr,omitempty"`
ZeroHeight float64 `xml:"zeroHeight,attr,omitempty"`
}

// xlsxSheetViews directly maps the sheetViews element in the namespace
Expand Down

0 comments on commit 5384756

Please sign in to comment.