cell.go 18.0 KB
Newer Older
xurime's avatar
xurime 已提交
1 2 3 4 5 6 7 8
// Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
// Package excelize providing a set of functions that allow you to write to
// and read from XLSX files. Support reads and writes XLSX file generated by
// Microsoft Excel™ 2007 and later. Support save file without losing original
// charts of XLSX. This library needs Go version 1.8 or later.
xurime's avatar
xurime 已提交
9

xurime's avatar
xurime 已提交
10 11 12
package excelize

import (
13 14
	"encoding/xml"
	"fmt"
15
	"reflect"
xurime's avatar
xurime 已提交
16 17
	"strconv"
	"strings"
18
	"time"
xurime's avatar
xurime 已提交
19 20
)

21 22 23 24 25 26 27 28 29 30 31
const (
	// STCellFormulaTypeArray defined the formula is an array formula.
	STCellFormulaTypeArray = "array"
	// STCellFormulaTypeDataTable defined the formula is a data table formula.
	STCellFormulaTypeDataTable = "dataTable"
	// STCellFormulaTypeNormal defined the formula is a regular cell formula.
	STCellFormulaTypeNormal = "normal"
	// STCellFormulaTypeShared defined the formula is part of a shared formula.
	STCellFormulaTypeShared = "shared"
)

xurime's avatar
xurime 已提交
32
// mergeCellsParser provides a function to check merged cells in worksheet by
xurime's avatar
xurime 已提交
33
// given axis.
34 35
func (f *File) mergeCellsParser(xlsx *xlsxWorksheet, axis string) string {
	axis = strings.ToUpper(axis)
36 37 38 39 40 41 42
	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]
			}
		}
	}
43
	return axis
xurime's avatar
xurime 已提交
44 45
}

xurime's avatar
xurime 已提交
46 47
// SetCellValue provides a function to set value of a cell. The following
// shows the supported data types:
48 49 50 51 52 53
//
//    int
//    int8
//    int16
//    int32
//    int64
54 55 56 57 58
//    uint
//    uint8
//    uint16
//    uint32
//    uint64
59 60 61 62
//    float32
//    float64
//    string
//    []byte
63
//    time.Duration
64
//    time.Time
65
//    bool
66 67 68 69 70 71
//    nil
//
// Note that default date format is m/d/yy h:mm of time.Time type value. You can
// set numbers format by SetCellStyle() method.
func (f *File) SetCellValue(sheet, axis string, value interface{}) {
	switch t := value.(type) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	case float32:
		f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float32)), 'f', -1, 32))
	case float64:
		f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float64)), 'f', -1, 64))
	case string:
		f.SetCellStr(sheet, axis, t)
	case []byte:
		f.SetCellStr(sheet, axis, string(t))
	case time.Duration:
		f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(time.Duration).Seconds()/86400), 'f', -1, 32))
		f.setDefaultTimeStyle(sheet, axis, 21)
	case time.Time:
		f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(timeToExcelTime(timeToUTCTime(value.(time.Time)))), 'f', -1, 64))
		f.setDefaultTimeStyle(sheet, axis, 22)
	case nil:
		f.SetCellStr(sheet, axis, "")
88 89
	case bool:
		f.SetCellBool(sheet, axis, bool(value.(bool)))
90 91 92 93 94
	default:
		f.setCellIntValue(sheet, axis, value)
	}
}

xurime's avatar
xurime 已提交
95
// setCellIntValue provides a function to set int value of a cell.
96 97
func (f *File) setCellIntValue(sheet, axis string, value interface{}) {
	switch value.(type) {
98 99 100 101 102 103 104 105 106 107
	case int:
		f.SetCellInt(sheet, axis, value.(int))
	case int8:
		f.SetCellInt(sheet, axis, int(value.(int8)))
	case int16:
		f.SetCellInt(sheet, axis, int(value.(int16)))
	case int32:
		f.SetCellInt(sheet, axis, int(value.(int32)))
	case int64:
		f.SetCellInt(sheet, axis, int(value.(int64)))
108 109 110 111 112 113 114 115 116 117
	case uint:
		f.SetCellInt(sheet, axis, int(value.(uint)))
	case uint8:
		f.SetCellInt(sheet, axis, int(value.(uint8)))
	case uint16:
		f.SetCellInt(sheet, axis, int(value.(uint16)))
	case uint32:
		f.SetCellInt(sheet, axis, int(value.(uint32)))
	case uint64:
		f.SetCellInt(sheet, axis, int(value.(uint64)))
118 119 120 121 122
	default:
		f.SetCellStr(sheet, axis, fmt.Sprintf("%v", value))
	}
}

