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.

BaseTestBugzillaIssues.java 77KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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.ss.usermodel;
  16. import static org.apache.logging.log4j.util.Unbox.box;
  17. import static org.junit.jupiter.api.Assertions.assertArrayEquals;
  18. import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
  19. import static org.junit.jupiter.api.Assertions.assertEquals;
  20. import static org.junit.jupiter.api.Assertions.assertFalse;
  21. import static org.junit.jupiter.api.Assertions.assertNotNull;
  22. import static org.junit.jupiter.api.Assertions.assertNull;
  23. import static org.junit.jupiter.api.Assertions.assertThrows;
  24. import static org.junit.jupiter.api.Assertions.assertTrue;
  25. import static org.junit.jupiter.api.Assumptions.assumeTrue;
  26. import java.awt.font.FontRenderContext;
  27. import java.awt.font.TextAttribute;
  28. import java.awt.font.TextLayout;
  29. import java.awt.geom.Rectangle2D;
  30. import java.io.IOException;
  31. import java.text.AttributedString;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  36. import org.apache.logging.log4j.LogManager;
  37. import org.apache.logging.log4j.Logger;
  38. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  39. import org.apache.poi.poifs.filesystem.FileMagic;
  40. import org.apache.poi.ss.ITestDataProvider;
  41. import org.apache.poi.ss.SpreadsheetVersion;
  42. import org.apache.poi.ss.formula.FormulaParseException;
  43. import org.apache.poi.ss.util.CellAddress;
  44. import org.apache.poi.ss.util.CellRangeAddress;
  45. import org.apache.poi.ss.util.CellRangeAddressList;
  46. import org.apache.poi.ss.util.PaneInformation;
  47. import org.apache.poi.ss.util.SheetUtil;
  48. import org.junit.jupiter.api.Disabled;
  49. import org.junit.jupiter.api.Test;
  50. /**
  51. * A base class for bugzilla issues that can be described in terms of common ss interfaces.
  52. */
  53. public abstract class BaseTestBugzillaIssues {
  54. private static final Logger LOG = LogManager.getLogger(BaseTestBugzillaIssues.class);
  55. private static final String TEST_32 = "Some text with 32 characters to ";
  56. private static final String TEST_255 = "Some very long text that is exactly 255 characters, which are allowed here, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla.....";
  57. private static final String TEST_256 = "Some very long text that is longer than the 255 characters allowed in HSSF here, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla bla, bla1";
  58. private static final String TEST_SPECIAL_TITLE = "special \n\t\r\u0002characters";
  59. private static final String TEST_SPECIAL = "Some text with special \n\t\r\u0002characters to s";
  60. private final ITestDataProvider _testDataProvider;
  61. protected BaseTestBugzillaIssues(ITestDataProvider testDataProvider) {
  62. _testDataProvider = testDataProvider;
  63. }
  64. /**
  65. * Unlike org.junit.Assert.assertEquals(double expected, double actual, double delta),
  66. * where delta is an absolute error value, this function's factor is a relative error,
  67. * so it's easier to express "actual is within 5% of expected".
  68. */
  69. private static void assertAlmostEquals(double expected, double actual, float factor) {
  70. double diff = Math.abs(expected - actual);
  71. double fuzz = expected * factor;
  72. assertTrue(diff <= fuzz, actual + " not within " + fuzz + " of " + expected);
  73. }
  74. /**
  75. * Test writing a hyperlink
  76. * Open resulting sheet in Excel and check that A1 contains a hyperlink
  77. *
  78. * Also tests bug 15353 (problems with hyperlinks to Google)
  79. */
  80. @Test
  81. public final void bug23094() throws IOException {
  82. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  83. Sheet s = wb1.createSheet();
  84. Row r = s.createRow(0);
  85. r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")");
  86. r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
  87. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  88. r = wb2.getSheetAt(0).getRow(0);
  89. Cell cell_0 = r.getCell(0);
  90. assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula());
  91. Cell cell_1 = r.getCell(1);
  92. assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula());
  93. }
  94. }
  95. }
  96. /**
  97. * test writing a file with large number of unique strings,
  98. * open resulting file in Excel to check results!
  99. */
  100. @Test
  101. public final void bug15375_2() throws IOException {
  102. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  103. Sheet sheet = wb1.createSheet();
  104. CreationHelper factory = wb1.getCreationHelper();
  105. final int num = wb1 instanceof HSSFWorkbook ? 6000 : 1000;
  106. for (int i = 0; i < num; i++) {
  107. Row row = sheet.createRow(i);
  108. Cell cell = row.createCell(0);
  109. cell.setCellValue(factory.createRichTextString("Test1" + i));
  110. cell = row.createCell(1);
  111. cell.setCellValue(factory.createRichTextString("Test2" + i));
  112. cell = row.createCell(2);
  113. cell.setCellValue(factory.createRichTextString("Test3" + i));
  114. }
  115. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  116. sheet = wb2.getSheetAt(0);
  117. for (int i = 0; i < num; i++) {
  118. Row row = sheet.getRow(i);
  119. assertEquals("Test1" + i, row.getCell(0).getStringCellValue());
  120. assertEquals("Test2" + i, row.getCell(1).getStringCellValue());
  121. assertEquals("Test3" + i, row.getCell(2).getStringCellValue());
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Merged regions were being removed from the parent in cloned sheets
  128. */
  129. @Test
  130. protected void bug22720() throws IOException {
  131. try (Workbook wb = _testDataProvider.createWorkbook()) {
  132. wb.createSheet("TEST");
  133. Sheet template = wb.getSheetAt(0);
  134. assertEquals(0, template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2)));
  135. assertEquals(1, template.addMergedRegion(new CellRangeAddress(2, 3, 0, 2)));
  136. Sheet clone = wb.cloneSheet(0);
  137. int originalMerged = template.getNumMergedRegions();
  138. assertEquals(2, originalMerged, "2 merged regions");
  139. //remove merged regions from clone
  140. for (int i = template.getNumMergedRegions() - 1; i >= 0; i--) {
  141. clone.removeMergedRegion(i);
  142. }
  143. assertEquals(originalMerged, template.getNumMergedRegions(), "Original Sheet's Merged Regions were removed");
  144. //check if template's merged regions are OK
  145. if (template.getNumMergedRegions() > 0) {
  146. // fetch the first merged region...EXCEPTION OCCURS HERE
  147. template.getMergedRegion(0);
  148. }
  149. }
  150. }
  151. @Test
  152. public final void bug28031() throws IOException {
  153. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  154. Sheet sheet = wb1.createSheet();
  155. wb1.setSheetName(0, "Sheet1");
  156. Row row = sheet.createRow(0);
  157. Cell cell = row.createCell(0);
  158. String formulaText =
  159. "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";
  160. cell.setCellFormula(formulaText);
  161. assertEquals(formulaText, cell.getCellFormula());
  162. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  163. cell = wb2.getSheetAt(0).getRow(0).getCell(0);
  164. assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula());
  165. }
  166. }
  167. }
  168. /**
  169. * Bug 21334: "File error: data may have been lost" with a file
  170. * that contains macros and this formula:
  171. * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}
  172. */
  173. @Test
  174. public final void bug21334() throws IOException {
  175. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  176. Sheet sh = wb1.createSheet();
  177. Cell cell = sh.createRow(0).createCell(0);
  178. String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";
  179. cell.setCellFormula(formula);
  180. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  181. Cell cell_sv = wb2.getSheetAt(0).getRow(0).getCell(0);
  182. assertEquals(formula, cell_sv.getCellFormula());
  183. }
  184. }
  185. }
  186. /** another test for the number of unique strings issue
  187. *test opening the resulting file in Excel*/
  188. @Test
  189. public final void bug22568() throws IOException {
  190. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  191. Sheet sheet = wb1.createSheet("ExcelTest");
  192. int col_cnt = 3;
  193. int rw_cnt = 2000;
  194. Row rw;
  195. rw = sheet.createRow(0);
  196. //Header row
  197. for (int j = 0; j < col_cnt; j++) {
  198. Cell cell = rw.createCell(j);
  199. cell.setCellValue("Col " + (j + 1));
  200. }
  201. for (int i = 1; i < rw_cnt; i++) {
  202. rw = sheet.createRow(i);
  203. for (int j = 0; j < col_cnt; j++) {
  204. Cell cell = rw.createCell(j);
  205. cell.setCellValue("Row:" + (i + 1) + ",Column:" + (j + 1));
  206. }
  207. }
  208. sheet.setDefaultColumnWidth(18);
  209. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  210. sheet = wb2.getSheetAt(0);
  211. rw = sheet.getRow(0);
  212. //Header row
  213. for (int j = 0; j < col_cnt; j++) {
  214. Cell cell = rw.getCell(j);
  215. assertEquals("Col " + (j + 1), cell.getStringCellValue());
  216. }
  217. for (int i = 1; i < rw_cnt; i++) {
  218. rw = sheet.getRow(i);
  219. for (int j = 0; j < col_cnt; j++) {
  220. Cell cell = rw.getCell(j);
  221. assertEquals("Row:" + (i + 1) + ",Column:" + (j + 1), cell.getStringCellValue());
  222. }
  223. }
  224. }
  225. }
  226. }
  227. /**
  228. * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69
  229. */
  230. @Test
  231. public final void bug42448() throws IOException {
  232. String exp = "SUMPRODUCT(A!C7:A!C67, B8:B68) / B69";
  233. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  234. Cell cell = wb1.createSheet().createRow(0).createCell(0);
  235. cell.setCellFormula(exp);
  236. wb1.createSheet("A");
  237. cell.setCellFormula(exp);
  238. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  239. String act = wb2.getSheetAt(0).getRow(0).getCell(0).getCellFormula();
  240. // XSSF saves formula as-is, HSSF saves as PTG and strips the whitespace
  241. assertEquals(exp.replace(" ",""), act.replace(" ", ""));
  242. }
  243. }
  244. }
  245. @Test
  246. protected void bug18800() throws IOException {
  247. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  248. wb1.createSheet("TEST");
  249. Sheet sheet = wb1.cloneSheet(0);
  250. wb1.setSheetName(1, "CLONE");
  251. sheet.createRow(0).createCell(0).setCellValue("Test");
  252. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  253. sheet = wb2.getSheet("CLONE");
  254. Row row = sheet.getRow(0);
  255. Cell cell = row.getCell(0);
  256. assertEquals("Test", cell.getRichStringCellValue().getString());
  257. }
  258. }
  259. }
  260. private static void addNewSheetWithCellsA1toD4(Workbook book, int sheet) {
  261. Sheet sht = book .createSheet("s" + sheet);
  262. for (int r=0; r < 4; r++) {
  263. Row row = sht.createRow (r);
  264. for (int c=0; c < 4; c++) {
  265. Cell cel = row.createCell(c);
  266. cel.setCellValue(sheet*100 + r*10 + c);
  267. }
  268. }
  269. }
  270. @Test
  271. void bug43093() throws IOException {
  272. try (Workbook wb = _testDataProvider.createWorkbook()) {
  273. addNewSheetWithCellsA1toD4(wb, 1);
  274. addNewSheetWithCellsA1toD4(wb, 2);
  275. addNewSheetWithCellsA1toD4(wb, 3);
  276. addNewSheetWithCellsA1toD4(wb, 4);
  277. Sheet s2 = wb.getSheet("s2");
  278. Row s2r3 = s2.getRow(3);
  279. Cell s2E4 = s2r3.createCell(4);
  280. s2E4.setCellFormula("SUM(s3!B2:C3)");
  281. FormulaEvaluator eva = wb.getCreationHelper().createFormulaEvaluator();
  282. double d = eva.evaluate(s2E4).getNumberValue();
  283. assertEquals(d, (311 + 312 + 321 + 322), 0.0000001);
  284. }
  285. }
  286. @Test
  287. protected void bug46729_testMaxFunctionArguments() throws IOException {
  288. String[] func = {"COUNT", "AVERAGE", "MAX", "MIN", "OR", "SUBTOTAL", "SKEW"};
  289. SpreadsheetVersion ssVersion = _testDataProvider.getSpreadsheetVersion();
  290. try (Workbook wb = _testDataProvider.createWorkbook()) {
  291. Cell cell = wb.createSheet().createRow(0).createCell(0);
  292. String fmla;
  293. for (String name : func) {
  294. fmla = createFunction(name, 5);
  295. cell.setCellFormula(fmla);
  296. fmla = createFunction(name, ssVersion.getMaxFunctionArgs());
  297. cell.setCellFormula(fmla);
  298. FormulaParseException e = assertThrows(FormulaParseException.class, () -> {
  299. String fmla2 = createFunction(name, ssVersion.getMaxFunctionArgs() + 1);
  300. cell.setCellFormula(fmla2);
  301. });
  302. assertTrue(e.getMessage().startsWith("Too many arguments to function '" + name + "'"));
  303. }
  304. }
  305. }
  306. private static String createFunction(String name, int maxArgs){
  307. StringBuilder fmla = new StringBuilder();
  308. fmla.append(name);
  309. fmla.append("(");
  310. for(int i=0; i < maxArgs; i++){
  311. if(i > 0) {
  312. fmla.append(',');
  313. }
  314. fmla.append("A1");
  315. }
  316. fmla.append(")");
  317. return fmla.toString();
  318. }
  319. @Test
  320. public final void bug50681_testAutoSize() throws IOException {
  321. try (Workbook wb = _testDataProvider.createWorkbook()) {
  322. Sheet sheet = wb.createSheet("Sheet1");
  323. _testDataProvider.trackAllColumnsForAutosizing(sheet);
  324. Row row = sheet.createRow(0);
  325. Cell cell0 = row.createCell(0);
  326. String longValue = "www.hostname.com, www.hostname.com, " +
  327. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  328. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  329. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  330. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  331. "www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com";
  332. cell0.setCellValue(longValue);
  333. // autoSize will fail if required fonts are not installed, skip this test then
  334. Font font = wb.getFontAt(cell0.getCellStyle().getFontIndex());
  335. assumeTrue(SheetUtil.canComputeColumnWidth(font),
  336. "Cannot verify autoSizeColumn() because the necessary Fonts are not installed on this machine: " + font);
  337. assertEquals(0, cell0.getCellStyle().getIndention(), "Expecting no indentation in this test");
  338. assertEquals(0, cell0.getCellStyle().getRotation(), "Expecting no rotation in this test");
  339. // check computing size up to a large size
  340. // StringBuilder b = new StringBuilder();
  341. // for(int i = 0;i < longValue.length()*5;i++) {
  342. // b.append("w");
  343. // assertTrue("Had zero length starting at length " + i, computeCellWidthFixed(font, b.toString()) > 0);
  344. // }
  345. double widthManual = computeCellWidthManually(cell0, font);
  346. double widthBeforeCell = SheetUtil.getCellWidth(cell0, 8, null, false);
  347. double widthBeforeCol = SheetUtil.getColumnWidth(sheet, 0, false);
  348. String info = widthManual + "/" + widthBeforeCell + "/" + widthBeforeCol + "/" +
  349. SheetUtil.canComputeColumnWidth(font) + "/" + computeCellWidthFixed(font, "1") + "/" + computeCellWidthFixed(font, "w") + "/" +
  350. computeCellWidthFixed(font, "1w") + "/" + computeCellWidthFixed(font, "0000") + "/" + computeCellWidthFixed(font, longValue);
  351. assertTrue(widthManual > 0, "Expected to have cell width > 0 when computing manually, but had " + info);
  352. assertTrue(widthBeforeCell > 0, "Expected to have cell width > 0 BEFORE auto-size, but had " + info);
  353. assertTrue(widthBeforeCol > 0, "Expected to have column width > 0 BEFORE auto-size, but had " + info);
  354. sheet.autoSizeColumn(0);
  355. double width = SheetUtil.getColumnWidth(sheet, 0, false);
  356. assertTrue(width > 0, "Expected to have column width > 0 AFTER auto-size, but had " + width);
  357. width = SheetUtil.getCellWidth(cell0, 8, null, false);
  358. assertTrue(width > 0, "Expected to have cell width > 0 AFTER auto-size, but had " + width);
  359. assertEquals(255 * 256, sheet.getColumnWidth(0)); // maximum column width is 255 characters
  360. sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 50681 reports exception at this point
  361. }
  362. }
  363. @Test
  364. public final void bug51622_testAutoSizeShouldRecognizeLeadingSpaces() throws IOException {
  365. try (Workbook wb = _testDataProvider.createWorkbook()) {
  366. Sheet sheet = wb.createSheet();
  367. _testDataProvider.trackAllColumnsForAutosizing(sheet);
  368. Row row = sheet.createRow(0);
  369. Cell cell0 = row.createCell(0);
  370. Cell cell1 = row.createCell(1);
  371. Cell cell2 = row.createCell(2);
  372. cell0.setCellValue("Test Column AutoSize");
  373. cell1.setCellValue(" Test Column AutoSize");
  374. cell2.setCellValue("Test Column AutoSize ");
  375. sheet.autoSizeColumn(0);
  376. sheet.autoSizeColumn(1);
  377. sheet.autoSizeColumn(2);
  378. int noWhitespaceColWidth = sheet.getColumnWidth(0);
  379. int leadingWhitespaceColWidth = sheet.getColumnWidth(1);
  380. int trailingWhitespaceColWidth = sheet.getColumnWidth(2);
  381. // Based on the amount of text and whitespace used, and the default font
  382. // assume that the cell with whitespace should be at least 20% wider than
  383. // the cell without whitespace. This number is arbitrary, but should be large
  384. // enough to guarantee that the whitespace cell isn't wider due to chance.
  385. // Experimentally, I calculated the ratio as 1.2478181, though this ratio may change
  386. // if the default font or margins change.
  387. final double expectedRatioThreshold = 1.2f;
  388. double leadingWhitespaceRatio = ((double) leadingWhitespaceColWidth) / noWhitespaceColWidth;
  389. double trailingWhitespaceRatio = ((double) leadingWhitespaceColWidth) / noWhitespaceColWidth;
  390. assertGreaterThan("leading whitespace is longer than no whitespace",
  391. leadingWhitespaceRatio, expectedRatioThreshold);
  392. assertGreaterThan("trailing whitespace is longer than no whitespace",
  393. trailingWhitespaceRatio, expectedRatioThreshold);
  394. assertEquals(leadingWhitespaceColWidth, trailingWhitespaceColWidth,
  395. "cells with equal leading and trailing whitespace have equal width");
  396. }
  397. }
  398. /**
  399. * Test if a > b. Fails if false.
  400. */
  401. private void assertGreaterThan(String message, double a, double b) {
  402. assertTrue(a > b, message + ": " + "Expected: " + a + " > " + b);
  403. }
  404. // FIXME: this function is a self-fulfilling prophecy: this test will always pass as long
  405. // as the code-under-test and the testcase code are written the same way (have the same bugs).
  406. private double computeCellWidthManually(Cell cell0, Font font) {
  407. final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
  408. RichTextString rt = cell0.getRichStringCellValue();
  409. String[] lines = rt.getString().split("\\n");
  410. assertEquals(1, lines.length);
  411. String txt = lines[0] + "0";
  412. AttributedString str = new AttributedString(txt);
  413. copyAttributes(font, str, txt.length());
  414. // TODO: support rich text fragments
  415. /*if (rt.numFormattingRuns() > 0) {
  416. }*/
  417. TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
  418. double frameWidth = getFrameWidth(layout);
  419. return (frameWidth / 8);
  420. }
  421. private double getFrameWidth(TextLayout layout) {
  422. Rectangle2D bounds = layout.getBounds();
  423. return bounds.getX() + bounds.getWidth();
  424. }
  425. private double computeCellWidthFixed(Font font, String txt) {
  426. final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
  427. AttributedString str = new AttributedString(txt);
  428. copyAttributes(font, str, txt.length());
  429. TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
  430. return getFrameWidth(layout);
  431. }
  432. private static void copyAttributes(Font font, AttributedString str, int endIdx) {
  433. str.addAttribute(TextAttribute.FAMILY, font.getFontName(), 0, endIdx);
  434. str.addAttribute(TextAttribute.SIZE, (float)font.getFontHeightInPoints());
  435. if (font.getBold()) {
  436. str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, 0, endIdx);
  437. }
  438. if (font.getItalic() ) {
  439. str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, 0, endIdx);
  440. }
  441. if (font.getUnderline() == Font.U_SINGLE ) {
  442. str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 0, endIdx);
  443. }
  444. }
  445. /**
  446. * CreateFreezePane column/row order check
  447. */
  448. @Test
  449. void bug49381() throws IOException {
  450. try (Workbook wb = _testDataProvider.createWorkbook()) {
  451. int colSplit = 1;
  452. int rowSplit = 2;
  453. int leftmostColumn = 3;
  454. int topRow = 4;
  455. Sheet s = wb.createSheet();
  456. // Populate
  457. for (int rn = 0; rn <= topRow; rn++) {
  458. Row r = s.createRow(rn);
  459. for (int cn = 0; cn < leftmostColumn; cn++) {
  460. Cell c = r.createCell(cn, CellType.NUMERIC);
  461. c.setCellValue(100 * rn + cn);
  462. }
  463. }
  464. // Create the Freeze Pane
  465. s.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
  466. PaneInformation paneInfo = s.getPaneInformation();
  467. // Check it
  468. assertEquals(colSplit, paneInfo.getVerticalSplitPosition());
  469. assertEquals(rowSplit, paneInfo.getHorizontalSplitPosition());
  470. assertEquals(leftmostColumn, paneInfo.getVerticalSplitLeftColumn());
  471. assertEquals(topRow, paneInfo.getHorizontalSplitTopRow());
  472. // Now a row only freezepane
  473. s.createFreezePane(0, 3);
  474. paneInfo = s.getPaneInformation();
  475. assertEquals(0, paneInfo.getVerticalSplitPosition());
  476. assertEquals(3, paneInfo.getHorizontalSplitPosition());
  477. assertEquals(0, paneInfo.getVerticalSplitLeftColumn());
  478. assertEquals(3, paneInfo.getHorizontalSplitTopRow());
  479. // Now a column only freezepane
  480. s.createFreezePane(4, 0);
  481. paneInfo = s.getPaneInformation();
  482. assertEquals(4, paneInfo.getVerticalSplitPosition());
  483. assertEquals(0, paneInfo.getHorizontalSplitPosition());
  484. assertEquals(4, paneInfo.getVerticalSplitLeftColumn());
  485. assertEquals(0, paneInfo.getHorizontalSplitTopRow());
  486. }
  487. }
  488. /**
  489. * Test hyperlinks
  490. * open resulting file in excel, and check that there is a link to Google
  491. */
  492. @Test
  493. void bug15353() throws IOException {
  494. String hyperlinkF = "HYPERLINK(\"http://google.com\",\"Google\")";
  495. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  496. Sheet sheet = wb1.createSheet("My sheet");
  497. Row row = sheet.createRow(0);
  498. Cell cell = row.createCell(0);
  499. cell.setCellFormula(hyperlinkF);
  500. assertEquals(hyperlinkF, cell.getCellFormula());
  501. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  502. sheet = wb2.getSheet("My Sheet");
  503. row = sheet.getRow(0);
  504. cell = row.getCell(0);
  505. assertEquals(hyperlinkF, cell.getCellFormula());
  506. }
  507. }
  508. }
  509. /**
  510. * HLookup and VLookup with optional arguments
  511. */
  512. @Test
  513. void bug51024() throws IOException {
  514. try (Workbook wb = _testDataProvider.createWorkbook()) {
  515. Sheet s = wb.createSheet();
  516. Row r1 = s.createRow(0);
  517. Row r2 = s.createRow(1);
  518. r1.createCell(0).setCellValue("v A1");
  519. r2.createCell(0).setCellValue("v A2");
  520. r1.createCell(1).setCellValue("v B1");
  521. Cell c = r1.createCell(4);
  522. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  523. c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1)");
  524. assertEquals("v A1", eval.evaluate(c).getStringValue());
  525. c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1, 1)");
  526. assertEquals("v A1", eval.evaluate(c).getStringValue());
  527. c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1, )");
  528. assertEquals("v A1", eval.evaluate(c).getStringValue());
  529. c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1)");
  530. assertEquals("v A1", eval.evaluate(c).getStringValue());
  531. c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1, 1)");
  532. assertEquals("v A1", eval.evaluate(c).getStringValue());
  533. c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1, )");
  534. assertEquals("v A1", eval.evaluate(c).getStringValue());
  535. }
  536. }
  537. @Test
  538. void stackoverflow23114397() throws IOException {
  539. try (Workbook wb = _testDataProvider.createWorkbook()) {
  540. DataFormat format = wb.getCreationHelper().createDataFormat();
  541. // How close the sizing should be, given that not all
  542. // systems will have quite the same fonts on them
  543. float fontAccuracy = 0.22f;
  544. // x%
  545. CellStyle iPercent = wb.createCellStyle();
  546. iPercent.setDataFormat(format.getFormat("0%"));
  547. // x.x%
  548. CellStyle d1Percent = wb.createCellStyle();
  549. d1Percent.setDataFormat(format.getFormat("0.0%"));
  550. // x.xx%
  551. CellStyle d2Percent = wb.createCellStyle();
  552. d2Percent.setDataFormat(format.getFormat("0.00%"));
  553. Sheet s = wb.createSheet();
  554. _testDataProvider.trackAllColumnsForAutosizing(s);
  555. Row r1 = s.createRow(0);
  556. for (int i = 0; i < 3; i++) {
  557. r1.createCell(i, CellType.NUMERIC).setCellValue(0);
  558. }
  559. for (int i = 3; i < 6; i++) {
  560. r1.createCell(i, CellType.NUMERIC).setCellValue(1);
  561. }
  562. for (int i = 6; i < 9; i++) {
  563. r1.createCell(i, CellType.NUMERIC).setCellValue(0.12345);
  564. }
  565. for (int i = 9; i < 12; i++) {
  566. r1.createCell(i, CellType.NUMERIC).setCellValue(1.2345);
  567. }
  568. for (int i = 0; i < 12; i += 3) {
  569. r1.getCell(i).setCellStyle(iPercent);
  570. r1.getCell(i + 1).setCellStyle(d1Percent);
  571. r1.getCell(i + 2).setCellStyle(d2Percent);
  572. }
  573. for (int i = 0; i < 12; i++) {
  574. s.autoSizeColumn(i);
  575. }
  576. // Check the 0(.00)% ones
  577. assertAlmostEquals(980, s.getColumnWidth(0), fontAccuracy);
  578. assertAlmostEquals(1400, s.getColumnWidth(1), fontAccuracy);
  579. assertAlmostEquals(1700, s.getColumnWidth(2), fontAccuracy);
  580. // Check the 100(.00)% ones
  581. assertAlmostEquals(1500, s.getColumnWidth(3), fontAccuracy);
  582. assertAlmostEquals(1950, s.getColumnWidth(4), fontAccuracy);
  583. assertAlmostEquals(2225, s.getColumnWidth(5), fontAccuracy);
  584. // Check the 12(.34)% ones
  585. assertAlmostEquals(1225, s.getColumnWidth(6), fontAccuracy);
  586. assertAlmostEquals(1650, s.getColumnWidth(7), fontAccuracy);
  587. assertAlmostEquals(1950, s.getColumnWidth(8), fontAccuracy);
  588. // Check the 123(.45)% ones
  589. assertAlmostEquals(1500, s.getColumnWidth(9), fontAccuracy);
  590. assertAlmostEquals(1950, s.getColumnWidth(10), fontAccuracy);
  591. assertAlmostEquals(2225, s.getColumnWidth(11), fontAccuracy);
  592. }
  593. }
  594. /**
  595. * =ISNUMBER(SEARCH("AM",A1)) evaluation
  596. */
  597. @Test
  598. void stackoverflow26437323() throws IOException {
  599. try (Workbook wb = _testDataProvider.createWorkbook()) {
  600. Sheet s = wb.createSheet();
  601. Row r1 = s.createRow(0);
  602. Row r2 = s.createRow(1);
  603. // A1 is a number
  604. r1.createCell(0).setCellValue(1.1);
  605. // B1 is a string, with the wanted text in it
  606. r1.createCell(1).setCellValue("This is text with AM in it");
  607. // C1 is a string, with different text
  608. r1.createCell(2).setCellValue("This some other text");
  609. // D1 is a blank cell
  610. r1.createCell(3, CellType.BLANK);
  611. // E1 is null
  612. // A2 will hold our test formulas
  613. Cell cf = r2.createCell(0, CellType.FORMULA);
  614. // First up, check that TRUE and ISLOGICAL both behave
  615. cf.setCellFormula("TRUE()");
  616. cf = evaluateCell(wb, cf);
  617. assertTrue(cf.getBooleanCellValue());
  618. cf.setCellFormula("ISLOGICAL(TRUE())");
  619. cf = evaluateCell(wb, cf);
  620. assertTrue(cf.getBooleanCellValue());
  621. cf.setCellFormula("ISLOGICAL(4)");
  622. cf = evaluateCell(wb, cf);
  623. assertFalse(cf.getBooleanCellValue());
  624. // Now, check ISNUMBER / ISTEXT / ISNONTEXT
  625. cf.setCellFormula("ISNUMBER(A1)");
  626. cf = evaluateCell(wb, cf);
  627. assertTrue(cf.getBooleanCellValue());
  628. cf.setCellFormula("ISNUMBER(B1)");
  629. cf = evaluateCell(wb, cf);
  630. assertFalse(cf.getBooleanCellValue());
  631. cf.setCellFormula("ISNUMBER(C1)");
  632. cf = evaluateCell(wb, cf);
  633. assertFalse(cf.getBooleanCellValue());
  634. cf.setCellFormula("ISNUMBER(D1)");
  635. cf = evaluateCell(wb, cf);
  636. assertFalse(cf.getBooleanCellValue());
  637. cf.setCellFormula("ISNUMBER(E1)");
  638. cf = evaluateCell(wb, cf);
  639. assertFalse(cf.getBooleanCellValue());
  640. cf.setCellFormula("ISTEXT(A1)");
  641. cf = evaluateCell(wb, cf);
  642. assertFalse(cf.getBooleanCellValue());
  643. cf.setCellFormula("ISTEXT(B1)");
  644. cf = evaluateCell(wb, cf);
  645. assertTrue(cf.getBooleanCellValue());
  646. cf.setCellFormula("ISTEXT(C1)");
  647. cf = evaluateCell(wb, cf);
  648. assertTrue(cf.getBooleanCellValue());
  649. cf.setCellFormula("ISTEXT(D1)");
  650. cf = evaluateCell(wb, cf);
  651. assertFalse(cf.getBooleanCellValue());
  652. cf.setCellFormula("ISTEXT(E1)");
  653. cf = evaluateCell(wb, cf);
  654. assertFalse(cf.getBooleanCellValue());
  655. cf.setCellFormula("ISNONTEXT(A1)");
  656. cf = evaluateCell(wb, cf);
  657. assertTrue(cf.getBooleanCellValue());
  658. cf.setCellFormula("ISNONTEXT(B1)");
  659. cf = evaluateCell(wb, cf);
  660. assertFalse(cf.getBooleanCellValue());
  661. cf.setCellFormula("ISNONTEXT(C1)");
  662. cf = evaluateCell(wb, cf);
  663. assertFalse(cf.getBooleanCellValue());
  664. cf.setCellFormula("ISNONTEXT(D1)");
  665. cf = evaluateCell(wb, cf);
  666. assertTrue(cf.getBooleanCellValue());
  667. cf.setCellFormula("ISNONTEXT(E1)");
  668. cf = evaluateCell(wb, cf);
  669. assertTrue(cf.getBooleanCellValue()); // Blank and Null the same
  670. // Next up, SEARCH on its own
  671. cf.setCellFormula("SEARCH(\"am\", A1)");
  672. cf = evaluateCell(wb, cf);
  673. assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
  674. cf.setCellFormula("SEARCH(\"am\", B1)");
  675. cf = evaluateCell(wb, cf);
  676. assertEquals(19, (int) cf.getNumericCellValue());
  677. cf.setCellFormula("SEARCH(\"am\", C1)");
  678. cf = evaluateCell(wb, cf);
  679. assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
  680. cf.setCellFormula("SEARCH(\"am\", D1)");
  681. cf = evaluateCell(wb, cf);
  682. assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
  683. // Finally, bring it all together
  684. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", A1))");
  685. cf = evaluateCell(wb, cf);
  686. assertFalse(cf.getBooleanCellValue());
  687. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", B1))");
  688. cf = evaluateCell(wb, cf);
  689. assertTrue(cf.getBooleanCellValue());
  690. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", C1))");
  691. cf = evaluateCell(wb, cf);
  692. assertFalse(cf.getBooleanCellValue());
  693. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", D1))");
  694. cf = evaluateCell(wb, cf);
  695. assertFalse(cf.getBooleanCellValue());
  696. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", E1))");
  697. cf = evaluateCell(wb, cf);
  698. assertFalse(cf.getBooleanCellValue());
  699. }
  700. }
  701. private Cell evaluateCell(Workbook wb, Cell c) {
  702. Sheet s = c.getSheet();
  703. wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(c);
  704. return s.getRow(c.getRowIndex()).getCell(c.getColumnIndex());
  705. }
  706. /**
  707. * Should be able to write then read formulas with references
  708. * to cells in other files, eg '[refs/airport.xls]Sheet1'!$A$2
  709. * or 'http://192.168.1.2/[blank.xls]Sheet1'!$A$1 .
  710. * Additionally, if a reference to that file is provided, it should
  711. * be possible to evaluate them too
  712. * TODO Fix this to evaluate for XSSF
  713. * TODO Fix this to work at all for HSSF
  714. */
  715. @Disabled("Fix this to evaluate for XSSF, Fix this to work at all for HSSF")
  716. @Test
  717. void bug46670() throws IOException {
  718. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  719. Sheet s = wb1.createSheet();
  720. Row r1 = s.createRow(0);
  721. // References to try
  722. String ext = _testDataProvider.getStandardFileNameExtension();
  723. String refLocal = "'[test." + ext + "]Sheet1'!$A$2";
  724. String refHttp = "'[http://example.com/test." + ext + "]Sheet1'!$A$2";
  725. String otherCellText = "In Another Workbook";
  726. // Create the references
  727. r1.createCell(0, CellType.FORMULA).setCellFormula(refLocal);
  728. r1.createCell(1, CellType.FORMULA).setCellFormula(refHttp);
  729. // Check they were set correctly
  730. assertEquals(refLocal, r1.getCell(0).getCellFormula());
  731. assertEquals(refHttp, r1.getCell(1).getCellFormula());
  732. // Reload, and ensure they were serialised and read correctly
  733. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  734. s = wb2.getSheetAt(0);
  735. r1 = s.getRow(0);
  736. final Cell c1 = r1.getCell(0);
  737. final Cell c2 = r1.getCell(1);
  738. assertEquals(refLocal, c1.getCellFormula());
  739. assertEquals(refHttp, c2.getCellFormula());
  740. // Try to evaluate, without giving a way to get at the other file
  741. assertThrows(Exception.class, () -> evaluateCell(wb2, c1),
  742. "Shouldn't be able to evaluate without the other file");
  743. assertThrows(Exception.class, () -> evaluateCell(wb2, c2),
  744. "Shouldn't be able to evaluate without the other file");
  745. // Set up references to the other file
  746. try (Workbook wb3 = _testDataProvider.createWorkbook()) {
  747. wb3.createSheet().createRow(1).createCell(0).setCellValue(otherCellText);
  748. Map<String, FormulaEvaluator> evaluators = new HashMap<>();
  749. evaluators.put(refLocal, wb3.getCreationHelper().createFormulaEvaluator());
  750. evaluators.put(refHttp, wb3.getCreationHelper().createFormulaEvaluator());
  751. FormulaEvaluator evaluator = wb2.getCreationHelper().createFormulaEvaluator();
  752. evaluator.setupReferencedWorkbooks(evaluators);
  753. // Try to evaluate, with the other file
  754. evaluator.evaluateFormulaCell(c1);
  755. evaluator.evaluateFormulaCell(c2);
  756. assertEquals(otherCellText, c1.getStringCellValue());
  757. assertEquals(otherCellText, c2.getStringCellValue());
  758. }
  759. }
  760. }
  761. }
  762. @Test
  763. void test56574OverwriteExistingRow() throws IOException {
  764. try (Workbook wb = _testDataProvider.createWorkbook()) {
  765. Sheet sheet = wb.createSheet();
  766. { // create the Formula-Cell
  767. Row row = sheet.createRow(0);
  768. Cell cell = row.createCell(0);
  769. cell.setCellFormula("A2");
  770. }
  771. { // check that it is there now
  772. Row row = sheet.getRow(0);
  773. /* CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray();
  774. assertEquals(1, cArray.length);*/
  775. Cell cell = row.getCell(0);
  776. assertEquals(CellType.FORMULA, cell.getCellType());
  777. }
  778. { // overwrite the row
  779. Row row = sheet.createRow(0);
  780. assertNotNull(row);
  781. }
  782. { // creating a row in place of another should remove the existing data,
  783. // check that the cell is gone now
  784. Row row = sheet.getRow(0);
  785. /*CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray();
  786. assertEquals(0, cArray.length);*/
  787. Cell cell = row.getCell(0);
  788. assertNull(cell);
  789. }
  790. // the calculation chain in XSSF is empty in a newly created workbook, so we cannot check if it is correctly updated
  791. /*assertNull(((XSSFWorkbook)wb).getCalculationChain());
  792. assertNotNull(((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain());
  793. assertNotNull(((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain().getCArray());
  794. assertEquals(0, ((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain().getCArray().length);*/
  795. }
  796. }
  797. /**
  798. * With HSSF, if you create a font, don't change it, and
  799. * create a 2nd, you really do get two fonts that you
  800. * can alter as and when you want.
  801. * With XSSF, that wasn't the case, but this verifies
  802. * that it now is again
  803. */
  804. @Test
  805. void bug48718() throws IOException {
  806. try (Workbook wb = _testDataProvider.createWorkbook()) {
  807. int startingFonts = wb instanceof HSSFWorkbook ? 4 : 1;
  808. assertEquals(startingFonts, wb.getNumberOfFonts());
  809. // Get a font, and slightly change it
  810. Font a = wb.createFont();
  811. assertEquals(startingFonts + 1, wb.getNumberOfFonts());
  812. a.setFontHeightInPoints((short) 23);
  813. assertEquals(startingFonts + 1, wb.getNumberOfFonts());
  814. // Get two more, unchanged
  815. /*Font b =*/
  816. wb.createFont();
  817. assertEquals(startingFonts + 2, wb.getNumberOfFonts());
  818. /*Font c =*/
  819. wb.createFont();
  820. assertEquals(startingFonts + 3, wb.getNumberOfFonts());
  821. }
  822. }
  823. @Test
  824. void bug57430() throws IOException {
  825. try (Workbook wb = _testDataProvider.createWorkbook()) {
  826. wb.createSheet("Sheet1");
  827. Name name1 = wb.createName();
  828. name1.setNameName("FMLA");
  829. assertDoesNotThrow(() -> name1.setRefersToFormula("Sheet1!$B$3"));
  830. }
  831. }
  832. @Test
  833. void bug56981() throws IOException {
  834. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  835. CellStyle vertTop = wb1.createCellStyle();
  836. vertTop.setVerticalAlignment(VerticalAlignment.TOP);
  837. CellStyle vertBottom = wb1.createCellStyle();
  838. vertBottom.setVerticalAlignment(VerticalAlignment.BOTTOM);
  839. Sheet sheet = wb1.createSheet("Sheet 1");
  840. Row row = sheet.createRow(0);
  841. Cell top = row.createCell(0);
  842. Cell bottom = row.createCell(1);
  843. top.setCellValue("Top");
  844. top.setCellStyle(vertTop); // comment this out to get all bottom-aligned
  845. // cells
  846. bottom.setCellValue("Bottom");
  847. bottom.setCellStyle(vertBottom);
  848. row.setHeightInPoints(85.75f); // make it obvious
  849. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  850. Cell cell = wb2.getSheetAt(0).getRow(0).getCell(1);
  851. assertEquals(VerticalAlignment.BOTTOM, cell.getCellStyle().getVerticalAlignment());
  852. }
  853. }
  854. }
  855. @Test
  856. void test57973() throws IOException {
  857. String[] vals = { "Cell0", "F4", "C3" };
  858. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  859. CreationHelper helper1 = wb1.getCreationHelper();
  860. Sheet sheet1 = wb1.createSheet();
  861. Drawing<?> drawing1 = sheet1.createDrawingPatriarch();
  862. for (int i=0; i< vals.length; i++) {
  863. Cell cell = sheet1.createRow(i).createCell(i);
  864. cell.setCellValue(vals[i]);
  865. ClientAnchor anchor = helper1.createClientAnchor();
  866. anchor.setCol1(i);
  867. anchor.setCol2(i);
  868. anchor.setRow1(i);
  869. anchor.setRow2(i);
  870. Comment comment = drawing1.createCellComment(anchor);
  871. RichTextString str = helper1.createRichTextString("Hello, World"+i);
  872. comment.setString(str);
  873. comment.setAuthor("Apache POI");
  874. cell.setCellComment(comment);
  875. comment.setColumn(i);
  876. comment.setRow(i);
  877. //apply custom font to the text in the comment
  878. Font font = wb1.createFont();
  879. font.setFontName("Arial");
  880. font.setFontHeightInPoints((short) (14+i));
  881. font.setBold(true);
  882. font.setColor(IndexedColors.RED.getIndex());
  883. str.applyFont(font);
  884. }
  885. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  886. Sheet sheet2 = wb2.getSheetAt(0);
  887. for (int i=0; i<vals.length; i++) {
  888. Cell cell = sheet2.getRow(i).getCell(i);
  889. assertEquals(vals[i], cell.getStringCellValue());
  890. CellAddress cr = new CellAddress(cell);
  891. Comment comment = sheet2.getCellComment(cr);
  892. assertEquals("Apache POI", comment.getAuthor());
  893. RichTextString str = comment.getString();
  894. assertEquals("Hello, World"+i, str.getString());
  895. }
  896. }
  897. }
  898. }
  899. /**
  900. * Ensures that XSSF and HSSF agree with each other,
  901. * and with the docs on when fetching the wrong
  902. * kind of value from a Formula cell
  903. */
  904. @Test
  905. protected void bug47815() throws IOException {
  906. try (Workbook wb = _testDataProvider.createWorkbook()) {
  907. Sheet s = wb.createSheet();
  908. Row r = s.createRow(0);
  909. // Setup
  910. Cell cn = r.createCell(0, CellType.NUMERIC);
  911. cn.setCellValue(1.2);
  912. Cell cs = r.createCell(1, CellType.STRING);
  913. cs.setCellValue("Testing");
  914. Cell cfn = r.createCell(2, CellType.FORMULA);
  915. cfn.setCellFormula("A1");
  916. Cell cfs = r.createCell(3, CellType.FORMULA);
  917. cfs.setCellFormula("B1");
  918. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  919. assertEquals(CellType.NUMERIC, fe.evaluate(cfn).getCellType());
  920. assertEquals(CellType.STRING, fe.evaluate(cfs).getCellType());
  921. fe.evaluateFormulaCell(cfn);
  922. fe.evaluateFormulaCell(cfs);
  923. // Now test
  924. assertEquals(CellType.NUMERIC, cn.getCellType());
  925. assertEquals(CellType.STRING, cs.getCellType());
  926. assertEquals(CellType.FORMULA, cfn.getCellType());
  927. assertEquals(CellType.NUMERIC, cfn.getCachedFormulaResultType());
  928. assertEquals(CellType.FORMULA, cfs.getCellType());
  929. assertEquals(CellType.STRING, cfs.getCachedFormulaResultType());
  930. // Different ways of retrieving
  931. assertEquals(1.2, cn.getNumericCellValue(), 0);
  932. assertThrows(IllegalStateException.class, cn::getRichStringCellValue);
  933. assertEquals("Testing", cs.getStringCellValue());
  934. assertThrows(IllegalStateException.class, cs::getNumericCellValue);
  935. assertEquals(1.2, cfn.getNumericCellValue(), 0);
  936. assertThrows(IllegalStateException.class, cfn::getRichStringCellValue);
  937. assertEquals("Testing", cfs.getStringCellValue());
  938. assertThrows(IllegalStateException.class, cfs::getNumericCellValue);
  939. }
  940. }
  941. @Test
  942. void test58113() throws IOException {
  943. try (Workbook wb = _testDataProvider.createWorkbook()) {
  944. Sheet sheet = wb.createSheet("Test");
  945. Row row = sheet.createRow(0);
  946. Cell cell = row.createCell(0);
  947. // verify that null-values can be set, this was possible up to 3.11, but broken in 3.12
  948. cell.setCellValue((String) null);
  949. String value = cell.getStringCellValue();
  950. assertTrue(value == null || value.length() == 0,
  951. "HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value);
  952. cell = row.createCell(1);
  953. cell.setCellFormula("0");
  954. cell.setCellValue((String) null);
  955. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  956. value = cell.getStringCellValue();
  957. assertTrue(value == null || value.length() == 0,
  958. "HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value);
  959. // set some value
  960. cell.setCellValue("somevalue");
  961. value = cell.getStringCellValue();
  962. assertEquals("somevalue", value, "can set value afterwards: " + value);
  963. // verify that the null-value is actually set even if there was some value in the cell before
  964. cell.setCellValue((String) null);
  965. value = cell.getStringCellValue();
  966. assertTrue(value == null || value.length() == 0,
  967. "HSSF will currently return empty string, XSSF/SXSSF will return null, but had: " + value);
  968. }
  969. }
  970. /**
  971. * Formulas with Nested Ifs, or If with text functions like
  972. * Mid in it, can give #VALUE in Excel
  973. */
  974. @Test
  975. void bug55747() throws IOException {
  976. try (Workbook wb1 = _testDataProvider.createWorkbook()) {
  977. FormulaEvaluator ev = wb1.getCreationHelper().createFormulaEvaluator();
  978. Sheet s = wb1.createSheet();
  979. Row row = s.createRow(0);
  980. row.createCell(0).setCellValue("abc");
  981. row.createCell(1).setCellValue("");
  982. row.createCell(2).setCellValue(3);
  983. Cell cell = row.createCell(5);
  984. cell.setCellFormula("IF(A1<>\"\",MID(A1,1,2),\" \")");
  985. ev.evaluateAll();
  986. assertEquals("ab", cell.getStringCellValue());
  987. cell = row.createCell(6);
  988. cell.setCellFormula("IF(B1<>\"\",MID(A1,1,2),\"empty\")");
  989. ev.evaluateAll();
  990. assertEquals("empty", cell.getStringCellValue());
  991. cell = row.createCell(7);
  992. cell.setCellFormula("IF(A1<>\"\",IF(C1<>\"\",MID(A1,1,2),\"c1\"),\"c2\")");
  993. ev.evaluateAll();
  994. assertEquals("ab", cell.getStringCellValue());
  995. // Write it back out, and re-read
  996. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  997. ev = wb2.getCreationHelper().createFormulaEvaluator();
  998. s = wb2.getSheetAt(0);
  999. row = s.getRow(0);
  1000. // Check read ok, and re-evaluate fine
  1001. cell = row.getCell(5);
  1002. assertEquals("ab", cell.getStringCellValue());
  1003. assertEquals(CellType.FORMULA, cell.getCellType());
  1004. assertEquals("IF(A1<>\"\",MID(A1,1,2),\" \")", cell.getCellFormula());
  1005. ev.evaluateFormulaCell(cell);
  1006. assertEquals("ab", cell.getStringCellValue());
  1007. assertEquals(CellType.FORMULA, cell.getCellType());
  1008. assertEquals("IF(A1<>\"\",MID(A1,1,2),\" \")", cell.getCellFormula());
  1009. cell = row.getCell(6);
  1010. assertEquals("empty", cell.getStringCellValue());
  1011. assertEquals(CellType.FORMULA, cell.getCellType());
  1012. assertEquals("IF(B1<>\"\",MID(A1,1,2),\"empty\")", cell.getCellFormula());
  1013. ev.evaluateFormulaCell(cell);
  1014. assertEquals("empty", cell.getStringCellValue());
  1015. assertEquals(CellType.FORMULA, cell.getCellType());
  1016. assertEquals("IF(B1<>\"\",MID(A1,1,2),\"empty\")", cell.getCellFormula());
  1017. cell = row.getCell(7);
  1018. assertEquals("ab", cell.getStringCellValue());
  1019. assertEquals(CellType.FORMULA, cell.getCellType());
  1020. assertEquals("IF(A1<>\"\",IF(C1<>\"\",MID(A1,1,2),\"c1\"),\"c2\")", cell.getCellFormula());
  1021. ev.evaluateFormulaCell(cell);
  1022. assertEquals("ab", cell.getStringCellValue());
  1023. assertEquals(CellType.FORMULA, cell.getCellType());
  1024. assertEquals("IF(A1<>\"\",IF(C1<>\"\",MID(A1,1,2),\"c1\"),\"c2\")", cell.getCellFormula());
  1025. }
  1026. }
  1027. }
  1028. @Test
  1029. void bug58260() throws IOException {
  1030. //Create workbook and worksheet
  1031. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1032. //Sheet worksheet = wb.createSheet("sample");
  1033. //Loop through and add all values from array list
  1034. // use a fixed seed to always produce the same file which makes comparing stuff easier
  1035. //Random rnd = new Random(4352345);
  1036. int maxStyles = (wb instanceof HSSFWorkbook) ? 4009 : 64000;
  1037. for (int i = 0; i < maxStyles; i++) {
  1038. //Create new row
  1039. //Row row = worksheet.createRow(i);
  1040. //Create cell style
  1041. CellStyle style = wb.createCellStyle();
  1042. style.setAlignment(HorizontalAlignment.RIGHT);
  1043. if ((wb instanceof HSSFWorkbook)) {
  1044. // there are some predefined styles
  1045. assertEquals(i + 21, style.getIndex());
  1046. } else {
  1047. // getIndex() returns short, which is not sufficient for > 32767
  1048. // we should really change the API to be "int" for getIndex() but
  1049. // that needs API changes
  1050. assertEquals(i + 1, style.getIndex() & 0xffff);
  1051. }
  1052. //Create cell
  1053. //Cell cell = row.createCell(0);
  1054. //Set cell style
  1055. //cell.setCellStyle(style);
  1056. //Set cell value
  1057. //cell.setCellValue("r" + rnd.nextInt());
  1058. }
  1059. // should fail if we try to add more now
  1060. assertThrows(IllegalStateException.class, wb::createCellStyle,
  1061. "Should fail after " + maxStyles + " styles, but did not fail");
  1062. /*//add column width for appearance sake
  1063. worksheet.setColumnWidth(0, 5000);
  1064. // Write the output to a file
  1065. System.out.println("Writing...");
  1066. OutputStream fileOut = new FileOutputStream("C:\\temp\\58260." + _testDataProvider.getStandardFileNameExtension());
  1067. // the resulting file can be compressed nicely, so we need to disable the zip bomb detection here
  1068. double before = ZipSecureFile.getMinInflateRatio();
  1069. try {
  1070. ZipSecureFile.setMinInflateRatio(0.00001);
  1071. wb.write(fileOut);
  1072. } finally {
  1073. fileOut.close();
  1074. ZipSecureFile.setMinInflateRatio(before);
  1075. }*/
  1076. }
  1077. }
  1078. @Test
  1079. void test50319() throws IOException {
  1080. try (Workbook wb = new HSSFWorkbook()) {
  1081. Sheet sheet = wb.createSheet("Test");
  1082. sheet.createRow(0);
  1083. sheet.groupRow(0, 0);
  1084. sheet.setRowGroupCollapsed(0, true);
  1085. sheet.groupColumn(0, 0);
  1086. assertDoesNotThrow(() -> sheet.setColumnGroupCollapsed(0, true));
  1087. }
  1088. }
  1089. // Bug 58648: FormulaParser throws exception in parseSimpleFactor() when getCellFormula()
  1090. // is called on a cell and the formula contains spaces between closing parentheses ") )"
  1091. @Test
  1092. void test58648() throws IOException {
  1093. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1094. Cell cell = wb.createSheet().createRow(0).createCell(0);
  1095. assertDoesNotThrow(() -> cell.setCellFormula("((1 + 1) )"));
  1096. }
  1097. }
  1098. /**
  1099. * If someone sets a null string as a cell value, treat
  1100. * it as an empty cell, and avoid a NPE on auto-sizing
  1101. */
  1102. @Test
  1103. void test57034() throws Exception {
  1104. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1105. Sheet s = wb.createSheet();
  1106. Cell cell = s.createRow(0).createCell(0);
  1107. cell.setCellValue((String) null);
  1108. assertEquals(CellType.BLANK, cell.getCellType());
  1109. _testDataProvider.trackAllColumnsForAutosizing(s);
  1110. s.autoSizeColumn(0);
  1111. assertEquals(2048, s.getColumnWidth(0));
  1112. s.autoSizeColumn(0, true);
  1113. assertEquals(2048, s.getColumnWidth(0));
  1114. }
  1115. }
  1116. @Test
  1117. void test52684() throws IOException {
  1118. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1119. Sheet sheet = wb.createSheet("test");
  1120. Row row = sheet.createRow(0);
  1121. Cell cell = row.createCell(0);
  1122. cell.setCellValue(12312345123L);
  1123. DataFormat format = wb.createDataFormat();
  1124. CellStyle style = wb.createCellStyle();
  1125. style.setDataFormat(format.getFormat("000-00000-000"));
  1126. cell.setCellStyle(style);
  1127. assertEquals("000-00000-000",
  1128. cell.getCellStyle().getDataFormatString());
  1129. assertEquals(164, cell.getCellStyle().getDataFormat());
  1130. DataFormatter formatter = new DataFormatter();
  1131. assertEquals("12-312-345-123", formatter.formatCellValue(cell));
  1132. }
  1133. }
  1134. @Test
  1135. @SuppressWarnings("java:S2699")
  1136. void test58896() throws IOException {
  1137. final int nrows = 160;
  1138. final int ncols = 139;
  1139. // Create a workbook
  1140. try (Workbook wb = _testDataProvider.createWorkbook(nrows+1)) {
  1141. final Sheet sh = wb.createSheet();
  1142. LOG.atDebug().log("{} column autosizing timing...", wb.getClass().getName());
  1143. final long t0 = time();
  1144. _testDataProvider.trackAllColumnsForAutosizing(sh);
  1145. for (int r = 0; r < nrows; r++) {
  1146. final Row row = sh.createRow(r);
  1147. for (int c = 0; c < ncols; c++) {
  1148. final Cell cell = row.createCell(c);
  1149. cell.setCellValue("Cell[r=" + r + ",c=" + c + "]");
  1150. }
  1151. }
  1152. final double populateSheetTime = delta(t0);
  1153. final double populateSheetTimePerCell_ns = (1000000 * populateSheetTime / (nrows * ncols));
  1154. LOG.atDebug().log("Populate sheet time: {} ms ({} ns/cell)", populateSheetTime, populateSheetTimePerCell_ns);
  1155. LOG.atDebug().log("Autosizing...");
  1156. final long t1 = time();
  1157. for (int c = 0; c < ncols; c++) {
  1158. final long t2 = time();
  1159. sh.autoSizeColumn(c);
  1160. LOG.atDebug().log("Column {} took {} ms", box(c),delta(t2));
  1161. }
  1162. final double autoSizeColumnsTime = delta(t1);
  1163. final double autoSizeColumnsTimePerColumn = autoSizeColumnsTime / ncols;
  1164. final double bestFitWidthTimePerCell_ns = 1000000 * autoSizeColumnsTime / (ncols * nrows);
  1165. LOG.atDebug().log("Auto sizing columns took a total of {} ms ({} ms per column)", autoSizeColumnsTime, autoSizeColumnsTimePerColumn);
  1166. LOG.atDebug().log("Best fit width time per cell: {} ns", bestFitWidthTimePerCell_ns);
  1167. final double totalTime_s = (populateSheetTime + autoSizeColumnsTime) / 1000;
  1168. LOG.atDebug().log("Total time: {} s", totalTime_s);
  1169. }
  1170. //if (bestFitWidthTimePerCell_ns > 50000) {
  1171. // fail("Best fit width time per cell exceeded 50000 ns: " + bestFitWidthTimePerCell_ns + " ns");
  1172. //}
  1173. //if (totalTime_s > 10) {
  1174. // fail("Total time exceeded 10 seconds: " + totalTime_s + " s");
  1175. //}
  1176. }
  1177. protected long time() {
  1178. return System.currentTimeMillis();
  1179. }
  1180. protected double delta(long startTimeMillis) {
  1181. return time() - startTimeMillis;
  1182. }
  1183. @Test
  1184. void bug59393_commentsCanHaveSameAnchor() throws IOException {
  1185. // only executed for HSSF currently
  1186. assumeTrue("xls".equals(_testDataProvider.getStandardFileNameExtension()));
  1187. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1188. Sheet sheet = wb.createSheet();
  1189. CreationHelper helper = wb.getCreationHelper();
  1190. ClientAnchor anchor = helper.createClientAnchor();
  1191. Drawing<?> drawing = sheet.createDrawingPatriarch();
  1192. Row row = sheet.createRow(0);
  1193. Cell cell1 = row.createCell(0);
  1194. Cell cell2 = row.createCell(1);
  1195. Cell cell3 = row.createCell(2);
  1196. Comment comment1 = drawing.createCellComment(anchor);
  1197. RichTextString richTextString1 = helper.createRichTextString("comment1");
  1198. comment1.setString(richTextString1);
  1199. cell1.setCellComment(comment1);
  1200. // fails with IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: A1")
  1201. // because createCellComment tries to create a cell at A1
  1202. // (from CellAddress(anchor.getRow1(), anchor.getCell1())),
  1203. // but cell A1 already has a comment (comment1).
  1204. // Need to atomically create a comment and attach it to a cell.
  1205. // Current workaround: change anchor between each usage
  1206. // anchor.setCol1(1);
  1207. Comment comment2 = drawing.createCellComment(anchor);
  1208. RichTextString richTextString2 = helper.createRichTextString("comment2");
  1209. comment2.setString(richTextString2);
  1210. assertDoesNotThrow(() -> cell2.setCellComment(comment2));
  1211. // anchor.setCol1(2);
  1212. Comment comment3 = drawing.createCellComment(anchor);
  1213. RichTextString richTextString3 = helper.createRichTextString("comment3");
  1214. comment3.setString(richTextString3);
  1215. assertDoesNotThrow(() -> cell3.setCellComment(comment3));
  1216. }
  1217. }
  1218. @Test
  1219. protected void bug57798() throws Exception {
  1220. String fileName = "57798." + _testDataProvider.getStandardFileNameExtension();
  1221. try (Workbook workbook = _testDataProvider.openSampleWorkbook(fileName)) {
  1222. Sheet sheet = workbook.getSheet("Sheet1");
  1223. // *******************************
  1224. // First cell of array formula, OK
  1225. final int rowId = 0;
  1226. final int cellId = 1;
  1227. Row row = sheet.getRow(rowId);
  1228. Cell cell = row.getCell(cellId);
  1229. assertEquals("A1", cell.getCellFormula());
  1230. if (CellType.FORMULA == cell.getCellType()) {
  1231. CellType formulaResultType = cell.getCachedFormulaResultType();
  1232. assertEquals(CellType.STRING, formulaResultType);
  1233. }
  1234. // *******************************
  1235. // Second cell of array formula, NOT OK for xlsx files
  1236. row = sheet.getRow(rowId);
  1237. cell = row.getCell(cellId);
  1238. assertEquals("A1", cell.getCellFormula());
  1239. if (CellType.FORMULA == cell.getCellType()) {
  1240. CellType formulaResultType = cell.getCachedFormulaResultType();
  1241. assertEquals(CellType.STRING, formulaResultType);
  1242. }
  1243. }
  1244. }
  1245. @Disabled
  1246. @Test
  1247. void test57929() throws IOException {
  1248. // Create a workbook with print areas on 2 sheets
  1249. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1250. wb.createSheet("Sheet0");
  1251. wb.createSheet("Sheet1");
  1252. wb.setPrintArea(0, "$A$1:$C$6");
  1253. wb.setPrintArea(1, "$B$1:$C$5");
  1254. // Verify the print areas were set correctly
  1255. assertEquals("Sheet0!$A$1:$C$6", wb.getPrintArea(0));
  1256. assertEquals("Sheet1!$B$1:$C$5", wb.getPrintArea(1));
  1257. // Remove the print area on Sheet0 and change the print area on Sheet1
  1258. wb.removePrintArea(0);
  1259. wb.setPrintArea(1, "$A$1:$A$1");
  1260. // Verify that the changes were made
  1261. assertNull(wb.getPrintArea(0), "Sheet0 before write");
  1262. assertEquals("Sheet1!$A$1:$A$1", wb.getPrintArea(1), "Sheet1 before write");
  1263. // Verify that the changes are non-volatile
  1264. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb)) {
  1265. // CURRENTLY FAILS with "Sheet0!$A$1:$C$6"
  1266. assertNull(wb2.getPrintArea(0), "Sheet0 after write");
  1267. assertEquals("Sheet1!$A$1:$A$1", wb2.getPrintArea(1), "Sheet1 after write");
  1268. }
  1269. }
  1270. }
  1271. @Test
  1272. void test55384() throws Exception {
  1273. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1274. Sheet sh = wb.createSheet();
  1275. for (int rownum = 0; rownum < 10; rownum++) {
  1276. Row row = sh.createRow(rownum);
  1277. for (int cellnum = 0; cellnum < 3; cellnum++) {
  1278. Cell cell = row.createCell(cellnum);
  1279. cell.setCellValue(rownum + cellnum);
  1280. }
  1281. }
  1282. Row row = sh.createRow(10);
  1283. // setting no precalculated value works just fine.
  1284. Cell cell1 = row.createCell(0);
  1285. cell1.setCellFormula("SUM(A1:A10)");
  1286. // but setting a precalculated STRING value fails totally in SXSSF
  1287. Cell cell2 = row.createCell(1);
  1288. cell2.setCellFormula("SUM(B1:B10)");
  1289. cell2.setCellValue("55");
  1290. // setting a precalculated int value works as expected
  1291. Cell cell3 = row.createCell(2);
  1292. cell3.setCellFormula("SUM(C1:C10)");
  1293. cell3.setCellValue(65);
  1294. assertEquals(CellType.FORMULA, cell1.getCellType());
  1295. assertEquals(CellType.FORMULA, cell2.getCellType());
  1296. assertEquals(CellType.FORMULA, cell3.getCellType());
  1297. assertEquals("SUM(A1:A10)", cell1.getCellFormula());
  1298. assertEquals("SUM(B1:B10)", cell2.getCellFormula());
  1299. assertEquals("SUM(C1:C10)", cell3.getCellFormula());
  1300. try (Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb)) {
  1301. checkFormulaPreevaluatedString(wbBack);
  1302. }
  1303. }
  1304. }
  1305. private void checkFormulaPreevaluatedString(Workbook readFile) {
  1306. Sheet sheet = readFile.getSheetAt(0);
  1307. Row row = sheet.getRow(sheet.getLastRowNum());
  1308. assertEquals(10, row.getRowNum());
  1309. for (Cell cell : row) {
  1310. CellType ctype = cell.getCellType();
  1311. assertTrue(ctype == CellType.STRING || ctype == CellType.FORMULA, "unexpected cell type");
  1312. String cellValue = (ctype == CellType.STRING)
  1313. ? cell.getRichStringCellValue().getString()
  1314. : cell.getCellFormula();
  1315. assertNotNull(cellValue);
  1316. cellValue = cellValue.isEmpty() ? null : cellValue;
  1317. assertNotNull(cellValue);
  1318. }
  1319. }
  1320. // bug 60197: setSheetOrder should update sheet-scoped named ranges to maintain references to the sheets before the re-order
  1321. @Test
  1322. protected void bug60197_NamedRangesReferToCorrectSheetWhenSheetOrderIsChanged() throws Exception {
  1323. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1324. Sheet sheet1 = wb.createSheet("Sheet1");
  1325. Sheet sheet2 = wb.createSheet("Sheet2");
  1326. Sheet sheet3 = wb.createSheet("Sheet3");
  1327. Name nameOnSheet1 = wb.createName();
  1328. nameOnSheet1.setSheetIndex(wb.getSheetIndex(sheet1));
  1329. nameOnSheet1.setNameName("NameOnSheet1");
  1330. nameOnSheet1.setRefersToFormula("Sheet1!A1");
  1331. Name nameOnSheet2 = wb.createName();
  1332. nameOnSheet2.setSheetIndex(wb.getSheetIndex(sheet2));
  1333. nameOnSheet2.setNameName("NameOnSheet2");
  1334. nameOnSheet2.setRefersToFormula("Sheet2!A1");
  1335. Name nameOnSheet3 = wb.createName();
  1336. nameOnSheet3.setSheetIndex(wb.getSheetIndex(sheet3));
  1337. nameOnSheet3.setNameName("NameOnSheet3");
  1338. nameOnSheet3.setRefersToFormula("Sheet3!A1");
  1339. // workbook-scoped name
  1340. Name name = wb.createName();
  1341. name.setNameName("WorkbookScopedName");
  1342. name.setRefersToFormula("Sheet2!A1");
  1343. assertEquals("Sheet1", nameOnSheet1.getSheetName());
  1344. assertEquals("Sheet2", nameOnSheet2.getSheetName());
  1345. assertEquals("Sheet3", nameOnSheet3.getSheetName());
  1346. assertEquals(-1, name.getSheetIndex());
  1347. assertEquals("Sheet2!A1", name.getRefersToFormula());
  1348. // rearrange the sheets several times to make sure the names always refer to the right sheet
  1349. for (int i = 0; i <= 9; i++) {
  1350. wb.setSheetOrder("Sheet3", i % 3);
  1351. // Current bug in XSSF:
  1352. // Call stack:
  1353. // XSSFWorkbook.write(OutputStream)
  1354. // XSSFWorkbook.commit()
  1355. // XSSFWorkbook.saveNamedRanges()
  1356. // This dumps the current namedRanges to CTDefinedName and writes these to the CTWorkbook
  1357. // Then the XSSFName namedRanges list is cleared and rebuilt
  1358. // Thus, any XSSFName object becomes invalid after a write
  1359. // This re-assignment to the XSSFNames is not necessary if wb.write is not called.
  1360. nameOnSheet1 = wb.getName("NameOnSheet1");
  1361. nameOnSheet2 = wb.getName("NameOnSheet2");
  1362. nameOnSheet3 = wb.getName("NameOnSheet3");
  1363. name = wb.getName("WorkbookScopedName");
  1364. // The name should still refer to the same sheet after the sheets are re-ordered
  1365. assertEquals(i % 3, wb.getSheetIndex("Sheet3"));
  1366. assertEquals("Sheet1", nameOnSheet1.getSheetName());
  1367. assertEquals("Sheet2", nameOnSheet2.getSheetName());
  1368. assertEquals("Sheet3", nameOnSheet3.getSheetName());
  1369. assertEquals(-1, name.getSheetIndex());
  1370. assertEquals("Sheet2!A1", name.getRefersToFormula());
  1371. // make sure the changes to the names stick after writing out the workbook
  1372. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb)) {
  1373. // See note above. XSSFNames become invalid after workbook write
  1374. // Without reassignment here, an XmlValueDisconnectedException may occur
  1375. nameOnSheet1 = wb2.getName("NameOnSheet1");
  1376. nameOnSheet2 = wb2.getName("NameOnSheet2");
  1377. nameOnSheet3 = wb2.getName("NameOnSheet3");
  1378. name = wb2.getName("WorkbookScopedName");
  1379. // Saving the workbook should not change the sheet names
  1380. assertEquals(i % 3, wb2.getSheetIndex("Sheet3"));
  1381. assertEquals("Sheet1", nameOnSheet1.getSheetName());
  1382. assertEquals("Sheet2", nameOnSheet2.getSheetName());
  1383. assertEquals("Sheet3", nameOnSheet3.getSheetName());
  1384. assertEquals(-1, name.getSheetIndex());
  1385. assertEquals("Sheet2!A1", name.getRefersToFormula());
  1386. // Verify names in wb2
  1387. nameOnSheet1 = wb2.getName("NameOnSheet1");
  1388. nameOnSheet2 = wb2.getName("NameOnSheet2");
  1389. nameOnSheet3 = wb2.getName("NameOnSheet3");
  1390. name = wb2.getName("WorkbookScopedName");
  1391. assertEquals(i % 3, wb2.getSheetIndex("Sheet3"));
  1392. assertEquals("Sheet1", nameOnSheet1.getSheetName());
  1393. assertEquals("Sheet2", nameOnSheet2.getSheetName());
  1394. assertEquals("Sheet3", nameOnSheet3.getSheetName());
  1395. assertEquals(-1, name.getSheetIndex());
  1396. assertEquals("Sheet2!A1", name.getRefersToFormula());
  1397. }
  1398. }
  1399. }
  1400. }
  1401. @Test
  1402. void test59200() throws IOException {
  1403. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1404. final Sheet sheet = wb.createSheet();
  1405. DataValidation dataValidation;
  1406. CellRangeAddressList headerCell = new CellRangeAddressList(0, 1, 0, 1);
  1407. DataValidationConstraint constraint = sheet.getDataValidationHelper().createCustomConstraint("A1<>\"\"");
  1408. dataValidation = sheet.getDataValidationHelper().createValidation(constraint, headerCell);
  1409. // HSSF has 32/255 limits as part of the Spec, XSSF has no limit in the spec, but Excel applies a 255 length limit!
  1410. // more than 255 fail for all
  1411. checkFails(dataValidation, TEST_256, TEST_32);
  1412. checkFails(dataValidation, TEST_32, TEST_256);
  1413. // null does work
  1414. checkPasses(dataValidation, null, null);
  1415. // more than 32 title fail for HSSFWorkbook
  1416. if (wb instanceof HSSFWorkbook) {
  1417. checkFails(dataValidation, TEST_255, TEST_32);
  1418. } else {
  1419. checkPasses(dataValidation, TEST_255, TEST_32);
  1420. }
  1421. // special characters work
  1422. checkPasses(dataValidation, TEST_SPECIAL_TITLE, TEST_SPECIAL);
  1423. // 32 length title and 255 length text work for both
  1424. checkPasses(dataValidation, TEST_32, TEST_255);
  1425. dataValidation.setShowErrorBox(false);
  1426. sheet.addValidationData(dataValidation);
  1427. // write out and read back in to trigger some more validation
  1428. final Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
  1429. final Sheet sheetBack = wbBack.getSheetAt(0);
  1430. final List<? extends DataValidation> dataValidations = sheetBack.getDataValidations();
  1431. assertEquals(1, dataValidations.size());
  1432. }
  1433. }
  1434. private void checkFails(DataValidation dataValidation, String title, String text) {
  1435. assertThrows(IllegalStateException.class, () -> dataValidation.createPromptBox(title, text));
  1436. assertThrows(IllegalStateException.class, () -> dataValidation.createErrorBox(title, text));
  1437. }
  1438. private void checkPasses(DataValidation dataValidation, String title, String text) {
  1439. dataValidation.createPromptBox(title, text);
  1440. dataValidation.createErrorBox(title, text);
  1441. }
  1442. @Test
  1443. void test60370() throws IOException {
  1444. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1445. final Sheet sheet = wb.createSheet();
  1446. DataValidation dataValidation;
  1447. CellRangeAddressList headerCell = new CellRangeAddressList(0, 1, 0, 1);
  1448. DataValidationConstraint constraint = sheet.getDataValidationHelper().createCustomConstraint("A1<>\"\"");
  1449. dataValidation = sheet.getDataValidationHelper().createValidation(constraint, headerCell);
  1450. checkPasses(dataValidation, TEST_SPECIAL_TITLE, TEST_SPECIAL);
  1451. dataValidation.setShowErrorBox(true);
  1452. dataValidation.setShowPromptBox(true);
  1453. sheet.addValidationData(dataValidation);
  1454. // write out and read back in to trigger some more validation
  1455. final Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
  1456. final Sheet sheetBack = wbBack.getSheetAt(0);
  1457. final List<? extends DataValidation> dataValidations = sheetBack.getDataValidations();
  1458. assertEquals(1, dataValidations.size());
  1459. }
  1460. }
  1461. protected void assertFormula(Workbook wb, Cell intF, String expectedFormula, String expectedResultOrNull) {
  1462. assertEquals(CellType.FORMULA, intF.getCellType());
  1463. if (null == expectedResultOrNull) {
  1464. assertEquals(CellType.ERROR, intF.getCachedFormulaResultType());
  1465. expectedResultOrNull = "#VALUE!";
  1466. } else {
  1467. assertEquals(CellType.NUMERIC, intF.getCachedFormulaResultType());
  1468. }
  1469. assertEquals(expectedFormula, intF.getCellFormula());
  1470. // Check we can evaluate it correctly
  1471. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  1472. assertEquals(expectedResultOrNull, eval.evaluate(intF).formatAsString());
  1473. }
  1474. @Test
  1475. void testWriteDocumentTwice() throws Exception {
  1476. try (Workbook wb = _testDataProvider.createWorkbook()) {
  1477. Sheet sheet = wb.createSheet("RawData");
  1478. Row row = sheet.createRow(0);
  1479. Cell cell;
  1480. cell = row.createCell(0);
  1481. cell.setCellValue("Ernie & Bert");
  1482. cell = row.createCell(1);
  1483. // Set a precalculated formula value containing a special character.
  1484. cell.setCellValue("Ernie & Bert are cool!");
  1485. cell.setCellFormula("A1 & \" are cool!\"");
  1486. try (UnsynchronizedByteArrayOutputStream out1 = UnsynchronizedByteArrayOutputStream.builder().get();
  1487. UnsynchronizedByteArrayOutputStream out2 = UnsynchronizedByteArrayOutputStream.builder().get()) {
  1488. wb.write(out1);
  1489. wb.write(out2);
  1490. out1.flush();
  1491. out2.flush();
  1492. // to avoid flaky tests if the documents are written at slightly different timestamps
  1493. // we clear some bytes which contain timestamps
  1494. assertArrayEquals(
  1495. removeTimestamp(out1.toByteArray()),
  1496. removeTimestamp(out2.toByteArray()));
  1497. }
  1498. }
  1499. }
  1500. private byte[] removeTimestamp(byte[] bytes) {
  1501. if (FileMagic.valueOf(bytes) == FileMagic.OOXML) {
  1502. // This removes the timestamp in the header of the ZIP-Format
  1503. // see "Local file header" at https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
  1504. bytes[10] = 0;
  1505. bytes[11] = 0;
  1506. bytes[12] = 0;
  1507. bytes[13] = 0;
  1508. // there is a timestamp for every entry, so try to replace a few more byte-positions
  1509. // to reduce flakiness of this test, however we likely do not yet cover all entries
  1510. bytes[390] = 0;
  1511. bytes[391] = 0;
  1512. bytes[674] = 0;
  1513. bytes[676] = 0;
  1514. bytes[883] = 0;
  1515. bytes[1207] = 0;
  1516. bytes[1208] = 0;
  1517. bytes[1433] = 0;
  1518. bytes[1434] = 0;
  1519. bytes[1817] = 0;
  1520. bytes[1818] = 0;
  1521. bytes[2098] = 0;
  1522. bytes[2099] = 0;
  1523. bytes[2762] = 0;
  1524. bytes[2763] = 0;
  1525. bytes[2382] = 0;
  1526. bytes[2383] = 0;
  1527. bytes[2827] = 0;
  1528. bytes[2828] = 0;
  1529. bytes[2884] = 0;
  1530. bytes[2885] = 0;
  1531. bytes[2946] = 0;
  1532. bytes[2947] = 0;
  1533. bytes[3009] = 0;
  1534. bytes[3010] = 0;
  1535. bytes[3075] = 0;
  1536. bytes[3076] = 0;
  1537. bytes[3134] = 0;
  1538. bytes[3135] = 0;
  1539. bytes[3195] = 0;
  1540. bytes[3196] = 0;
  1541. bytes[3267] = 0;
  1542. bytes[3268] = 0;
  1543. }
  1544. return bytes;
  1545. }
  1546. }