ASF Bugzilla – Attachment 21421 Details for
Bug 44291
XSSFSheet get/setColumnWidth
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch with tests and implementations for the listed methods
diff_20080124.txt (text/plain), 8.33 KB, created by
Paolo Mottadelli
on 2008-01-24 06:06:07 UTC
(
hide
)
Description:
Patch with tests and implementations for the listed methods
Filename:
MIME Type:
Creator:
Paolo Mottadelli
Created:
2008-01-24 06:06:07 UTC
Size:
8.33 KB
patch
obsolete
>Index: src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java (revision 0) >+++ src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java (revision 7223) >@@ -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<CTCol> columns; >+ >+ public ColumnHelper(CTWorksheet worksheet) { >+ super(); >+ setColumns(worksheet); >+ } >+ >+ public List<CTCol> getColumns() { >+ return columns; >+ } >+ >+ public void setColumns(CTWorksheet worksheet) { >+ columns = new ArrayList<CTCol>(); >+ 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<CTCol> 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<CTCol> columns, long k) { >+ for (Iterator<CTCol> it = columns.iterator() ; it.hasNext() ; ) { >+ CTCol column = it.next(); >+ if (column.getMin() == k) { >+ return column; >+ } >+ } >+ return null; >+ } >+ >+ public CTCol getColumn(long index) { >+ for (Iterator<CTCol> 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); >+ } >+ >+} >Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (revision 7218) >+++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (working copy) >@@ -31,6 +31,7 @@ > 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 @@ > private CTSheet sheet; > private CTWorksheet worksheet; > private List<Row> rows; >+ private ColumnHelper columnHelper; > > public XSSFSheet(CTSheet sheet) { > this.sheet = sheet; >@@ -84,6 +86,7 @@ > 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 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 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) { >Index: src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java >=================================================================== >--- src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java (revision 0) >+++ src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java (revision 7219) >@@ -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() { >+ ColumnHelper columnHelper = new ColumnHelper(); >+ CTWorksheet worksheet = CTWorksheet.Factory.newInstance(); >+ >+ 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<CTCol> columns = columnHelper.getColumns(); >+ >+ assertEquals(12, columns.size()); >+ assertEquals((double) 14, columnHelper.getColumn(15).getWidth()); >+ } >+ >+} >Index: src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java >=================================================================== >--- src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java (revision 7218) >+++ src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java (working copy) >@@ -111,4 +111,12 @@ > 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)); >+ } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 44291
: 21421