From 751dab9bcdf0da59d74972c2cfe6c1adb203bead Mon Sep 17 00:00:00 2001 From: Danny Mui Date: Fri, 7 Mar 2003 21:52:37 +0000 Subject: [PATCH] Added remove and another PrintArea setting method and documentation update git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353025 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/xdocs/hssf/quick-guide.xml | 9 +- .../org/apache/poi/hssf/model/Workbook.java | 17 +++- .../poi/hssf/usermodel/HSSFWorkbook.java | 82 ++++++++++++++----- .../poi/hssf/usermodel/TestNamedRange.java | 58 +++++++++++++ 4 files changed, 140 insertions(+), 26 deletions(-) diff --git a/src/documentation/xdocs/hssf/quick-guide.xml b/src/documentation/xdocs/hssf/quick-guide.xml index d4d3be4ab9..070a2f5468 100644 --- a/src/documentation/xdocs/hssf/quick-guide.xml +++ b/src/documentation/xdocs/hssf/quick-guide.xml @@ -445,15 +445,18 @@ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Sheet1"); - wb.setPrintArea(0, "Sheet1!$A$1:$C$2"); + wb.setPrintArea(0, "$A$1:$C$2"); //sets the print area for the first sheet - + //Alternatively: + //wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details) // Create various cells and rows for spreadsheet. FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); + + @@ -479,7 +482,7 @@

