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 30KB

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