cell.go 7.1 KB
Newer Older
xurime's avatar
xurime 已提交
1 2 3 4 5 6 7
package excelize

import (
	"strconv"
	"strings"
)

xurime's avatar
xurime 已提交
8 9
// mergeCellsParser provides function to check merged cells in worksheet by
// given axis.
10 11
func (f *File) mergeCellsParser(xlsx *xlsxWorksheet, axis string) string {
	axis = strings.ToUpper(axis)
12 13 14 15 16 17 18
	if xlsx.MergeCells != nil {
		for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
			if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
				axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
			}
		}
	}
19
	return axis
xurime's avatar
xurime 已提交
20 21 22 23 24 25 26 27
}

// GetCellValue provides function to get formatted value from cell by given
// sheet index and axis in XLSX file. If it is possible to apply a format to the
// cell value, it will do so, if not then an error will be returned, along with
// the raw value of the cell.
func (f *File) GetCellValue(sheet, axis string) string {
	xlsx := f.workSheetReader(sheet)
28
	axis = f.mergeCellsParser(xlsx, axis)
29 30
	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	xAxis := row - 1
xurime's avatar
xurime 已提交
31
	rows := len(xlsx.SheetData.Row)
xurime's avatar
xurime 已提交
32 33 34 35 36 37
	if rows > 1 {
		lastRow := xlsx.SheetData.Row[rows-1].R
		if lastRow >= rows {
			rows = lastRow
		}
	}
38
	if rows < xAxis {
39
		return ""
xurime's avatar
xurime 已提交
40 41 42 43 44 45 46 47 48 49 50
	}
	for _, v := range xlsx.SheetData.Row {
		if v.R != row {
			continue
		}
		for _, r := range v.C {
			if axis != r.R {
				continue
			}
			switch r.T {
			case "s":
51
				shardStrings := f.sharedStringsReader()
xurime's avatar
xurime 已提交
52 53
				xlsxSI := 0
				xlsxSI, _ = strconv.Atoi(r.V)
54
				return f.formattedValue(r.S, shardStrings.SI[xlsxSI].T)
xurime's avatar
xurime 已提交
55
			case "str":
56
				return f.formattedValue(r.S, r.V)
xurime's avatar
xurime 已提交
57
			default:
58
				return f.formattedValue(r.S, r.V)
xurime's avatar
xurime 已提交
59 60 61
			}
		}
	}
62
	return ""
xurime's avatar
xurime 已提交
63 64
}

65 66 67 68 69 70 71
// formattedValue provides function to returns a value after formatted. If it is
// possible to apply a format to the cell value, it will do so, if not then an
// error will be returned, along with the raw value of the cell.
func (f *File) formattedValue(s int, v string) string {
	if s == 0 {
		return v
	}
72
	styleSheet := f.stylesReader()
73 74 75 76 77
	ok := builtInNumFmtFunc[styleSheet.CellXfs.Xf[s].NumFmtID]
	if ok != nil {
		return ok(styleSheet.CellXfs.Xf[s].NumFmtID, v)
	}
	return v
78 79
}

80 81
// GetCellFormula provides function to get formula from cell by given sheet
// index and axis in XLSX file.
82
func (f *File) GetCellFormula(sheet, axis string) string {
xurime's avatar
xurime 已提交
83
	xlsx := f.workSheetReader(sheet)
84
	axis = f.mergeCellsParser(xlsx, axis)
85 86
	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	xAxis := row - 1
xurime's avatar
xurime 已提交
87 88 89 90 91 92 93
	rows := len(xlsx.SheetData.Row)
	if rows > 1 {
		lastRow := xlsx.SheetData.Row[rows-1].R
		if lastRow >= rows {
			rows = lastRow
		}
	}
94
	if rows < xAxis {
95
		return ""
xurime's avatar
xurime 已提交
96
	}
xurime's avatar
xurime 已提交
97 98 99 100 101 102 103 104 105 106
	for _, v := range xlsx.SheetData.Row {
		if v.R != row {
			continue
		}
		for _, f := range v.C {
			if axis != f.R {
				continue
			}
			if f.F != nil {
				return f.F.Content
xurime's avatar
xurime 已提交
107 108 109
			}
		}
	}
110
	return ""
xurime's avatar
xurime 已提交
111
}
112 113 114 115

// SetCellFormula provides function to set cell formula by given string and
// sheet index.
func (f *File) SetCellFormula(sheet, axis, formula string) {
xurime's avatar
xurime 已提交
116
	xlsx := f.workSheetReader(sheet)
117
	axis = f.mergeCellsParser(xlsx, axis)
118 119 120
	col := string(strings.Map(letterOnlyMapF, axis))
	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	xAxis := row - 1
121
	yAxis := TitleToNumber(col)
122 123 124 125

	rows := xAxis + 1
	cell := yAxis + 1

xurime's avatar
xurime 已提交
126 127
	completeRow(xlsx, rows, cell)
	completeCol(xlsx, rows, cell)
128 129 130 131 132 133 134 135 136 137

	if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
		xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
	} else {
		f := xlsxF{
			Content: formula,
		}
		xlsx.SheetData.Row[xAxis].C[yAxis].F = &f
	}
}
138 139 140 141