- The convience functions live in contrib and provide + The convenience functions live in contrib and provide utility features such as setting borders around merged regions and changing style attributes without explicitly creating new styles. diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index f622707d38..c6d38c99ee 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -339,7 +339,7 @@ public class Workbook implements Model { /**Retrieves the Builtin NameRecord that matches the name and index * There shouldn't be too many names to make the sequential search too slow * @param name byte representation of the builtin name to match - * @param sheetIndex zero-based sheet reference + * @param sheetIndex Index to match * @return null if no builtin NameRecord matches */ public NameRecord getSpecificBuiltinRecord(byte name, int sheetIndex) @@ -358,6 +358,21 @@ public class Workbook implements Model { } + /** + * Removes the specified Builtin NameRecord that matches the name and index + * @param name byte representation of the builtin to match + * @param sheetIndex zero-based sheet reference + */ + public void removeBuiltinRecord(byte name, int sheetIndex) { + //the name array is smaller so searching through it should be faster than + //using the findFirstXXXX methods + NameRecord record = getSpecificBuiltinRecord(name, sheetIndex); + if (record != null) { + names.remove(record); + } + + } + public int getNumRecords() { return records.size(); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index e76f9c0147..c45f5eef80 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -1,7 +1,7 @@ /* ==================================================================== * The Apache Software License, Version 1.1 * - * Copyright (c) 2002 The Apache Software Foundation. All rights + * Copyright (c) 2002, 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -59,30 +59,38 @@ */ package org.apache.poi.hssf.usermodel; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.hssf.eventmodel.EventRecordFactory; -import org.apache.poi.hssf.model.Sheet; -import org.apache.poi.hssf.model.Workbook; -import org.apache.poi.hssf.record.*; -import org.apache.poi.hssf.record.formula.MemFuncPtg; -import org.apache.poi.hssf.record.formula.Area3DPtg; -import org.apache.poi.hssf.record.formula.UnionPtg; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.apache.poi.poifs.filesystem.Entry; -import org.apache.poi.poifs.filesystem.DirectoryEntry; -import org.apache.poi.poifs.filesystem.DocumentEntry; -import org.apache.poi.poifs.filesystem.DocumentInputStream; -import org.apache.poi.util.POILogger; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; -import java.util.List; import java.util.Iterator; +import java.util.List; import java.util.Stack; +import org.apache.poi.hssf.eventmodel.EventRecordFactory; +import org.apache.poi.hssf.model.Sheet; +import org.apache.poi.hssf.model.Workbook; +import org.apache.poi.hssf.record.BackupRecord; +import org.apache.poi.hssf.record.ExtendedFormatRecord; +import org.apache.poi.hssf.record.FontRecord; +import org.apache.poi.hssf.record.NameRecord; +import org.apache.poi.hssf.record.RecordFactory; +import org.apache.poi.hssf.record.SSTRecord; +import org.apache.poi.hssf.record.UnknownRecord; +import org.apache.poi.hssf.record.WindowTwoRecord; +import org.apache.poi.hssf.record.formula.Area3DPtg; +import org.apache.poi.hssf.record.formula.MemFuncPtg; +import org.apache.poi.hssf.record.formula.UnionPtg; +import org.apache.poi.hssf.util.CellReference; +import org.apache.poi.poifs.filesystem.DirectoryEntry; +import org.apache.poi.poifs.filesystem.DocumentEntry; +import org.apache.poi.poifs.filesystem.DocumentInputStream; +import org.apache.poi.poifs.filesystem.Entry; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; + /** * High level representation of a workbook. This is the first object most users * will construct whether they are reading or writing a workbook. It is also the @@ -816,7 +824,7 @@ public class HSSFWorkbook /** * Sets the printarea for the sheet provided *

- * i.e. Reference = Sheet2!$A$1:$B$2 + * i.e. Reference = $A$1:$B$2 * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java) * @param reference Valid name Reference for the Print Area */ @@ -829,15 +837,38 @@ public class HSSFWorkbook name = workbook.createBuiltInName(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1); //adding one here because 0 indicates a global named region; doesnt make sense for print areas - HSSFName nameWrapper = new HSSFName(workbook, name); - //the external name does some housekeeping, refactor to lower level? + short externSheetIndex = getWorkbook().checkExternSheet(sheetIndex); + name.setExternSheetNumber(externSheetIndex); + name.setAreaReference(reference); - nameWrapper.setReference(reference); + } + /** + * For the Convenience of Java Programmers maintaining pointers. + * @see setPrintArea(int, String) + * @param sheetIndex Zero-based sheet index (0 = First Sheet) + * @param startColumn Column to begin printarea + * @param endColumn Column to end the printarea + * @param startRow Row to begin the printarea + * @param endRow Row to end the printarea + */ + public void setPrintArea(int sheetIndex, int startColumn, int endColumn, + int startRow, int endRow) { + + //using absolute references because they dont get copied and pasted anyway + CellReference cell = new CellReference(startRow, startColumn, true, true); + String reference = cell.toString(); + + cell = new CellReference(endRow, endColumn, true, true); + reference = reference+":"+cell.toString(); + + setPrintArea(sheetIndex, reference); + } + /** - * Retrieves the reference for the printarea of the specified sheet + * Retrieves the reference for the printarea of the specified sheet, the sheet name is appended to the reference even if it was not specified. * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java) * @return String Null if no print area has been defined */ @@ -850,6 +881,13 @@ public class HSSFWorkbook return name.getAreaReference(workbook.getSheetReferences()); } + /** + * Delete the printarea for the sheet specified + * @param sheetIndex Zero-based sheet index (0 = First Sheet) + */ + public void removePrintArea(int sheetIndex) { + getWorkbook().removeBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1); + } /** creates a new named range and add it to the model * @return named range high level diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java b/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java index d7f4f5f444..5fe50e8bb1 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestNamedRange.java @@ -369,6 +369,26 @@ public class TestNamedRange } + /** + * For Convenience, dont force sheet names to be used + */ + public void testSinglePrintAreaWOSheet() + { + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet("Test Print Area"); + String sheetName = workbook.getSheetName(0); + + String reference = "$A$1:$B$1"; + workbook.setPrintArea(0, reference); + + String retrievedPrintArea = workbook.getPrintArea(0); + + assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); + assertEquals(sheetName+"!"+reference, retrievedPrintArea); + + } + + /** * Test to see if the print area can be retrieved from an excel created file */ @@ -486,6 +506,44 @@ public class TestNamedRange } + + /** + * Tests the setting of print areas with coordinates (Row/Column designations) + * + */ + public void testPrintAreaCoords(){ + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet("Test Print Area"); + String sheetName = workbook.getSheetName(0); + + String reference = sheetName+"!$A$1:$B$1"; + workbook.setPrintArea(0, 0, 1, 0, 0); + + String retrievedPrintArea = workbook.getPrintArea(0); + + assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); + assertEquals(reference, retrievedPrintArea); + } + + /** + * Verifies an existing print area is deleted + * + */ + public void testPrintAreaRemove() { + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet("Test Print Area"); + String sheetName = workbook.getSheetName(0); + + String reference = sheetName+"!$A$1:$B$1"; + workbook.setPrintArea(0, 0, 1, 0, 0); + + String retrievedPrintArea = workbook.getPrintArea(0); + + assertNotNull("Print Area not defined for first sheet", retrievedPrintArea); + + workbook.removePrintArea(0); + assertNull("PrintArea was not removed", workbook.getPrintArea(0)); + } }