aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
blob: e65224e72f43a89d9e96651829746a5cfcb394e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* ====================================================================
   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 java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;


public class XSSFRow implements Row, Comparable {

    private CTRow row;
    
    private List<Cell> cells;
    
    private XSSFSheet sheet;
    
    /**
     * Create a new XSSFRow. This method is protected to be used only by
     * tests.
     */
    protected XSSFRow(XSSFSheet sheet) {
        this(CTRow.Factory.newInstance(), sheet);
    }
    
    /**
     * Create a new XSSFRow.
     * 
     * @param row The underlying XMLBeans row.
     * @param sheet The parent sheet.
     */
    public XSSFRow(CTRow row, XSSFSheet sheet) {
        this.row = row;
        this.sheet = sheet;
        this.cells = new LinkedList<Cell>(); 
        for (CTCell c : row.getCArray()) {
            this.cells.add(new XSSFCell(this, c));
        }
        
    }

    public XSSFSheet getSheet() {
        return this.sheet;
    }
    
    public Iterator<Cell> cellIterator() {
        return cells.iterator();
    }
    /**
     * Alias for {@link #cellIterator()} to allow
     *  foreach loops
     */
    public Iterator<Cell> iterator() {
    	return cellIterator();
    }

    public int compareTo(Object obj) {
        XSSFRow loc = (XSSFRow) obj;
        if (this.getRowNum() == loc.getRowNum())
        {
            return 0;
        }
        if (this.getRowNum() < loc.getRowNum())
        {
            return -1;
        }
        if (this.getRowNum() > loc.getRowNum())
        {
            return 1;
        }
        return -1;
    }

    public XSSFCell createCell(int column) {
    	return createCell(column, Cell.CELL_TYPE_BLANK);
    }
    public XSSFCell createCell(short column) {
    	return createCell((int)column);
    }

    /**
     * Add a new empty cell to this row.
     * 
     * @param column Cell column number.
     * @param index Position where to insert cell.
     * @param type TODO
     * @return The new cell.
     */
    protected XSSFCell addCell(int column, int index, int type) {
        CTCell ctcell = row.insertNewC(index);
        XSSFCell xcell = new XSSFCell(this, ctcell);
        xcell.setCellNum(column);
        if (type != Cell.CELL_TYPE_BLANK) {
        	xcell.setCellType(type);
        }
        return xcell;
    }

    public XSSFCell createCell(short column, int type) {
    	return createCell((int)column, type);
    }
    public XSSFCell createCell(int column, int type) {
        int index = 0;
        for (Cell c : this.cells) {
            if (c.getCellNum() == column) {
                // Replace c with new Cell
                XSSFCell xcell = addCell(column, index, type);
                cells.set(index, xcell);
                return xcell;
            }
            if (c.getCellNum() > column) {
                XSSFCell xcell = addCell(column, index, type);
                cells.add(index, xcell);
                return xcell;
            }
            ++index;
        }
        XSSFCell xcell = addCell(column, index, type);
        cells.add(xcell);
        return xcell;
    }

    private XSSFCell retrieveCell(int cellnum) {
        Iterator<Cell> it = cellIterator();
        for ( ; it.hasNext() ; ) {
        	Cell cell = it.next();
        	if (cell.getCellNum() == cellnum) {
        		return (XSSFCell)cell;
        	}
        }
        return null;
    }
    
    /**
     * Returns the cell at the given (0 based) index,
     *  with the {@link MissingCellPolicy} from the
     *  parent Workbook.
     */
    public XSSFCell getCell(int cellnum) {
    	return getCell(cellnum, sheet.getWorkbook().getMissingCellPolicy());
    }
    
