summaryrefslogtreecommitdiff
path: root/col.go
blob: 7b583c5273521f1247f154c4be65f8704e026a54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)))
}