2016-10-31 19:13:22 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2016-11-02 12:58:51 +08:00
|
|
|
"strconv"
|
2016-10-31 19:13:22 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-04-25 18:43:10 +08:00
|
|
|
// GetRows return all the rows in a sheet by given "sheet" + index. For now you
|
|
|
|
// should use sheet_name like "sheet3" where "sheet" is a constant part and "3"
|
|
|
|
// is a sheet number. For example, if sheet named as "SomeUniqueData" and it is
|
|
|
|
// second if spreadsheet program interface - you should use "sheet2" here. For
|
|
|
|
// example:
|
2016-11-02 12:58:51 +08:00
|
|
|
//
|
2017-04-23 00:10:23 +08:00
|
|
|
// index := xlsx.GetSheetIndex("Sheet2")
|
|
|
|
// rows := xlsx.GetRows("sheet" + strconv.Itoa(index))
|
2016-11-02 12:58:51 +08:00
|
|
|
// for _, row := range rows {
|
|
|
|
// for _, colCell := range row {
|
|
|
|
// fmt.Print(colCell, "\t")
|
|
|
|
// }
|
|
|
|
// fmt.Println()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
func (f *File) GetRows(sheet string) [][]string {
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-02-12 19:03:24 +08:00
|
|
|
rows := [][]string{}
|
2017-01-18 14:47:23 +08:00
|
|
|
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
|
2017-03-12 20:38:26 +08:00
|
|
|
if xlsx != nil {
|
|
|
|
output, _ := xml.Marshal(f.Sheet[name])
|
|
|
|
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
|
2016-10-31 19:13:22 +08:00
|
|
|
}
|
2017-03-12 20:38:26 +08:00
|
|
|
decoder := xml.NewDecoder(strings.NewReader(f.readXML(name)))
|
|
|
|
d, _ := readXMLSST(f)
|
2017-02-12 19:03:24 +08:00
|
|
|
var inElement string
|
2017-02-17 17:41:11 +08:00
|
|
|
var r xlsxRow
|
2017-02-12 19:03:24 +08:00
|
|
|
var row []string
|
2017-02-17 17:41:11 +08:00
|
|
|
tr, tc := f.getTotalRowsCols(sheet)
|
|
|
|
for i := 0; i < tr; i++ {
|
|
|
|
row = []string{}
|
|
|
|
for j := 0; j <= tc; j++ {
|
|
|
|
row = append(row, "")
|
|
|
|
}
|
|
|
|
rows = append(rows, row)
|
|
|
|
}
|
|
|
|
decoder = xml.NewDecoder(strings.NewReader(f.readXML(name)))
|
2017-02-12 19:03:24 +08:00
|
|
|
for {
|
|
|
|
token, _ := decoder.Token()
|
|
|
|
if token == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch startElement := token.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
inElement = startElement.Name.Local
|
|
|
|
if inElement == "row" {
|
2017-02-17 17:41:11 +08:00
|
|
|
r = xlsxRow{}
|
2017-02-12 19:03:24 +08:00
|
|
|
decoder.DecodeElement(&r, &startElement)
|
2017-02-17 17:41:11 +08:00
|
|
|
cr := r.R - 1
|
2017-02-12 19:03:24 +08:00
|
|
|
for _, colCell := range r.C {
|
2017-06-27 17:53:06 +08:00
|
|
|
c := TitleToNumber(strings.Map(letterOnlyMapF, colCell.R))
|
2017-02-12 19:03:24 +08:00
|
|
|
val, _ := colCell.getValueFrom(f, d)
|
2017-02-17 17:41:11 +08:00
|
|
|
rows[cr][c] = val
|
2017-02-12 19:03:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2016-11-02 12:58:51 +08:00
|
|
|
}
|
|
|
|
}
|
2017-02-12 19:03:24 +08:00
|
|
|
return rows
|
2016-10-31 19:13:22 +08:00
|
|
|
}
|
|
|
|
|
2017-02-17 17:41:11 +08:00
|
|
|
// getTotalRowsCols provides a function to get total columns and rows in a
|
|
|
|
// sheet.
|
|
|
|
func (f *File) getTotalRowsCols(sheet string) (int, int) {
|
|
|
|
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
|
|
|
|
decoder := xml.NewDecoder(strings.NewReader(f.readXML(name)))
|
|
|
|
var inElement string
|
|
|
|
var r xlsxRow
|
|
|
|
var tr, tc int
|
|
|
|
for {
|
|
|
|
token, _ := decoder.Token()
|
|
|
|
if token == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch startElement := token.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
inElement = startElement.Name.Local
|
|
|
|
if inElement == "row" {
|
|
|
|
r = xlsxRow{}
|
|
|
|
decoder.DecodeElement(&r, &startElement)
|
|
|
|
tr = r.R
|
|
|
|
for _, colCell := range r.C {
|
2017-06-27 17:53:06 +08:00
|
|
|
col := TitleToNumber(strings.Map(letterOnlyMapF, colCell.R))
|
2017-02-17 17:41:11 +08:00
|
|
|
if col > tc {
|
|
|
|
tc = col
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tr, tc
|
|
|
|
}
|
|
|
|
|
2017-02-02 10:59:31 +08:00
|
|
|
// SetRowHeight provides a function to set the height of a single row.
|
|
|
|
// For example:
|
|
|
|
//
|
2017-06-28 17:03:20 +08:00
|
|
|
// xlsx := excelize.NewFile()
|
2017-02-02 10:59:31 +08:00
|
|
|
// xlsx.SetRowHeight("Sheet1", 0, 50)
|
|
|
|
// err := xlsx.Save()
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Println(err)
|
|
|
|
// os.Exit(1)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
func (f *File) SetRowHeight(sheet string, rowIndex int, height float64) {
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-02-02 10:59:31 +08:00
|
|
|
rows := rowIndex + 1
|
|
|
|
cells := 0
|
2017-03-12 20:38:26 +08:00
|
|
|
completeRow(xlsx, rows, cells)
|
2017-02-02 10:59:31 +08:00
|
|
|
xlsx.SheetData.Row[rowIndex].Ht = strconv.FormatFloat(height, 'f', -1, 64)
|
|
|
|
xlsx.SheetData.Row[rowIndex].CustomHeight = true
|
|
|
|
}
|
|
|
|
|
2016-11-02 12:58:51 +08:00
|
|
|
// readXMLSST read xmlSST simple function.
|
2017-02-12 19:03:24 +08:00
|
|
|
func readXMLSST(f *File) (*xlsxSST, error) {
|
2016-10-31 19:13:22 +08:00
|
|
|
shardStrings := xlsxSST{}
|
2017-01-18 14:47:23 +08:00
|
|
|
err := xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
|
2017-02-12 19:03:24 +08:00
|
|
|
return &shardStrings, err
|
2016-10-31 19:13:22 +08:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:05:01 +08:00
|
|
|
// getValueFrom return a value from a column/row cell, this function is inteded
|
|
|
|
// to be used with for range on rows an argument with the xlsx opened file.
|
2017-02-12 19:03:24 +08:00
|
|
|
func (xlsx *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) {
|
2016-11-02 12:58:51 +08:00
|
|
|
switch xlsx.T {
|
|
|
|
case "s":
|
|
|
|
xlsxSI := 0
|
|
|
|
xlsxSI, _ = strconv.Atoi(xlsx.V)
|
2017-02-07 14:03:03 +08:00
|
|
|
if len(d.SI[xlsxSI].R) > 0 {
|
|
|
|
value := ""
|
|
|
|
for _, v := range d.SI[xlsxSI].R {
|
|
|
|
value += v.T
|
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|
2017-05-05 14:40:28 +08:00
|
|
|
return f.formattedValue(xlsx.S, d.SI[xlsxSI].T), nil
|
2016-11-02 12:58:51 +08:00
|
|
|
case "str":
|
2017-05-05 14:40:28 +08:00
|
|
|
return f.formattedValue(xlsx.S, xlsx.V), nil
|
2016-11-02 12:58:51 +08:00
|
|
|
default:
|
2017-05-05 14:40:28 +08:00
|
|
|
return f.formattedValue(xlsx.S, xlsx.V), nil
|
2016-11-02 12:58:51 +08:00
|
|
|
}
|
2016-10-31 19:13:22 +08:00
|
|
|
}
|
2017-06-08 11:11:11 +08:00
|
|
|
|
2017-06-14 15:01:49 +08:00
|
|
|
// SetRowVisible provides a function to set visible of a single row by given
|
|
|
|
// worksheet index and row index. For example, hide row 3 in Sheet1:
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
2017-06-14 15:01:49 +08:00
|
|
|
// xlsx.SetRowVisible("Sheet1", 2, false)
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
2017-06-14 15:01:49 +08:00
|
|
|
func (f *File) SetRowVisible(sheet string, rowIndex int, visible bool) {
|
2017-06-08 11:11:11 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
|
|
|
rows := rowIndex + 1
|
|
|
|
cells := 0
|
|
|
|
completeRow(xlsx, rows, cells)
|
2017-06-14 15:01:49 +08:00
|
|
|
if visible {
|
|
|
|
xlsx.SheetData.Row[rowIndex].Hidden = false
|
|
|
|
return
|
|
|
|
}
|
2017-06-08 11:11:11 +08:00
|
|
|
xlsx.SheetData.Row[rowIndex].Hidden = true
|
|
|
|
}
|
|
|
|
|
2017-06-14 15:01:49 +08:00
|
|
|
// GetRowVisible provides a function to get visible of a single row by given
|
|
|
|
// worksheet index and row index. For example, get visible state of row 3 in
|
|
|
|
// Sheet1:
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
2017-06-14 15:01:49 +08:00
|
|
|
// xlsx.GetRowVisible("Sheet1", 2)
|
2017-06-08 11:11:11 +08:00
|
|
|
//
|
2017-06-14 15:01:49 +08:00
|
|
|
func (f *File) GetRowVisible(sheet string, rowIndex int) bool {
|
2017-06-08 11:11:11 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
|
|
|
rows := rowIndex + 1
|
|
|
|
cells := 0
|
|
|
|
completeRow(xlsx, rows, cells)
|
2017-06-14 15:01:49 +08:00
|
|
|
return !xlsx.SheetData.Row[rowIndex].Hidden
|
2017-06-08 11:11:11 +08:00
|
|
|
}
|