View | Details | Raw Unified | Return to bug 44291
Collapse All | Expand All

(-)src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java (+99 lines)
Line 0 Link Here
1
package org.apache.poi.xssf.usermodel.helpers;
2
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6
7
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
8
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
9
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
10
11
public class ColumnHelper {
12
	
13
	private List<CTCol> columns;
14
	
15
    public ColumnHelper(CTWorksheet worksheet) {
16
		super();
17
		setColumns(worksheet);
18
	}
19
20
	public List<CTCol> getColumns() {
21
    	return columns;
22
    }
23
	
24
	public void setColumns(CTWorksheet worksheet) {
25
    	columns = new ArrayList<CTCol>();
26
    	CTCols[] colsArray = worksheet.getColsArray();
27
    	for (int i = 0 ; i < colsArray.length ; i++) {
28
    		CTCols cols = colsArray[i];
29
    		CTCol[] colArray = cols.getColArray();
30
    		for (int y = 0 ; y < colArray.length ; y++) {
31
    		 	CTCol col = colArray[y];
32
    		 	for (long k = col.getMin() ; k <= col.getMax() ; k++) {
33
    		 		setColumn(columns, col, k);
34
    		 	}
35
    		}
36
    	}
37
	}
38
39
	private void setColumn(List<CTCol> columns, CTCol col, long k) {
40
		CTCol column = getColumn(columns, k);
41
		if (column == null) {
42
			column = CTCol.Factory.newInstance();
43
			column.setMin(k);
44
			column.setMax(k);
45
			setColumnAttributes(col, column);
46
			columns.add(column);
47
		}
48
		else {
49
			setColumnAttributes(col, column);
50
		}
51
	}
52
53
	private void setColumnAttributes(CTCol col, CTCol column) {
54
		if (col.getWidth() > 0) {
55
			column.setWidth(col.getWidth());
56
		}
57
		// TODO set all col attributes
58
	}
59
    
60
    public CTCol getColumn(List<CTCol> columns, long k) {
61
    	for (Iterator<CTCol> it = columns.iterator() ; it.hasNext() ; ) {
62
    		CTCol column = it.next();
63
    		if (column.getMin() == k) {
64
    			return column;
65
    		}
66
    	}
67
    	return null;
68
    }
69
    
70
    public CTCol getColumn(long index) {
71
    	for (Iterator<CTCol> it = columns.iterator() ; it.hasNext() ; ) {
72
    		CTCol column = it.next();
73
    		if (getColumnIndex(column) == index) {
74
    			return column;
75
    		}
76
    	}
77
    	return null;
78
    }
79
    
80
    public long getColumnIndex(CTCol column) {
81
    	if (column.getMin() == column.getMax()) {
82
    		return column.getMin();
83
    	}
84
    	return -1;
85
    }
86
    
87
    public CTCol createColumn(long index) {
88
    	CTCol column = CTCol.Factory.newInstance();
89
    	setIndex(column, index);
90
    	columns.add(column);
91
    	return column;
92
    }
93
94
	private void setIndex(CTCol column, long index) {
95
		column.setMin(index);
96
		column.setMax(index);
97
	}
98
99
}
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (-3 / +9 lines)
Lines 31-36 Link Here
31
import org.apache.poi.ss.usermodel.PrintSetup;
31
import org.apache.poi.ss.usermodel.PrintSetup;
32
import org.apache.poi.ss.usermodel.Row;
32
import org.apache.poi.ss.usermodel.Row;
33
import org.apache.poi.ss.usermodel.Sheet;
33
import org.apache.poi.ss.usermodel.Sheet;
34
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
34
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
35
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
35
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
36
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
36
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
37
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
Lines 48-53 Link Here
48
    private CTSheet sheet;
49
    private CTSheet sheet;
49
    private CTWorksheet worksheet;
50
    private CTWorksheet worksheet;
50
    private List<Row> rows;
51
    private List<Row> rows;
52
    private ColumnHelper columnHelper;
51
    
53
    
