Lines 29-35
import org.apache.poi.ss.util.CellRangeAddress;
Link Here
|
29 |
import org.apache.poi.ss.util.CellUtil; |
29 |
import org.apache.poi.ss.util.CellUtil; |
30 |
import org.apache.poi.xssf.XSSFITestDataProvider; |
30 |
import org.apache.poi.xssf.XSSFITestDataProvider; |
31 |
import org.apache.poi.xssf.XSSFTestDataSamples; |
31 |
import org.apache.poi.xssf.XSSFTestDataSamples; |
32 |
import org.junit.Test; |
|
|
33 |
|
32 |
|
34 |
/** |
33 |
/** |
35 |
* @author Yegor Kozlov |
34 |
* @author Yegor Kozlov |
Lines 190-202
public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows {
Link Here
|
190 |
assertEquals("Amdocs:\ntest\n", comment.getString().getString()); |
189 |
assertEquals("Amdocs:\ntest\n", comment.getString().getString()); |
191 |
} |
190 |
} |
192 |
|
191 |
|
193 |
@Test |
192 |
public void testBug55280() throws IOException { |
194 |
public void testBug55280() { |
|
|
195 |
Workbook w = new XSSFWorkbook(); |
193 |
Workbook w = new XSSFWorkbook(); |
196 |
Sheet s = w.createSheet(); |
194 |
try { |
197 |
for (int row = 0; row < 5000; ++row) |
195 |
Sheet s = w.createSheet(); |
198 |
s.addMergedRegion(new CellRangeAddress(row, row, 0, 3)); |
196 |
for (int row = 0; row < 5000; ++row) |
|
|
197 |
s.addMergedRegion(new CellRangeAddress(row, row, 0, 3)); |
199 |
|
198 |
|
200 |
s.shiftRows(0, 4999, 1); // takes a long time... |
199 |
s.shiftRows(0, 4999, 1); // takes a long time... |
|
|
200 |
} finally { |
201 |
w.close(); |
202 |
} |
201 |
} |
203 |
} |
|
|
204 |
|
205 |
public void test57171() throws Exception { |
206 |
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx"); |
207 |
assertEquals(5, wb.getActiveSheetIndex()); |
208 |
removeAllSheetsBut(5, wb); // 5 is the active / selected sheet |
209 |
assertEquals(0, wb.getActiveSheetIndex()); |
210 |
|
211 |
Workbook wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb); |
212 |
assertEquals(0, wbRead.getActiveSheetIndex()); |
213 |
|
214 |
wbRead.removeSheetAt(0); |
215 |
assertEquals(0, wbRead.getActiveSheetIndex()); |
216 |
|
217 |
//wb.write(new FileOutputStream("/tmp/57171.xls")); |
218 |
} |
219 |
|
220 |
public void test57163() throws IOException { |
221 |
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx"); |
222 |
assertEquals(5, wb.getActiveSheetIndex()); |
223 |
wb.removeSheetAt(0); |
224 |
assertEquals(4, wb.getActiveSheetIndex()); |
225 |
|
226 |
//wb.write(new FileOutputStream("/tmp/57163.xls")); |
227 |
} |
228 |
|
229 |
public void testSetSheetOrderAndAdjustActiveSheet() throws Exception { |
230 |
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx"); |
231 |
|
232 |
assertEquals(5, wb.getActiveSheetIndex()); |
233 |
|
234 |
// move the sheets around in all possible combinations to check that the active sheet |
235 |
// is set correctly in all cases |
236 |
wb.setSheetOrder(wb.getSheetName(5), 4); |
237 |
assertEquals(4, wb.getActiveSheetIndex()); |
238 |
|
239 |
wb.setSheetOrder(wb.getSheetName(5), 5); |
240 |
assertEquals(4, wb.getActiveSheetIndex()); |
241 |
|
242 |
wb.setSheetOrder(wb.getSheetName(3), 5); |
243 |
assertEquals(3, wb.getActiveSheetIndex()); |
244 |
|
245 |
wb.setSheetOrder(wb.getSheetName(4), 5); |
246 |
assertEquals(3, wb.getActiveSheetIndex()); |
247 |
|
248 |
wb.setSheetOrder(wb.getSheetName(2), 2); |
249 |
assertEquals(3, wb.getActiveSheetIndex()); |
250 |
|
251 |
wb.setSheetOrder(wb.getSheetName(2), 1); |
252 |
assertEquals(3, wb.getActiveSheetIndex()); |
253 |
|
254 |
wb.setSheetOrder(wb.getSheetName(3), 5); |
255 |
assertEquals(5, wb.getActiveSheetIndex()); |
256 |
|
257 |
wb.setSheetOrder(wb.getSheetName(0), 5); |
258 |
assertEquals(4, wb.getActiveSheetIndex()); |
259 |
|
260 |
wb.setSheetOrder(wb.getSheetName(0), 5); |
261 |
assertEquals(3, wb.getActiveSheetIndex()); |
262 |
|
263 |
wb.setSheetOrder(wb.getSheetName(0), 5); |
264 |
assertEquals(2, wb.getActiveSheetIndex()); |
265 |
|
266 |
wb.setSheetOrder(wb.getSheetName(0), 5); |
267 |
assertEquals(1, wb.getActiveSheetIndex()); |
268 |
|
269 |
wb.setSheetOrder(wb.getSheetName(0), 5); |
270 |
assertEquals(0, wb.getActiveSheetIndex()); |
271 |
|
272 |
wb.setSheetOrder(wb.getSheetName(0), 5); |
273 |
assertEquals(5, wb.getActiveSheetIndex()); |
274 |
} |
275 |
|
276 |
public void testRemoveSheetAndAdjustActiveSheet() throws Exception { |
277 |
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx"); |
278 |
|
279 |
assertEquals(5, wb.getActiveSheetIndex()); |
280 |
|
281 |
wb.removeSheetAt(0); |
282 |
assertEquals(4, wb.getActiveSheetIndex()); |
283 |
|
284 |
wb.setActiveSheet(3); |
285 |
assertEquals(3, wb.getActiveSheetIndex()); |
286 |
|
287 |
wb.removeSheetAt(4); |
288 |
assertEquals(3, wb.getActiveSheetIndex()); |
289 |
|
290 |
wb.removeSheetAt(3); |
291 |
assertEquals(2, wb.getActiveSheetIndex()); |
292 |
|
293 |
wb.removeSheetAt(0); |
294 |
assertEquals(1, wb.getActiveSheetIndex()); |
295 |
|
296 |
wb.removeSheetAt(1); |
297 |
assertEquals(0, wb.getActiveSheetIndex()); |
298 |
|
299 |
wb.removeSheetAt(0); |
300 |
assertEquals(0, wb.getActiveSheetIndex()); |
301 |
|
302 |
try { |
303 |
wb.removeSheetAt(0); |
304 |
fail("Should catch exception as no more sheets are there"); |
305 |
} catch (IllegalArgumentException e) { |
306 |
// expected |
307 |
} |
308 |
assertEquals(0, wb.getActiveSheetIndex()); |
309 |
|
310 |
wb.createSheet(); |
311 |
assertEquals(0, wb.getActiveSheetIndex()); |
312 |
|
313 |
wb.removeSheetAt(0); |
314 |
assertEquals(0, wb.getActiveSheetIndex()); |
315 |
} |
316 |
|
317 |
// TODO: enable when bug 57165 is fixed |
318 |
public void disabled_test57165() throws IOException { |
319 |
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx"); |
320 |
assertEquals(5, wb.getActiveSheetIndex()); |
321 |
removeAllSheetsBut(3, wb); |
322 |
assertEquals(0, wb.getActiveSheetIndex()); |
323 |
wb.createSheet("New Sheet1"); |
324 |
assertEquals(0, wb.getActiveSheetIndex()); |
325 |
wb.cloneSheet(0); // Throws exception here |
326 |
wb.setSheetName(1, "New Sheet"); |
327 |
assertEquals(0, wb.getActiveSheetIndex()); |
328 |
|
329 |
//wb.write(new FileOutputStream("/tmp/57165.xls")); |
330 |
} |
331 |
|
332 |
// public void test57165b() throws IOException { |
333 |
// Workbook wb = new XSSFWorkbook(); |
334 |
// try { |
335 |
// wb.createSheet("New Sheet 1"); |
336 |
// wb.createSheet("New Sheet 2"); |
337 |
// } finally { |
338 |
// wb.close(); |
339 |
// } |
340 |
// } |
341 |
|
342 |
private static void removeAllSheetsBut(int sheetIndex, Workbook wb) |
343 |
{ |
344 |
int sheetNb = wb.getNumberOfSheets(); |
345 |
// Move this sheet at the first position |
346 |
wb.setSheetOrder(wb.getSheetName(sheetIndex), 0); |
347 |
// Must make this sheet active (otherwise, for XLSX, Excel might protest that active sheet no longer exists) |
348 |
// I think POI should automatically handle this case when deleting sheets... |
349 |
// wb.setActiveSheet(0); |
350 |
for (int sn = sheetNb - 1; sn > 0; sn--) |
351 |
{ |
352 |
wb.removeSheetAt(sn); |
353 |
} |
354 |
} |
202 |
} |
355 |
} |