From 9874fdf90c8e18608927f4cc2a9307c7fec74abd Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Mon, 29 Sep 2008 04:50:20 +0000 Subject: [PATCH] Applied patch 45899 - XSSFWorkbook getActiveSheetIndex / setActiveSheet git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@699989 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/usermodel/Workbook.java | 11 ++-- .../org/apache/poi/ss/usermodel/Workbook.java | 21 +++---- .../poi/xssf/usermodel/XSSFWorkbook.java | 61 +++++++++++-------- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 13 +++- 4 files changed, 62 insertions(+), 44 deletions(-) diff --git a/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Workbook.java b/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Workbook.java index eca702a379..c4cdd21532 100644 --- a/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Workbook.java @@ -26,6 +26,13 @@ import org.apache.poi.hssf.usermodel.*; * which work properly for both HSSFWorkbook and XSSFWorkbook */ public interface Workbook { + + int getActiveSheetIndex(); + void setActiveSheet(int sheetIndex); + + int getFirstVisibleTab(); + void setFirstVisibleTab(int sheetIndex); + int getNumberOfSheets(); short getNumberOfFonts(); int getNumberOfNames(); @@ -34,15 +41,11 @@ public interface Workbook { HSSFName getNameAt(int index); int getNameIndex(String name); String getNameName(int index); - String resolveNameXText(int refIndex, int definedNameIndex); String getSheetName(int sheet); HSSFSheet getSheetAt(int index); - String findSheetNameFromExternSheet(int externSheetIndex); int getSheetIndex(String name); int getSheetIndex(Sheet sheet); - int getSheetIndexFromExternSheetIndex(int externSheetNumber); - int getExternalSheetIndex(int internalSheetIndex); CreationHelper getCreationHelper(); diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java index f1a9908658..f4defbae65 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java @@ -49,9 +49,15 @@ public interface Workbook { /** PNG format */ public static final int PICTURE_TYPE_PNG = 6; - /** Device independant bitmap */ + /** Device independent bitmap */ public static final int PICTURE_TYPE_DIB = 7; + int getActiveSheetIndex(); + void setActiveSheet(int sheetIndex); + + int getFirstVisibleTab(); + void setFirstVisibleTab(int sheetIndex); + /** * sets the order of appearance for a given sheet. * @@ -80,19 +86,6 @@ public interface Workbook { */ short getSelectedTab(); - /** - * sets the first tab that is displayed in the list of tabs - * in excel. - * @param index - */ - void setDisplayedTab(short index); - - /** - * sets the first tab that is displayed in the list of tabs - * in excel. - */ - short getDisplayedTab(); - /** * set the sheet name. * Will throw IllegalArgumentException if the name is greater than 31 chars diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 62743c6183..141bda2928 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -27,10 +27,8 @@ import java.util.List; import javax.xml.namespace.QName; import org.apache.poi.POIXMLDocument; -import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CommentsSource; import org.apache.poi.ss.usermodel.CreationHelper; -import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Palette; import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.Row; @@ -337,6 +335,18 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { return null; } + /** + * Convenience method to get the active sheet. The active sheet is is the sheet + * which is currently displayed when the workbook is viewed in Excel. + * 'Selected' sheet(s) is a distinct concept. + */ + public int getActiveSheetIndex() { + //activeTab (Active Sheet Index) Specifies an unsignedInt + //that contains the index to the active sheet in this book view. + Long index = workbook.getBookViews().getWorkbookViewArray(0).getActiveTab(); + return index.intValue(); + } + public List getAllEmbeddedObjects() { // TODO Auto-generated method stub return null; @@ -518,6 +528,29 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { this.missingCellPolicy = missingCellPolicy; } + /** + * Convenience method to set the active sheet. The active sheet is is the sheet + * which is currently displayed when the workbook is viewed in Excel. + * 'Selected' sheet(s) is a distinct concept. + */ + public void setActiveSheet(int index) { + + validateSheetIndex(index); + //activeTab (Active Sheet Index) Specifies an unsignedInt that contains the index to the active sheet in this book view. + CTBookView[] arrayBook = workbook.getBookViews().getWorkbookViewArray(); + for (int i = 0; i < arrayBook.length; i++) { + workbook.getBookViews().getWorkbookViewArray(i).setActiveTab(index); + } + } + + private void validateSheetIndex(int index) { + int lastSheetIx = sheets.size() - 1; + if (index < 0 || index > lastSheetIx) { + throw new IllegalArgumentException("Sheet index (" + + index +") is out of range (0.." + lastSheetIx + ")"); + } + } + public void setBackupFlag(boolean backupValue) { // TODO Auto-generated method stub @@ -539,34 +572,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { * * @param index integer that contains the index to the active sheet in this book view. */ - public void setFirstVisibleTab(short index) { + public void setFirstVisibleTab(int index) { CTBookViews bookViews = workbook.getBookViews(); CTBookView bookView= bookViews.getWorkbookViewArray(0); bookView.setActiveTab(index); } - /** - * Gets the first tab that is displayed in the list of tabs - * in excel. - * @return an integer that contains the index to the active sheet in this book view. - * - * @deprecated Aug 2008 - Misleading name - use #getFirstVisibleTab() - */ - public short getDisplayedTab() { - return (short) getFirstVisibleTab(); - } - - /** - * sets the first tab that is displayed in the list of tabs - * in excel. - * @param index integer that contains the index to the active sheet in this book view. - * - * @deprecated Aug 2008 - Misleading name - use #setFirstVisibleTab() - */ - public void setDisplayedTab(short index) { - setFirstVisibleTab(index); - } - public void setPrintArea(int sheetIndex, String reference) { // TODO Auto-generated method stub diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 38769743c1..4bfaf3ac2a 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -43,7 +43,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; public final class TestXSSFWorkbook extends TestCase { @Override - protected void setUp() throws Exception { + protected void setUp() { // Use system out logger System.setProperty( "org.apache.poi.util.POILogger", @@ -51,6 +51,17 @@ public final class TestXSSFWorkbook extends TestCase { ); } + public void testGetSetActiveSheet(){ + XSSFWorkbook workbook = new XSSFWorkbook(); + workbook.createSheet("sheet1"); + workbook.createSheet("sheet2"); + workbook.createSheet("sheet3"); + // set second sheet + workbook.setActiveSheet(1); + // test if second sheet is set up + assertEquals(1, workbook.getActiveSheetIndex()); + } + public void testGetSheetIndex() { XSSFWorkbook workbook = new XSSFWorkbook(); Sheet sheet1 = workbook.createSheet("sheet1"); -- 2.39.5