From c2d4b8b736a1b89901ae33ba66b3731c6d45e500 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 25 Jan 2008 16:25:14 +0000 Subject: [PATCH] [ooxml branch] XSSFSheet get/setColumnWidth methods and tests, patch from Paolo git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@615258 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/usermodel/XSSFSheet.java | 12 ++- .../xssf/usermodel/helpers/ColumnHelper.java | 99 +++++++++++++++++++ .../poi/xssf/usermodel/TestXSSFSheet.java | 8 ++ .../usermodel/helpers/TestColumnHelper.java | 71 +++++++++++++ 4 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java create mode 100644 src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 9c2762142..75af71a9e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -31,6 +31,7 @@ import org.apache.poi.ss.usermodel.Patriarch; import org.apache.poi.ss.usermodel.PrintSetup; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.helpers.ColumnHelper; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter; @@ -48,6 +49,7 @@ public class XSSFSheet implements Sheet { private CTSheet sheet; private CTWorksheet worksheet; private List rows; + private ColumnHelper columnHelper; public XSSFSheet(CTSheet sheet) { this.sheet = sheet; @@ -84,6 +86,7 @@ public class XSSFSheet implements Sheet { CTHeaderFooter hf = this.worksheet.addNewHeaderFooter(); hf.setOddHeader("&C&A"); hf.setOddFooter("&C&\"Arial\"&10Page &P"); + columnHelper = new ColumnHelper(worksheet); } protected CTSheet getSheet() { @@ -184,8 +187,7 @@ public class XSSFSheet implements Sheet { } public short getColumnWidth(short column) { - // TODO Auto-generated method stub - return 0; + return (short) columnHelper.getColumn(column).getWidth(); } public short getDefaultColumnWidth() { @@ -440,7 +442,11 @@ public class XSSFSheet implements Sheet { } public void setColumnWidth(short column, short width) { - // TODO Auto-generated method stub + CTCol col = columnHelper.getColumn(column); + if (col == null) { + col = columnHelper.createColumn(column); + } + col.setWidth(width); } public void setDefaultColumnStyle(short column, CellStyle style) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java new file mode 100644 index 000000000..a05a3e6db --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java @@ -0,0 +1,99 @@ +package org.apache.poi.xssf.usermodel.helpers; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; + +public class ColumnHelper { + + private List columns; + + public ColumnHelper(CTWorksheet worksheet) { + super(); + setColumns(worksheet); + } + + public List getColumns() { + return columns; + } + + public void setColumns(CTWorksheet worksheet) { + columns = new ArrayList(); + CTCols[] colsArray = worksheet.getColsArray(); + for (int i = 0 ; i < colsArray.length ; i++) { + CTCols cols = colsArray[i]; + CTCol[] colArray = cols.getColArray(); + for (int y = 0 ; y < colArray.length ; y++) { + CTCol col = colArray[y]; + for (long k = col.getMin() ; k <= col.getMax() ; k++) { + setColumn(columns, col, k); + } + } + } + } + + private void setColumn(List columns, CTCol col, long k) { + CTCol column = getColumn(columns, k); + if (column == null) { + column = CTCol.Factory.newInstance(); + column.setMin(k); + column.setMax(k); + setColumnAttributes(col, column); + columns.add(column); + } + else { + setColumnAttributes(col, column); + } + } + + private void setColumnAttributes(CTCol col, CTCol column) { + if (col.getWidth() > 0) { + column.setWidth(col.getWidth()); + } + // TODO set all col attributes + } + + public CTCol getColumn(List columns, long k) { + for (Iterator it = columns.iterator() ; it.hasNext() ; ) { + CTCol column = it.next(); + if (column.getMin() == k) { + return column; + } + } + return null; + } + + public CTCol getColumn(long index) { + for (Iterator it = columns.iterator() ; it.hasNext() ; ) { + CTCol column = it.next(); + if (getColumnIndex(column) == index) { + return column; + } + } + return null; + } + + public long getColumnIndex(CTCol column) { + if (column.getMin() == column.getMax()) { + return column.getMin(); + } + return -1; + } + + public CTCol createColumn(long index) { + CTCol column = CTCol.Factory.newInstance(); + setIndex(column, index); + columns.add(column); + return column; + } + + private void setIndex(CTCol column, long index) { + column.setMin(index); + column.setMax(index); + } + +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index f0a9f37ac..38a287a31 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -111,4 +111,12 @@ public class TestXSSFSheet extends TestCase { sheet.setDefaultColumnWidth((short) 14); assertEquals((short) 14, sheet.getDefaultColumnWidth()); } + + public void testGetSetColumnWidth() throws Exception { + XSSFWorkbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet("Sheet 1"); + // Test setting a column width and getting that value + sheet.setColumnWidth((short) 0, (short) 16); + assertEquals(16, sheet.getColumnWidth((short) 0)); + } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java new file mode 100644 index 000000000..8c799a542 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java @@ -0,0 +1,71 @@ +/* ==================================================================== + 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.helpers; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.SharedStringSource; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; + + +public class TestColumnHelper extends TestCase { + + public void testGetColumnList() { + CTWorksheet worksheet = CTWorksheet.Factory.newInstance(); + ColumnHelper columnHelper = new ColumnHelper(worksheet); + + CTCols cols1 = worksheet.addNewCols(); + CTCols cols2 = worksheet.addNewCols(); + + CTCol col1_1 = cols1.addNewCol(); + col1_1.setMin(1); + col1_1.setMax(10); + col1_1.setWidth(13); + CTCol col1_2 = cols1.addNewCol(); + col1_2.setMin(15); + col1_2.setMax(15); + col1_2.setWidth(14); + + CTCol col2_1 = cols2.addNewCol(); + col2_1.setMin(6); + col2_1.setMax(10); + CTCol col2_2 = cols2.addNewCol(); + col2_2.setMin(20); + col2_2.setMax(20); + + columnHelper.setColumns(worksheet); + List columns = columnHelper.getColumns(); + + assertEquals(12, columns.size()); + assertEquals((double) 14, columnHelper.getColumn(15).getWidth()); + } + +}