xurime's avatar
xurime 已提交
123
// SetCellBool provides a function to set bool type value of a cell by given
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
// worksheet name, cell coordinates and cell value.
func (f *File) SetCellBool(sheet, axis string, value bool) {
	xlsx := f.workSheetReader(sheet)
	axis = f.mergeCellsParser(xlsx, axis)
	col := string(strings.Map(letterOnlyMapF, axis))
	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	if err != nil {
		return
	}
	xAxis := row - 1
	yAxis := TitleToNumber(col)

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

	completeRow(xlsx, rows, cell)
	completeCol(xlsx, rows, cell)

	xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
	xlsx.SheetData.Row[xAxis].C[yAxis].T = "b"
	if value {
		xlsx.SheetData.Row[xAxis].C[yAxis].V = "1"
	} else {
		xlsx.SheetData.Row[xAxis].C[yAxis].V = "0"
	}
}

xurime's avatar
xurime 已提交
151 152 153 154
// GetCellValue provides a function to get formatted value from cell by given
// worksheet name 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.
xurime's avatar
xurime 已提交
155 156
func (f *File) GetCellValue(sheet, axis string) string {
	xlsx := f.workSheetReader(sheet)
157
	axis = f.mergeCellsParser(xlsx, axis)
158 159 160 161
	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	if err != nil {
		return ""
	}
162
	xAxis := row - 1
xurime's avatar
xurime 已提交
163
	rows := len(xlsx.SheetData.Row)
xurime's avatar
xurime 已提交
164 165 166 167 168 169
	if rows > 1 {
		lastRow := xlsx.SheetData.Row[rows-1].R
		if lastRow >= rows {
			rows = lastRow
		}
	}
170
	if rows < xAxis {
171
		return ""
xurime's avatar
xurime 已提交
172
	}
173 174 175 176 177
	for k := range xlsx.SheetData.Row {
		if xlsx.SheetData.Row[k].R == row {
			for i := range xlsx.SheetData.Row[k].C {
				if axis == xlsx.SheetData.Row[k].C[i].R {
					val, _ := xlsx.SheetData.Row[k].C[i].getValueFrom(f, f.sharedStringsReader())
xurime's avatar
xurime 已提交
178 179
					return val
				}
xurime's avatar
xurime 已提交
180 181 182
			}
		}
	}
183
	return ""
xurime's avatar
xurime 已提交
184 185
}

xurime's avatar
xurime 已提交
186 187 188
// formattedValue provides a 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.
189 190 191 192
func (f *File) formattedValue(s int, v string) string {
	if s == 0 {
		return v
	}
193
	styleSheet := f.stylesReader()
194 195 196 197 198
	ok := builtInNumFmtFunc[styleSheet.CellXfs.Xf[s].NumFmtID]
	if ok != nil {
		return ok(styleSheet.CellXfs.Xf[s].NumFmtID, v)
	}
	return v
199 200
}

xurime's avatar
xurime 已提交
201
// GetCellStyle provides a function to get cell style index by given worksheet
202 203 204 205 206
// name and cell coordinates.
func (f *File) GetCellStyle(sheet, axis string) int {
	xlsx := f.workSheetReader(sheet)
	axis = f.mergeCellsParser(xlsx, axis)
	col := string(strings.Map(letterOnlyMapF, axis))
207 208 209 210
	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	if err != nil {
		return 0
	}
211 212 213 214 215 216 217 218 219 220 221 222
	xAxis := row - 1
	yAxis := TitleToNumber(col)

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

	completeRow(xlsx, rows, cell)
	completeCol(xlsx, rows, cell)

	return f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
}

