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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
/* ====================================================================
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 static org.junit.Assert.*;
import java.io.IOException;
import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.junit.Test;
/**
* @author Yegor Kozlov
*/
public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows {
public TestXSSFSheetShiftRows(){
super(XSSFITestDataProvider.instance);
}
@Override
@Test
public void testShiftRowBreaks() { // disabled test from superclass
// TODO - support shifting of page breaks
}
@Test
public void testBug54524() throws IOException {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("54524.xlsx");
XSSFSheet sheet = workbook.getSheetAt(0);
sheet.shiftRows(3, 5, -1);
Cell cell = CellUtil.getCell(sheet.getRow(1), 0);
assertEquals(1.0, cell.getNumericCellValue(), 0);
cell = CellUtil.getCell(sheet.getRow(2), 0);
assertEquals("SUM(A2:A2)", cell.getCellFormula());
cell = CellUtil.getCell(sheet.getRow(3), 0);
assertEquals("X", cell.getStringCellValue());
workbook.close();
}
@Test
public void testBug53798() throws IOException {
// NOTE that for HSSF (.xls) negative shifts combined with positive ones do work as expected
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
Sheet testSheet = wb.getSheetAt(0);
// 1) corrupted xlsx (unreadable data in the first row of a shifted group) already comes about
// when shifted by less than -1 negative amount (try -2)
testSheet.shiftRows(3, 3, -2);
Row newRow = null; Cell newCell = null;
// 2) attempt to create a new row IN PLACE of a removed row by a negative shift causes corrupted
// xlsx file with unreadable data in the negative shifted row.
// NOTE it's ok to create any other row.
newRow = testSheet.createRow(3);
newCell = newRow.createCell(0);
newCell.setCellValue("new Cell in row "+newRow.getRowNum());
// 3) once a negative shift has been made any attempt to shift another group of rows
// (note: outside of previously negative shifted rows) by a POSITIVE amount causes POI exception:
// org.apache.xmlbeans.impl.values.XmlValueDisconnectedException.
// NOTE: another negative shift on another group of rows is successful, provided no new rows in
// place of previously shifted rows were attempted to be created as explained above.
testSheet.shiftRows(6, 7, 1); // -- CHANGE the shift to positive once the behaviour of
// the above has been tested
//saveReport(wb, new File("/tmp/53798.xlsx"));
Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
assertNotNull(read);
Sheet readSheet = read.getSheetAt(0);
verifyCellContent(readSheet, 0, "0.0");
verifyCellContent(readSheet, 1, "3.0");
verifyCellContent(readSheet, 2, "2.0");
verifyCellContent(readSheet, 3, "new Cell in row 3");
verifyCellContent(readSheet, 4, "4.0");
verifyCellContent(readSheet, 5, "5.0");
verifyCellContent(readSheet, 6, null);
verifyCellContent(readSheet, 7, "6.0");
verifyCellContent(readSheet, 8, "7.0");
read.close();
}
private void verifyCellContent(Sheet readSheet, int row, String expect) {
Row readRow = readSheet.getRow(row);
if(expect == null) {
assertNull(readRow);
return;
}
Cell readCell = readRow.getCell(0);
if(readCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
assertEquals(expect, Double.toString(readCell.getNumericCellValue()));
} else {
assertEquals(expect, readCell.getStringCellValue());
}
}
@Test
public void testBug53798a() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
Sheet testSheet = wb.getSheetAt(0);
testSheet.shiftRows(3, 3, -1);
for (Row r : testSheet) {
r.getRowNum();
}
testSheet.shiftRows(6, 6, 1);
//saveReport(wb, new File("/tmp/53798.xlsx"));
Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
assertNotNull(read);
Sheet readSheet = read.getSheetAt(0);
verifyCellContent(readSheet, 0, "0.0");
verifyCellContent(readSheet, 1, "1.0");
verifyCellContent(readSheet, 2, "3.0");
verifyCellContent(readSheet, 3, null);
verifyCellContent(readSheet, 4, "4.0");
verifyCellContent(readSheet, 5, "5.0");
verifyCellContent(readSheet, 6, null);
verifyCellContent(readSheet, 7, "6.0");
verifyCellContent(readSheet, 8, "8.0");
read.close();
}
@Test
public void testBug56017() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56017.xlsx");
Sheet sheet = wb.getSheetAt(0);
Comment comment = sheet.getCellComment(0, 0);
assertNotNull(comment);
assertEquals("Amdocs", comment.getAuthor());
assertEquals("Amdocs:\ntest\n", comment.getString().getString());
sheet.shiftRows(0, 1, 1);
// comment in row 0 is gone
comment = sheet.getCellComment(0, 0);
assertNull(comment);
// comment is now in row 1
comment = sheet.getCellComment(1, 0);
assertNotNull(comment);
assertEquals("Amdocs", comment.getAuthor());
assertEquals("Amdocs:\ntest\n", comment.getString().getString());
// FileOutputStream outputStream = new FileOutputStream("/tmp/56017.xlsx");
// try {
// wb.write(outputStream);
// } finally {
// outputStream.close();
// }
Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
assertNotNull(wbBack);
Sheet sheetBack = wbBack.getSheetAt(0);
// comment in row 0 is gone
comment = sheetBack.getCellComment(0, 0);
assertNull(comment);
// comment is now in row 1
comment = sheetBack.getCellComment(1, 0);
assertNotNull(comment);
assertEquals("Amdocs", comment.getAuthor());
assertEquals("Amdocs:\ntest\n", comment.getString().getString());
wbBack.close();
}
@Test
public void test57171() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
assertEquals(5, wb.getActiveSheetIndex());
removeAllSheetsBut(5, wb); // 5 is the active / selected sheet
assertEquals(0, wb.getActiveSheetIndex());
Workbook wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
assertEquals(0, wbRead.getActiveSheetIndex());
wbRead.removeSheetAt(0);
assertEquals(0, wbRead.getActiveSheetIndex());
//wb.write(new FileOutputStream("/tmp/57171.xls"));
wbRead.close();
}
@Test
public void test57163() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
assertEquals(5, wb.getActiveSheetIndex());
wb.removeSheetAt(0);
assertEquals(4, wb.getActiveSheetIndex());
//wb.write(new FileOutputStream("/tmp/57163.xls"));
wb.close();
}
@Test
public void testSetSheetOrderAndAdjustActiveSheet() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
assertEquals(5, wb.getActiveSheetIndex());
// move the sheets around in all possible combinations to check that the active sheet
// is set correctly in all cases
wb.setSheetOrder(wb.getSheetName(5), 4);
assertEquals(4, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(5), 5);
assertEquals(4, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(3), 5);
assertEquals(3, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(4), 5);
assertEquals(3, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(2), 2);
assertEquals(3, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(2), 1);
assertEquals(3, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(3), 5);
assertEquals(5, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(0), 5);
assertEquals(4, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(0), 5);
assertEquals(3, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(0), 5);
assertEquals(2, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(0), 5);
assertEquals(1, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(0), 5);
assertEquals(0, wb.getActiveSheetIndex());
wb.setSheetOrder(wb.getSheetName(0), 5);
assertEquals(5, wb.getActiveSheetIndex());
wb.close();
}
@Test
public void testRemoveSheetAndAdjustActiveSheet() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
assertEquals(5, wb.getActiveSheetIndex());
wb.removeSheetAt(0);
assertEquals(4, wb.getActiveSheetIndex());
wb.setActiveSheet(3);
assertEquals(3, wb.getActiveSheetIndex());
wb.removeSheetAt(4);
assertEquals(3, wb.getActiveSheetIndex());
wb.removeSheetAt(3);
assertEquals(2, wb.getActiveSheetIndex());
wb.removeSheetAt(0);
assertEquals(1, wb.getActiveSheetIndex());
wb.removeSheetAt(1);
assertEquals(0, wb.getActiveSheetIndex());
wb.removeSheetAt(0);
assertEquals(0, wb.getActiveSheetIndex());
try {
wb.removeSheetAt(0);
fail("Should catch exception as no more sheets are there");
} catch (IllegalArgumentException e) {
// expected
}
assertEquals(0, wb.getActiveSheetIndex());
wb.createSheet();
assertEquals(0, wb.getActiveSheetIndex());
wb.removeSheetAt(0);
assertEquals(0, wb.getActiveSheetIndex());
wb.close();
}
// TODO: enable when bug 57165 is fixed
@Test
public void test57165() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
assertEquals(5, wb.getActiveSheetIndex());
removeAllSheetsBut(3, wb);
assertEquals(0, wb.getActiveSheetIndex());
wb.createSheet("New Sheet1");
assertEquals(0, wb.getActiveSheetIndex());
wb.cloneSheet(0); // Throws exception here
wb.setSheetName(1, "New Sheet");
assertEquals(0, wb.getActiveSheetIndex());
//wb.write(new FileOutputStream("/tmp/57165.xls"));
wb.close();
}
// public void test57165b() throws IOException {
// Workbook wb = new XSSFWorkbook();
// try {
// wb.createSheet("New Sheet 1");
// wb.createSheet("New Sheet 2");
// } finally {
// wb.close();
// }
// }
private static void removeAllSheetsBut(int sheetIndex, Workbook wb) {
int sheetNb = wb.getNumberOfSheets();
// Move this sheet at the first position
wb.setSheetOrder(wb.getSheetName(sheetIndex), 0);
// Must make this sheet active (otherwise, for XLSX, Excel might protest that active sheet no longer exists)
// I think POI should automatically handle this case when deleting sheets...
// wb.setActiveSheet(0);
for (int sn = sheetNb - 1; sn > 0; sn--)
{
wb.removeSheetAt(sn);
}
}
@Test
public void testBug57828_OnlyOneCommentShiftedInRow() throws IOException {
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57828.xlsx");
XSSFSheet sheet = wb.getSheetAt(0);
Comment comment1 = sheet.getCellComment(2, 1);
assertNotNull(comment1);
Comment comment2 = sheet.getCellComment(2, 2);
assertNotNull(comment2);
Comment comment3 = sheet.getCellComment(1, 1);
assertNull("NO comment in (1,1) and it should be null", comment3);
sheet.shiftRows(2, 2, -1);
comment3 = sheet.getCellComment(1, 1);
assertNotNull("Comment in (2,1) moved to (1,1) so its not null now.", comment3);
comment1 = sheet.getCellComment(2, 1);
assertNull("No comment currently in (2,1) and hence it is null", comment1);
comment2 = sheet.getCellComment(1, 2);
assertNotNull("Comment in (2,2) should have moved as well because of shift rows. But its not", comment2);
// OutputStream stream = new FileOutputStream("/tmp/57828.xlsx");
// try {
// wb.write(stream);
// } finally {
// stream.close();
// }
wb.close();
}
@Test
public void testBug46742_shiftHyperlinks() throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("test");
Row row = sheet.createRow(0);
// How to create hyperlinks
// https://poi.apache.org/spreadsheet/quick-guide.html#Hyperlinks
XSSFCreationHelper helper = wb.getCreationHelper();
CellStyle hlinkStyle = wb.createCellStyle();
Font hlinkFont = wb.createFont();
hlinkFont.setUnderline(Font.U_SINGLE);
hlinkFont.setColor(IndexedColors.BLUE.getIndex());
hlinkStyle.setFont(hlinkFont);
// 3D relative document link
Cell cell = row.createCell(0);
cell.setCellStyle(hlinkStyle);
createHyperlink(helper, cell, Hyperlink.LINK_DOCUMENT, "test!E1");
// URL
cell = row.createCell(1);
cell.setCellStyle(hlinkStyle);
createHyperlink(helper, cell, Hyperlink.LINK_URL, "http://poi.apache.org/");
// row0 will be shifted on top of row1, so this URL should be removed from the workbook
Row overwrittenRow = sheet.createRow(3);
cell = overwrittenRow.createCell(2);
cell.setCellStyle(hlinkStyle);
createHyperlink(helper, cell, Hyperlink.LINK_EMAIL, "mailto:poi@apache.org");
// hyperlinks on this row are unaffected by the row shifting, so the hyperlinks should not move
Row unaffectedRow = sheet.createRow(20);
cell = unaffectedRow.createCell(3);
cell.setCellStyle(hlinkStyle);
createHyperlink(helper, cell, Hyperlink.LINK_FILE, "54524.xlsx");
cell = wb.createSheet("other").createRow(0).createCell(0);
cell.setCellStyle(hlinkStyle);
createHyperlink(helper, cell, Hyperlink.LINK_URL, "http://apache.org/");
int startRow = 0;
int endRow = 0;
int n = 3;
sheet.shiftRows(startRow, endRow, n);
XSSFWorkbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
wb.close();
XSSFSheet sh = read.getSheet("test");
Row shiftedRow = sh.getRow(3);
// document link anchored on a shifted cell should be moved
// Note that hyperlinks do not track what they point to, so this hyperlink should still refer to test!E1
verifyHyperlink(shiftedRow.getCell(0), Hyperlink.LINK_DOCUMENT, "test!E1");
// URL, EMAIL, and FILE links anchored on a shifted cell should be moved
verifyHyperlink(shiftedRow.getCell(1), Hyperlink.LINK_URL, "http://poi.apache.org/");
// Make sure hyperlinks were moved and not copied
assertNull(sh.getRow(0));
// Make sure hyperlink in overwritten row is deleted
assertEquals(3, sh.getHyperlinkList().size());
for (XSSFHyperlink link : sh.getHyperlinkList()) {
if ("C4".equals(link.getCellRef())) {
fail("Row 4, including the hyperlink at C4, should have been deleted when Row 1 was shifted on top of it.");
}
}
// Make sure unaffected rows are not shifted
Cell unaffectedCell = sh.getRow(20).getCell(3);
assertTrue(cellHasHyperlink(unaffectedCell));
verifyHyperlink(unaffectedCell, Hyperlink.LINK_FILE, "54524.xlsx");
// Make sure cells on other sheets are not affected
unaffectedCell = read.getSheet("other").getRow(0).getCell(0);
assertTrue(cellHasHyperlink(unaffectedCell));
verifyHyperlink(unaffectedCell, Hyperlink.LINK_URL, "http://apache.org/");
read.close();
}
private void createHyperlink(CreationHelper helper, Cell cell, int linkType, String ref) {
cell.setCellValue(ref);
Hyperlink link = helper.createHyperlink(linkType);
link.setAddress(ref);
cell.setHyperlink(link);
}
private void verifyHyperlink(Cell cell, int linkType, String ref) {
assertTrue(cellHasHyperlink(cell));
Hyperlink link = cell.getHyperlink();
assertEquals(linkType, link.getType());
assertEquals(ref, link.getAddress());
}
private boolean cellHasHyperlink(Cell cell) {
return (cell != null) && (cell.getHyperlink() != null);
}
}
|