cell.go 964 字节
Newer Older
xurime's avatar
xurime 已提交
1 2 3 4 5 6 7 8
package excelize

import (
	"encoding/xml"
	"strconv"
	"strings"
)

xurime's avatar
xurime 已提交
9
// GetCellValue provide function get value from cell by given sheet index and axis in XLSX file.
10
func (f *File) GetCellValue(sheet string, axis string) string {
xurime's avatar
xurime 已提交
11 12
	axis = strings.ToUpper(axis)
	var xlsx xlsxWorksheet
13
	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xurime's avatar
xurime 已提交
14 15
	xAxis := row - 1
	name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
16
	xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
xurime's avatar
xurime 已提交
17 18 19 20 21 22 23 24 25 26 27 28
	rows := len(xlsx.SheetData.Row)
	if rows <= xAxis {
		return ``
	}
	for _, v := range xlsx.SheetData.Row[xAxis].C {
		if xlsx.SheetData.Row[xAxis].R == row {
			if axis == v.R {
				switch v.T {
				case "s":
					shardStrings := xlsxSST{}
					xlsxSI := 0
					xlsxSI, _ = strconv.Atoi(v.V)
29
					xml.Unmarshal([]byte(f.readXML(`xl/sharedStrings.xml`)), &shardStrings)
xurime's avatar
xurime 已提交
30 31 32 33 34 35 36 37 38 39 40
					return shardStrings.SI[xlsxSI].T
				case "str":
					return v.V
				default:
					return v.V
				}
			}
		}
	}
	return ``
}