Skip to content

Commit

Permalink
- New function SetSheetName and SetColWidth added, support rename…
Browse files Browse the repository at this point in the history
… sheet and set column width;

- Add escape characters of sheet name;
- Update go test and fix typo
  • Loading branch information
xuri committed Jan 18, 2017
1 parent a99f022 commit f05df2a
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 65 deletions.
14 changes: 7 additions & 7 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (f *File) GetCellValue(sheet string, axis string) string {
var xlsx xlsxWorksheet
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
rows := len(xlsx.SheetData.Row)
if rows > 1 {
Expand All @@ -23,7 +23,7 @@ func (f *File) GetCellValue(sheet string, axis string) string {
}
}
if rows <= xAxis {
return ``
return ""
}
for _, v := range xlsx.SheetData.Row {
if v.R != row {
Expand All @@ -38,7 +38,7 @@ func (f *File) GetCellValue(sheet string, axis string) string {
shardStrings := xlsxSST{}
xlsxSI := 0
xlsxSI, _ = strconv.Atoi(r.V)
xml.Unmarshal([]byte(f.readXML(`xl/sharedStrings.xml`)), &shardStrings)
xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
return shardStrings.SI[xlsxSI].T
case "str":
return r.V
Expand All @@ -47,7 +47,7 @@ func (f *File) GetCellValue(sheet string, axis string) string {
}
}
}
return ``
return ""
}

// GetCellFormula provide function get formula from cell by given sheet index and axis in XLSX file.
Expand All @@ -56,7 +56,7 @@ func (f *File) GetCellFormula(sheet string, axis string) string {
var xlsx xlsxWorksheet
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
rows := len(xlsx.SheetData.Row)
if rows > 1 {
Expand All @@ -66,7 +66,7 @@ func (f *File) GetCellFormula(sheet string, axis string) string {
}
}
if rows <= xAxis {
return ``
return ""
}
for _, v := range xlsx.SheetData.Row {
if v.R != row {
Expand All @@ -81,5 +81,5 @@ func (f *File) GetCellFormula(sheet string, axis string) string {
}
}
}
return ``
return ""
}
43 changes: 43 additions & 0 deletions col.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package excelize

import (
"encoding/xml"
"strings"
)

// SetColWidth provides function to set the width of a single column or multiple columns.
// For example:
//
// xlsx := excelize.CreateFile()
// xlsx.SetColWidth("Sheet1", "A", "H", 20)
// err := xlsx.Save()
// if err != nil {
// fmt.Println(err)
// os.Exit(1)
// }
//
func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) {
min := titleToNumber(strings.ToUpper(startcol)) + 1
max := titleToNumber(strings.ToUpper(endcol)) + 1
if min > max {
min, max = max, min
}
var xlsx xlsxWorksheet
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
col := xlsxCol{
Min: min,
Max: max,
Width: width,
CustomWidth: true,
}
if xlsx.Cols != nil {
xlsx.Cols.Col = append(xlsx.Cols.Col, col)
} else {
cols := xlsxCols{}
cols.Col = append(cols.Col, col)
xlsx.Cols = &cols
}
output, _ := xml.Marshal(xlsx)
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
}
4 changes: 2 additions & 2 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (f *File) SetCellValue(sheet string, axis string, value interface{}) {
case []byte:
f.SetCellStr(sheet, axis, string(t))
default:
f.SetCellStr(sheet, axis, ``)
f.SetCellStr(sheet, axis, "")
}
}

