Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spark lines duplicate when creating spark lines on multiple sheets #1910

Closed
dazfuller opened this issue May 29, 2024 · 2 comments
Closed

Spark lines duplicate when creating spark lines on multiple sheets #1910

dazfuller opened this issue May 29, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@dazfuller
Copy link

Description

When adding spark lines to multiple sheets the lines created for the first sheet are also added to the second sheet. For example.

Sheet 1
image

Sheet 2
image

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:

  1. Add data to the sheets
  2. Call method to create spark lines for sheet 1
  3. Call method to create spark lines for sheet 2
  4. 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

@xuri xuri added the bug Something isn't working label May 29, 2024
@xuri xuri closed this as completed in c349313 May 29, 2024
@xuri
Copy link
Member

xuri commented May 29, 2024

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.

@dazfuller
Copy link
Author

Just switched to master and that works perfectly

@xuri xuri moved this to Bugfix in Excelize v2.9.0 Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Status: Bugfix
Development

No branches or pull requests

2 participants