2009-05-20 20:55:56 -04:00
|
|
|
/* ====================================================================
|
|
|
|
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;
|
|
|
|
|
2016-09-21 01:20:36 -04:00
|
|
|
import static org.apache.poi.POITestCase.skipTest;
|
|
|
|
import static org.apache.poi.POITestCase.testPassesNow;
|
2016-03-20 20:14:21 -04:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertNull;
|
|
|
|
import static org.junit.Assert.fail;
|
2015-10-31 23:23:16 -04:00
|
|
|
|
2013-08-26 16:09:19 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2009-05-20 20:55:56 -04:00
|
|
|
import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows;
|
2013-08-26 16:09:19 -04:00
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
2016-07-04 06:15:18 -04:00
|
|
|
import org.apache.poi.ss.usermodel.CellType;
|
2014-02-02 05:34:42 -05:00
|
|
|
import org.apache.poi.ss.usermodel.Comment;
|
2013-09-09 05:24:05 -04:00
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
2016-03-20 20:14:21 -04:00
|
|
|
import org.apache.poi.ss.util.CellAddress;
|
2013-08-26 16:09:19 -04:00
|
|
|
import org.apache.poi.ss.util.CellUtil;
|
2009-05-20 20:55:56 -04:00
|
|
|
import org.apache.poi.xssf.XSSFITestDataProvider;
|
2013-08-26 16:09:19 -04:00
|
|
|
import org.apache.poi.xssf.XSSFTestDataSamples;
|
2016-09-21 01:20:36 -04:00
|
|
|
import org.apache.xmlbeans.impl.values.XmlValueDisconnectedException;
|
2015-10-31 23:23:16 -04:00
|
|
|
import org.junit.Test;
|
2009-05-20 20:55:56 -04:00
|
|
|
|
2009-12-25 19:25:59 -05:00
|
|
|
public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows {
|
2009-05-20 20:55:56 -04:00
|
|
|
|
2009-12-25 19:25:59 -05:00
|
|
|
public TestXSSFSheetShiftRows(){
|
|
|
|
super(XSSFITestDataProvider.instance);
|
2009-05-20 20:55:56 -04:00
|
|
|
}
|
|
|
|
|
2013-09-09 05:24:05 -04:00
|
|
|
@Override
|
2015-10-31 23:23:16 -04:00
|
|
|
@Test
|
2016-06-20 00:21:26 -04:00
|
|
|
public void testShiftRowBreaks() {
|
2016-03-20 20:14:21 -04:00
|
|
|
// disabled test from superclass
|
2009-12-25 19:25:59 -05:00
|
|
|
// TODO - support shifting of page breaks
|
2009-05-20 20:55:56 -04:00
|
|
|
}
|
|
|
|
|
2016-09-10 01:22:58 -04:00
|
|
|
/** Error occurred at FormulaShifter#rowMoveAreaPtg while shift rows upward. */
|
2015-10-31 23:23:16 -04:00
|
|
|
@Test
|
2016-06-20 00:21:26 -04:00
|
|
|
public void testBug54524() throws IOException {
|
2013-08-26 16:09:19 -04:00
|
|
|
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("54524.xlsx");
|
|
|
|
XSSFSheet sheet = workbook.getSheetAt(0);
|
2016-06-20 00:21:26 -04:00
|
|
|
sheet.shiftRows(3, 5, -1);
|
2013-08-26 16:09:19 -04:00
|
|
|
|
|
|
|
Cell cell = CellUtil.getCell(sheet.getRow(1), 0);
|
2016-06-20 00:21:26 -04:00
|
|
|
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();
|
|
|
|
}
|
2013-09-09 05:24:05 -04:00
|
|
|
|
2016-09-10 01:22:58 -04:00
|
|
|
/** negative row shift causes corrupted data or throws exception */
|
2015-10-31 23:23:16 -04:00
|
|
|
@Test
|
2016-06-20 00:21:26 -04:00
|
|
|
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);
|
|
|
|
|
2016-07-16 16:52:28 -04:00
|
|
|
// 2) attempt to create a new row IN PLACE of a removed row by a negative shift causes corrupted
|
2016-06-20 00:21:26 -04:00
|
|
|
// xlsx file with unreadable data in the negative shifted row.
|
|
|
|
// NOTE it's ok to create any other row.
|
2016-07-16 16:52:28 -04:00
|
|
|
Row newRow = testSheet.createRow(3);
|
|
|
|
Cell newCell = newRow.createCell(0);
|
2016-06-20 00:21:26 -04:00
|
|
|
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.
|
|
|
|
|
|
|
|
// -- CHANGE the shift to positive once the behaviour of the above has been tested
|
|
|
|
testSheet.shiftRows(6, 7, 1);
|
|
|
|
|
|
|
|
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);
|
2016-07-16 16:52:28 -04:00
|
|
|
//noinspection deprecation
|
2016-07-04 07:54:20 -04:00
|
|
|
if(readCell.getCellTypeEnum() == CellType.NUMERIC) {
|
2016-06-20 00:21:26 -04:00
|
|
|
assertEquals(expect, Double.toString(readCell.getNumericCellValue()));
|
|
|
|
} else {
|
|
|
|
assertEquals(expect, readCell.getStringCellValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 01:22:58 -04:00
|
|
|
/** negative row shift causes corrupted data or throws exception */
|
2016-06-20 00:21:26 -04:00
|
|
|
@Test
|
|
|
|
public void testBug53798a() throws IOException {
|
|
|
|
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
|
|
|
|
|
|
|
|
Sheet testSheet = wb.getSheetAt(0);
|
|
|
|
testSheet.shiftRows(3, 3, -1);
|
2013-09-09 05:24:05 -04:00
|
|
|
for (Row r : testSheet) {
|
2016-06-20 00:21:26 -04:00
|
|
|
r.getRowNum();
|
2013-09-09 05:24:05 -04:00
|
|
|
}
|
2016-06-20 00:21:26 -04:00
|
|
|
testSheet.shiftRows(6, 6, 1);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-09-10 01:22:58 -04:00
|
|
|
/** Shifting rows with comment result - Unreadable content error and comment deletion */
|
2016-06-20 00:21:26 -04:00
|
|
|
@Test
|
|
|
|
public void testBug56017() throws IOException {
|
|
|
|
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56017.xlsx");
|
2014-02-02 05:34:42 -05:00
|
|
|
|
|
|
|
Sheet sheet = wb.getSheetAt(0);
|
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
Comment comment = sheet.getCellComment(new CellAddress(0, 0));
|
2014-02-02 05:34:42 -05:00
|
|
|
assertNotNull(comment);
|
|
|
|
assertEquals("Amdocs", comment.getAuthor());
|
|
|
|
assertEquals("Amdocs:\ntest\n", comment.getString().getString());
|
|
|
|
|
|
|
|
sheet.shiftRows(0, 1, 1);
|
|
|
|
|
|
|
|
// comment in row 0 is gone
|
2016-03-20 20:14:21 -04:00
|
|
|
comment = sheet.getCellComment(new CellAddress(0, 0));
|
2014-02-02 05:34:42 -05:00
|
|
|
assertNull(comment);
|
|
|
|
|
|
|
|
// comment is now in row 1
|
2016-03-20 20:14:21 -04:00
|
|
|
comment = sheet.getCellComment(new CellAddress(1, 0));
|
2014-02-02 05:34:42 -05:00
|
|
|
assertNotNull(comment);
|
|
|
|
assertEquals("Amdocs", comment.getAuthor());
|
|
|
|
assertEquals("Amdocs:\ntest\n", comment.getString().getString());
|
|
|
|
|
|
|
|
Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
|
2015-10-31 23:23:16 -04:00
|
|
|
wb.close();
|
2014-02-02 05:34:42 -05:00
|
|
|
assertNotNull(wbBack);
|
|
|
|
|
|
|
|
Sheet sheetBack = wbBack.getSheetAt(0);
|
|
|
|
|
|
|
|
// comment in row 0 is gone
|
2016-03-20 20:14:21 -04:00
|
|
|
comment = sheetBack.getCellComment(new CellAddress(0, 0));
|
2014-02-02 05:34:42 -05:00
|
|
|
assertNull(comment);
|
|
|
|
|
|
|
|
// comment is now in row 1
|
2016-03-20 20:14:21 -04:00
|
|
|
comment = sheetBack.getCellComment(new CellAddress(1, 0));
|
2014-02-02 05:34:42 -05:00
|
|
|
assertNotNull(comment);
|
|
|
|
assertEquals("Amdocs", comment.getAuthor());
|
|
|
|
assertEquals("Amdocs:\ntest\n", comment.getString().getString());
|
2015-10-31 23:23:16 -04:00
|
|
|
wbBack.close();
|
2016-06-20 00:21:26 -04:00
|
|
|
}
|
2014-08-31 16:24:42 -04:00
|
|
|
|
2016-09-10 01:22:58 -04:00
|
|
|
/** Moving the active sheet and deleting the others results in a corrupted file */
|
2016-06-20 00:21:26 -04:00
|
|
|
@Test
|
2015-10-31 23:23:16 -04:00
|
|
|
public void test57171() throws IOException {
|
2016-06-20 00:21:26 -04:00
|
|
|
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
|
2014-12-22 04:00:18 -05:00
|
|
|
assertEquals(5, wb.getActiveSheetIndex());
|
|
|
|
removeAllSheetsBut(5, wb); // 5 is the active / selected sheet
|
|
|
|
assertEquals(0, wb.getActiveSheetIndex());
|
|
|
|
|
|
|
|
Workbook wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
|
2015-10-31 23:23:16 -04:00
|
|
|
wb.close();
|
2014-12-22 04:00:18 -05:00
|
|
|
assertEquals(0, wbRead.getActiveSheetIndex());
|
|
|
|
|
|
|
|
wbRead.removeSheetAt(0);
|
|
|
|
assertEquals(0, wbRead.getActiveSheetIndex());
|
|
|
|
|
2015-10-31 23:23:16 -04:00
|
|
|
wbRead.close();
|
2014-12-22 04:00:18 -05:00
|
|
|
}
|
|
|
|
|
2016-09-10 01:22:58 -04:00
|
|
|
/** Cannot delete an arbitrary sheet in an XLS workbook (only the last one) */
|
2016-06-20 00:21:26 -04:00
|
|
|
@Test
|
2014-12-22 04:00:18 -05:00
|
|
|
public void test57163() throws IOException {
|
|
|
|
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
|
|
|
|
assertEquals(5, wb.getActiveSheetIndex());
|
|
|
|
wb.removeSheetAt(0);
|
|
|
|
assertEquals(4, wb.getActiveSheetIndex());
|
|
|
|
|
2015-10-31 23:23:16 -04:00
|
|
|
wb.close();
|
2014-12-22 04:00:18 -05:00
|
|
|
}
|
|
|
|
|
2016-06-20 00:21:26 -04:00
|
|
|
@Test
|
2015-10-31 23:23:16 -04:00
|
|
|
public void testSetSheetOrderAndAdjustActiveSheet() throws IOException {
|
2014-12-22 04:00:18 -05:00
|
|
|
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());
|
2015-10-31 23:23:16 -04:00
|
|
|
|
|
|
|
wb.close();
|
2014-12-22 04:00:18 -05:00
|
|
|
}
|
|
|
|
|
2016-06-20 00:21:26 -04:00
|
|
|
@Test
|
2015-10-31 23:23:16 -04:00
|
|
|
public void testRemoveSheetAndAdjustActiveSheet() throws IOException {
|
2014-12-22 04:00:18 -05:00
|
|
|
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());
|
2015-10-31 23:23:16 -04:00
|
|
|
|
|
|
|
wb.close();
|
2014-12-22 04:00:18 -05:00
|
|
|
}
|
|
|
|
|
2016-09-10 01:22:58 -04:00
|
|
|
/** Failed to clone a sheet from an Excel 2010 */
|
2016-06-20 00:21:26 -04:00
|
|
|
@Test
|
2015-10-31 23:23:16 -04:00
|
|
|
public void test57165() throws IOException {
|
2014-12-22 04:00:18 -05:00
|
|
|
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());
|
|
|
|
|
2015-10-31 23:23:16 -04:00
|
|
|
wb.close();
|
2014-12-22 04:00:18 -05:00
|
|
|
}
|
|
|
|
|
2015-10-31 23:23:16 -04:00
|
|
|
private static void removeAllSheetsBut(int sheetIndex, Workbook wb) {
|
2014-12-22 04:00:18 -05:00
|
|
|
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...
|
2016-03-20 20:14:21 -04:00
|
|
|
// wb.setActiveSheet(0);
|
|
|
|
for (int sn = sheetNb - 1; sn > 0; sn--) {
|
2014-12-22 04:00:18 -05:00
|
|
|
wb.removeSheetAt(sn);
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 16:13:33 -04:00
|
|
|
|
2016-09-10 01:22:58 -04:00
|
|
|
/** Shifting rows with cell comments only shifts comments from first such cell. Other cell comments not shifted */
|
2015-10-31 23:23:16 -04:00
|
|
|
@Test
|
2015-04-20 16:13:33 -04:00
|
|
|
public void testBug57828_OnlyOneCommentShiftedInRow() throws IOException {
|
|
|
|
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57828.xlsx");
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(0);
|
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
Comment comment1 = sheet.getCellComment(new CellAddress(2, 1));
|
2015-04-20 16:13:33 -04:00
|
|
|
assertNotNull(comment1);
|
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
Comment comment2 = sheet.getCellComment(new CellAddress(2, 2));
|
2015-04-20 16:13:33 -04:00
|
|
|
assertNotNull(comment2);
|
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
Comment comment3 = sheet.getCellComment(new CellAddress(1, 1));
|
2015-04-20 16:13:33 -04:00
|
|
|
assertNull("NO comment in (1,1) and it should be null", comment3);
|
|
|
|
|
|
|
|
sheet.shiftRows(2, 2, -1);
|
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
comment3 = sheet.getCellComment(new CellAddress(1, 1));
|
2015-04-20 16:13:33 -04:00
|
|
|
assertNotNull("Comment in (2,1) moved to (1,1) so its not null now.", comment3);
|
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
comment1 = sheet.getCellComment(new CellAddress(2, 1));
|
2015-04-20 16:13:33 -04:00
|
|
|
assertNull("No comment currently in (2,1) and hence it is null", comment1);
|
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
comment2 = sheet.getCellComment(new CellAddress(1, 2));
|
2015-04-20 16:13:33 -04:00
|
|
|
assertNotNull("Comment in (2,2) should have moved as well because of shift rows. But its not", comment2);
|
|
|
|
|
|
|
|
wb.close();
|
|
|
|
}
|
2016-06-20 06:41:22 -04:00
|
|
|
|
2016-09-21 01:20:36 -04:00
|
|
|
// This test is written as expected-to-fail and should be rewritten
|
|
|
|
// as expected-to-pass when the bug is fixed.
|
|
|
|
//@Ignore("Bug 59733 - shiftRows() causes org.apache.xmlbeans.impl.values.XmlValueDisconnectedException")
|
2016-06-20 06:41:22 -04:00
|
|
|
@Test
|
|
|
|
public void bug59733() throws IOException {
|
|
|
|
Workbook workbook = new XSSFWorkbook();
|
2016-06-20 06:49:05 -04:00
|
|
|
Sheet sheet = workbook.createSheet();
|
2016-07-16 16:52:28 -04:00
|
|
|
for (int r=0; r<4; r++) {
|
2016-06-20 06:49:05 -04:00
|
|
|
sheet.createRow(r);
|
2016-06-20 06:41:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shift the 2nd row on top of the 0th row
|
|
|
|
sheet.shiftRows(2, 2, -2);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following error is thrown when shifting the 3rd row on top of the 0th row
|
2016-06-20 06:49:05 -04:00
|
|
|
* If the rows are not created, the error does not occur
|
|
|
|
|
2016-06-20 06:41:22 -04:00
|
|
|
org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
|
|
|
|
at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1258)
|
|
|
|
at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl.getR(Unknown Source)
|
|
|
|
at org.apache.poi.xssf.usermodel.XSSFRow.getRowNum(XSSFRow.java:363)
|
|
|
|
at org.apache.poi.xssf.usermodel.TestXSSFSheetShiftRows.bug59733(TestXSSFSheetShiftRows.java:393)
|
|
|
|
*/
|
2016-09-21 01:32:26 -04:00
|
|
|
// FIXME: remove try, catch, and testPassesNow, skipTest when test passes
|
2016-09-21 01:20:36 -04:00
|
|
|
try {
|
|
|
|
sheet.removeRow(sheet.getRow(0));
|
|
|
|
assertEquals(1, sheet.getRow(1).getRowNum());
|
|
|
|
testPassesNow(59733);
|
|
|
|
} catch (XmlValueDisconnectedException e) {
|
|
|
|
skipTest(e);
|
|
|
|
}
|
|
|
|
|
2016-07-16 16:52:28 -04:00
|
|
|
|
2016-06-20 06:41:22 -04:00
|
|
|
workbook.close();
|
2016-09-21 01:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String getCellFormula(Sheet sheet, String address) {
|
|
|
|
CellAddress cellAddress = new CellAddress(address);
|
|
|
|
Row row = sheet.getRow(cellAddress.getRow());
|
|
|
|
assertNotNull(row);
|
|
|
|
Cell cell = row.getCell(cellAddress.getColumn());
|
|
|
|
assertNotNull(cell);
|
|
|
|
assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
|
|
|
|
return cell.getCellFormula();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is written as expected-to-fail and should be rewritten
|
|
|
|
// as expected-to-pass when the bug is fixed.
|
|
|
|
@Test
|
|
|
|
public void testSharedFormulas() throws Exception {
|
|
|
|
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("TestShiftRowSharedFormula.xlsx");
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(0);
|
|
|
|
assertEquals("SUM(C2:C4)", getCellFormula(sheet, "C5"));
|
|
|
|
assertEquals("SUM(D2:D4)", getCellFormula(sheet, "D5"));
|
|
|
|
assertEquals("SUM(E2:E4)", getCellFormula(sheet, "E5"));
|
|
|
|
|
2016-09-21 01:32:26 -04:00
|
|
|
sheet.shiftRows(3, sheet.getLastRowNum(), 1);
|
|
|
|
// FIXME: remove try, catch, and testPassesNow, skipTest when test passes
|
2016-09-21 01:20:36 -04:00
|
|
|
try {
|
|
|
|
assertEquals("SUM(C2:C5)", getCellFormula(sheet, "C6"));
|
|
|
|
assertEquals("SUM(D2:D5)", getCellFormula(sheet, "D6"));
|
|
|
|
assertEquals("SUM(E2:E5)", getCellFormula(sheet, "E6"));
|
|
|
|
testPassesNow(59983);
|
|
|
|
} catch (AssertionError e) {
|
|
|
|
skipTest(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.close();
|
2016-06-20 06:41:22 -04:00
|
|
|
}
|
2009-05-20 20:55:56 -04:00
|
|
|
}
|