Skip to content

Commit f05df2a

Browse files
committed
- New function SetSheetName and SetColWidth added, support rename sheet and set column width;
- Add escape characters of sheet name; - Update go test and fix typo
1 parent a99f022 commit f05df2a

10 files changed

+151
-65
lines changed

cell.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (f *File) GetCellValue(sheet string, axis string) string {
1313
var xlsx xlsxWorksheet
1414
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
1515
xAxis := row - 1
16-
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
16+
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
1717
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
1818
rows := len(xlsx.SheetData.Row)
1919
if rows > 1 {
@@ -23,7 +23,7 @@ func (f *File) GetCellValue(sheet string, axis string) string {
2323
}
2424
}
2525
if rows <= xAxis {
26-
return ``
26+
return ""
2727
}
2828
for _, v := range xlsx.SheetData.Row {
2929
if v.R != row {
@@ -38,7 +38,7 @@ func (f *File) GetCellValue(sheet string, axis string) string {
3838
shardStrings := xlsxSST{}
3939
xlsxSI := 0
4040
xlsxSI, _ = strconv.Atoi(r.V)
41-
xml.Unmarshal([]byte(f.readXML(`xl/sharedStrings.xml`)), &shardStrings)
41+
xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
4242
return shardStrings.SI[xlsxSI].T
4343
case "str":
4444
return r.V
@@ -47,7 +47,7 @@ func (f *File) GetCellValue(sheet string, axis string) string {
4747
}
4848
}
4949
}
50-
return ``
50+
return ""
5151
}
5252

5353
// GetCellFormula provide function get formula from cell by given sheet index and axis in XLSX file.
@@ -56,7 +56,7 @@ func (f *File) GetCellFormula(sheet string, axis string) string {
5656
var xlsx xlsxWorksheet
5757
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
5858
xAxis := row - 1
59-
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
59+
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
6060
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
6161
rows := len(xlsx.SheetData.Row)
6262
if rows > 1 {
@@ -66,7 +66,7 @@ func (f *File) GetCellFormula(sheet string, axis string) string {
6666
}
6767
}
6868
if rows <= xAxis {
69-
return ``
69+
return ""
7070
}
7171
for _, v := range xlsx.SheetData.Row {
7272
if v.R != row {
@@ -81,5 +81,5 @@ func (f *File) GetCellFormula(sheet string, axis string) string {
8181
}
8282
}
8383
}
84-
return ``
84+
return ""
8585
}

col.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package excelize
2+
3+
import (
4+
"encoding/xml"
5+
"strings"
6+
)
7+
8+
// SetColWidth provides function to set the width of a single column or multiple columns.
9+
// For example:
10+
//
11+
// xlsx := excelize.CreateFile()
12+
// xlsx.SetColWidth("Sheet1", "A", "H", 20)
13+
// err := xlsx.Save()
14+
// if err != nil {
15+
// fmt.Println(err)
16+
// os.Exit(1)
17+
// }
18+
//
19+
func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) {
20+
min := titleToNumber(strings.ToUpper(startcol)) + 1
21+
max := titleToNumber(strings.ToUpper(endcol)) + 1
22+
if min > max {
23+
min, max = max, min
24+
}
25+
var xlsx xlsxWorksheet
26+
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
27+
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
28+
col := xlsxCol{
29+
Min: min,
30+
Max: max,
31+
Width: width,
32+
CustomWidth: true,
33+
}
34+
if xlsx.Cols != nil {
35+
xlsx.Cols.Col = append(xlsx.Cols.Col, col)
36+
} else {
37+
cols := xlsxCols{}
38+
cols.Col = append(cols.Col, col)
39+
xlsx.Cols = &cols
40+
}
41+
output, _ := xml.Marshal(xlsx)
42+
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
43+
}

excelize.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (f *File) SetCellValue(sheet string, axis string, value interface{}) {
5656
case []byte:
5757
f.SetCellStr(sheet, axis, string(t))
5858
default:
59-
f.SetCellStr(sheet, axis, ``)
59+
f.SetCellStr(sheet, axis, "")
6060
}
6161
}
6262

