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.

TestXSSFCell.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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.xssf.usermodel;
  16. import java.io.IOException;
  17. import java.util.List;
  18. import org.apache.poi.common.usermodel.HyperlinkType;
  19. import org.apache.poi.hssf.HSSFITestDataProvider;
  20. import org.apache.poi.ss.SpreadsheetVersion;
  21. import org.apache.poi.ss.formula.FormulaParseException;
  22. import org.apache.poi.ss.usermodel.BaseTestXCell;
  23. import org.apache.poi.ss.usermodel.BorderStyle;
  24. import org.apache.poi.ss.usermodel.Cell;
  25. import org.apache.poi.ss.usermodel.CellCopyPolicy;
  26. import org.apache.poi.ss.usermodel.CellStyle;
  27. import org.apache.poi.ss.usermodel.CellType;
  28. import org.apache.poi.ss.usermodel.CreationHelper;
  29. import org.apache.poi.ss.usermodel.DataFormatter;
  30. import org.apache.poi.ss.usermodel.Font;
  31. import org.apache.poi.ss.usermodel.Hyperlink;
  32. import org.apache.poi.ss.usermodel.IndexedColors;
  33. import org.apache.poi.ss.usermodel.RichTextString;
  34. import org.apache.poi.ss.usermodel.Row;
  35. import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
  36. import org.apache.poi.ss.usermodel.Sheet;
  37. import org.apache.poi.ss.usermodel.Workbook;
  38. import org.apache.poi.ss.util.CellRangeAddress;
  39. import org.apache.poi.ss.util.CellReference;
  40. import org.apache.poi.xssf.SXSSFITestDataProvider;
  41. import org.apache.poi.xssf.XSSFITestDataProvider;
  42. import org.apache.poi.xssf.XSSFTestDataSamples;
  43. import org.apache.poi.xssf.model.SharedStringsTable;
  44. import org.junit.Test;
  45. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
  46. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType;
  47. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
  48. import static org.junit.Assert.*;
  49. public final class TestXSSFCell extends BaseTestXCell {
  50. public TestXSSFCell() {
  51. super(XSSFITestDataProvider.instance);
  52. }
  53. /**
  54. * Bug 47026: trouble changing cell type when workbook doesn't contain
  55. * Shared String Table
  56. */
  57. @Test
  58. public void test47026_1() throws IOException {
  59. Workbook wb = _testDataProvider.openSampleWorkbook("47026.xlsm");
  60. Sheet sheet = wb.getSheetAt(0);
  61. Row row = sheet.getRow(0);
  62. Cell cell = row.getCell(0);
  63. cell.setCellType(CellType.STRING);
  64. cell.setCellValue("456");
  65. wb.close();
  66. }
  67. @Test
  68. public void test47026_2() throws IOException {
  69. Workbook wb = _testDataProvider.openSampleWorkbook("47026.xlsm");
  70. Sheet sheet = wb.getSheetAt(0);
  71. Row row = sheet.getRow(0);
  72. Cell cell = row.getCell(0);
  73. cell.setCellFormula(null);
  74. cell.setCellValue("456");
  75. wb.close();
  76. }
  77. /**
  78. * Test that we can read inline strings that are expressed directly in the cell definition
  79. * instead of implementing the shared string table.
  80. *
  81. * Some programs, for example, Microsoft Excel Driver for .xlsx insert inline string
  82. * instead of using the shared string table. See bug 47206
  83. */
  84. @Test
  85. public void testInlineString() throws IOException {
  86. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("xlsx-jdbc.xlsx");
  87. XSSFSheet sheet = wb.getSheetAt(0);
  88. XSSFRow row = sheet.getRow(1);
  89. XSSFCell cell_0 = row.getCell(0);
  90. assertEquals(STCellType.INT_INLINE_STR, cell_0.getCTCell().getT().intValue());
  91. assertTrue(cell_0.getCTCell().isSetIs());
  92. assertEquals("A Very large string in column 1 AAAAAAAAAAAAAAAAAAAAA", cell_0.getStringCellValue());
  93. XSSFCell cell_1 = row.getCell(1);
  94. assertEquals(STCellType.INT_INLINE_STR, cell_1.getCTCell().getT().intValue());
  95. assertTrue(cell_1.getCTCell().isSetIs());
  96. assertEquals("foo", cell_1.getStringCellValue());
  97. XSSFCell cell_2 = row.getCell(2);
  98. assertEquals(STCellType.INT_INLINE_STR, cell_2.getCTCell().getT().intValue());
  99. assertTrue(cell_2.getCTCell().isSetIs());
  100. assertEquals("bar", row.getCell(2).getStringCellValue());
  101. wb.close();
  102. }
  103. /**
  104. * Bug 47278 - xsi:nil attribute for <t> tag caused Excel 2007 to fail to open workbook
  105. */
  106. @Test
  107. public void test47278() throws IOException {
  108. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.createWorkbook();
  109. Sheet sheet = wb.createSheet();
  110. Row row = sheet.createRow(0);
  111. SharedStringsTable sst = wb.getSharedStringSource();
  112. assertEquals(0, sst.getCount());
  113. //case 1. cell.setCellValue(new XSSFRichTextString((String)null));
  114. Cell cell_0 = row.createCell(0);
  115. RichTextString str = new XSSFRichTextString((String)null);
  116. assertNull(str.getString());
  117. cell_0.setCellValue(str);
  118. assertEquals(0, sst.getCount());
  119. assertEquals(CellType.BLANK, cell_0.getCellType());
  120. //case 2. cell.setCellValue((String)null);
  121. Cell cell_1 = row.createCell(1);
  122. cell_1.setCellValue((String)null);
  123. assertEquals(0, sst.getCount());
  124. assertEquals(CellType.BLANK, cell_1.getCellType());
  125. wb.close();
  126. }
  127. @Test
  128. public void testFormulaString() throws IOException {
  129. try (XSSFWorkbook wb = (XSSFWorkbook) _testDataProvider.createWorkbook()) {
  130. XSSFCell cell = wb.createSheet().createRow(0).createCell(0);
  131. CTCell ctCell = cell.getCTCell(); //low-level bean holding cell's xml
  132. cell.setCellFormula("A2");
  133. assertEquals(CellType.FORMULA, cell.getCellType());
  134. assertEquals("A2", cell.getCellFormula());
  135. //the value is not set and cell's type='N' which means blank
  136. assertEquals(STCellType.N, ctCell.getT());
  137. //set cached formula value
  138. cell.setCellValue("t='str'");
  139. //we are still of 'formula' type
  140. assertEquals(CellType.FORMULA, cell.getCellType());
  141. assertEquals("A2", cell.getCellFormula());
  142. //cached formula value is set and cell's type='STR'
  143. assertEquals(STCellType.STR, ctCell.getT());
  144. assertEquals("t='str'", cell.getStringCellValue());
  145. //now remove the formula, the cached formula result remains
  146. cell.setCellFormula(null);
  147. assertEquals(CellType.STRING, cell.getCellType());
  148. assertEquals(STCellType.STR, ctCell.getT());
  149. //the line below failed prior to fix of Bug #47889
  150. assertEquals("t='str'", cell.getStringCellValue());
  151. //revert to a blank cell
  152. cell.setCellValue((String) null);
  153. assertEquals(CellType.BLANK, cell.getCellType());
  154. assertEquals(STCellType.N, ctCell.getT());
  155. assertEquals("", cell.getStringCellValue());
  156. // check behavior with setCellFormulaValidation
  157. final String invalidFormula = "A", validFormula = "A2";
  158. // check that default is true
  159. assertTrue(wb.getCellFormulaValidation());
  160. // check that valid formula does not throw exception
  161. cell.setCellFormula(validFormula);
  162. // check that invalid formula does throw exception
  163. try {
  164. cell.setCellFormula(invalidFormula);
  165. fail("Should catch exception here");
  166. } catch (FormulaParseException e) {
  167. // expected here
  168. }
  169. // set cell formula validation to false
  170. wb.setCellFormulaValidation(false);
  171. assertFalse(wb.getCellFormulaValidation());
  172. // check that neither valid nor invalid formula throw an exception
  173. cell.setCellFormula(validFormula);
  174. cell.setCellFormula(invalidFormula);
  175. }
  176. }
  177. /**
  178. * Bug 47889: problems when calling XSSFCell.getStringCellValue() on a workbook created in Gnumeric
  179. */
  180. @Test
  181. public void test47889() throws IOException {
  182. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("47889.xlsx");
  183. XSSFSheet sh = wb.getSheetAt(0);
  184. XSSFCell cell;
  185. //try a string cell
  186. cell = sh.getRow(0).getCell(0);
  187. assertEquals(CellType.STRING, cell.getCellType());
  188. assertEquals("a", cell.getStringCellValue());
  189. assertEquals("a", cell.toString());
  190. //Gnumeric produces spreadsheets without styles
  191. //make sure we return null for that instead of throwing OutOfBounds
  192. assertEquals(null, cell.getCellStyle());
  193. //try a numeric cell
  194. cell = sh.getRow(1).getCell(0);
  195. assertEquals(CellType.NUMERIC, cell.getCellType());
  196. assertEquals(1.0, cell.getNumericCellValue(), 0);
  197. assertEquals("1.0", cell.toString());
  198. //Gnumeric produces spreadsheets without styles
  199. //make sure we return null for that instead of throwing OutOfBounds
  200. assertEquals(null, cell.getCellStyle());
  201. wb.close();
  202. }
  203. @Test
  204. public void testMissingRAttribute() throws IOException {
  205. XSSFWorkbook wb1 = new XSSFWorkbook();
  206. XSSFSheet sheet = wb1.createSheet();
  207. XSSFRow row = sheet.createRow(0);
  208. XSSFCell a1 = row.createCell(0);
  209. a1.setCellValue("A1");
  210. XSSFCell a2 = row.createCell(1);
  211. a2.setCellValue("B1");
  212. XSSFCell a4 = row.createCell(4);
  213. a4.setCellValue("E1");
  214. XSSFCell a6 = row.createCell(5);
  215. a6.setCellValue("F1");
  216. assertCellsWithMissingR(row);
  217. a2.getCTCell().unsetR();
  218. a6.getCTCell().unsetR();
  219. assertCellsWithMissingR(row);
  220. XSSFWorkbook wb2 = (XSSFWorkbook)_testDataProvider.writeOutAndReadBack(wb1);
  221. row = wb2.getSheetAt(0).getRow(0);
  222. assertCellsWithMissingR(row);
  223. wb2.close();
  224. wb1.close();
  225. }
  226. private void assertCellsWithMissingR(XSSFRow row){
  227. XSSFCell a1 = row.getCell(0);
  228. assertNotNull(a1);
  229. XSSFCell a2 = row.getCell(1);
  230. assertNotNull(a2);
  231. XSSFCell a5 = row.getCell(4);
  232. assertNotNull(a5);
  233. XSSFCell a6 = row.getCell(5);
  234. assertNotNull(a6);
  235. assertEquals(6, row.getLastCellNum());
  236. assertEquals(4, row.getPhysicalNumberOfCells());
  237. assertEquals("A1", a1.getStringCellValue());
  238. assertEquals("B1", a2.getStringCellValue());
  239. assertEquals("E1", a5.getStringCellValue());
  240. assertEquals("F1", a6.getStringCellValue());
  241. // even if R attribute is not set,
  242. // POI is able to re-construct it from column and row indexes
  243. assertEquals("A1", a1.getReference());
  244. assertEquals("B1", a2.getReference());
  245. assertEquals("E1", a5.getReference());
  246. assertEquals("F1", a6.getReference());
  247. }
  248. @Test
  249. public void testMissingRAttributeBug54288() throws IOException {
  250. // workbook with cells missing the R attribute
  251. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("54288.xlsx");
  252. // same workbook re-saved in Excel 2010, the R attribute is updated for every cell with the right value.
  253. XSSFWorkbook wbRef = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("54288-ref.xlsx");
  254. XSSFSheet sheet = wb.getSheetAt(0);
  255. XSSFSheet sheetRef = wbRef.getSheetAt(0);
  256. assertEquals(sheetRef.getPhysicalNumberOfRows(), sheet.getPhysicalNumberOfRows());
  257. // Test idea: iterate over cells in the reference worksheet, they all have the R attribute set.
  258. // For each cell from the reference sheet find the corresponding cell in the problematic file (with missing R)
  259. // and assert that POI reads them equally:
  260. DataFormatter formater = new DataFormatter();
  261. for(Row r : sheetRef){
  262. XSSFRow rowRef = (XSSFRow)r;
  263. XSSFRow row = sheet.getRow(rowRef.getRowNum());
  264. assertEquals("number of cells in row["+row.getRowNum()+"]",
  265. rowRef.getPhysicalNumberOfCells(), row.getPhysicalNumberOfCells());
  266. for(Cell c : rowRef){
  267. XSSFCell cellRef = (XSSFCell)c;
  268. XSSFCell cell = row.getCell(cellRef.getColumnIndex());
  269. assertEquals(cellRef.getColumnIndex(), cell.getColumnIndex());
  270. assertEquals(cellRef.getReference(), cell.getReference());
  271. if(!cell.getCTCell().isSetR()){
  272. assertTrue("R must e set in cellRef", cellRef.getCTCell().isSetR());
  273. String valRef = formater.formatCellValue(cellRef);
  274. String val = formater.formatCellValue(cell);
  275. assertEquals(valRef, val);
  276. }
  277. }
  278. }
  279. wbRef.close();
  280. wb.close();
  281. }
  282. @Test
  283. public void test56170() throws IOException {
  284. final Workbook wb1 = XSSFTestDataSamples.openSampleWorkbook("56170.xlsx");
  285. final XSSFSheet sheet = (XSSFSheet) wb1.getSheetAt(0);
  286. Workbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
  287. Cell cell;
  288. // add some contents to table so that the table will need expansion
  289. Row row = sheet.getRow(0);
  290. Workbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
  291. cell = row.createCell(0);
  292. Workbook wb4 = XSSFTestDataSamples.writeOutAndReadBack(wb3);
  293. cell.setCellValue("demo1");
  294. Workbook wb5 = XSSFTestDataSamples.writeOutAndReadBack(wb4);
  295. cell = row.createCell(1);
  296. Workbook wb6 = XSSFTestDataSamples.writeOutAndReadBack(wb5);
  297. cell.setCellValue("demo2");
  298. Workbook wb7 = XSSFTestDataSamples.writeOutAndReadBack(wb6);
  299. cell = row.createCell(2);
  300. Workbook wb8 = XSSFTestDataSamples.writeOutAndReadBack(wb7);
  301. cell.setCellValue("demo3");
  302. Workbook wb9 = XSSFTestDataSamples.writeOutAndReadBack(wb8);
  303. row = sheet.getRow(1);
  304. cell = row.createCell(0);
  305. cell.setCellValue("demo1");
  306. cell = row.createCell(1);
  307. cell.setCellValue("demo2");
  308. cell = row.createCell(2);
  309. cell.setCellValue("demo3");
  310. Workbook wb10 = XSSFTestDataSamples.writeOutAndReadBack(wb9);
  311. // expand table
  312. XSSFTable table = sheet.getTables().get(0);
  313. final CellReference startRef = table.getStartCellReference();
  314. final CellReference endRef = table.getEndCellReference();
  315. table.getCTTable().setRef(new CellRangeAddress(startRef.getRow(), 1, startRef.getCol(), endRef.getCol()).formatAsString());
  316. Workbook wb11 = XSSFTestDataSamples.writeOutAndReadBack(wb10);
  317. assertNotNull(wb11);
  318. wb11.close();
  319. wb10.close();
  320. wb9.close();
  321. wb8.close();
  322. wb7.close();
  323. wb6.close();
  324. wb5.close();
  325. wb4.close();
  326. wb3.close();
  327. wb2.close();
  328. wb1.close();
  329. }
  330. @Test
  331. public void test56170Reproduce() throws IOException {
  332. try (Workbook wb = new XSSFWorkbook()) {
  333. final Sheet sheet = wb.createSheet();
  334. Row row = sheet.createRow(0);
  335. // by creating Cells out of order we trigger the handling in onDocumentWrite()
  336. Cell cell1 = row.createCell(1);
  337. Cell cell2 = row.createCell(0);
  338. validateRow(row);
  339. validateRow(row);
  340. // once again with removing one cell
  341. row.removeCell(cell1);
  342. validateRow(row);
  343. // once again with removing one cell
  344. row.removeCell(cell1);
  345. // now check again
  346. validateRow(row);
  347. // once again with removing one cell
  348. row.removeCell(cell2);
  349. // now check again
  350. validateRow(row);
  351. }
  352. }
  353. private void validateRow(Row row) {
  354. // trigger bug with CArray handling
  355. ((XSSFRow)row).onDocumentWrite();
  356. for(Cell cell : row) {
  357. assertNotNull(cell.toString());
  358. }
  359. }
  360. @Test
  361. public void testBug56644ReturnNull() throws IOException {
  362. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx")) {
  363. wb.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
  364. Sheet sheet = wb.getSheet("samplelist");
  365. Row row = sheet.getRow(20);
  366. row.createCell(2);
  367. }
  368. }
  369. @Test
  370. public void testBug56644ReturnBlank() throws IOException {
  371. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx")) {
  372. wb.setMissingCellPolicy(MissingCellPolicy.RETURN_NULL_AND_BLANK);
  373. Sheet sheet = wb.getSheet("samplelist");
  374. Row row = sheet.getRow(20);
  375. row.createCell(2);
  376. }
  377. }
  378. @Test
  379. public void testBug56644CreateBlank() throws IOException {
  380. try (Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56644.xlsx")) {
  381. wb.setMissingCellPolicy(MissingCellPolicy.CREATE_NULL_AS_BLANK);
  382. Sheet sheet = wb.getSheet("samplelist");
  383. Row row = sheet.getRow(20);
  384. row.createCell(2);
  385. }
  386. }
  387. @Test
  388. public void testEncodingBelowAscii() throws IOException {
  389. StringBuilder sb = new StringBuilder();
  390. // test all possible characters
  391. for(int i = 0; i < Character.MAX_VALUE; i++) {
  392. sb.append((char)i);
  393. }
  394. String strAll = sb.toString();
  395. // process in chunks as we have a limit on size of column now
  396. int pos = 0;
  397. while(pos < strAll.length()) {
  398. String str = strAll.substring(pos, Math.min(strAll.length(), pos+SpreadsheetVersion.EXCEL2007.getMaxTextLength()));
  399. Workbook wb = HSSFITestDataProvider.instance.createWorkbook();
  400. Cell cell = wb.createSheet().createRow(0).createCell(0);
  401. Workbook xwb = XSSFITestDataProvider.instance.createWorkbook();
  402. Cell xCell = xwb.createSheet().createRow(0).createCell(0);
  403. Workbook swb = SXSSFITestDataProvider.instance.createWorkbook();
  404. Cell sCell = swb.createSheet().createRow(0).createCell(0);
  405. cell.setCellValue(str);
  406. assertEquals(str, cell.getStringCellValue());
  407. xCell.setCellValue(str);
  408. assertEquals(str, xCell.getStringCellValue());
  409. sCell.setCellValue(str);
  410. assertEquals(str, sCell.getStringCellValue());
  411. Workbook wbBack = HSSFITestDataProvider.instance.writeOutAndReadBack(wb);
  412. Workbook xwbBack = XSSFITestDataProvider.instance.writeOutAndReadBack(xwb);
  413. Workbook swbBack = SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
  414. cell = wbBack.getSheetAt(0).createRow(0).createCell(0);
  415. xCell = xwbBack.getSheetAt(0).createRow(0).createCell(0);
  416. sCell = swbBack.getSheetAt(0).createRow(0).createCell(0);
  417. assertEquals(cell.getStringCellValue(), xCell.getStringCellValue());
  418. assertEquals(cell.getStringCellValue(), sCell.getStringCellValue());
  419. pos += SpreadsheetVersion.EXCEL97.getMaxTextLength();
  420. swbBack.close();
  421. xwbBack.close();
  422. wbBack.close();
  423. swb.close();
  424. xwb.close();
  425. wb.close();
  426. }
  427. }
  428. private XSSFCell srcCell, destCell; //used for testCopyCellFrom_CellCopyPolicy
  429. @Test
  430. public final void testCopyCellFrom_CellCopyPolicy_default() {
  431. setUp_testCopyCellFrom_CellCopyPolicy();
  432. // default copy policy
  433. final CellCopyPolicy policy = new CellCopyPolicy();
  434. destCell.copyCellFrom(srcCell, policy);
  435. assertEquals(CellType.FORMULA, destCell.getCellType());
  436. assertEquals("2+3", destCell.getCellFormula());
  437. assertEquals(srcCell.getCellStyle(), destCell.getCellStyle());
  438. }
  439. @Test
  440. public final void testCopyCellFrom_CellCopyPolicy_value() {
  441. setUp_testCopyCellFrom_CellCopyPolicy();
  442. // Paste values only
  443. final CellCopyPolicy policy = new CellCopyPolicy.Builder().cellFormula(false).build();
  444. destCell.copyCellFrom(srcCell, policy);
  445. assertEquals(CellType.NUMERIC, destCell.getCellType());
  446. }
  447. @Test
  448. public final void testCopyCellFrom_CellCopyPolicy_formulaWithUnregisteredUDF() {
  449. setUp_testCopyCellFrom_CellCopyPolicy();
  450. srcCell.setCellFormula("MYFUNC2(123, $A5, Sheet1!$B7)");
  451. // Copy formula verbatim (no shifting). This is okay because copyCellFrom is Internal.
  452. // Users should use higher-level copying functions to row- or column-shift formulas.
  453. final CellCopyPolicy policy = new CellCopyPolicy.Builder().cellFormula(true).build();
  454. destCell.copyCellFrom(srcCell, policy);
  455. assertEquals("MYFUNC2(123, $A5, Sheet1!$B7)", destCell.getCellFormula());
  456. }
  457. @Test
  458. public final void testCopyCellFrom_CellCopyPolicy_style() {
  459. setUp_testCopyCellFrom_CellCopyPolicy();
  460. srcCell.setCellValue((String) null);
  461. // Paste styles only
  462. final CellCopyPolicy policy = new CellCopyPolicy.Builder().cellValue(false).build();
  463. destCell.copyCellFrom(srcCell, policy);
  464. assertEquals(srcCell.getCellStyle(), destCell.getCellStyle());
  465. // Old cell value should not have been overwritten
  466. assertNotEquals(CellType.BLANK, destCell.getCellType());
  467. assertEquals(CellType.BOOLEAN, destCell.getCellType());
  468. assertEquals(true, destCell.getBooleanCellValue());
  469. }
  470. @Test
  471. public final void testCopyCellFrom_CellCopyPolicy_copyHyperlink() throws IOException {
  472. setUp_testCopyCellFrom_CellCopyPolicy();
  473. final Workbook wb = srcCell.getSheet().getWorkbook();
  474. final CreationHelper createHelper = wb.getCreationHelper();
  475. srcCell.setCellValue("URL LINK");
  476. Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
  477. link.setAddress("http://poi.apache.org/");
  478. srcCell.setHyperlink(link);
  479. // Set link cell style (optional)
  480. CellStyle hlinkStyle = wb.createCellStyle();
  481. Font hlinkFont = wb.createFont();
  482. hlinkFont.setUnderline(Font.U_SINGLE);
  483. hlinkFont.setColor(IndexedColors.BLUE.getIndex());
  484. hlinkStyle.setFont(hlinkFont);
  485. srcCell.setCellStyle(hlinkStyle);
  486. // Copy hyperlink
  487. final CellCopyPolicy policy = new CellCopyPolicy.Builder().copyHyperlink(true).mergeHyperlink(false).build();
  488. destCell.copyCellFrom(srcCell, policy);
  489. assertNotNull(destCell.getHyperlink());
  490. assertSame("unit test assumes srcCell and destCell are on the same sheet",
  491. srcCell.getSheet(), destCell.getSheet());
  492. final List<XSSFHyperlink> links = srcCell.getSheet().getHyperlinkList();
  493. assertEquals("number of hyperlinks on sheet", 2, links.size());
  494. assertEquals("source hyperlink",
  495. new CellReference(srcCell).formatAsString(), links.get(0).getCellRef());
  496. assertEquals("destination hyperlink",
  497. new CellReference(destCell).formatAsString(), links.get(1).getCellRef());
  498. wb.close();
  499. }
  500. @Test
  501. public final void testCopyCellFrom_CellCopyPolicy_mergeHyperlink() throws IOException {
  502. setUp_testCopyCellFrom_CellCopyPolicy();
  503. final Workbook wb = srcCell.getSheet().getWorkbook();
  504. final CreationHelper createHelper = wb.getCreationHelper();
  505. srcCell.setCellValue("URL LINK");
  506. Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
  507. link.setAddress("http://poi.apache.org/");
  508. destCell.setHyperlink(link);
  509. // Set link cell style (optional)
  510. CellStyle hlinkStyle = wb.createCellStyle();
  511. Font hlinkFont = wb.createFont();
  512. hlinkFont.setUnderline(Font.U_SINGLE);
  513. hlinkFont.setColor(IndexedColors.BLUE.getIndex());
  514. hlinkStyle.setFont(hlinkFont);
  515. destCell.setCellStyle(hlinkStyle);
  516. // Pre-condition assumptions. This test is broken if either of these fail.
  517. assertSame("unit test assumes srcCell and destCell are on the same sheet",
  518. srcCell.getSheet(), destCell.getSheet());
  519. assertNull(srcCell.getHyperlink());
  520. // Merge hyperlink - since srcCell doesn't have a hyperlink, destCell's hyperlink is not overwritten (cleared).
  521. final CellCopyPolicy policy = new CellCopyPolicy.Builder().mergeHyperlink(true).copyHyperlink(false).build();
  522. destCell.copyCellFrom(srcCell, policy);
  523. assertNull(srcCell.getHyperlink());
  524. assertNotNull(destCell.getHyperlink());
  525. assertSame(link, destCell.getHyperlink());
  526. List<XSSFHyperlink> links;
  527. links = srcCell.getSheet().getHyperlinkList();
  528. assertEquals("number of hyperlinks on sheet", 1, links.size());
  529. assertEquals("source hyperlink",
  530. new CellReference(destCell).formatAsString(), links.get(0).getCellRef());
  531. // Merge destCell's hyperlink to srcCell. Since destCell does have a hyperlink, this should copy destCell's hyperlink to srcCell.
  532. srcCell.copyCellFrom(destCell, policy);
  533. assertNotNull(srcCell.getHyperlink());
  534. assertNotNull(destCell.getHyperlink());
  535. links = srcCell.getSheet().getHyperlinkList();
  536. assertEquals("number of hyperlinks on sheet", 2, links.size());
  537. assertEquals("dest hyperlink",
  538. new CellReference(destCell).formatAsString(), links.get(0).getCellRef());
  539. assertEquals("source hyperlink",
  540. new CellReference(srcCell).formatAsString(), links.get(1).getCellRef());
  541. wb.close();
  542. }
  543. private void setUp_testCopyCellFrom_CellCopyPolicy() {
  544. @SuppressWarnings("resource")
  545. final XSSFWorkbook wb = new XSSFWorkbook();
  546. final XSSFRow row = wb.createSheet("Sheet1").createRow(0);
  547. srcCell = row.createCell(0);
  548. destCell = row.createCell(1);
  549. srcCell.setCellFormula("2+3");
  550. final CellStyle style = wb.createCellStyle();
  551. style.setBorderTop(BorderStyle.THICK);
  552. style.setFillBackgroundColor((short) 5);
  553. srcCell.setCellStyle(style);
  554. destCell.setCellValue(true);
  555. }
  556. /**
  557. * Bug 61869: updating a shared formula produces an unreadable file
  558. */
  559. @Test
  560. public void test61869() throws Exception {
  561. try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61869.xlsx")) {
  562. XSSFSheet sheet = wb.getSheetAt(0);
  563. XSSFCell c2 = sheet.getRow(1).getCell(2);
  564. assertEquals("SUM(A2,B2)", c2.getCellFormula());
  565. assertEquals(STCellFormulaType.SHARED, c2.getCTCell().getF().getT());
  566. assertEquals(0, c2.getCTCell().getF().getSi());
  567. XSSFCell c3 = sheet.getRow(2).getCell(2);
  568. assertEquals(STCellFormulaType.SHARED, c3.getCTCell().getF().getT());
  569. assertEquals(0, c3.getCTCell().getF().getSi());
  570. assertEquals("SUM(A3,B3)", c3.getCellFormula());
  571. assertEquals("SUM(A2,B2)", sheet.getSharedFormula(0).getStringValue());
  572. c2.setCellFormula("SUM(A2:B2)");
  573. assertEquals(STCellFormulaType.SHARED, c2.getCTCell().getF().getT()); // c2 remains the master formula
  574. assertEquals("SUM(A2:B2)", sheet.getSharedFormula(0).getStringValue());
  575. assertEquals(STCellFormulaType.SHARED, c3.getCTCell().getF().getT());
  576. assertEquals(0, c3.getCTCell().getF().getSi());
  577. assertEquals("SUM(A3:B3)", c3.getCellFormula()); // formula in the follower cell is rebuilt
  578. }
  579. }
  580. }