    /**
     * Returns the cell at the given (0 based) index,
     *  with the specified {@link MissingCellPolicy}
     */
    public XSSFCell getCell(int cellnum, MissingCellPolicy policy) {
    	XSSFCell cell = retrieveCell(cellnum);
    	if(policy == RETURN_NULL_AND_BLANK) {
    		return cell;
    	}
    	if(policy == RETURN_BLANK_AS_NULL) {
    		if(cell == null) return cell;
    		if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
    			return null;
    		}
    		return cell;
    	}
    	if(policy == CREATE_NULL_AS_BLANK) {
    		if(cell == null) {
    			return createCell((short)cellnum, HSSFCell.CELL_TYPE_BLANK);
    		}
    		return cell;
    	}
    	throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")");
    }

    public short getFirstCellNum() {
    	for (Iterator<Cell> it = cellIterator() ; it.hasNext() ; ) {
    		Cell cell = it.next();
    		if (cell != null) {
    			return cell.getCellNum();
    		}
    	}
    	return -1;
    }

    /**
     * Get the row's height measured in twips (1/20th of a point). If the height is not set, the default worksheet value is returned,
     * See {@link org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()}
     *
     * @return row height measured in twips (1/20th of a point)
     */
    public short getHeight() {
        return (short)(getHeightInPoints()*20);
    }

    /**
     * Returns row height measured in point size. If the height is not set, the default worksheet value is returned,
     * See {@link org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()}
     *
     * @return row height measured in point size
     * @see org.apache.poi.xssf.usermodel.XSSFSheet#getDefaultRowHeightInPoints()
     */
    public float getHeightInPoints() {
    	if (this.row.isSetHt()) {
    		return (float) this.row.getHt();
    	} else {
            return sheet.getDefaultRowHeightInPoints();
        }
    }

    /**
     * Gets the index of the last cell contained in this row <b>PLUS ONE</b>. The result also
     * happens to be the 1-based column number of the last cell.  This value can be used as a
     * standard upper bound when iterating over cells:
     * <pre>
     * short minColIx = row.getFirstCellNum();
     * short maxColIx = row.getLastCellNum();
     * for(short colIx=minColIx; colIx&lt;maxColIx; colIx++) {
     *   XSSFCell cell = row.getCell(colIx);
     *   if(cell == null) {
     *     continue;
     *   }
     *   //... do something with cell
     * }
     * </pre>
     *
     * @return short representing the last logical cell in the row <b>PLUS ONE</b>, or -1 if the
     *  row does not contain any cells.
     */
    public short getLastCellNum() {
    	short lastCellNum = -1;
    	for (Iterator<Cell> it = cellIterator() ; it.hasNext() ; ) {
    		Cell cell = it.next();
    		if (cell != null) {
    			lastCellNum = (short)(cell.getCellNum() + 1);
    		}
    	}
    	return lastCellNum;
    }

    public int getPhysicalNumberOfCells() {
    	int count = 0;
    	for (Iterator<Cell> it = cellIterator() ; it.hasNext() ; ) {
    		if (it.next() != null) {
    			count++;
    		}
    	}
    	return count;
    }

    public int getRowNum() {
        return (int) (row.getR() - 1);
    }

    public boolean getZeroHeight() {
    	return this.row.getHidden();
    }

    public void removeCell(Cell cell) {
    	int counter = 0;
    	for (Iterator<Cell> it = cellIterator(); it.hasNext(); ) {
    		Cell c = it.next();
    		if (c.getCellNum() == cell.getCellNum()) {
    			it.remove();
    			row.removeC(counter);
    			continue;
    		}
    		counter++;
    	}
    }

    /**
     *  Set the height in "twips" or  1/20th of a point.
     *
     * @param height the height in "twips" or  1/20th of a point. <code>-1</code>  resets to the default height
     */
    public void setHeight(short height) {
    	if(height == -1){
            this.row.unsetHt();
            this.row.unsetCustomHeight();
        } else {
            this.row.setHt((double)height/20);
            this.row.setCustomHeight(true);

        }
    }
    /**
     * Set the row's height in points.
     *
     * @param height the height in points. <code>-1</code>  resets to the default height
     */
    public void setHeightInPoints(float height) {
	    setHeight((short)(height*20));
    }

    public void setRowNum(int rowNum) {
        this.row.setR(rowNum + 1);

    }

    public void setZeroHeight(boolean height) {
    	this.row.setHidden(height);

    }
    
    public CTRow getCTRow(){
    	return this.row;
    }

}