Tests for XSSFWorkbook.

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@614199 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ugo Cei 2008-01-22 13:28:48 +00:00
parent 5faf83a911
commit bd8185c485
4 changed files with 180 additions and 23 deletions

View File

@ -155,8 +155,7 @@ public class XSSFRow implements Row {
}
public int getRowNum() {
// TODO Auto-generated method stub
return 0;
return (int) (row.getR() - 1);
}
public boolean getZeroHeight() {
@ -188,7 +187,7 @@ public class XSSFRow implements Row {
}
public void setRowNum(int rowNum) {
// TODO Auto-generated method stub
this.row.setR(rowNum + 1);
}

View File

@ -18,6 +18,8 @@
package org.apache.poi.xssf.usermodel;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.poi.hssf.util.PaneInformation;
import org.apache.poi.hssf.util.Region;
@ -44,13 +46,17 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
public class XSSFSheet implements Sheet {
private CTSheet sheet;
private CTWorksheet worksheet;
private List<Row> rows;
public XSSFSheet(CTSheet sheet) {
this.sheet = sheet;
this.worksheet = CTWorksheet.Factory.newInstance();
this.worksheet.addNewSheetData();
this.rows = new LinkedList<Row>();
for (CTRow row : worksheet.getSheetData().getRowArray()) {
this.rows.add(new XSSFRow(row));
}
// XXX ???
CTSheetViews views = this.worksheet.addNewSheetViews();
CTSheetView view = views.addNewSheetView();
@ -109,11 +115,33 @@ public class XSSFSheet implements Sheet {
}
protected XSSFRow addRow(int index, int rownum) {
CTRow row = this.worksheet.getSheetData().insertNewRow(index);
XSSFRow xrow = new XSSFRow(row);
xrow.setRowNum(rownum);
// xrow.setHeight(13.41);
return xrow;
}
public Row createRow(int rownum) {
CTRow row = this.worksheet.getSheetData().insertNewRow(rownum);
row.setR(rownum + 1);
row.setHt(13.41); // XXX ???
return new XSSFRow(row);
int index = 0;
for (Row r : this.rows) {
if (r.getRowNum() == rownum) {
// Replace r with new row
XSSFRow xrow = addRow(index, rownum);
rows.set(index, xrow);
return xrow;
}
if (r.getRowNum() > rownum) {
XSSFRow xrow = addRow(index, rownum);
rows.add(index, xrow);
return xrow;
}
++index;
}
XSSFRow xrow = addRow(index, rownum);
rows.add(xrow);
return xrow;
}
public void createSplitPane(int splitPos, int splitPos2, int leftmostColumn, int topRow, int activePane) {
@ -262,7 +290,12 @@ public class XSSFSheet implements Sheet {
}
public Row getRow(int rownum) {
// TODO Auto-generated method stub
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
Row row = it.next();
if (row.getRowNum() == rownum) {
return row;
}
}
return null;
}
@ -371,9 +404,8 @@ public class XSSFSheet implements Sheet {
}
public Iterator rowIterator() {
// TODO Auto-generated method stub
return null;
public Iterator<Row> rowIterator() {
return rows.iterator();
}
public void setAlternativeExpression(boolean b) {
@ -546,4 +578,32 @@ public class XSSFSheet implements Sheet {
}
public void setTabSelected(boolean flag) {
CTSheetViews views = this.worksheet.getSheetViews();
for (CTSheetView view : views.getSheetViewArray()) {
view.setTabSelected(flag);
}
}
public boolean isTabSelected() {
CTSheetView view = getDefaultSheetView();
return view != null && view.getTabSelected();
}
/**
* Return the default sheet view. This is the last one if the sheet's views, according to sec. 3.3.1.83
* of the OOXML spec: "A single sheet view definition. When more than 1 sheet view is defined in the file,
* it means that when opening the workbook, each sheet view corresponds to a separate window within the
* spreadsheet application, where each window is showing the particular sheet. containing the same
* workbookViewId value, the last sheetView definition is loaded, and the others are discarded.
* When multiple windows are viewing the same sheet, multiple sheetView elements (with corresponding
* workbookView entries) are saved."
*/
private CTSheetView getDefaultSheetView() {
CTSheetViews views = this.worksheet.getSheetViews();
if (views == null || views.getSheetViewArray() == null || views.getSheetViewArray().length <= 0) {
return null;
}
return views.getSheetViewArray(views.getSheetViewArray().length - 1);
}
}

View File

@ -34,6 +34,7 @@ import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Palette;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.openxml4j.exceptions.InvalidFormatException;
import org.openxml4j.opc.Package;
@ -63,6 +64,10 @@ public class XSSFWorkbook implements Workbook {
this.workbook.addNewSheets();
}
protected CTWorkbook getWorkbook() {
return this.workbook;
}
public int addPicture(byte[] pictureData, int format) {
// TODO Auto-generated method stub
return 0;
@ -207,8 +212,14 @@ public class XSSFWorkbook implements Workbook {
}
public short getSelectedTab() {
// TODO Auto-generated method stub
return 0;
short i = 0;
for (XSSFSheet sheet : this.sheets) {
if (sheet.isTabSelected()) {
return i;
}
++i;
}
return -1;
}
public Sheet getSheet(String name) {
@ -217,17 +228,21 @@ public class XSSFWorkbook implements Workbook {
}
public Sheet getSheetAt(int index) {
return this.sheets.get(index - 1);
return this.sheets.get(index);
}
public int getSheetIndex(String name) {
// TODO Auto-generated method stub
return 0;
CTSheet[] sheets = this.workbook.getSheets().getSheetArray();
for (int i = 0 ; i < sheets.length ; ++i) {
if (name.equals(sheets[i].getName())) {
return i;
}
}
return -1;
}
public int getSheetIndex(Sheet sheet) {
// TODO Auto-generated method stub
return 0;
return this.sheets.indexOf(sheet);
}
public String getSheetName(int sheet) {
@ -285,9 +300,14 @@ public class XSSFWorkbook implements Workbook {
}
/**
* We only set one sheet as selected for compatibility with HSSF.
*/
public void setSelectedTab(short index) {
// TODO Auto-generated method stub
for (int i = 0 ; i < this.sheets.size() ; ++i) {
XSSFSheet sheet = this.sheets.get(i);
sheet.setTabSelected(i == index);
}
}
public void setSheetName(int sheet, String name) {
@ -301,8 +321,13 @@ public class XSSFWorkbook implements Workbook {
}
public void setSheetOrder(String sheetname, int pos) {
// TODO Auto-generated method stub
int idx = getSheetIndex(sheetname);
sheets.add(pos, sheets.remove(idx));
// Reorder CTSheets
XmlObject cts = this.workbook.getSheets().getSheetArray(idx).copy();
this.workbook.getSheets().removeSheet(idx);
CTSheet newcts = this.workbook.getSheets().insertNewSheet(pos);
newcts.set(cts);
}
public void unwriteProtectWorkbook() {

View File

@ -0,0 +1,73 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.Sheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
public class TestXSSFWorkbook extends TestCase {
public void testGetSheetIndex() {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
Sheet sheet2 = workbook.createSheet("sheet2");
assertEquals(0, workbook.getSheetIndex(sheet1));
assertEquals(0, workbook.getSheetIndex("sheet1"));
assertEquals(1, workbook.getSheetIndex(sheet2));
assertEquals(1, workbook.getSheetIndex("sheet2"));
assertEquals(-1, workbook.getSheetIndex("noSheet"));
}
public void testSetSheetOrder() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
Sheet sheet2 = workbook.createSheet("sheet2");
assertSame(sheet1, workbook.getSheetAt(0));
assertSame(sheet2, workbook.getSheetAt(1));
workbook.setSheetOrder("sheet2", 0);
assertSame(sheet2, workbook.getSheetAt(0));
assertSame(sheet1, workbook.getSheetAt(1));
// Test reordering of CTSheets
CTWorkbook ctwb = workbook.getWorkbook();
CTSheet[] ctsheets = ctwb.getSheets().getSheetArray();
assertEquals("sheet2", ctsheets[0].getName());
assertEquals("sheet1", ctsheets[1].getName());
// Borderline case: only one sheet
workbook = new XSSFWorkbook();
sheet1 = workbook.createSheet("sheet1");
assertSame(sheet1, workbook.getSheetAt(0));
workbook.setSheetOrder("sheet1", 0);
assertSame(sheet1, workbook.getSheetAt(0));
}
public void testSetSelectedTab() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
Sheet sheet2 = workbook.createSheet("sheet2");
assertEquals(-1, workbook.getSelectedTab());
workbook.setSelectedTab((short) 0);
assertEquals(0, workbook.getSelectedTab());
workbook.setSelectedTab((short) 1);
assertEquals(1, workbook.getSelectedTab());
}
}