Bug 17759

Summary: [PATCH][RFE]Add a way to change the displayed sheet in usermodel API
Product: POI Reporter: Toshiaki Kamoshida <kamoshida.toshiaki>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: minor    
Priority: P3    
Version: 2.0-dev   
Target Milestone: ---   
Hardware: All   
OS: All   
Attachments: Testcase to find this problem.Please do it and open "Output.xls" with Excel Application.
PATCH to fix (with some Proposals)
PATCH to do it.

Description Toshiaki Kamoshida 2003-03-07 04:14:35 UTC
When you call HSSFSheet#setSelected(boolean) to change the selected sheet,
and save the workbook and open with Excel Application,you will see it;

1."selected tab" is changed to the sheet as you call the method.
2.but the sheet "currently showing" is not the sheet tab selecting.

Secondly,if you call HSSFSheet#setSelected(false) to all sheets,you can make 
the workbook with selecting no sheet.
It isn't good interface,I feel.I feel the parameter of the method should 
be "void" and works like HSSFSheet#setSelected(true).
Comment 1 Toshiaki Kamoshida 2003-03-07 04:16:26 UTC
Created attachment 5196 [details]
Testcase to find this problem.Please do it and open "Output.xls" with Excel Application.
Comment 2 Toshiaki Kamoshida 2003-05-08 09:07:42 UTC
Created attachment 6265 [details]
PATCH to fix (with some Proposals)
Comment 3 Toshiaki Kamoshida 2003-05-08 09:16:41 UTC
I searched the way to fix it,and found;

To change the selection correctly,we must cange both WindowOneRecord stored at
Workbook and WindowTwoRecord stored at Sheet.

So,we can't make the method for this function at HSSFSheet,but only at 
HSSFWorkbook.

Plz test and eval the PATCH with the testcase a little changed from I submitted.

<s2.setSelected(true);
>b.setSelectedSheet("Sheet1");
or
>b.setSelecteSheetAt((short)1);

Regards.
Comment 4 Toshiaki Kamoshida 2003-05-08 09:32:32 UTC
I know there is an another way,
Add an instance-field "the reference to parent Workbook object" at Sheet,
and once the event to send other Sheets or Workbook happend at a Sheet(like  
Sheet#setSelected(boolean)),the method send the event by using the reference.

I feel it is cool,but I don't know we can cange the reference-structure of 
objects or not...
Comment 5 Toshiaki Kamoshida 2003-05-20 04:43:16 UTC
Sorry,I didn't know that we can select multiple sheet-tabs with "ctrl+mouse 
click"etc. at Excel XP.So this proposal is invalid.Please forget the 
attatchments id=5196 & id=6265.

But "There is no way to change the displayed sheet with usermodel APIs" is an 
another problem.Please add a way to do it.
I found that we can do it with hssf.record.WindowOneRecord#setSelectedTab(short)
Comment 6 Toshiaki Kamoshida 2003-05-20 07:13:15 UTC
Created attachment 6416 [details]
PATCH to do it.
Comment 7 Toshiaki Kamoshida 2003-05-20 07:17:19 UTC
Please apply the PATCH and execute it and check Test1.xls and Test2.xls with 
Excel Application.

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class Test {
	public static void main(String[] args) throws Exception{
		HSSFWorkbook book = new HSSFWorkbook();
		HSSFSheet s1 = book.createSheet("Test1");
		HSSFSheet s2 = book.createSheet("Test2");
		book.write(new FileOutputStream("Test1.xls"));
		s1.setSelected(false);
		s2.setSelected(true);
		book.setSelectedSheet("Test2");
		book.write(new FileOutputStream("Test2.xls"));
	}
}
Comment 8 Jason Height 2006-07-25 12:06:23 UTC
It appears that a variant of the patch 6416 was applied at some stage, but this
bug never closed. The method HSSFWorkbook.setSelectedTab is now available & it
does what the bug decribes.

Closing....

Jason