// SetCellHyperLink provides function to set cell hyperlink by given sheet index
// and link URL address. Only support external link currently.
func (f *File) SetCellHyperLink(sheet, axis, link string) {
xurime's avatar
xurime 已提交
142
	xlsx := f.workSheetReader(sheet)
143
	axis = f.mergeCellsParser(xlsx, axis)
144 145 146 147 148 149 150 151 152 153 154 155 156
	rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
	hyperlink := xlsxHyperlink{
		Ref: axis,
		RID: "rId" + strconv.Itoa(rID),
	}
	if xlsx.Hyperlinks != nil {
		xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
	} else {
		hyperlinks := xlsxHyperlinks{}
		hyperlinks.Hyperlink = append(hyperlinks.Hyperlink, hyperlink)
		xlsx.Hyperlinks = &hyperlinks
	}
}
xurime's avatar
xurime 已提交
157

158 159
// MergeCell provides function to merge cells by given coordinate area and sheet
// name. For example create a merged cell of D3:E9 on Sheet1:
xurime's avatar
xurime 已提交
160
//
161
//    xlsx.MergeCell("sheet1", "D3", "E9")
xurime's avatar
xurime 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
//
// If you create a merged cell that overlaps with another existing merged cell,
// those merged cells that already exist will be removed.
func (f *File) MergeCell(sheet, hcell, vcell string) {
	if hcell == vcell {
		return
	}

	hcell = strings.ToUpper(hcell)
	vcell = strings.ToUpper(vcell)

	// Coordinate conversion, convert C1:B3 to 2,0,1,2.
	hcol := string(strings.Map(letterOnlyMapF, hcell))
	hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
	hyAxis := hrow - 1
177
	hxAxis := TitleToNumber(hcol)
xurime's avatar
xurime 已提交
178 179 180 181

	vcol := string(strings.Map(letterOnlyMapF, vcell))
	vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
	vyAxis := vrow - 1
182
	vxAxis := TitleToNumber(vcol)
xurime's avatar
xurime 已提交
183 184 185 186 187 188 189 190 191 192 193

	if vxAxis < hxAxis {
		hcell, vcell = vcell, hcell
		vxAxis, hxAxis = hxAxis, vxAxis
	}

	if vyAxis < hyAxis {
		hcell, vcell = vcell, hcell
		vyAxis, hyAxis = hyAxis, vyAxis
	}

xurime's avatar
xurime 已提交
194
	xlsx := f.workSheetReader(sheet)
xurime's avatar
xurime 已提交
195 196 197
	if xlsx.MergeCells != nil {
		mergeCell := xlsxMergeCell{}
		// Correct the coordinate area, such correct C1:B3 to B1:C3.
198
		mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
xurime's avatar
xurime 已提交
199 200 201 202 203 204 205 206 207 208 209 210
		// Delete the merged cells of the overlapping area.
		for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
			if checkCellInArea(hcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0], mergeCell.Ref) {
				xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
			} else if checkCellInArea(vcell, xlsx.MergeCells.Cells[i].Ref) || checkCellInArea(strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[1], mergeCell.Ref) {
				xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells[:i], xlsx.MergeCells.Cells[i+1:]...)
			}
		}
		xlsx.MergeCells.Cells = append(xlsx.MergeCells.Cells, &mergeCell)
	} else {
		mergeCell := xlsxMergeCell{}
		// Correct the coordinate area, such correct C1:B3 to B1:C3.
211
		mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
xurime's avatar
xurime 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225
		mergeCells := xlsxMergeCells{}
		mergeCells.Cells = append(mergeCells.Cells, &mergeCell)
		xlsx.MergeCells = &mergeCells
	}
}

// checkCellInArea provides function to determine if a given coordinate is
// within an area.
func checkCellInArea(cell, area string) bool {
	result := false
	cell = strings.ToUpper(cell)
	col := string(strings.Map(letterOnlyMapF, cell))
	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
	xAxis := row - 1
226
	yAxis := TitleToNumber(col)
xurime's avatar
xurime 已提交
227 228 229 230 231

	ref := strings.Split(area, ":")
	hCol := string(strings.Map(letterOnlyMapF, ref[0]))
	hRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[0]))
	hyAxis := hRow - 1
232
	hxAxis := TitleToNumber(hCol)
xurime's avatar
xurime 已提交
233 234 235 236

	vCol := string(strings.Map(letterOnlyMapF, ref[1]))
	vRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, ref[1]))
	vyAxis := vRow - 1
237
	vxAxis := TitleToNumber(vCol)
xurime's avatar
xurime 已提交
238 239 240 241 242 243 244

	if hxAxis <= yAxis && yAxis <= vxAxis && hyAxis <= xAxis && xAxis <= vyAxis {
		result = true
	}

	return result
}