2016-08-30 21:54:28 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
2017-07-24 10:26:02 +08:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
2016-08-30 21:54:28 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-07-24 10:26:02 +08:00
|
|
|
"time"
|
2016-08-30 21:54:28 +08:00
|
|
|
)
|
|
|
|
|
2017-05-24 14:17:35 +08:00
|
|
|
// mergeCellsParser provides function to check merged cells in worksheet by
|
|
|
|
// given axis.
|
2017-07-05 13:30:56 +08:00
|
|
|
func (f *File) mergeCellsParser(xlsx *xlsxWorksheet, axis string) string {
|
|
|
|
axis = strings.ToUpper(axis)
|
2017-01-25 18:13:11 +08:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-05 13:30:56 +08:00
|
|
|
return axis
|
2017-05-24 14:17:35 +08:00
|
|
|
}
|
|
|
|
|
2017-07-24 10:26:02 +08:00
|
|
|
// SetCellValue provides function to set value of a cell. The following shows
|
|
|
|
// the supported data types:
|
|
|
|
//
|
|
|
|
// int
|
|
|
|
// int8
|
|
|
|
// int16
|
|
|
|
// int32
|
|
|
|
// int64
|
2017-10-18 18:42:20 +08:00
|
|
|
// uint
|
|
|
|
// uint8
|
|
|
|
// uint16
|
|
|
|
// uint32
|
|
|
|
// uint64
|
2017-07-24 10:26:02 +08:00
|
|
|
// float32
|
|
|
|
// float64
|
|
|
|
// string
|
|
|
|
// []byte
|
2018-01-05 09:39:31 +08:00
|
|
|
// time.Duration
|
2017-07-24 10:26:02 +08:00
|
|
|
// time.Time
|
|
|
|
// 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) {
|
2018-01-05 09:39:31 +08:00
|
|
|
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, "")
|
|
|
|
default:
|
|
|
|
f.setCellIntValue(sheet, axis, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setCellIntValue provides function to set int value of a cell.
|
|
|
|
func (f *File) setCellIntValue(sheet, axis string, value interface{}) {
|
|
|
|
switch value.(type) {
|
2017-07-24 10:26:02 +08:00
|
|
|
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)))
|
2017-10-18 18:42:20 +08:00
|
|
|
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)))
|
2017-07-24 10:26:02 +08:00
|
|
|
default:
|
|
|
|
f.SetCellStr(sheet, axis, fmt.Sprintf("%v", value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 14:17:35 +08:00
|
|
|
// GetCellValue provides function to get formatted value from cell by given
|
2017-10-31 16:33:36 +08:00
|
|
|
// 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.
|
2017-05-24 14:17:35 +08:00
|
|
|
func (f *File) GetCellValue(sheet, axis string) string {
|
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-07-05 13:30:56 +08:00
|
|
|
axis = f.mergeCellsParser(xlsx, axis)
|
2017-09-19 11:59:33 +08:00
|
|
|
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2017-01-25 18:13:11 +08:00
|
|
|
xAxis := row - 1
|
2016-08-30 21:54:28 +08:00
|
|
|
rows := len(xlsx.SheetData.Row)
|
2016-12-24 22:47:36 +08:00
|
|
|
if rows > 1 {
|
|
|
|
lastRow := xlsx.SheetData.Row[rows-1].R
|
|
|
|
if lastRow >= rows {
|
|
|
|
rows = lastRow
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 16:15:01 +08:00
|
|
|
if rows < xAxis {
|
2017-01-18 14:47:23 +08:00
|
|
|
return ""
|
2016-12-24 22:47:36 +08:00
|
|
|
}
|
2017-10-16 10:42:43 +08:00
|
|
|
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())
|
2017-09-30 17:07:59 +08:00
|
|
|
return val
|
|
|
|
}
|
2016-12-24 22:47:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 14:47:23 +08:00
|
|
|
return ""
|
2016-12-24 22:47:36 +08:00
|
|
|
}
|
|
|
|
|
2017-05-05 14:40:28 +08:00
|
|
|
// 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
|
|
|
|
}
|
2017-06-26 18:44:19 +08:00
|
|
|
styleSheet := f.stylesReader()
|
2017-05-05 19:15:39 +08:00
|
|
|
ok := builtInNumFmtFunc[styleSheet.CellXfs.Xf[s].NumFmtID]
|
|
|
|
if ok != nil {
|
|
|
|
return ok(styleSheet.CellXfs.Xf[s].NumFmtID, v)
|
|
|
|
}
|
|
|
|
return v
|
2017-05-05 14:40:28 +08:00
|
|
|
}
|
|
|
|
|
2017-07-24 10:26:02 +08:00
|
|
|
// GetCellStyle provides function to get cell style index by given worksheet
|
|
|
|
// 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))
|
2017-09-19 11:59:33 +08:00
|
|
|
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
2017-07-24 10:26:02 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-09-13 22:17:40 +08:00
|
|
|
// GetCellFormula provides function to get formula from cell by given worksheet
|
|
|
|
// name and axis in XLSX file.
|
2017-03-06 12:05:41 +08:00
|
|
|
func (f *File) GetCellFormula(sheet, axis string) string {
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-07-05 13:30:56 +08:00
|
|
|
axis = f.mergeCellsParser(xlsx, axis)
|
2017-09-19 11:59:33 +08:00
|
|
|
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2017-01-25 18:13:11 +08:00
|
|
|
xAxis := row - 1
|
2016-12-24 22:47:36 +08:00
|
|
|
rows := len(xlsx.SheetData.Row)
|
|
|
|
if rows > 1 {
|
|
|
|
lastRow := xlsx.SheetData.Row[rows-1].R
|
|
|
|
if lastRow >= rows {
|
|
|
|
rows = lastRow
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 16:15:01 +08:00
|
|
|
if rows < xAxis {
|
2017-01-18 14:47:23 +08:00
|
|
|
return ""
|
2016-08-30 21:54:28 +08:00
|
|
|
}
|
2017-10-16 10:42:43 +08:00
|
|
|
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 {
|
|
|
|
if xlsx.SheetData.Row[k].C[i].F != nil {
|
|
|
|
return xlsx.SheetData.Row[k].C[i].F.Content
|
2017-09-30 17:07:59 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 21:54:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 14:47:23 +08:00
|
|
|
return ""
|
2016-08-30 21:54:28 +08:00
|
|
|
}
|
2017-01-19 14:05:32 +08:00
|
|
|
|
|
|
|
// SetCellFormula provides function to set cell formula by given string and
|
2017-10-31 16:33:36 +08:00
|
|
|
// worksheet name.
|
2017-01-19 14:05:32 +08:00
|
|
|
func (f *File) SetCellFormula(sheet, axis, formula string) {
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-07-05 13:30:56 +08:00
|
|
|
axis = f.mergeCellsParser(xlsx, axis)
|
2017-01-19 14:05:32 +08:00
|
|
|
col := string(strings.Map(letterOnlyMapF, axis))
|
2017-09-19 11:59:33 +08:00
|
|
|
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-19 14:05:32 +08:00
|
|
|
xAxis := row - 1
|
2017-06-27 17:53:06 +08:00
|
|
|
yAxis := TitleToNumber(col)
|
2017-01-19 14:05:32 +08:00
|
|
|
|
|
|
|
rows := xAxis + 1
|
|
|
|
cell := yAxis + 1
|
|
|
|
|
2017-03-12 20:38:26 +08:00
|
|
|
completeRow(xlsx, rows, cell)
|
|
|
|
completeCol(xlsx, rows, cell)
|
2017-01-19 14:05:32 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 16:15:01 +08:00
|
|
|
|
2017-09-13 22:17:40 +08:00
|
|
|
// SetCellHyperLink provides 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.
|
2017-07-26 18:37:00 +08:00
|
|
|
//
|
2017-11-16 11:07:39 +08:00
|
|
|
// xlsx.SetCellHyperLink("Sheet1", "A3", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
|
2017-07-26 18:37:00 +08:00
|
|
|
// // Set underline and font color style for the cell.
|
|
|
|
// style, _ := xlsx.NewStyle(`{"font":{"color":"#1265BE","underline":"single"}}`)
|
|
|
|
// xlsx.SetCellStyle("Sheet1", "A3", "A3", style)
|
|
|
|
//
|
2017-07-30 15:46:04 +08:00
|
|
|
// A this is another example for "Location":
|
2017-07-30 13:52:37 +08:00
|
|
|
//
|
|
|
|
// xlsx.SetCellHyperLink("Sheet1", "A3", "Sheet1!A40", "Location")
|
2017-07-30 15:46:04 +08:00
|
|
|
//
|
2017-07-30 13:52:37 +08:00
|
|
|
func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) {
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-07-05 13:30:56 +08:00
|
|
|
axis = f.mergeCellsParser(xlsx, axis)
|
2017-07-30 15:46:04 +08:00
|
|
|
linkTypes := map[string]xlsxHyperlink{
|
|
|
|
"External": {},
|
|
|
|
"Location": {Location: link},
|
|
|
|
}
|
|
|
|
hyperlink, ok := linkTypes[linkType]
|
|
|
|
if !ok || axis == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hyperlink.Ref = axis
|
2017-07-30 13:52:37 +08:00
|
|
|
if linkType == "External" {
|
2017-07-30 15:46:04 +08:00
|
|
|
rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, linkType)
|
|
|
|
hyperlink.RID = "rId" + strconv.Itoa(rID)
|
2017-01-23 16:15:01 +08:00
|
|
|
}
|
2017-07-30 15:46:04 +08:00
|
|
|
if xlsx.Hyperlinks == nil {
|
|
|
|
xlsx.Hyperlinks = &xlsxHyperlinks{}
|
2017-01-23 16:15:01 +08:00
|
|
|
}
|
2017-07-30 15:46:04 +08:00
|
|
|
xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
|
2017-01-23 16:15:01 +08:00
|
|
|
}
|
2017-01-25 15:44:18 +08:00
|
|
|
|
2017-09-13 22:17:40 +08:00
|
|
|
// GetCellHyperLink provides 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
|
|
|
|
// string. For example get hyperlink of Sheet1!H6:
|
2017-08-08 20:08:54 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
2017-10-16 10:42:43 +08:00
|
|
|
for h := range xlsx.Hyperlinks.Hyperlink {
|
|
|
|
if xlsx.Hyperlinks.Hyperlink[h].Ref == axis {
|
2017-08-08 20:08:54 +08:00
|
|
|
link = true
|
2017-10-16 10:42:43 +08:00
|
|
|
target = xlsx.Hyperlinks.Hyperlink[h].Location
|
|
|
|
if xlsx.Hyperlinks.Hyperlink[h].RID != "" {
|
|
|
|
target = f.getSheetRelationshipsTargetByID(sheet, xlsx.Hyperlinks.Hyperlink[h].RID)
|
2017-08-08 20:08:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return link, target
|
|
|
|
}
|
|
|
|
|
2017-03-06 12:05:41 +08:00
|
|
|
// MergeCell provides function to merge cells by given coordinate area and sheet
|
|
|
|
// name. For example create a merged cell of D3:E9 on Sheet1:
|
2017-01-25 15:44:18 +08:00
|
|
|
//
|
2017-09-13 22:00:33 +08:00
|
|
|
// xlsx.MergeCell("Sheet1", "D3", "E9")
|
2017-01-25 15:44:18 +08:00
|
|
|
//
|
|
|
|
// 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
|
2017-06-27 17:53:06 +08:00
|
|
|
hxAxis := TitleToNumber(hcol)
|
2017-01-25 15:44:18 +08:00
|
|
|
|
|
|
|
vcol := string(strings.Map(letterOnlyMapF, vcell))
|
|
|
|
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
|
|
|
|
vyAxis := vrow - 1
|
2017-06-27 17:53:06 +08:00
|
|
|
vxAxis := TitleToNumber(vcol)
|
2017-01-25 15:44:18 +08:00
|
|
|
|
|
|
|
if vxAxis < hxAxis {
|
|
|
|
hcell, vcell = vcell, hcell
|
|
|
|
vxAxis, hxAxis = hxAxis, vxAxis
|
|
|
|
}
|
|
|
|
|
|
|
|
if vyAxis < hyAxis {
|
|
|
|
hcell, vcell = vcell, hcell
|
|
|
|
vyAxis, hyAxis = hyAxis, vyAxis
|
|
|
|
}
|
|
|
|
|
2017-03-12 20:38:26 +08:00
|
|
|
xlsx := f.workSheetReader(sheet)
|
2017-01-25 15:44:18 +08:00
|
|
|
if xlsx.MergeCells != nil {
|
|
|
|
mergeCell := xlsxMergeCell{}
|
|
|
|
// Correct the coordinate area, such correct C1:B3 to B1:C3.
|
2017-06-27 17:53:06 +08:00
|
|
|
mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
|
2017-01-25 15:44:18 +08:00
|
|
|
// 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.
|
2017-06-27 17:53:06 +08:00
|
|
|
mergeCell.Ref = ToAlphaString(hxAxis) + strconv.Itoa(hyAxis+1) + ":" + ToAlphaString(vxAxis) + strconv.Itoa(vyAxis+1)
|
2017-01-25 15:44:18 +08:00
|
|
|
mergeCells := xlsxMergeCells{}
|
|
|
|
mergeCells.Cells = append(mergeCells.Cells, &mergeCell)
|
|
|
|
xlsx.MergeCells = &mergeCells
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-24 10:26:02 +08:00
|
|
|
// SetCellInt provides function to set int type value of a cell by given
|
|
|
|
// 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))
|
2017-09-19 11:59:33 +08:00
|
|
|
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-07-24 10:26:02 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareCellStyle provides function to prepare style index of cell in
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCellStr provides function to set string type value of a cell. Total number
|
|
|
|
// of characters that a cell can contain 32767 characters.
|
|
|
|
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))
|
2017-09-19 11:59:33 +08:00
|
|
|
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-07-24 10:26:02 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCellDefault provides function to set string type value of a cell as
|
|
|
|
// 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))
|
2017-09-19 11:59:33 +08:00
|
|
|
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-07-24 10:26:02 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-25 15:44:18 +08:00
|
|
|
// checkCellInArea provides function to determine if a given coordinate is
|
|
|
|
// within an area.
|
|
|
|
func checkCellInArea(cell, area string) bool {
|
|
|
|
cell = strings.ToUpper(cell)
|
2018-01-19 17:32:54 +08:00
|
|
|
area = strings.ToUpper(area)
|
2017-01-25 15:44:18 +08:00
|
|
|
|
|
|
|
ref := strings.Split(area, ":")
|
2018-01-19 17:32:54 +08:00
|
|
|
from := ref[0]
|
|
|
|
to := ref[1]
|
2017-01-25 15:44:18 +08:00
|
|
|
|
2018-01-19 17:32:54 +08:00
|
|
|
col, row := getCellColRow(cell)
|
|
|
|
fromCol, fromRow := getCellColRow(from)
|
|
|
|
toCol, toRow := getCellColRow(to)
|
2017-01-25 15:44:18 +08:00
|
|
|
|
2018-01-19 17:32:54 +08:00
|
|
|
return axisLowerOrEqualThan(fromCol, col) && axisLowerOrEqualThan(col, toCol) && axisLowerOrEqualThan(fromRow, row) && axisLowerOrEqualThan(row, toRow)
|
2017-01-25 15:44:18 +08:00
|
|
|
}
|