@@ -296,7 +296,7 @@ func (f *File) UpdateLinkedValue() {
296296
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
297297
for indexR, row := range xlsx.SheetData.Row {
298298
for indexC, col := range row.C {
299-
if col.F != nil && col.V != `` {
299+
if col.F != nil && col.V != "" {
300300
xlsx.SheetData.Row[indexR].C[indexC].V = ""
301301
xlsx.SheetData.Row[indexR].C[indexC].T = ""
302302
}

excelize_test.go

+22-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66
)
77

8-
func TestExcelize(t *testing.T) {
8+
func TestOpenFile(t *testing.T) {
99
// Test update a XLSX file.
1010
f1, err := OpenFile("./test/Workbook1.xlsx")
1111
if err != nil {
@@ -26,7 +26,9 @@ func TestExcelize(t *testing.T) {
2626
f1.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))
2727
f1.SetCellInt("SHEET2", "A1", 100)
2828
f1.SetCellStr("SHEET2", "C11", "Knowns")
29-
f1.NewSheet(3, "Maximum 31 characters allowed in sheet title.")
29+
f1.NewSheet(3, ":\\/?*[]Maximum 31 characters allowed in sheet title.")
30+
// Test set sheet name with illegal name.
31+
f1.SetSheetName("Maximum 31 characters allowed i", "[Rename]:\\/?* Maximum 31 characters allowed in sheet title.")
3032
f1.SetCellInt("Sheet3", "A23", 10)
3133
f1.SetCellStr("SHEET3", "b230", "10")
3234
f1.SetCellStr("SHEET10", "b230", "10")
@@ -122,7 +124,9 @@ func TestExcelize(t *testing.T) {
122124
if err != nil {
123125
t.Log(err)
124126
}
127+
}
125128

129+
func TestCreateFile(t *testing.T) {
126130
// Test create a XLSX file.
127131
f3 := CreateFile()
128132
f3.NewSheet(2, "XLSXSheet2")
@@ -131,7 +135,7 @@ func TestExcelize(t *testing.T) {
131135
f3.SetCellStr("SHEET1", "B20", "42")
132136
f3.SetActiveSheet(0)
133137
// Test add picture to sheet.
134-
err = f3.AddPicture("Sheet1", "H2", "K12", "./test/images/excel.gif")
138+
err := f3.AddPicture("Sheet1", "H2", "K12", "./test/images/excel.gif")
135139
if err != nil {
136140
t.Log(err)
137141
}
@@ -143,7 +147,9 @@ func TestExcelize(t *testing.T) {
143147
if err != nil {
144148
t.Log(err)
145149
}
150+
}
146151

152+
func TestBrokenFile(t *testing.T) {
147153
// Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
148154
f4, err := OpenFile("./test/badWorkbook.xlsx")
149155
f4.SetActiveSheet(2)
@@ -157,3 +163,16 @@ func TestExcelize(t *testing.T) {
157163
t.Log(err)
158164
}
159165
}
166+
167+
func TestSetColWidth(t *testing.T) {
168+
f5, err := OpenFile("./test/Workbook1.xlsx")
169+
if err != nil {
170+
t.Log(err)
171+
}
172+
f5.SetColWidth("sheet1", "B", "A", 12)
173+
f5.SetColWidth("sheet1", "A", "B", 12)
174+
err = f5.Save()
175+
if err != nil {
176+
t.Log(err)
177+
}
178+
}

file.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
// xlsx := CreateFile()
1212
func CreateFile() *File {
1313
file := make(map[string]string)
14-
file[`_rels/.rels`] = templateRels
15-
file[`docProps/app.xml`] = templateDocpropsApp
16-
file[`docProps/core.xml`] = templateDocpropsCore
17-
file[`xl/_rels/workbook.xml.rels`] = templateWorkbookRels
18-
file[`xl/theme/theme1.xml`] = templateTheme
19-
file[`xl/worksheets/sheet1.xml`] = templateSheet
20-
file[`xl/styles.xml`] = templateStyles
21-
file[`xl/workbook.xml`] = templateWorkbook
22-
file[`[Content_Types].xml`] = templateContentTypes
14+
file["_rels/.rels"] = templateRels
15+
file["docProps/app.xml"] = templateDocpropsApp
16+
file["docProps/core.xml"] = templateDocpropsCore
17+
file["xl/_rels/workbook.xml.rels"] = templateWorkbookRels
18+
file["xl/theme/theme1.xml"] = templateTheme
19+
file["xl/worksheets/sheet1.xml"] = templateSheet
20+
file["xl/styles.xml"] = templateStyles
21+
file["xl/workbook.xml"] = templateWorkbook
22+
file["[Content_Types].xml"] = templateContentTypes
2323
return &File{
2424
XLSX: file,
2525
}

lib.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
2525
for _, v := range r.File {
2626
fileList[v.Name] = readFile(v)
2727
if len(v.Name) > 18 {
28-
if v.Name[0:19] == `xl/worksheets/sheet` {
28+
if v.Name[0:19] == "xl/worksheets/sheet" {
2929
var xlsx xlsxWorksheet
3030
xml.Unmarshal([]byte(fileList[v.Name]), &xlsx)
3131
xlsx = checkRow(xlsx)

rows.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
func (f *File) GetRows(sheet string) [][]string {
2020
xlsx := xlsxWorksheet{}
2121
r := [][]string{}
22-
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
22+
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
2323
err := xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
2424
if err != nil {
2525
return r
@@ -39,7 +39,7 @@ func (f *File) GetRows(sheet string) [][]string {
3939
// readXMLSST read xmlSST simple function.
4040
func readXMLSST(f *File) (xlsxSST, error) {
4141
shardStrings := xlsxSST{}
42-
err := xml.Unmarshal([]byte(f.readXML(`xl/sharedStrings.xml`)), &shardStrings)
42+
err := xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
4343
return shardStrings, err
4444
}
4545

0 commit comments

Comments
 (0)