Lines 21-27
Link Here
|
21 |
|
21 |
|
22 |
import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows; |
22 |
import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows; |
23 |
import org.apache.poi.ss.usermodel.Cell; |
23 |
import org.apache.poi.ss.usermodel.Cell; |
|
|
24 |
import org.apache.poi.ss.usermodel.CellStyle; |
24 |
import org.apache.poi.ss.usermodel.Comment; |
25 |
import org.apache.poi.ss.usermodel.Comment; |
|
|
26 |
import org.apache.poi.ss.usermodel.CreationHelper; |
27 |
import org.apache.poi.ss.usermodel.Font; |
28 |
import org.apache.poi.ss.usermodel.Hyperlink; |
29 |
import org.apache.poi.ss.usermodel.IndexedColors; |
25 |
import org.apache.poi.ss.usermodel.Row; |
30 |
import org.apache.poi.ss.usermodel.Row; |
26 |
import org.apache.poi.ss.usermodel.Sheet; |
31 |
import org.apache.poi.ss.usermodel.Sheet; |
27 |
import org.apache.poi.ss.usermodel.Workbook; |
32 |
import org.apache.poi.ss.usermodel.Workbook; |
Lines 366-369
Link Here
|
366 |
|
371 |
|
367 |
wb.close(); |
372 |
wb.close(); |
368 |
} |
373 |
} |
|
|
374 |
|
375 |
public void testBug46742_shiftHyperlinks() throws IOException { |
376 |
XSSFWorkbook wb = new XSSFWorkbook(); |
377 |
Sheet sheet = wb.createSheet("test"); |
378 |
Row row = sheet.createRow(0); |
379 |
|
380 |
// How to create hyperlinks |
381 |
// https://poi.apache.org/spreadsheet/quick-guide.html#Hyperlinks |
382 |
XSSFCreationHelper helper = wb.getCreationHelper(); |
383 |
CellStyle hlinkStyle = wb.createCellStyle(); |
384 |
Font hlinkFont = wb.createFont(); |
385 |
hlinkFont.setUnderline(Font.U_SINGLE); |
386 |
hlinkFont.setColor(IndexedColors.BLUE.getIndex()); |
387 |
hlinkStyle.setFont(hlinkFont); |
388 |
|
389 |
// 3D relative document link |
390 |
Cell cell = row.createCell(0); |
391 |
cell.setCellStyle(hlinkStyle); |
392 |
createHyperlink(helper, cell, Hyperlink.LINK_DOCUMENT, "test!E1"); |
393 |
|
394 |
// URL |
395 |
cell = row.createCell(1); |
396 |
cell.setCellStyle(hlinkStyle); |
397 |
createHyperlink(helper, cell, Hyperlink.LINK_URL, "http://poi.apache.org/"); |
398 |
|
399 |
// row0 will be shifted on top of row1, so this URL should be removed from the workbook |
400 |
Row overwrittenRow = sheet.createRow(3); |
401 |
cell = overwrittenRow.createCell(2); |
402 |
cell.setCellStyle(hlinkStyle); |
403 |
createHyperlink(helper, cell, Hyperlink.LINK_EMAIL, "mailto:poi@apache.org"); |
404 |
|
405 |
// hyperlinks on this row are unaffected by the row shifting, so the hyperlinks should not move |
406 |
Row unaffectedRow = sheet.createRow(20); |
407 |
cell = unaffectedRow.createCell(3); |
408 |
cell.setCellStyle(hlinkStyle); |
409 |
createHyperlink(helper, cell, Hyperlink.LINK_FILE, "54524.xlsx"); |
410 |
|
411 |
cell = wb.createSheet("other").createRow(0).createCell(0); |
412 |
cell.setCellStyle(hlinkStyle); |
413 |
createHyperlink(helper, cell, Hyperlink.LINK_URL, "http://apache.org/"); |
414 |
|
415 |
int startRow = 0; |
416 |
int endRow = 0; |
417 |
int n = 3; |
418 |
sheet.shiftRows(startRow, endRow, n); |
419 |
|
420 |
XSSFWorkbook read = XSSFTestDataSamples.writeOutAndReadBack(wb); |
421 |
wb.close(); |
422 |
|
423 |
XSSFSheet sh = read.getSheet("test"); |
424 |
|
425 |
Row shiftedRow = sh.getRow(3); |
426 |
|
427 |
// document link anchored on a shifted cell should be moved |
428 |
// Note that hyperlinks do not track what they point to, so this hyperlink should still refer to test!E1 |
429 |
verifyHyperlink(shiftedRow.getCell(0), Hyperlink.LINK_DOCUMENT, "test!E1"); |
430 |
|
431 |
// URL, EMAIL, and FILE links anchored on a shifted cell should be moved |
432 |
verifyHyperlink(shiftedRow.getCell(1), Hyperlink.LINK_URL, "http://poi.apache.org/"); |
433 |
|
434 |
// Make sure hyperlinks were moved and not copied |
435 |
assertNull(sh.getRow(0)); |
436 |
|
437 |
// Make sure hyperlink in overwritten row is deleted |
438 |
assertEquals(3, sh.getHyperlinkList().size()); |
439 |
for (XSSFHyperlink link : sh.getHyperlinkList()) { |
440 |
if ("C4".equals(link.getCellRef())) { |
441 |
fail("Row 4, including the hyperlink at C4, should have been deleted when Row 1 was shifted on top of it."); |
442 |
} |
443 |
} |
444 |
|
445 |
// Make sure unaffected rows are not shifted |
446 |
Cell unaffectedCell = sh.getRow(20).getCell(3); |
447 |
assertTrue(cellHasHyperlink(unaffectedCell)); |
448 |
verifyHyperlink(unaffectedCell, Hyperlink.LINK_FILE, "54524.xlsx"); |
449 |
|
450 |
// Make sure cells on other sheets are not affected |
451 |
unaffectedCell = read.getSheet("other").getRow(0).getCell(0); |
452 |
assertTrue(cellHasHyperlink(unaffectedCell)); |
453 |
verifyHyperlink(unaffectedCell, Hyperlink.LINK_URL, "http://apache.org/"); |
454 |
|
455 |
read.close(); |
456 |
} |
457 |
|
458 |
private void createHyperlink(CreationHelper helper, Cell cell, int linkType, String ref) { |
459 |
cell.setCellValue(ref); |
460 |
Hyperlink link = helper.createHyperlink(linkType); |
461 |
link.setAddress(ref); |
462 |
cell.setHyperlink(link); |
463 |
} |
464 |
|
465 |
private void verifyHyperlink(Cell cell, int linkType, String ref) { |
466 |
assertTrue(cellHasHyperlink(cell)); |
467 |
Hyperlink link = cell.getHyperlink(); |
468 |
assertEquals(linkType, link.getType()); |
469 |
assertEquals(ref, link.getAddress()); |
470 |
} |
471 |
|
472 |
private boolean cellHasHyperlink(Cell cell) { |
473 |
return (cell != null) && (cell.getHyperlink() != null); |
474 |
} |
369 |
} |
475 |
} |