xurime's avatar
xurime 已提交
223 224
// GetCellFormula provides a function to get formula from cell by given
// worksheet name and axis in XLSX file.
225
func (f *File) GetCellFormula(sheet, axis string) string {
xurime's avatar
xurime 已提交
226
	xlsx := f.workSheetReader(sheet)
227
	axis = f.mergeCellsParser(xlsx, axis)
228 229 230 231
	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	if err != nil {
		return ""
	}
232
	xAxis := row - 1
xurime's avatar
xurime 已提交
233 234 235 236 237 238 239
	rows := len(xlsx.SheetData.Row)
	if rows > 1 {
		lastRow := xlsx.SheetData.Row[rows-1].R
		if lastRow >= rows {
			rows = lastRow
		}
	}
240
	if rows < xAxis {
241
		return ""
xurime's avatar
xurime 已提交
242
	}
243 244 245 246
	for k := range xlsx.SheetData.Row {
		if xlsx.SheetData.Row[k].R == row {
			for i := range xlsx.SheetData.Row[k].C {
				if axis == xlsx.SheetData.Row[k].C[i].R {
247 248 249
					if xlsx.SheetData.Row[k].C[i].F.T == STCellFormulaTypeShared {
						return getSharedForumula(xlsx, xlsx.SheetData.Row[k].C[i].F.Si)
					}
250 251
					if xlsx.SheetData.Row[k].C[i].F != nil {
						return xlsx.SheetData.Row[k].C[i].F.Content
xurime's avatar
xurime 已提交
252 253
					}
				}
xurime's avatar
xurime 已提交
254 255 256
			}
		}
	}
257
	return ""
xurime's avatar
xurime 已提交
258
}
259

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
// getSharedForumula find a cell contains the same formula as another cell,
// the "shared" value can be used for the t attribute and the si attribute can
// be used to refer to the cell containing the formula. Two formulas are
// considered to be the same when their respective representations in
// R1C1-reference notation, are the same.
//
// Note that this function not validate ref tag to check the cell if or not in
// allow area, and always return origin shared formula.
func getSharedForumula(xlsx *xlsxWorksheet, si string) string {
	for k := range xlsx.SheetData.Row {
		for i := range xlsx.SheetData.Row[k].C {
			if xlsx.SheetData.Row[k].C[i].F == nil {
				continue
			}
			if xlsx.SheetData.Row[k].C[i].F.T != STCellFormulaTypeShared {
				continue
			}
			if xlsx.SheetData.Row[k].C[i].F.Si != si {
				continue
			}
			if xlsx.SheetData.Row[k].C[i].F.Ref != "" {
				return xlsx.SheetData.Row[k].C[i].F.Content
			}
		}
	}
	return ""
}

xurime's avatar
xurime 已提交
288
// SetCellFormula provides a function to set cell formula by given string and
289
// worksheet name.
290
func (f *File) SetCellFormula(sheet, axis, formula string) {
xurime's avatar
xurime 已提交
291
	xlsx := f.workSheetReader(sheet)
292
	axis = f.mergeCellsParser(xlsx, axis)
293
	col := string(strings.Map(letterOnlyMapF, axis))
294 295 296 297
	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	if err != nil {
		return
	}
298
	xAxis := row - 1
299
	yAxis := TitleToNumber(col)
300 301 302 303

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

xurime's avatar
xurime 已提交
304 305
	completeRow(xlsx, rows, cell)
	completeCol(xlsx, rows, cell)
306 307 308 309 310 311 312 313 314 315

	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
	}
}
316

xurime's avatar
xurime 已提交
317 318 319 320
// SetCellHyperLink provides a function to set cell hyperlink by given
// worksheet name and link URL address. LinkType defines two types of
// hyperlink "External" for web site or "Location" for moving to one of cell
// in this workbook. The below is example for external link.
321
//
322
//    xlsx.SetCellHyperLink("Sheet1", "A3", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
323 324 325 326
//    // Set underline and font color style for the cell.
//    style, _ := xlsx.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`)
//    xlsx.SetCellStyle("Sheet1", "A3", "A3", style)
//
327
// A this is another example for "Location":
328 329
//
//    xlsx.SetCellHyperLink("Sheet1", "A3", "Sheet1!A40", "Location")
330
//
331
func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) {
xurime's avatar
xurime 已提交
332
	xlsx := f.workSheetReader(sheet)
333
	axis = f.mergeCellsParser(xlsx, axis)
334 335 336 337 338 339 340 341 342
	linkTypes := map[string]xlsxHyperlink{
		"External": {},
		"Location": {Location: link},
	}
	hyperlink, ok := linkTypes[linkType]
	if !ok || axis == "" {
		return
	}
	hyperlink.Ref = axis
343
	if linkType == "External" {
344 345
		rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, linkType)
		hyperlink.RID = "rId" + strconv.Itoa(rID)
346
	}
