You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestSheet.java 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.model;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import static org.junit.jupiter.api.Assertions.assertThrows;
  21. import static org.junit.jupiter.api.Assertions.assertTrue;
  22. import static org.junit.jupiter.api.Assertions.fail;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import org.apache.poi.ddf.EscherDggRecord;
  28. import org.apache.poi.hssf.HSSFTestDataSamples;
  29. import org.apache.poi.hssf.record.*;
  30. import org.apache.poi.hssf.record.aggregates.ConditionalFormattingTable;
  31. import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
  32. import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
  33. import org.apache.poi.hssf.usermodel.HSSFCell;
  34. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  35. import org.apache.poi.hssf.usermodel.HSSFRow;
  36. import org.apache.poi.hssf.usermodel.HSSFSheet;
  37. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  38. import org.apache.poi.ss.SpreadsheetVersion;
  39. import org.apache.poi.ss.formula.FormulaShifter;
  40. import org.apache.poi.ss.util.CellRangeAddress;
  41. import org.apache.poi.util.HexRead;
  42. import org.junit.jupiter.api.Test;
  43. /**
  44. * Unit test for the {@link InternalSheet} class.
  45. */
  46. final class TestSheet {
  47. private static InternalSheet createSheet(List<org.apache.poi.hssf.record.Record> inRecs) {
  48. return InternalSheet.createSheet(new RecordStream(inRecs, 0));
  49. }
  50. @Test
  51. void testCreateSheet() {
  52. // Check we're adding row and cell aggregates
  53. List<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
  54. records.add(BOFRecord.createSheetBOF());
  55. records.add( new DimensionsRecord() );
  56. records.add(createWindow2Record());
  57. records.add(EOFRecord.instance);
  58. InternalSheet sheet = createSheet(records);
  59. List<org.apache.poi.hssf.record.Record> outRecs = new ArrayList<>();
  60. sheet.visitContainedRecords(outRecs::add, 0);
  61. Iterator<org.apache.poi.hssf.record.Record> iter = outRecs.iterator();
  62. assertTrue(iter.next() instanceof BOFRecord );
  63. assertTrue(iter.next() instanceof IndexRecord);
  64. assertTrue(iter.next() instanceof DimensionsRecord);
  65. assertTrue(iter.next() instanceof WindowTwoRecord );
  66. assertTrue(iter.next() instanceof EOFRecord);
  67. }
  68. private static org.apache.poi.hssf.record.Record createWindow2Record() {
  69. WindowTwoRecord result = new WindowTwoRecord();
  70. result.setOptions(( short ) 0x6b6);
  71. result.setTopRow(( short ) 0);
  72. result.setLeftCol(( short ) 0);
  73. result.setHeaderColor(0x40);
  74. result.setPageBreakZoom(( short ) 0);
  75. result.setNormalZoom(( short ) 0);
  76. return result;
  77. }
  78. private static final class MergedCellListener implements RecordVisitor {
  79. private int _count;
  80. public MergedCellListener() {
  81. _count = 0;
  82. }
  83. @Override
  84. public void visitRecord(org.apache.poi.hssf.record.Record r) {
  85. if (r instanceof MergeCellsRecord) {
  86. _count++;
  87. }
  88. }
  89. public int getCount() {
  90. return _count;
  91. }
  92. }
  93. @Test
  94. void testAddMergedRegion() {
  95. InternalSheet sheet = InternalSheet.createSheet();
  96. final int regionsToAdd = 4096;
  97. //simple test that adds a load of regions
  98. for (int n = 0; n < regionsToAdd; n++)
  99. {
  100. int index = sheet.addMergedRegion(0, (short) 0, 1, (short) 1);
  101. assertEquals(index, n, "Merged region index expected to be " + n + " got " + index);
  102. }
  103. //test all the regions were indeed added
  104. assertEquals(sheet.getNumMergedRegions(), regionsToAdd);
  105. //test that the regions were spread out over the appropriate number of records
  106. MergedCellListener mcListener = new MergedCellListener();
  107. sheet.visitContainedRecords(mcListener, 0);
  108. int recordsAdded = mcListener.getCount();
  109. int recordsExpected = regionsToAdd/1027;
  110. //noinspection ConstantConditions
  111. if ((regionsToAdd % 1027) != 0) {
  112. recordsExpected++;
  113. }
  114. assertEquals(recordsAdded, recordsExpected,
  115. "The " + regionsToAdd + " merged regions should have been spread out over "
  116. + recordsExpected + " records, not " + recordsAdded);
  117. // Check we can't add one with invalid date
  118. IllegalArgumentException e;
  119. e = assertThrows(IllegalArgumentException.class, () -> sheet.addMergedRegion(10, (short)10, 9, (short)12));
  120. assertEquals("The 'to' row (9) must not be less than the 'from' row (10)", e.getMessage());
  121. e = assertThrows(IllegalArgumentException.class, () -> sheet.addMergedRegion(10, (short)10, 12, (short)9));
  122. assertEquals("The 'to' col (9) must not be less than the 'from' col (10)", e.getMessage());
  123. }
  124. @Test
  125. void testRemoveMergedRegion() {
  126. InternalSheet sheet = InternalSheet.createSheet();
  127. int regionsToAdd = 4096;
  128. for (int n = 0; n < regionsToAdd; n++) {
  129. sheet.addMergedRegion(n, 0, n, 1);
  130. }
  131. int nSheetRecords = sheet.getRecords().size();
  132. //remove a third from the beginning
  133. for (int n = 0; n < regionsToAdd/3; n++)
  134. {
  135. sheet.removeMergedRegion(0);
  136. //assert they have been deleted
  137. assertEquals(regionsToAdd - n - 1, sheet.getNumMergedRegions(), "Num of regions");
  138. }
  139. // merge records are removed from within the MergedCellsTable,
  140. // so the sheet record count should not change
  141. assertEquals(nSheetRecords, sheet.getRecords().size(), "Sheet Records");
  142. }
  143. /**
  144. * Bug: 22922 (Reported by Xuemin Guan)
  145. * <p>
  146. * Remove mergedregion fails when a sheet loses records after an initial CreateSheet
  147. * fills up the records.
  148. *
  149. */
  150. @Test
  151. void testMovingMergedRegion() {
  152. List<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
  153. CellRangeAddress[] cras = {
  154. new CellRangeAddress(0, 1, 0, 2),
  155. };
  156. MergeCellsRecord merged = new MergeCellsRecord(cras, 0, cras.length);
  157. records.add(BOFRecord.createSheetBOF());
  158. records.add(new DimensionsRecord());
  159. records.add(new RowRecord(0));
  160. records.add(new RowRecord(1));
  161. records.add(new RowRecord(2));
  162. records.add(createWindow2Record());
  163. records.add(EOFRecord.instance);
  164. records.add(merged);
  165. InternalSheet sheet = createSheet(records);
  166. sheet.getRecords().remove(0); // TODO - what does this line do?
  167. //stub object to throw off list INDEX operations
  168. sheet.removeMergedRegion(0);
  169. assertEquals(0, sheet.getNumMergedRegions(), "Should be no more merged regions");
  170. }
  171. // @Test
  172. // void testGetMergedRegionAt() {
  173. // TODO
  174. // }
  175. // @Test
  176. // void testGetNumMergedRegions() {
  177. // TODO
  178. // }
  179. /**
  180. * Makes sure all rows registered for this sheet are aggregated, they were being skipped
  181. *
  182. */
  183. @Test
  184. void testRowAggregation() {
  185. List<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
  186. records.add(InternalSheet.createBOF());
  187. records.add(new DimensionsRecord());
  188. records.add(new RowRecord(0));
  189. records.add(new RowRecord(1));
  190. FormulaRecord formulaRecord = new FormulaRecord();
  191. formulaRecord.setCachedResultTypeString();
  192. records.add(formulaRecord);
  193. records.add(new StringRecord());
  194. records.add(new RowRecord(2));
  195. records.add(createWindow2Record());
  196. records.add(EOFRecord.instance);
  197. InternalSheet sheet = createSheet(records);
  198. assertNotNull(sheet.getRow(2), "Row [2] was skipped");
  199. }
  200. /**
  201. * Make sure page break functionality works (in memory)
  202. *
  203. */
  204. @Test
  205. void testRowPageBreaks() {
  206. short colFrom = 0;
  207. short colTo = 255;
  208. InternalSheet worksheet = InternalSheet.createSheet();
  209. PageSettingsBlock sheet = worksheet.getPageSettings();
  210. sheet.setRowBreak(0, colFrom, colTo);
  211. assertTrue(sheet.isRowBroken(0), "no row break at 0");
  212. assertEquals(1, sheet.getNumRowBreaks(), "1 row break available");
  213. sheet.setRowBreak(0, colFrom, colTo);
  214. sheet.setRowBreak(0, colFrom, colTo);
  215. assertTrue(sheet.isRowBroken(0), "no row break at 0");
  216. assertEquals(1, sheet.getNumRowBreaks(), "1 row break available");
  217. sheet.setRowBreak(10, colFrom, colTo);
  218. sheet.setRowBreak(11, colFrom, colTo);
  219. assertTrue(sheet.isRowBroken(10), "no row break at 10");
  220. assertTrue(sheet.isRowBroken(11), "no row break at 11");
  221. assertEquals(3, sheet.getNumRowBreaks(), "3 row break available");
  222. boolean is10 = false;
  223. boolean is0 = false;
  224. boolean is11 = false;
  225. int[] rowBreaks = sheet.getRowBreaks();
  226. for (int main : rowBreaks) {
  227. assertTrue(main == 0 || main == 10 || main == 11, "Invalid page break");
  228. if (main == 0) is0 = true;
  229. if (main == 10) is10 = true;
  230. if (main == 11) is11 = true;
  231. }
  232. assertTrue(is0 && is10 && is11, "one of the breaks didnt make it");
  233. sheet.removeRowBreak(11);
  234. assertFalse(sheet.isRowBroken(11), "row should be removed");
  235. sheet.removeRowBreak(0);
  236. assertFalse(sheet.isRowBroken(0), "row should be removed");
  237. sheet.removeRowBreak(10);
  238. assertFalse(sheet.isRowBroken(10), "row should be removed");
  239. assertEquals(0, sheet.getNumRowBreaks(), "no more breaks");
  240. }
  241. /**
  242. * Make sure column pag breaks works properly (in-memory)
  243. *
  244. */
  245. @Test
  246. void testColPageBreaks() {
  247. short rowFrom = 0;
  248. short rowTo = (short)65535;
  249. InternalSheet worksheet = InternalSheet.createSheet();
  250. PageSettingsBlock sheet = worksheet.getPageSettings();
  251. sheet.setColumnBreak((short)0, rowFrom, rowTo);
  252. assertTrue(sheet.isColumnBroken(0), "no col break at 0");
  253. assertEquals(1, sheet.getNumColumnBreaks(), "1 col break available");
  254. sheet.setColumnBreak((short)0, rowFrom, rowTo);
  255. assertTrue(sheet.isColumnBroken(0), "no col break at 0");
  256. assertEquals(1, sheet.getNumColumnBreaks(), "1 col break available");
  257. sheet.setColumnBreak((short)1, rowFrom, rowTo);
  258. sheet.setColumnBreak((short)10, rowFrom, rowTo);
  259. sheet.setColumnBreak((short)15, rowFrom, rowTo);
  260. assertTrue(sheet.isColumnBroken(1), "no col break at 1");
  261. assertTrue(sheet.isColumnBroken(10), "no col break at 10");
  262. assertTrue(sheet.isColumnBroken(15), "no col break at 15");
  263. assertEquals(4, sheet.getNumColumnBreaks(), "4 col break available");
  264. boolean is10 = false;
  265. boolean is0 = false;
  266. boolean is1 = false;
  267. boolean is15 = false;
  268. int[] colBreaks = sheet.getColumnBreaks();
  269. for (int main : colBreaks) {
  270. assertTrue(main == 0 || main == 1 || main == 10 || main == 15, "Invalid page break");
  271. if (main == 0) is0 = true;
  272. if (main == 1) is1 = true;
  273. if (main == 10) is10= true;
  274. if (main == 15) is15 = true;
  275. }
  276. assertTrue(is0 && is1 && is10 && is15, "one of the breaks didnt make it");
  277. sheet.removeColumnBreak(15);
  278. assertFalse(sheet.isColumnBroken(15), "column break should not be there");
  279. sheet.removeColumnBreak(0);
  280. assertFalse(sheet.isColumnBroken(0), "column break should not be there");
  281. sheet.removeColumnBreak(1);
  282. assertFalse(sheet.isColumnBroken(1), "column break should not be there");
  283. sheet.removeColumnBreak(10);
  284. assertFalse(sheet.isColumnBroken(10), "column break should not be there");
  285. assertEquals(0, sheet.getNumColumnBreaks(), "no more breaks");
  286. }
  287. /**
  288. * test newly added method Sheet.getXFIndexForColAt(..)
  289. * works as designed.
  290. */
  291. @Test
  292. void testXFIndexForColumn() {
  293. final short TEST_IDX = 10;
  294. final short DEFAULT_IDX = 0xF; // 15
  295. InternalSheet sheet = InternalSheet.createSheet();
  296. // without ColumnInfoRecord
  297. int xfindex = sheet.getXFIndexForColAt((short) 0);
  298. assertEquals(DEFAULT_IDX, xfindex);
  299. xfindex = sheet.getXFIndexForColAt((short) 1);
  300. assertEquals(DEFAULT_IDX, xfindex);
  301. ColumnInfoRecord nci = new ColumnInfoRecord();
  302. sheet._columnInfos.insertColumn(nci);
  303. // single column ColumnInfoRecord
  304. nci.setFirstColumn((short) 2);
  305. nci.setLastColumn((short) 2);
  306. nci.setXFIndex(TEST_IDX);
  307. xfindex = sheet.getXFIndexForColAt((short) 0);
  308. assertEquals(DEFAULT_IDX, xfindex);
  309. xfindex = sheet.getXFIndexForColAt((short) 1);
  310. assertEquals(DEFAULT_IDX, xfindex);
  311. xfindex = sheet.getXFIndexForColAt((short) 2);
  312. assertEquals(TEST_IDX, xfindex);
  313. xfindex = sheet.getXFIndexForColAt((short) 3);
  314. assertEquals(DEFAULT_IDX, xfindex);
  315. // ten column ColumnInfoRecord
  316. nci.setFirstColumn((short) 2);
  317. nci.setLastColumn((short) 11);
  318. nci.setXFIndex(TEST_IDX);
  319. xfindex = sheet.getXFIndexForColAt((short) 1);
  320. assertEquals(DEFAULT_IDX, xfindex);
  321. xfindex = sheet.getXFIndexForColAt((short) 2);
  322. assertEquals(TEST_IDX, xfindex);
  323. xfindex = sheet.getXFIndexForColAt((short) 6);
  324. assertEquals(TEST_IDX, xfindex);
  325. xfindex = sheet.getXFIndexForColAt((short) 11);
  326. assertEquals(TEST_IDX, xfindex);
  327. xfindex = sheet.getXFIndexForColAt((short) 12);
  328. assertEquals(DEFAULT_IDX, xfindex);
  329. // single column ColumnInfoRecord starting at index 0
  330. nci.setFirstColumn((short) 0);
  331. nci.setLastColumn((short) 0);
  332. nci.setXFIndex(TEST_IDX);
  333. xfindex = sheet.getXFIndexForColAt((short) 0);
  334. assertEquals(TEST_IDX, xfindex);
  335. xfindex = sheet.getXFIndexForColAt((short) 1);
  336. assertEquals(DEFAULT_IDX, xfindex);
  337. // ten column ColumnInfoRecord starting at index 0
  338. nci.setFirstColumn((short) 0);
  339. nci.setLastColumn((short) 9);
  340. nci.setXFIndex(TEST_IDX);
  341. xfindex = sheet.getXFIndexForColAt((short) 0);
  342. assertEquals(TEST_IDX, xfindex);
  343. xfindex = sheet.getXFIndexForColAt((short) 7);
  344. assertEquals(TEST_IDX, xfindex);
  345. xfindex = sheet.getXFIndexForColAt((short) 9);
  346. assertEquals(TEST_IDX, xfindex);
  347. xfindex = sheet.getXFIndexForColAt((short) 10);
  348. assertEquals(DEFAULT_IDX, xfindex);
  349. }
  350. /**
  351. * Prior to bug 45066, POI would get the estimated sheet size wrong
  352. * when an {@code UncalcedRecord} was present.
  353. */
  354. @Test
  355. void testUncalcSize_bug45066() {
  356. List<org.apache.poi.hssf.record.Record> records = new ArrayList<>();
  357. records.add(BOFRecord.createSheetBOF());
  358. records.add(new UncalcedRecord());
  359. records.add(new DimensionsRecord());
  360. records.add(createWindow2Record());
  361. records.add(EOFRecord.instance);
  362. InternalSheet sheet = createSheet(records);
  363. // The original bug was due to different logic for collecting records for sizing and
  364. // serialization. The code has since been refactored into a single method for visiting
  365. // all contained records. Now this test is much less interesting
  366. int[] totalSize = { 0 };
  367. byte[] buf = new byte[100];
  368. sheet.visitContainedRecords(r -> {
  369. int estimatedSize = r.getRecordSize();
  370. int serializedSize = r.serialize(0, buf);
  371. assertEquals(estimatedSize, serializedSize, "serialized size mismatch for record (" + r.getClass().getName() + ")");
  372. totalSize[0] += estimatedSize;
  373. }, 0);
  374. assertEquals(90, totalSize[0]);
  375. }
  376. /**
  377. * Prior to bug 45145 {@code RowRecordsAggregate} and {@code ValueRecordsAggregate} could
  378. * sometimes occur in reverse order. This test reproduces one of those situations and makes
  379. * sure that RRA comes before VRA.<br>
  380. *
  381. * The code here represents a normal POI use case where a spreadsheet is created from scratch.
  382. */
  383. @Test
  384. void testRowValueAggregatesOrder_bug45145() {
  385. InternalSheet sheet = InternalSheet.createSheet();
  386. RowRecord rr = new RowRecord(5);
  387. sheet.addRow(rr);
  388. CellValueRecordInterface cvr = new BlankRecord();
  389. cvr.setColumn((short)0);
  390. cvr.setRow(5);
  391. sheet.addValueRecord(5, cvr);
  392. int dbCellRecordPos = getDbCellRecordPos(sheet);
  393. // The overt symptom of the bug
  394. // DBCELL record pos is calculated wrong if VRA comes before RRA
  395. assertNotEquals (252, dbCellRecordPos);
  396. // if (false) {
  397. // // make sure that RRA and VRA are in the right place
  398. // // (Aug 2008) since the VRA is now part of the RRA, there is much less chance that
  399. // // they could get out of order. Still, one could write serialize the sheet here,
  400. // // and read back with EventRecordFactory to make sure...
  401. // }
  402. assertEquals(242, dbCellRecordPos);
  403. }
  404. /**
  405. * @return the value calculated for the position of the first DBCELL record for this sheet.
  406. * That value is found on the IndexRecord.
  407. */
  408. private static int getDbCellRecordPos(InternalSheet sheet) {
  409. MyIndexRecordListener myIndexListener = new MyIndexRecordListener();
  410. sheet.visitContainedRecords(myIndexListener, 0);
  411. IndexRecord indexRecord = myIndexListener.getIndexRecord();
  412. return indexRecord.getDbcellAt(0);
  413. }
  414. private static final class MyIndexRecordListener implements RecordVisitor {
  415. private IndexRecord _indexRecord;
  416. public MyIndexRecordListener() {
  417. // no-arg constructor
  418. }
  419. public IndexRecord getIndexRecord() {
  420. return _indexRecord;
  421. }
  422. @Override
  423. public void visitRecord(org.apache.poi.hssf.record.Record r) {
  424. if (r instanceof IndexRecord) {
  425. if (_indexRecord != null) {
  426. throw new RuntimeException("too many index records");
  427. }
  428. _indexRecord = (IndexRecord)r;
  429. }
  430. }
  431. }
  432. /**
  433. * Checks for bug introduced around r682282-r683880 that caused a second GUTS records
  434. * which in turn got the dimensions record out of alignment
  435. */
  436. @Test
  437. void testGutsRecord_bug45640() {
  438. InternalSheet sheet = InternalSheet.createSheet();
  439. sheet.addRow(new RowRecord(0));
  440. sheet.addRow(new RowRecord(1));
  441. sheet.groupRowRange( 0, 1, true );
  442. assertNotNull(sheet.toString());
  443. List<RecordBase> recs = sheet.getRecords();
  444. long count = recs.stream().filter(r -> r instanceof GutsRecord).count();
  445. assertNotEquals(2, count);
  446. assertEquals(1, count);
  447. }
  448. @Test
  449. void testMisplacedMergedCellsRecords_bug45699() throws Exception {
  450. try (HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("ex45698-22488.xls")) {
  451. HSSFSheet sheet = wb.getSheetAt(0);
  452. HSSFRow row = sheet.getRow(3);
  453. HSSFCell cell = row.getCell(4);
  454. assertNotNull(cell, "Identified bug 45699");
  455. assertEquals("Informations", cell.getRichStringCellValue().getString());
  456. }
  457. }
  458. /**
  459. * In 3.1, setting margins between creating first row and first cell caused an exception.
  460. */
  461. @Test
  462. void testSetMargins_bug45717() throws Exception {
  463. HSSFWorkbook workbook = new HSSFWorkbook();
  464. HSSFSheet sheet = workbook.createSheet("Vorschauliste");
  465. HSSFRow row = sheet.createRow(0);
  466. sheet.setMargin(HSSFSheet.LeftMargin, 0.3);
  467. try {
  468. row.createCell(0);
  469. } catch (IllegalStateException e) {
  470. if (e.getMessage().equals("Cannot create value records before row records exist")) {
  471. fail("Identified bug 45717");
  472. }
  473. throw e;
  474. } finally {
  475. workbook.close();
  476. }
  477. }
  478. /**
  479. * Some apps seem to write files with missing DIMENSION records.
  480. * Excel(2007) tolerates this, so POI should too.
  481. */
  482. @Test
  483. void testMissingDims() {
  484. int rowIx = 5;
  485. int colIx = 6;
  486. NumberRecord nr = new NumberRecord();
  487. nr.setRow(rowIx);
  488. nr.setColumn((short) colIx);
  489. nr.setValue(3.0);
  490. List<org.apache.poi.hssf.record.Record> inRecs = new ArrayList<>();
  491. inRecs.add(BOFRecord.createSheetBOF());
  492. inRecs.add(new RowRecord(rowIx));
  493. inRecs.add(nr);
  494. inRecs.add(createWindow2Record());
  495. inRecs.add(EOFRecord.instance);
  496. InternalSheet sheet = createSheet(inRecs);
  497. List<org.apache.poi.hssf.record.Record> outRecs = new ArrayList<>();
  498. sheet.visitContainedRecords(outRecs::add, rowIx);
  499. assertEquals(8, outRecs.size());
  500. DimensionsRecord dims = (DimensionsRecord) outRecs.get(5);
  501. assertEquals(rowIx, dims.getFirstRow());
  502. assertEquals(rowIx, dims.getLastRow());
  503. assertEquals(colIx, dims.getFirstCol());
  504. assertEquals(colIx, dims.getLastCol());
  505. }
  506. /**
  507. * Prior to the fix for bug 46547, shifting formulas would have the side-effect
  508. * of creating a {@link ConditionalFormattingTable}. There was no impairment to
  509. * functionality since empty record aggregates are equivalent to missing record
  510. * aggregates. However, since this unnecessary creation helped expose bug 46547b,
  511. * and since there is a slight performance hit the fix was made to avoid it.
  512. */
  513. @Test
  514. void testShiftFormulasAddCondFormat_bug46547() {
  515. // Create a sheet with data validity (similar to bugzilla attachment id=23131).
  516. InternalSheet sheet = InternalSheet.createSheet();
  517. List<RecordBase> sheetRecs = sheet.getRecords();
  518. assertEquals(23, sheetRecs.size());
  519. FormulaShifter shifter = FormulaShifter.createForRowShift(0, "", 0, 0, 1, SpreadsheetVersion.EXCEL97);
  520. sheet.updateFormulasAfterCellShift(shifter, 0);
  521. assertFalse(sheetRecs.size() == 24 && sheetRecs.get(22) instanceof ConditionalFormattingTable);
  522. assertEquals(23, sheetRecs.size());
  523. }
  524. /**
  525. * Bug 46547 happened when attempting to add conditional formatting to a sheet
  526. * which already had data validity constraints.
  527. */
  528. @Test
  529. void testAddCondFormatAfterDataValidation_bug46547() {
  530. // Create a sheet with data validity (similar to bugzilla attachment id=23131).
  531. InternalSheet sheet = InternalSheet.createSheet();
  532. sheet.getOrCreateDataValidityTable();
  533. // attempt to add conditional formatting
  534. ConditionalFormattingTable cft = sheet.getConditionalFormattingTable();
  535. assertNotNull(cft);
  536. }
  537. @Test
  538. void testCloneMulBlank_bug46776() {
  539. org.apache.poi.hssf.record.Record[] recs = {
  540. InternalSheet.createBOF(),
  541. new DimensionsRecord(),
  542. new RowRecord(1),
  543. new MulBlankRecord(1, 3, new short[] { 0x0F, 0x0F, 0x0F, } ),
  544. new RowRecord(2),
  545. createWindow2Record(),
  546. EOFRecord.instance,
  547. };
  548. InternalSheet sheet = createSheet(Arrays.asList(recs));
  549. InternalSheet sheet2 = sheet.cloneSheet();
  550. List<org.apache.poi.hssf.record.Record> clonedRecs = new ArrayList<>();
  551. sheet2.visitContainedRecords(clonedRecs::add, 0);
  552. // +2 for INDEX and DBCELL
  553. assertEquals(recs.length+2, clonedRecs.size());
  554. }
  555. @Test
  556. void testCreateAggregate() {
  557. String msoDrawingRecord1 =
  558. "0F 00 02 F0 20 01 00 00 10 00 08 F0 08 00 00 00 \n" +
  559. "03 00 00 00 02 04 00 00 0F 00 03 F0 08 01 00 00 \n" +
  560. "0F 00 04 F0 28 00 00 00 01 00 09 F0 10 00 00 00 \n" +
  561. "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 \n" +
  562. "02 00 0A F0 08 00 00 00 00 04 00 00 05 00 00 00 \n" +
  563. "0F 00 04 F0 64 00 00 00 42 01 0A F0 08 00 00 00 \n" +
  564. "01 04 00 00 00 0A 00 00 73 00 0B F0 2A 00 00 00 \n" +
  565. "BF 00 08 00 08 00 44 01 04 00 00 00 7F 01 00 00 \n" +
  566. "01 00 BF 01 00 00 11 00 C0 01 40 00 00 08 FF 01 \n" +
  567. "10 00 10 00 BF 03 00 00 08 00 00 00 10 F0 12 00 \n" +
  568. "00 00 00 00 01 00 54 00 05 00 45 00 01 00 88 03 \n" +
  569. "05 00 94 00 00 00 11 F0 00 00 00 00";
  570. String msoDrawingRecord2 =
  571. "0F 00 04 F0 64 00 00 00 42 01 0A F0 08 00 00 00 " +
  572. "02 04 00 00 80 0A 00 00 73 00 0B F0 2A 00 00 00 " +
  573. "BF 00 08 00 08 00 44 01 04 00 00 00 7F 01 00 00 " +
  574. "01 00 BF 01 00 00 11 00 C0 01 40 00 00 08 FF 01 " +
  575. "10 00 10 00 BF 03 00 00 08 00 00 00 10 F0 12 00 " +
  576. "00 00 00 00 01 00 8D 03 05 00 E4 00 03 00 4D 03 " +
  577. "0B 00 0C 00 00 00 11 F0 00 00 00 00";
  578. DrawingRecord d1 = new DrawingRecord();
  579. d1.setData( HexRead.readFromString( msoDrawingRecord1 ) );
  580. ObjRecord r1 = new ObjRecord();
  581. DrawingRecord d2 = new DrawingRecord();
  582. d2.setData( HexRead.readFromString( msoDrawingRecord2 ) );
  583. TextObjectRecord r2 = new TextObjectRecord();
  584. r2.setStr(new HSSFRichTextString("Aggregated"));
  585. NoteRecord n2 = new NoteRecord();
  586. List<org.apache.poi.hssf.record.Record> recordStream = new ArrayList<>();
  587. recordStream.add(InternalSheet.createBOF());
  588. recordStream.add( d1 );
  589. recordStream.add( r1 );
  590. recordStream.add(createWindow2Record());
  591. recordStream.add(EOFRecord.instance);
  592. confirmAggregatedRecords(recordStream);
  593. recordStream = new ArrayList<>();
  594. recordStream.add(InternalSheet.createBOF());
  595. recordStream.add( d1 );
  596. recordStream.add( r1 );
  597. recordStream.add( d2 );
  598. recordStream.add( r2 );
  599. recordStream.add(createWindow2Record());
  600. recordStream.add(EOFRecord.instance);
  601. confirmAggregatedRecords(recordStream);
  602. recordStream = new ArrayList<>();
  603. recordStream.add(InternalSheet.createBOF());
  604. recordStream.add( d1 );
  605. recordStream.add( r1 );
  606. recordStream.add( d2 );
  607. recordStream.add( r2 );
  608. recordStream.add( n2 );
  609. recordStream.add(createWindow2Record());
  610. recordStream.add(EOFRecord.instance);
  611. confirmAggregatedRecords(recordStream);
  612. }
  613. private void confirmAggregatedRecords(List<org.apache.poi.hssf.record.Record> recordStream){
  614. InternalSheet sheet = InternalSheet.createSheet();
  615. sheet.getRecords().clear();
  616. sheet.getRecords().addAll(recordStream);
  617. List<RecordBase> sheetRecords = sheet.getRecords();
  618. DrawingManager2 drawingManager = new DrawingManager2(new EscherDggRecord() );
  619. sheet.aggregateDrawingRecords(drawingManager, false);
  620. assertEquals(4, sheetRecords.size());
  621. assertEquals(BOFRecord.sid, ((org.apache.poi.hssf.record.Record)sheetRecords.get(0)).getSid());
  622. assertEquals(EscherAggregate.sid, ((org.apache.poi.hssf.record.Record)sheetRecords.get(1)).getSid());
  623. assertEquals(WindowTwoRecord.sid, ((org.apache.poi.hssf.record.Record)sheetRecords.get(2)).getSid());
  624. assertEquals(EOFRecord.sid, ((org.apache.poi.hssf.record.Record)sheetRecords.get(3)).getSid());
  625. }
  626. @Test
  627. void testSheetDimensions() {
  628. InternalSheet sheet = InternalSheet.createSheet();
  629. DimensionsRecord dimensions = (DimensionsRecord)sheet.findFirstRecordBySid(DimensionsRecord.sid);
  630. assertNotNull(dimensions);
  631. assertEquals(0, dimensions.getFirstCol());
  632. assertEquals(0, dimensions.getFirstRow());
  633. assertEquals(1, dimensions.getLastCol()); // plus pne
  634. assertEquals(1, dimensions.getLastRow()); // plus pne
  635. RowRecord rr = new RowRecord(0);
  636. sheet.addRow(rr);
  637. assertEquals(0, dimensions.getFirstCol());
  638. assertEquals(0, dimensions.getFirstRow());
  639. assertEquals(1, dimensions.getLastCol());
  640. assertEquals(1, dimensions.getLastRow());
  641. CellValueRecordInterface cvr;
  642. cvr = new BlankRecord();
  643. cvr.setColumn((short)0);
  644. cvr.setRow(0);
  645. sheet.addValueRecord(0, cvr);
  646. assertEquals(0, dimensions.getFirstCol());
  647. assertEquals(0, dimensions.getFirstRow());
  648. assertEquals(1, dimensions.getLastCol());
  649. assertEquals(1, dimensions.getLastRow());
  650. cvr = new BlankRecord();
  651. cvr.setColumn((short)1);
  652. cvr.setRow(0);
  653. sheet.addValueRecord(0, cvr);
  654. assertEquals(0, dimensions.getFirstCol());
  655. assertEquals(0, dimensions.getFirstRow());
  656. assertEquals(2, dimensions.getLastCol()); //YK: failed until Bugzilla 53414 was fixed
  657. assertEquals(1, dimensions.getLastRow());
  658. }
  659. }