Spark lines duplicate when creating spark lines on multiple sheets
dazfuller opened this issue · comments
Description
When adding spark lines to multiple sheets the lines created for the first sheet are also added to the second sheet. For example.
The spark line which was created for sheet 1 at position F2 also shows in sheet 2 at the same position.
Also, just want to say that this is an amazing library, so thank you :)
Steps to reproduce the issue:
- Add data to the sheets
- Call method to create spark lines for sheet 1
- Call method to create spark lines for sheet 2
- Save and view result
I created the following to re-produce the issue I'm seeing, the version of Excelize is below
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
"log"
)
func main() {
f := excelize.NewFile()
defer func(f *excelize.File) {
err := f.Close()
if err != nil {
log.Fatal("Unable to close workbook")
}
}(f)
_, err := f.NewSheet("Sheet2")
if err != nil {
log.Fatal("Unable to create new sheet")
}
data1 := [][]interface{}{
{"Item", "2020", "2021", "2022", "2023"},
{"Entry 1", 100, 200, 300, 400},
}
data2 := [][]interface{}{
{"Item", "2020", "2021", "2022", "2023", "2024"},
{"Entry 1", 100, 200, 300, 400, 500},
{"Entry 2", 200, 100, 400, 500, 300},
}
for i, row := range data1 {
rowStart, _ := excelize.JoinCellName("A", i+1)
_ = f.SetSheetRow("Sheet1", rowStart, &row)
}
for i, row := range data2 {
rowStart, _ := excelize.JoinCellName("A", i+1)
_ = f.SetSheetRow("Sheet2", rowStart, &row)
}
if err = addSparkLines(f, "Sheet1"); err != nil {
log.Fatal("Unable to add spark lines to sheet 1")
}
if err = addSparkLines(f, "Sheet2"); err != nil {
log.Fatal("Unable to add spark lines to sheet 2")
}
if err = f.SaveAs("demo.xlsx"); err != nil {
log.Fatal("Unable to save workbook")
}
}
func addSparkLines(f *excelize.File, sheetName string) error {
rows, _ := f.GetRows(sheetName)
cols, _ := f.GetCols(sheetName)
lastCol, _ := excelize.ColumnNumberToName(len(cols))
startCol, _ := excelize.ColumnNumberToName(2)
locationCol, _ := excelize.ColumnNumberToName(len(cols)+1)
var sparkLineLocation []string
var sparkLineRange []string
for i := range rows {
if i == 0 {
continue
}
ri := i + 1
location, _ := excelize.JoinCellName(locationCol, ri)
start, _ := excelize.JoinCellName(startCol, ri)
end, _ := excelize.JoinCellName(lastCol, ri)
sparkLineLocation = append(sparkLineLocation, location)
sparkLineRange = append(sparkLineRange, fmt.Sprintf("%s!%s:%s", sheetName, start, end))
}
return f.AddSparkline(sheetName, &excelize.SparklineOptions{
Location: sparkLineLocation,
Range: sparkLineRange,
Markers: true,
Type: "line",
Style: 18,
})
}
Describe the results you expected:
The spark lines for the first sheet should only be visible on the first sheet and not repeated on sheet 2
Output of go version
:
go version go1.22.3 darwin/arm64
Excelize version or commit ID:
v2.8.1
Environment details (OS, Microsoft Excel™ version, physical, etc.):
macOS Sonoma (14.5)
Excel Version 16.85 (24051214)
Apple MacBook M3 Pro
Thanks for your issue. This issue was introduced by commit 866f308
in v2.8.1, the v2.8.0 works well. I have fixed it, please upgrade to the master branch code, and this path will be released in the next version.
Just switched to master and that works perfectly