347 348
	if xlsx.Hyperlinks == nil {
		xlsx.Hyperlinks = &xlsxHyperlinks{}
349
	}
350
	xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
351
}
xurime's avatar
xurime 已提交
352

xurime's avatar
xurime 已提交
353 354 355 356
// GetCellHyperLink provides a function to get cell hyperlink by given
// worksheet name and axis. Boolean type value link will be ture if the cell
// has a hyperlink and the target is the address of the hyperlink. Otherwise,
// the value of link will be false and the value of the target will be a blank
xurime's avatar
xurime 已提交
357
// string. For example get hyperlink of Sheet1!H6:
358 359 360 361 362 363 364 365 366 367 368
//
//    link, target := xlsx.GetCellHyperLink("Sheet1", "H6")
//
func (f *File) GetCellHyperLink(sheet, axis string) (bool, string) {
	var link bool
	var target string
	xlsx := f.workSheetReader(sheet)
	axis = f.mergeCellsParser(xlsx, axis)
	if xlsx.Hyperlinks == nil || axis == "" {
		return link, target
	}
369 370
	for h := range xlsx.Hyperlinks.Hyperlink {
		if xlsx.Hyperlinks.Hyperlink[h].Ref == axis {
371
			link = true
372 373 374
			target = xlsx.Hyperlinks.Hyperlink[h].Location
			if xlsx.Hyperlinks.Hyperlink[h].RID != "" {
				target = f.getSheetRelationshipsTargetByID(sheet, xlsx.Hyperlinks.Hyperlink[h].RID)
375 376 377 378 379 380
			}
		}
	}
	return link, target
}

xurime's avatar
xurime 已提交
381 382
// MergeCell provides a 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 已提交
383
//
384
//    xlsx.MergeCell("Sheet1", "D3", "E9")
xurime's avatar
xurime 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
//
// 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
400
	hxAxis := TitleToNumber(hcol)
xurime's avatar
xurime 已提交
401 402 403 404

	vcol := string(strings.Map(letterOnlyMapF, vcell))
	vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
	vyAxis := vrow - 1
405
	vxAxis := TitleToNumber(vcol)
xurime's avatar
xurime 已提交
406 407 408 409 410 411 412 413 414 415 416

	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 已提交
417
	xlsx := f.workSheetReader(sheet)
xurime's avatar
xurime 已提交
418 419 420
	if xlsx.MergeCells != nil {
		mergeCell := xlsxMergeCell{}
		// Correct the coordinate area, such correct C1:B3 to B1:C3.
421
		mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
xurime's avatar
xurime 已提交
422 423 424 425 426 427 428 429 430 431 432 433
		// 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.
434
		mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
xurime's avatar
xurime 已提交
435 436 437 438 439 440
		mergeCells := xlsxMergeCells{}
		mergeCells.Cells = append(mergeCells.Cells, &mergeCell)
		xlsx.MergeCells = &mergeCells
	}
}

xurime's avatar
xurime 已提交
441
// SetCellInt provides a function to set int type value of a cell by given
442 443 444 445 446
// worksheet name, cell coordinates and cell value.
func (f *File) SetCellInt(sheet, axis string, value int) {
	xlsx := f.workSheetReader(sheet)
	axis = f.mergeCellsParser(xlsx, axis)
	col := string(strings.Map(letterOnlyMapF, axis))
447 448 449 450
	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	if err != nil {
		return
	}
451 452 453 454 455 456 457 458 459 460 461 462 463 464
	xAxis := row - 1
	yAxis := TitleToNumber(col)

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

	completeRow(xlsx, rows, cell)
	completeCol(xlsx, rows, cell)

	xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
	xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
	xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value)
}

xurime's avatar
xurime 已提交
465
// prepareCellStyle provides a function to prepare style index of cell in
466 467 468 469 470 471 472 473 474 475 476 477
// worksheet by given column index and style index.
func (f *File) prepareCellStyle(xlsx *xlsxWorksheet, col, style int) int {
	if xlsx.Cols != nil && style == 0 {
		for _, v := range xlsx.Cols.Col {
			if v.Min <= col && col <= v.Max {
				style = v.Style
			}
		}
	}
	return style
}

