summaryrefslogtreecommitdiff
path: root/col.go
diff options
context:
space:
mode:
authorRi Xu <xuri.me@gmail.com>2017-01-18 14:47:23 +0800
committerRi Xu <xuri.me@gmail.com>2017-01-18 14:47:23 +0800
commitf05df2a0182ee5761f5fbe7e56020313a0ab0b61 (patch)
tree7aaf5365c9082931f9d4cdd080ff2932918164fb /col.go
parenta99f0227b085d8f417f77864941650ef0e6273e4 (diff)
- 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
Diffstat (limited to 'col.go')
-rw-r--r--col.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/col.go b/col.go
new file mode 100644
index 0000000..dbe004d
--- /dev/null
+++ b/col.go
@@ -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)))
+}