diff --git a/README.md b/README.md index fc6df012..5457f89c 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,10 @@ func main() { xlsx := excelize.CreateFile() // Create a new sheet. xlsx.NewSheet(2, "Sheet2") - // Set int or string type value of a cell. + // Set value of a cell. xlsx.SetCellValue("Sheet2", "A2", "Hello world.") xlsx.SetCellValue("Sheet1", "B2", 100) - // Set active sheet of workbook. + // Set active sheet of the workbook. xlsx.SetActiveSheet(2) // Save xlsx file by the given path. err := xlsx.WriteTo("/tmp/Workbook.xlsx") @@ -82,12 +82,7 @@ func main() { for _, colCell := range row { fmt.Print(colCell, "\t") } - } - // Save the xlsx file with origin path. - err = xlsx.Save() - if err != nil { - fmt.Println(err) - os.Exit(1) + fmt.Println() } } ``` @@ -108,9 +103,13 @@ import ( ) func main() { - xlsx := excelize.CreateFile() + xlsx, err := excelize.OpenFile("/tmp/Workbook.xlsx") + if err != nil { + fmt.Println(err) + os.Exit(1) + } // Insert a picture. - err := xlsx.AddPicture("Sheet1", "A2", "/tmp/image1.gif", 0, 0, 1, 1) + err = xlsx.AddPicture("Sheet1", "A2", "/tmp/image1.gif", 0, 0, 1, 1) // Insert a picture to sheet with scaling. err = xlsx.AddPicture("Sheet1", "D2", "/tmp/image2.jpg", 0, 0, 0.5, 0.5) // Insert a picture offset in the cell. @@ -119,7 +118,8 @@ func main() { fmt.Println(err) os.Exit(1) } - err = xlsx.WriteTo("/tmp/Workbook.xlsx") + // Save the xlsx file with the origin path. + err = xlsx.Save() if err != nil { fmt.Println(err) os.Exit(1) diff --git a/cell.go b/cell.go index 5f8439e3..f20f05af 100644 --- a/cell.go +++ b/cell.go @@ -23,7 +23,7 @@ func (f *File) GetCellValue(sheet string, axis string) string { rows = lastRow } } - if rows <= xAxis { + if rows < xAxis { return "" } for _, v := range xlsx.SheetData.Row { @@ -67,7 +67,7 @@ func (f *File) GetCellFormula(sheet string, axis string) string { rows = lastRow } } - if rows <= xAxis { + if rows < xAxis { return "" } for _, v := range xlsx.SheetData.Row { @@ -86,29 +86,6 @@ 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) { @@ -139,3 +116,26 @@ func (f *File) SetCellFormula(sheet, axis, formula string) { output, _ := xml.Marshal(xlsx) f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output))) } + +// 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))) +} diff --git a/lib.go b/lib.go index 7b9174e5..f57f1607 100644 --- a/lib.go +++ b/lib.go @@ -43,7 +43,7 @@ func (f *File) readXML(name string) string { if content, ok := f.XLSX[name]; ok { return content } - return `` + return "" } // Update given file content in file list of XLSX. @@ -66,7 +66,7 @@ func readFile(file *zip.File) string { // Convert integer to Excel sheet column title. func toAlphaString(value int) string { if value < 0 { - return `` + return "" } var ans string i := value