diff options
author | Ri Xu <xuri.me@gmail.com> | 2017-01-19 14:05:32 +0800 |
---|---|---|
committer | Ri Xu <xuri.me@gmail.com> | 2017-01-19 14:05:32 +0800 |
commit | 4a9b39afc634a2399c4729f4fb47c9f290ab1ee5 (patch) | |
tree | 71d673195ccae30d6ef174611f6cf362be7e9133 /cell.go | |
parent | 52796f6e58e95145e2964d0d313a2f721dcc040e (diff) |
- Add hyperlink and set formula support for cell support;
- Character limits for cells added;
- Update go test and fix typo
Diffstat (limited to 'cell.go')
-rw-r--r-- | cell.go | 63 |
1 files changed, 59 insertions, 4 deletions
@@ -6,8 +6,9 @@ import ( "strings" ) -// GetCellValue provide function get value from cell by given sheet index and -// axis in XLSX file. The value of the merged cell is not available currently. +// GetCellValue provides function to get value from cell by given sheet index +// and axis in XLSX file. The value of the merged cell is not available +// currently. func (f *File) GetCellValue(sheet string, axis string) string { axis = strings.ToUpper(axis) var xlsx xlsxWorksheet @@ -50,8 +51,8 @@ func (f *File) GetCellValue(sheet string, axis string) string { return "" } -// GetCellFormula provide function get formula from cell by given sheet index -// and axis in XLSX file. +// GetCellFormula provides function to get formula from cell by given sheet +// index and axis in XLSX file. func (f *File) GetCellFormula(sheet string, axis string) string { axis = strings.ToUpper(axis) var xlsx xlsxWorksheet @@ -84,3 +85,57 @@ func (f *File) GetCellFormula(sheet string, axis string) string { } return "" } + +// 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) { + axis = strings.ToUpper(axis) + var xlsx xlsxWorksheet + name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml" + xml.Unmarshal([]byte(f.readXML(name)), &xlsx) + 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 + } + output, _ := xml.Marshal(xlsx) + f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output))) +} + +// SetCellFormula provides function to set cell formula by given string and +// sheet index. +func (f *File) SetCellFormula(sheet, axis, formula string) { + axis = strings.ToUpper(axis) + var xlsx xlsxWorksheet + col := string(strings.Map(letterOnlyMapF, axis)) + row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis)) + xAxis := row - 1 + yAxis := titleToNumber(col) + + name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml" + xml.Unmarshal([]byte(f.readXML(name)), &xlsx) + + rows := xAxis + 1 + cell := yAxis + 1 + + xlsx = completeRow(xlsx, rows, cell) + xlsx = completeCol(xlsx, rows, cell) + + 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 + } + output, _ := xml.Marshal(xlsx) + f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output))) +} |