52
    public XSSFSheet(CTSheet sheet) {
54
    public XSSFSheet(CTSheet sheet) {
53
        this.sheet = sheet;
55
        this.sheet = sheet;
Lines 84-89 Link Here
84
        CTHeaderFooter hf = this.worksheet.addNewHeaderFooter();
86
        CTHeaderFooter hf = this.worksheet.addNewHeaderFooter();
85
        hf.setOddHeader("&amp;C&amp;A");
87
        hf.setOddHeader("&amp;C&amp;A");
86
        hf.setOddFooter("&amp;C&amp;\"Arial\"&amp;10Page &amp;P");
88
        hf.setOddFooter("&amp;C&amp;\"Arial\"&amp;10Page &amp;P");
89
        columnHelper = new ColumnHelper(worksheet);
87
    }
90
    }
88
91
89
    protected CTSheet getSheet() {
92
    protected CTSheet getSheet() {
Lines 184-191 Link Here
184
    }
187
    }
185
188
186
    public short getColumnWidth(short column) {
189
    public short getColumnWidth(short column) {
187
    	// TODO Auto-generated method stub
190
    	return (short) columnHelper.getColumn(column).getWidth();
188
    	return 0;
189
    }
191
    }
190
192
191
    public short getDefaultColumnWidth() {
193
    public short getDefaultColumnWidth() {
Lines 440-446 Link Here
440
    }
442
    }
441
443
442
    public void setColumnWidth(short column, short width) {
444
    public void setColumnWidth(short column, short width) {
443
        // TODO Auto-generated method stub
445
    	CTCol col = columnHelper.getColumn(column);
446
    	if (col == null) {
447
    		col = columnHelper.createColumn(column);
448
    	}
449
    	col.setWidth(width);
444
    }
450
    }
445
451
446
    public void setDefaultColumnStyle(short column, CellStyle style) {
452
    public void setDefaultColumnStyle(short column, CellStyle style) {
(-)src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java (+71 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.xssf.usermodel.helpers;
19
20
import java.util.ArrayList;
21
import java.util.Calendar;
22
import java.util.Date;
23
import java.util.Iterator;
24
import java.util.List;
25
26
import junit.framework.TestCase;
27
28
import org.apache.poi.ss.usermodel.Cell;
29
import org.apache.poi.ss.usermodel.SharedStringSource;
30
import org.apache.poi.ss.usermodel.Sheet;
31
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
32
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
33
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
34
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
35
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
36
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
37
38
39
public class TestColumnHelper extends TestCase {
40
	
41
	public void testGetColumnList() {
42
		ColumnHelper columnHelper = new ColumnHelper();
43
		CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
44
		
45
		CTCols cols1 = worksheet.addNewCols();
46
		CTCols cols2 = worksheet.addNewCols();
47
		
48
		CTCol col1_1 = cols1.addNewCol();
49
		col1_1.setMin(1);
50
		col1_1.setMax(10);
51
		col1_1.setWidth(13);
52
		CTCol col1_2 = cols1.addNewCol();
53
		col1_2.setMin(15);
54
		col1_2.setMax(15);
55
		col1_2.setWidth(14);
56
57
		CTCol col2_1 = cols2.addNewCol();
58
		col2_1.setMin(6);
59
		col2_1.setMax(10);
60
		CTCol col2_2 = cols2.addNewCol();
61
		col2_2.setMin(20);
62
		col2_2.setMax(20);
63
		
64
		columnHelper.setColumns(worksheet);
65
		List<CTCol> columns = columnHelper.getColumns();
66
		
67
		assertEquals(12, columns.size());
68
		assertEquals((double) 14, columnHelper.getColumn(15).getWidth());
69
	}
70
	
71
}
(-)src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java (+8 lines)
Lines 111-114 Link Here
111
		sheet.setDefaultColumnWidth((short) 14);
111
		sheet.setDefaultColumnWidth((short) 14);
112
		assertEquals((short) 14, sheet.getDefaultColumnWidth());
112
		assertEquals((short) 14, sheet.getDefaultColumnWidth());
113
	}
113
	}
114
	
115
	public void testGetSetColumnWidth() throws Exception {
116
		XSSFWorkbook workbook = new XSSFWorkbook();
117
		Sheet sheet = workbook.createSheet("Sheet 1");
118
		// Test setting a column width and getting that value
119
		sheet.setColumnWidth((short) 0, (short) 16);
120
		assertEquals(16, sheet.getColumnWidth((short) 0));
121
	}
114
}
122
}

Return to bug 44291