xurime's avatar
xurime 已提交
478 479
// SetCellStr provides a function to set string type value of a cell. Total
// number of characters that a cell can contain 32767 characters.
480 481 482 483 484 485 486
func (f *File) SetCellStr(sheet, axis, value string) {
	xlsx := f.workSheetReader(sheet)
	axis = f.mergeCellsParser(xlsx, axis)
	if len(value) > 32767 {
		value = value[0:32767]
	}
	col := string(strings.Map(letterOnlyMapF, axis))
487 488 489 490
	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	if err != nil {
		return
	}
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	xAxis := row - 1
	yAxis := TitleToNumber(col)

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

	completeRow(xlsx, rows, cell)
	completeCol(xlsx, rows, cell)

	// Leading space(s) character detection.
	if len(value) > 0 {
		if value[0] == 32 {
			xlsx.SheetData.Row[xAxis].C[yAxis].XMLSpace = xml.Attr{
				Name:  xml.Name{Space: NameSpaceXML, Local: "space"},
				Value: "preserve",
			}
		}
	}
	xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
	xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
	xlsx.SheetData.Row[xAxis].C[yAxis].V = value
}

xurime's avatar
xurime 已提交
514
// SetCellDefault provides a function to set string type value of a cell as
515 516 517 518 519
// default format without escaping the cell.
func (f *File) SetCellDefault(sheet, axis, value string) {
	xlsx := f.workSheetReader(sheet)
	axis = f.mergeCellsParser(xlsx, axis)
	col := string(strings.Map(letterOnlyMapF, axis))
520 521 522 523
	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	if err != nil {
		return
	}
524 525 526 527 528 529 530 531 532 533 534 535 536 537
	xAxis := row - 1
	yAxis := TitleToNumber(col)

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

	completeRow(xlsx, rows, cell)
	completeCol(xlsx, rows, cell)

	xlsx.SheetData.Row[xAxis].C[yAxis].S = f.prepareCellStyle(xlsx, cell, xlsx.SheetData.Row[xAxis].C[yAxis].S)
	xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
	xlsx.SheetData.Row[xAxis].C[yAxis].V = value
}

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
// SetSheetRow writes an array to row by given worksheet name, starting
// coordinate and a pointer to array type 'slice'. For example, writes an
// array to row 6 start with the cell B6 on Sheet1:
//
//     xlsx.SetSheetRow("Sheet1", "B6", &[]interface{}{"1", nil, 2})
//
func (f *File) SetSheetRow(sheet, axis string, slice interface{}) {
	xlsx := f.workSheetReader(sheet)
	axis = f.mergeCellsParser(xlsx, axis)
	col := string(strings.Map(letterOnlyMapF, axis))
	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
	if err != nil {
		return
	}
	// Make sure 'slice' is a Ptr to Slice
	v := reflect.ValueOf(slice)
	if v.Kind() != reflect.Ptr {
		return
	}
	v = v.Elem()
	if v.Kind() != reflect.Slice {
		return
	}

	xAxis := row - 1
	yAxis := TitleToNumber(col)

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

	completeRow(xlsx, rows, cell)
	completeCol(xlsx, rows, cell)

	idx := 0
	for i := cell - 1; i < v.Len()+cell-1; i++ {
		c := ToAlphaString(i) + strconv.Itoa(row)
		f.SetCellValue(sheet, c, v.Index(idx).Interface())
		idx++
	}
}

xurime's avatar
xurime 已提交
579
// checkCellInArea provides a function to determine if a given coordinate is
xurime's avatar
xurime 已提交
580 581 582
// within an area.
func checkCellInArea(cell, area string) bool {
	cell = strings.ToUpper(cell)
583
	area = strings.ToUpper(area)
xurime's avatar
xurime 已提交
584 585

	ref := strings.Split(area, ":")
586 587 588 589
	if len(ref) < 2 {
		return false
	}

590 591
	from := ref[0]
	to := ref[1]
xurime's avatar
xurime 已提交
592

593 594 595
	col, row := getCellColRow(cell)
	fromCol, fromRow := getCellColRow(from)
	toCol, toRow := getCellColRow(to)
xurime's avatar
xurime 已提交
596

597
	return axisLowerOrEqualThan(fromCol, col) && axisLowerOrEqualThan(col, toCol) && axisLowerOrEqualThan(fromRow, row) && axisLowerOrEqualThan(row, toRow)
xurime's avatar
xurime 已提交
598
}