Expand Down Expand Up @@ -296,7 +296,7 @@ func (f *File) UpdateLinkedValue() {
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
for indexR, row := range xlsx.SheetData.Row {
for indexC, col := range row.C {
if col.F != nil && col.V != `` {
if col.F != nil && col.V != "" {
xlsx.SheetData.Row[indexR].C[indexC].V = ""
xlsx.SheetData.Row[indexR].C[indexC].T = ""
}
Expand Down
25 changes: 22 additions & 3 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestExcelize(t *testing.T) {
func TestOpenFile(t *testing.T) {
// Test update a XLSX file.
f1, err := OpenFile("./test/Workbook1.xlsx")
if err != nil {
Expand All @@ -26,7 +26,9 @@ func TestExcelize(t *testing.T) {
f1.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))
f1.SetCellInt("SHEET2", "A1", 100)
f1.SetCellStr("SHEET2", "C11", "Knowns")
f1.NewSheet(3, "Maximum 31 characters allowed in sheet title.")
f1.NewSheet(3, ":\\/?*[]Maximum 31 characters allowed in sheet title.")
// Test set sheet name with illegal name.
f1.SetSheetName("Maximum 31 characters allowed i", "[Rename]:\\/?* Maximum 31 characters allowed in sheet title.")
f1.SetCellInt("Sheet3", "A23", 10)
f1.SetCellStr("SHEET3", "b230", "10")
f1.SetCellStr("SHEET10", "b230", "10")
Expand Down Expand Up @@ -122,7 +124,9 @@ func TestExcelize(t *testing.T) {
if err != nil {
t.Log(err)
}
}

func TestCreateFile(t *testing.T) {
// Test create a XLSX file.
f3 := CreateFile()
f3.NewSheet(2, "XLSXSheet2")
Expand All @@ -131,7 +135,7 @@ func TestExcelize(t *testing.T) {
f3.SetCellStr("SHEET1", "B20", "42")
f3.SetActiveSheet(0)
// Test add picture to sheet.
err = f3.AddPicture("Sheet1", "H2", "K12", "./test/images/excel.gif")
err := f3.AddPicture("Sheet1", "H2", "K12", "./test/images/excel.gif")
if err != nil {
t.Log(err)
}
Expand All @@ -143,7 +147,9 @@ func TestExcelize(t *testing.T) {
if err != nil {
t.Log(err)
}
}

func TestBrokenFile(t *testing.T) {
// Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
f4, err := OpenFile("./test/badWorkbook.xlsx")
f4.SetActiveSheet(2)
Expand All @@ -157,3 +163,16 @@ func TestExcelize(t *testing.T) {
t.Log(err)
}
}

func TestSetColWidth(t *testing.T) {
f5, err := OpenFile("./test/Workbook1.xlsx")
if err != nil {
t.Log(err)
}
f5.SetColWidth("sheet1", "B", "A", 12)
f5.SetColWidth("sheet1", "A", "B", 12)
err = f5.Save()
if err != nil {
t.Log(err)
}
}
18 changes: 9 additions & 9 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
// xlsx := CreateFile()
func CreateFile() *File {
file := make(map[string]string)
file[`_rels/.rels`] = templateRels
file[`docProps/app.xml`] = templateDocpropsApp
file[`docProps/core.xml`] = templateDocpropsCore
file[`xl/_rels/workbook.xml.rels`] = templateWorkbookRels
file[`xl/theme/theme1.xml`] = templateTheme
file[`xl/worksheets/sheet1.xml`] = templateSheet
file[`xl/styles.xml`] = templateStyles
file[`xl/workbook.xml`] = templateWorkbook
file[`[Content_Types].xml`] = templateContentTypes
file["_rels/.rels"] = templateRels
file["docProps/app.xml"] = templateDocpropsApp
file["docProps/core.xml"] = templateDocpropsCore
file["xl/_rels/workbook.xml.rels"] = templateWorkbookRels
file["xl/theme/theme1.xml"] = templateTheme
file["xl/worksheets/sheet1.xml"] = templateSheet
file["xl/styles.xml"] = templateStyles
file["xl/workbook.xml"] = templateWorkbook
file["[Content_Types].xml"] = templateContentTypes
return &File{
XLSX: file,
}
Expand Down
2 changes: 1 addition & 1 deletion lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
for _, v := range r.File {
fileList[v.Name] = readFile(v)
if len(v.Name) > 18 {
if v.Name[0:19] == `xl/worksheets/sheet` {
if v.Name[0:19] == "xl/worksheets/sheet" {
var xlsx xlsxWorksheet
xml.Unmarshal([]byte(fileList[v.Name]), &xlsx)
xlsx = checkRow(xlsx)
Expand Down
4 changes: 2 additions & 2 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func (f *File) GetRows(sheet string) [][]string {
xlsx := xlsxWorksheet{}
r := [][]string{}
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
err := xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
if err != nil {
return r
Expand All @@ -39,7 +39,7 @@ func (f *File) GetRows(sheet string) [][]string {
// readXMLSST read xmlSST simple function.
func readXMLSST(f *File) (xlsxSST, error) {
shardStrings := xlsxSST{}
err := xml.Unmarshal([]byte(f.readXML(`xl/sharedStrings.xml`)), &shardStrings)
err := xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
return shardStrings, err
}

Expand Down
Loading

0 comments on commit f05df2a

Please sign in to comment.