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

Active tab is not set correctly on inserting sheet at the front #1383

Closed
1 of 2 tasks
ChristophHornung opened this issue Mar 30, 2020 · 10 comments · Fixed by #1384 or #1386
Closed
1 of 2 tasks

Active tab is not set correctly on inserting sheet at the front #1383

ChristophHornung opened this issue Mar 30, 2020 · 10 comments · Fixed by #1384 or #1386
Labels

Comments

@ChristophHornung
Copy link

Do you want to request a feature or report a bug?

  • Bug
  • Feature

Version of ClosedXML

0.94.2 and latest build

What is the current behavior?

Given a new empty excel file with one sheet.

Execute the following code:

this.worksheet = this.workbook.Worksheets.Add("NewFrontSheet", 1);

Afterwards open the file in Excel. Note that both the first and second sheet are selected and the first sheet is active.

image

If the sheet is added in position 2 note that the result will have only one sheet active and selected.

image

What is the expected behavior or new feature?

The active worksheet should correctly be kept when adding a new sheet in first position.

Did this work in previous versions of our tool? Which versions?

unknown

Reproducibility

Code to reproduce problem:

class Program
	{
		private XLWorkbook workbook;
		private IXLWorksheet worksheet;

		static void Main(string[] args)
		{
			Program p = new Program();
			p.Test();
		}

		private void Test()
		{
			this.workbook = new XLWorkbook(@"T:\tmp\test.xlsx");
			this.worksheet = this.workbook.Worksheets.Add("NewFrontSheet", 1);
			this.workbook.Save();
		}
	}

test.xlsx
result.xlsx

I assume this is an issue with how workbookView.activeTab is handled, since TabActive does return false for the single sheet directly after new XLWorkbook(..). The initial value in workbook.xml is not set (should default to 0).

I hope this helps.

@Pankraty
Copy link
Member

@_all you see what happens when the issue template is thoroughly filled? :D

@ChristophHornung
Copy link
Author

ChristophHornung commented Mar 31, 2020

I don't think #1384 fully resolves the general issue. Note that "IsActive" still returns false for the test.xlsx. From what I see "IsActive" should be set to the 1st tab if workbook.activeTab is not set (since according to the documentation the default value is 0) when loading the xlsx.

So another bug would occur in my opinion when after adding 'NewFrontSheet' it is set as TabSelected. This would result in it being also TabActive when saving, while the correct behavior should be that the prior active tab should remain active.

@ChristophHornung
Copy link
Author

ChristophHornung commented Mar 31, 2020

To reproduce simply change the test method to

private void Test()
		{
			this.workbook = new XLWorkbook(@"T:\tmp\test.xlsx");
			this.worksheet = this.workbook.Worksheets.Add("NewFrontSheet", 1);
			this.worksheet.TabSelected = true;
			this.workbook.Save();
		}

Afterwards NewFrontSheet is the active tab
image

When changing the position to 2 the initial sheet remains active
this.worksheet = this.workbook.Worksheets.Add("NewFrontSheet", 2);

image

@ChristophHornung
Copy link
Author

@Pankraty @igitur Should I open a separate issue now that this one is closed?

@Pankraty
Copy link
Member

Pankraty commented Apr 1, 2020

@ChristophHornung I think the main reason is that your file does not have any tab marked as active. How did you get such file? When I try to reproduce the issue on a fresh file there is always an active tab present. Moreover, if I manually remove activeTab="1" from the inner XML and open the file in Excel the first worksheet (index 0) becomes active - even though it was hidden before.

So I think it is best to specify the active tab explicitly if it is not set.

Anyway, I've prepared a fix for this too.

@ChristophHornung
Copy link
Author

@Pankraty test.xlsx was just "open excel (excel from office 365) - create new file (empty) - save"

@ChristophHornung
Copy link
Author

@Pankraty Even if I open a new empty file and add an additional sheet and then re-select the first one the workbookview.activeTab is still unset.
TwoSheetsFirstSelected.xlsx

@Pankraty
Copy link
Member

Pankraty commented Apr 1, 2020

Can you please check if the artifacts from this build fix the issue?

@ChristophHornung
Copy link
Author

@Pankraty If I select the second sheet activeTab is set to '1'.
TwoSheetsSecondSelected.xlsx

@ChristophHornung
Copy link
Author

@Pankraty Yes, that fixes the problem I am seeing. Thanks a lot! ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants