Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

TestBugs.java 98KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.apache.poi.hssf.HSSFTestDataSamples.openSampleWorkbook;
  18. import static org.apache.poi.hssf.HSSFTestDataSamples.writeOutAndReadBack;
  19. import static org.junit.jupiter.api.Assertions.*;
  20. import java.awt.image.BufferedImage;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.net.URL;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.Calendar;
  28. import java.util.HashSet;
  29. import java.util.Iterator;
  30. import java.util.LinkedHashMap;
  31. import java.util.List;
  32. import java.util.Locale;
  33. import java.util.Map;
  34. import java.util.Objects;
  35. import java.util.Set;
  36. import java.util.TimeZone;
  37. import java.util.function.Consumer;
  38. import java.util.stream.Collectors;
  39. import java.util.stream.IntStream;
  40. import javax.imageio.ImageIO;
  41. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  42. import org.apache.poi.hpsf.PropertySet;
  43. import org.apache.poi.hpsf.SummaryInformation;
  44. import org.apache.poi.hssf.HSSFITestDataProvider;
  45. import org.apache.poi.hssf.HSSFTestDataSamples;
  46. import org.apache.poi.hssf.extractor.ExcelExtractor;
  47. import org.apache.poi.hssf.model.InternalSheet;
  48. import org.apache.poi.hssf.model.InternalWorkbook;
  49. import org.apache.poi.hssf.record.CellValueRecordInterface;
  50. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  51. import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
  52. import org.apache.poi.hssf.record.NameRecord;
  53. import org.apache.poi.hssf.record.TabIdRecord;
  54. import org.apache.poi.hssf.record.UnknownRecord;
  55. import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
  56. import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
  57. import org.apache.poi.hssf.record.common.UnicodeString;
  58. import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
  59. import org.apache.poi.poifs.filesystem.DocumentEntry;
  60. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  61. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  62. import org.apache.poi.ss.formula.ptg.Area3DPtg;
  63. import org.apache.poi.ss.formula.ptg.DeletedArea3DPtg;
  64. import org.apache.poi.ss.formula.ptg.Ptg;
  65. import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
  66. import org.apache.poi.ss.usermodel.BorderStyle;
  67. import org.apache.poi.ss.usermodel.Cell;
  68. import org.apache.poi.ss.usermodel.CellStyle;
  69. import org.apache.poi.ss.usermodel.CellType;
  70. import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
  71. import org.apache.poi.ss.usermodel.ComparisonOperator;
  72. import org.apache.poi.ss.usermodel.DataFormatter;
  73. import org.apache.poi.ss.usermodel.FormulaError;
  74. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  75. import org.apache.poi.ss.usermodel.Name;
  76. import org.apache.poi.ss.usermodel.Row;
  77. import org.apache.poi.ss.usermodel.Sheet;
  78. import org.apache.poi.ss.usermodel.SheetVisibility;
  79. import org.apache.poi.ss.usermodel.Workbook;
  80. import org.apache.poi.ss.util.CellRangeAddress;
  81. import org.apache.poi.util.LocaleUtil;
  82. import org.junit.jupiter.api.AfterAll;
  83. import org.junit.jupiter.api.Assumptions;
  84. import org.junit.jupiter.api.BeforeAll;
  85. import org.junit.jupiter.api.Disabled;
  86. import org.junit.jupiter.api.Test;
  87. import org.junit.jupiter.params.ParameterizedTest;
  88. import org.junit.jupiter.params.provider.CsvSource;
  89. /**
  90. * Testcases for bugs entered in bugzilla
  91. * the Test name contains the bugzilla bug id
  92. * <p>
  93. * <b>YK: If a bug can be tested in terms of common ss interfaces,
  94. * define the test in the base class {@link BaseTestBugzillaIssues}</b>
  95. */
  96. final class TestBugs extends BaseTestBugzillaIssues {
  97. public TestBugs() {
  98. super(HSSFITestDataProvider.instance);
  99. }
  100. private static final Map<String,HSSFFormulaEvaluator> SIMPLE_REFS = new LinkedHashMap<>();
  101. // References used for the simpleTest convenience method
  102. @BeforeAll
  103. public static void initSimpleRefs() {
  104. String[] refs = {
  105. "Calculations",
  106. "/Documents and Settings/crawformk.EUU/Local Settings/Temporary Internet Files/OLK64/Daily Status Report Generation Files/DST - Daily Data Transfer Sheet - 2002.xls",
  107. "Sheet1",
  108. "/Documents and Settings/donnag/Local Settings/Temporary Internet Files/OLK7/0231324V1-1.xls",
  109. "Sheet1",
  110. "refs/airport.xls",
  111. "Sheet1",
  112. "9http://www.principlesofeconometrics.com/excel/airline.xls",
  113. "Sheet1",
  114. "C:Documents and Settings/Yegor/My Documents/csco.xls",
  115. };
  116. for (int i=0; i<refs.length; i+=2) {
  117. HSSFWorkbook wb = new HSSFWorkbook();
  118. wb.createSheet(refs[i]);
  119. String fileName = refs[i+1];
  120. if (!fileName.contains("://")) {
  121. fileName = fileName.replace("/", File.separator);
  122. }
  123. SIMPLE_REFS.put(fileName, wb.getCreationHelper().createFormulaEvaluator());
  124. }
  125. LocaleUtil.setUserLocale(Locale.US);
  126. }
  127. @AfterAll
  128. public static void cleanUpRefs() {
  129. SIMPLE_REFS.clear();
  130. }
  131. private static void setCellText(HSSFCell cell, String text) {
  132. cell.setCellValue(new HSSFRichTextString(text));
  133. }
  134. /**
  135. * test rewriting a file with large number of unique strings
  136. * open resulting file in Excel to check results!
  137. */
  138. @Test
  139. void bug15375() throws Exception {
  140. try (HSSFWorkbook wb1 = openSampleWorkbook("15375.xls")) {
  141. HSSFSheet sheet = wb1.getSheetAt(0);
  142. HSSFRow row = sheet.getRow(5);
  143. HSSFCell cell = row.getCell(3);
  144. if (cell == null) {
  145. cell = row.createCell(3);
  146. }
  147. // Write test
  148. setCellText(cell, "a test");
  149. // change existing numeric cell value
  150. HSSFRow oRow = sheet.getRow(14);
  151. oRow.getCell(4).setCellValue(75);
  152. setCellText(oRow.getCell(5), "0.3");
  153. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  154. HSSFRow oRow2 = wb2.getSheetAt(0).getRow(14);
  155. assertEquals(75, oRow2.getCell(4).getNumericCellValue());
  156. assertEquals("0.3", oRow2.getCell(5).getStringCellValue());
  157. }
  158. }
  159. }
  160. /**
  161. * Double byte strings
  162. */
  163. @Test
  164. void bug15556() throws Exception {
  165. simpleTest("15556.xls", wb -> assertNotNull(wb.getSheetAt(0).getRow(45)));
  166. }
  167. @Test
  168. void bug24215() throws Exception {
  169. try (HSSFWorkbook wb = openSampleWorkbook("24215.xls")) {
  170. for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) {
  171. HSSFSheet sheet = wb.getSheetAt(sheetIndex);
  172. assertNotNull(sheet);
  173. int rows = sheet.getLastRowNum();
  174. for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
  175. HSSFRow row = sheet.getRow(rowIndex);
  176. assertNotNull(row);
  177. int cells = row.getLastCellNum();
  178. for (int cellIndex = 0; cellIndex < cells; cellIndex++) {
  179. row.getCell(cellIndex);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. /**
  186. * Tests read and write of Unicode strings in formula results
  187. * bug and testcase submitted by Sompop Kumnoonsate
  188. * The file contains THAI unicode characters.
  189. */
  190. @Test
  191. void bugUnicodeStringFormulaRead() throws Exception {
  192. try (HSSFWorkbook w = openSampleWorkbook("25695.xls")) {
  193. HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell(0);
  194. HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell(1);
  195. HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell(0);
  196. HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell(1);
  197. HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell(0);
  198. HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell(1);
  199. HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell(0);
  200. HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell(1);
  201. /*
  202. // THAI code page
  203. System.out.println("a1="+unicodeString(a1));
  204. System.out.println("a2="+unicodeString(a2));
  205. // US code page
  206. System.out.println("b1="+unicodeString(b1));
  207. System.out.println("b2="+unicodeString(b2));
  208. // THAI+US
  209. System.out.println("c1="+unicodeString(c1));
  210. System.out.println("c2="+unicodeString(c2));
  211. // US+THAI
  212. System.out.println("d1="+unicodeString(d1));
  213. System.out.println("d2="+unicodeString(d2));
  214. */
  215. confirmSameCellText(a1, a2);
  216. confirmSameCellText(b1, b2);
  217. confirmSameCellText(c1, c2);
  218. confirmSameCellText(d1, d2);
  219. try (HSSFWorkbook rw = writeOutAndReadBack(w)) {
  220. HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0);
  221. HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1);
  222. HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell(0);
  223. HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell(1);
  224. HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell(0);
  225. HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell(1);
  226. HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell(0);
  227. HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell(1);
  228. confirmSameCellText(a1, ra1);
  229. confirmSameCellText(b1, rb1);
  230. confirmSameCellText(c1, rc1);
  231. confirmSameCellText(d1, rd1);
  232. confirmSameCellText(a1, ra2);
  233. confirmSameCellText(b1, rb2);
  234. confirmSameCellText(c1, rc2);
  235. confirmSameCellText(d1, rd2);
  236. }
  237. }
  238. }
  239. private static void confirmSameCellText(HSSFCell a, HSSFCell b) {
  240. assertEquals(a.getRichStringCellValue().getString(), b.getRichStringCellValue().getString());
  241. }
  242. /**
  243. * names and macros
  244. */
  245. @Test
  246. void bug27852() throws Exception {
  247. try (HSSFWorkbook wb = openSampleWorkbook("27852.xls")) {
  248. for (HSSFName name : wb.getAllNames()) {
  249. assertNotNull(name.getNameName());
  250. if (!name.isFunctionName()) {
  251. assertNotNull(name.getRefersToFormula());
  252. }
  253. }
  254. }
  255. }
  256. /**
  257. * Bug 29206: NPE on HSSFSheet.getRow for blank rows
  258. */
  259. @Test
  260. void bug29206() throws Exception {
  261. //the first check with blank workbook
  262. try (HSSFWorkbook wb = openSampleWorkbook("Simple.xls")) {
  263. HSSFSheet sheet = wb.createSheet();
  264. for (int i = 1; i < 400; i++) {
  265. HSSFRow row = sheet.getRow(i);
  266. assertNull(row);
  267. }
  268. }
  269. }
  270. /**
  271. * Bug 29942: Importing Excel files that have been created by Open Office on Linux
  272. */
  273. @Test
  274. void bug29942() throws Exception {
  275. try (HSSFWorkbook wb = openSampleWorkbook("29942.xls")) {
  276. HSSFSheet sheet = wb.getSheetAt(0);
  277. int count = 0;
  278. for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
  279. HSSFRow row = sheet.getRow(i);
  280. if (row != null) {
  281. HSSFCell cell = row.getCell(0);
  282. assertEquals(CellType.STRING, cell.getCellType());
  283. count++;
  284. }
  285. }
  286. assertEquals(85, count); //should read 85 rows
  287. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb)) {
  288. assertNotNull(wb2.getSheetAt(0));
  289. }
  290. }
  291. }
  292. /**
  293. * Bug 30540: HSSFSheet.setRowBreak throws NullPointerException
  294. */
  295. @Test
  296. void bug30540() throws Exception {
  297. try (HSSFWorkbook wb = openSampleWorkbook("30540.xls")) {
  298. HSSFSheet s1 = wb.getSheetAt(0);
  299. s1.setRowBreak(1);
  300. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb)) {
  301. HSSFSheet s2 = wb2.getSheetAt(0);
  302. int[] act = s2.getRowBreaks();
  303. int[] exp = { 1 };
  304. assertArrayEquals(exp, act);
  305. }
  306. }
  307. }
  308. /**
  309. * Bug 35564: HSSFCell.java: NullPtrExc in isGridsPrinted() and getProtect()
  310. * when HSSFWorkbook is created from file
  311. */
  312. @Test
  313. void bug35564() throws Exception {
  314. simpleTest("35564.xls", wb -> {
  315. HSSFSheet sheet = wb.getSheetAt(0);
  316. assertFalse(sheet.isGridsPrinted());
  317. assertFalse(sheet.getProtect());
  318. });
  319. }
  320. /**
  321. * Bug 35565: HSSFCell.java: NullPtrExc in getColumnBreaks() when HSSFWorkbook is created from file
  322. */
  323. @Test
  324. void bug35565() throws Exception {
  325. simpleTest("35565.xls", wb -> assertNotNull(wb.getSheetAt(0)));
  326. }
  327. /**
  328. * Bug 40285: CellIterator Skips First Column
  329. */
  330. @Test
  331. void bug40285() throws Exception {
  332. try (HSSFWorkbook wb = openSampleWorkbook("40285.xls")) {
  333. HSSFSheet sheet = wb.getSheetAt(0);
  334. int rownum = 0;
  335. for (Iterator<Row> it = sheet.rowIterator(); it.hasNext(); rownum++) {
  336. Row row = it.next();
  337. assertEquals(rownum, row.getRowNum());
  338. int cellNum = 0;
  339. for (Iterator<Cell> it2 = row.cellIterator(); it2.hasNext(); cellNum++) {
  340. Cell cell = it2.next();
  341. assertEquals(cellNum, cell.getColumnIndex());
  342. }
  343. }
  344. }
  345. }
  346. /**
  347. * Test bug 38266: NPE when adding a row break
  348. * <p>
  349. * User's diagnosis:
  350. * 1. Manually (i.e., not using POI) create an Excel Workbook, making sure it
  351. * contains a sheet that doesn't have any row breaks
  352. * 2. Using POI, create a new HSSFWorkbook from the template in step #1
  353. * 3. Try adding a row break (via sheet.setRowBreak()) to the sheet mentioned in step #1
  354. * 4. Get a NullPointerException
  355. */
  356. @Test
  357. void bug38266() throws Exception {
  358. String[] files = {"Simple.xls", "SimpleMultiCell.xls", "duprich1.xls"};
  359. for (String file : files) {
  360. try (HSSFWorkbook wb = openSampleWorkbook(file)) {
  361. HSSFSheet sheet = wb.getSheetAt(0);
  362. int[] breaks = sheet.getRowBreaks();
  363. assertEquals(0, breaks.length);
  364. //add 3 row breaks
  365. for (int j = 1; j <= 3; j++) {
  366. sheet.setRowBreak(j * 20);
  367. }
  368. }
  369. }
  370. }
  371. /**
  372. * Bug 44200: Sheet not cloneable when Note added to excel cell
  373. */
  374. @Test
  375. void bug44200() throws Exception {
  376. simpleTest("44200.xls", wb -> assertDoesNotThrow(() -> wb.cloneSheet(0)));
  377. }
  378. /**
  379. * Bug 41546: Constructing HSSFWorkbook is failed,
  380. * Unknown Ptg in Formula: 0x1a (26)
  381. */
  382. @Test
  383. void bug41546() throws Exception {
  384. simpleTest("41546.xls", wb -> assertEquals(1, wb.getNumberOfSheets()));
  385. }
  386. /**
  387. * Bug 42618: RecordFormatException reading a file containing
  388. * =CHOOSE(2,A2,A3,A4)
  389. */
  390. @Test
  391. void bug42618() throws Exception {
  392. try (HSSFWorkbook wb1 = openSampleWorkbook("SimpleWithChoose.xls");
  393. HSSFWorkbook wb2 = writeOutAndReadBack(wb1)){
  394. // Check we detect the string properly too
  395. HSSFSheet s = wb2.getSheetAt(0);
  396. // Textual value
  397. HSSFRow r1 = s.getRow(0);
  398. HSSFCell c1 = r1.getCell(1);
  399. assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString());
  400. // Formula Value
  401. HSSFRow r2 = s.getRow(1);
  402. HSSFCell c2 = r2.getCell(1);
  403. assertEquals(25, (int) c2.getNumericCellValue());
  404. try {
  405. assertEquals("CHOOSE(2,A2,A3,A4)", c2.getCellFormula());
  406. } catch (IllegalStateException e) {
  407. if (e.getMessage().startsWith("Too few arguments")
  408. && e.getMessage().indexOf("ConcatPtg") > 0) {
  409. fail("identified bug 44306");
  410. }
  411. }
  412. }
  413. }
  414. /**
  415. * Something up with the FileSharingRecord
  416. */
  417. @Test
  418. void bug43251() throws Exception {
  419. // Used to blow up with an IllegalArgumentException when creating a FileSharingRecord
  420. simpleTest("43251.xls", wb -> assertEquals(1, wb.getNumberOfSheets()));
  421. }
  422. /**
  423. * Crystal reports generates files with short
  424. * StyleRecords, which is against the spec
  425. */
  426. @Test
  427. void bug44471() throws Exception {
  428. // Used to blow up with an ArrayIndexOutOfBounds when creating a StyleRecord
  429. simpleTest("OddStyleRecord.xls", wb -> assertEquals(1, wb.getNumberOfSheets()));
  430. }
  431. /**
  432. * Files with "read only recommended" were giving
  433. * grief on the FileSharingRecord
  434. */
  435. @Test
  436. void bug44536() throws Exception {
  437. // Used to blow up with an IllegalArgumentException when creating a FileSharingRecord
  438. simpleTest("ReadOnlyRecommended.xls", wb -> {
  439. // Check read only advised
  440. assertEquals(3, wb.getNumberOfSheets());
  441. assertTrue(wb.isWriteProtected());
  442. });
  443. // But also check that another wb isn't
  444. simpleTest("SimpleWithChoose.xls", wb -> assertFalse(wb.isWriteProtected()));
  445. }
  446. /**
  447. * Some files were having problems with the DVRecord,
  448. * probably due to dropdowns
  449. */
  450. @Test
  451. void bug44593() throws Exception {
  452. // Used to blow up with an IllegalArgumentException when creating a DVRecord
  453. // Now won't, but no idea if this means we have rubbish in the DVRecord or not...
  454. simpleTest("44593.xls", wb -> assertEquals(2, wb.getNumberOfSheets()));
  455. }
  456. /**
  457. * Used to give problems due to trying to read a zero
  458. * length string, but that's now properly handled
  459. */
  460. @Test
  461. void bug44643() throws Exception {
  462. // Used to blow up with an IllegalArgumentException
  463. simpleTest("44643.xls", wb -> assertEquals(1, wb.getNumberOfSheets()));
  464. }
  465. /**
  466. * User reported the wrong number of rows from the
  467. * iterator, but we can't replicate that
  468. */
  469. @Test
  470. void bug44693() throws Exception {
  471. try (HSSFWorkbook wb = openSampleWorkbook("44693.xls")) {
  472. HSSFSheet s = wb.getSheetAt(0);
  473. // Rows are 1 to 713
  474. assertEquals(0, s.getFirstRowNum());
  475. assertEquals(712, s.getLastRowNum());
  476. assertEquals(713, s.getPhysicalNumberOfRows());
  477. // Now check the iterator
  478. int rowsSeen = 0;
  479. for (Iterator<Row> i = s.rowIterator(); i.hasNext(); ) {
  480. Row r = i.next();
  481. assertNotNull(r);
  482. rowsSeen++;
  483. }
  484. assertEquals(713, rowsSeen);
  485. }
  486. }
  487. /**
  488. * Problems with extracting check boxes from
  489. * HSSFObjectData
  490. */
  491. @Test
  492. void bug44840() throws Exception {
  493. try (HSSFWorkbook wb = openSampleWorkbook("WithCheckBoxes.xls")) {
  494. // Take a look at the embedded objects
  495. List<HSSFObjectData> objects = wb.getAllEmbeddedObjects();
  496. assertEquals(1, objects.size());
  497. HSSFObjectData obj = objects.get(0);
  498. assertNotNull(obj);
  499. // Peek inside the underlying record
  500. EmbeddedObjectRefSubRecord rec = obj.findObjectRecord();
  501. assertNotNull(rec);
  502. // assertEquals(32, rec.field_1_stream_id_offset);
  503. assertEquals(0, rec.getStreamId().intValue()); // WRONG!
  504. assertEquals("Forms.CheckBox.1", rec.getOLEClassName());
  505. assertEquals(12, rec.getObjectData().length);
  506. // Doesn't have a directory
  507. assertFalse(obj.hasDirectoryEntry());
  508. assertNotNull(obj.getObjectData());
  509. assertEquals(12, obj.getObjectData().length);
  510. assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName());
  511. assertThrows(IllegalArgumentException.class, obj::getDirectory);
  512. }
  513. }
  514. /**
  515. * Test that we can delete sheets without
  516. * breaking the build in named ranges
  517. * used for printing stuff.
  518. */
  519. @Test
  520. void bug30978() throws Exception {
  521. try (HSSFWorkbook wb1 = openSampleWorkbook("30978-alt.xls")) {
  522. assertEquals(1, wb1.getNumberOfNames());
  523. assertEquals(3, wb1.getNumberOfSheets());
  524. // Check all names fit within range, and use
  525. // DeletedArea3DPtg
  526. InternalWorkbook w = wb1.getWorkbook();
  527. assertNames(wb1, w);
  528. // Delete the 2nd sheet
  529. wb1.removeSheetAt(1);
  530. // Re-check
  531. assertEquals(1, wb1.getNumberOfNames());
  532. assertEquals(2, wb1.getNumberOfSheets());
  533. assertNames(wb1, w);
  534. // Save and re-load
  535. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  536. w = wb2.getWorkbook();
  537. assertEquals(1, wb2.getNumberOfNames());
  538. assertEquals(2, wb2.getNumberOfSheets());
  539. assertNames(wb2, w);
  540. }
  541. }
  542. }
  543. private void assertNames(HSSFWorkbook wb1, InternalWorkbook w) {
  544. for (int i = 0; i < w.getNumNames(); i++) {
  545. NameRecord r = w.getNameRecord(i);
  546. assertTrue(r.getSheetNumber() <= wb1.getNumberOfSheets());
  547. Ptg[] nd = r.getNameDefinition();
  548. assertEquals(1, nd.length);
  549. assertTrue(nd[0] instanceof DeletedArea3DPtg);
  550. }
  551. }
  552. /**
  553. * Test that fonts get added properly
  554. */
  555. @Test
  556. void bug45338() throws IOException {
  557. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  558. assertEquals(4, wb.getNumberOfFonts());
  559. HSSFSheet s = wb.createSheet();
  560. s.createRow(0);
  561. s.createRow(1);
  562. s.getRow(0).createCell(0);
  563. s.getRow(1).createCell(0);
  564. assertEquals(4, wb.getNumberOfFonts());
  565. HSSFFont f1 = wb.getFontAt(0);
  566. assertFalse(f1.getBold());
  567. // Check that asking for the same font multiple times gives you the same thing.
  568. // Otherwise, our tests wouldn't work!
  569. assertSame(wb.getFontAt(0), wb.getFontAt(0));
  570. assertEquals(wb.getFontAt(0), wb.getFontAt(0));
  571. assertEquals(wb.getFontAt(2), wb.getFontAt(2));
  572. assertNotSame(wb.getFontAt(0), wb.getFontAt(2));
  573. // Look for a new font we have yet to add
  574. assertNull(wb.findFont(false, (short) 123, (short) 22, "Thingy", false, true, (short) 2, (byte) 2));
  575. HSSFFont nf = wb.createFont();
  576. assertEquals(5, wb.getNumberOfFonts());
  577. assertEquals(5, nf.getIndex());
  578. assertEquals(nf, wb.getFontAt(5));
  579. nf.setBold(false);
  580. nf.setColor((short) 123);
  581. nf.setFontHeight((short) 22);
  582. nf.setFontName("Thingy");
  583. nf.setItalic(false);
  584. nf.setStrikeout(true);
  585. nf.setTypeOffset((short) 2);
  586. nf.setUnderline((byte) 2);
  587. assertEquals(5, wb.getNumberOfFonts());
  588. assertEquals(nf, wb.getFontAt(5));
  589. // Find it now
  590. assertNotNull(wb.findFont(false, (short) 123, (short) 22, "Thingy", false, true, (short) 2, (byte) 2));
  591. HSSFFont font = wb.findFont(false, (short) 123, (short) 22, "Thingy", false, true, (short) 2, (byte) 2);
  592. assertNotNull(font);
  593. assertEquals(5, font.getIndex());
  594. assertEquals(nf, wb.findFont(false, (short) 123, (short) 22, "Thingy", false, true, (short) 2, (byte) 2));
  595. }
  596. }
  597. /**
  598. * From the mailing list - ensure we can handle a formula
  599. * containing a zip code, eg ="70164"
  600. */
  601. @Test
  602. void bugZipCodeFormulas() throws Exception {
  603. try (HSSFWorkbook wb1 = new HSSFWorkbook()) {
  604. HSSFSheet s = wb1.createSheet();
  605. s.createRow(0);
  606. HSSFCell c1 = s.getRow(0).createCell(0);
  607. HSSFCell c2 = s.getRow(0).createCell(1);
  608. HSSFCell c3 = s.getRow(0).createCell(2);
  609. // As number and string
  610. c1.setCellFormula("70164");
  611. c2.setCellFormula("\"70164\"");
  612. c3.setCellFormula("\"90210\"");
  613. // Check the formulas
  614. assertEquals("70164", c1.getCellFormula());
  615. assertEquals("\"70164\"", c2.getCellFormula());
  616. // And check the values - blank
  617. confirmCachedValue(0.0, c1);
  618. confirmCachedValue(0.0, c2);
  619. confirmCachedValue(0.0, c3);
  620. // Try changing the cached value on one of the string
  621. // formula cells, so we can see it updates properly
  622. c3.setCellValue(new HSSFRichTextString("test"));
  623. confirmCachedValue("test", c3);
  624. IllegalStateException e = assertThrows(IllegalStateException.class, c3::getNumericCellValue);
  625. assertEquals("Cannot get a NUMERIC value from a STRING formula cell", e.getMessage());
  626. // Now evaluate, they should all be changed
  627. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb1);
  628. eval.evaluateFormulaCell(c1);
  629. eval.evaluateFormulaCell(c2);
  630. eval.evaluateFormulaCell(c3);
  631. // Check that the cells now contain
  632. // the correct values
  633. confirmCachedValue(70164.0, c1);
  634. confirmCachedValue("70164", c2);
  635. confirmCachedValue("90210", c3);
  636. // Write and read
  637. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  638. HSSFSheet ns = wb2.getSheetAt(0);
  639. HSSFCell nc1 = ns.getRow(0).getCell(0);
  640. HSSFCell nc2 = ns.getRow(0).getCell(1);
  641. HSSFCell nc3 = ns.getRow(0).getCell(2);
  642. // Re-check
  643. confirmCachedValue(70164.0, nc1);
  644. confirmCachedValue("70164", nc2);
  645. confirmCachedValue("90210", nc3);
  646. int i = 0;
  647. for (Iterator<CellValueRecordInterface> it = ns.getSheet().getCellValueIterator(); it.hasNext(); i++) {
  648. CellValueRecordInterface cvr = it.next();
  649. if (cvr instanceof FormulaRecordAggregate) {
  650. FormulaRecordAggregate fr = (FormulaRecordAggregate) cvr;
  651. if (i == 0) {
  652. assertEquals(70164.0, fr.getFormulaRecord().getValue(), 0.0001);
  653. assertNull(fr.getStringRecord());
  654. } else if (i == 1) {
  655. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  656. assertNotNull(fr.getStringRecord());
  657. assertEquals("70164", fr.getStringRecord().getString());
  658. } else {
  659. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  660. assertNotNull(fr.getStringRecord());
  661. assertEquals("90210", fr.getStringRecord().getString());
  662. }
  663. }
  664. }
  665. assertEquals(3, i);
  666. }
  667. }
  668. }
  669. private static void confirmCachedValue(double expectedValue, HSSFCell cell) {
  670. assertEquals(CellType.FORMULA, cell.getCellType());
  671. assertEquals(CellType.NUMERIC, cell.getCachedFormulaResultType());
  672. assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
  673. }
  674. private static void confirmCachedValue(String expectedValue, HSSFCell cell) {
  675. assertEquals(CellType.FORMULA, cell.getCellType());
  676. assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
  677. assertEquals(expectedValue, cell.getRichStringCellValue().getString());
  678. }
  679. /**
  680. * Problem with "Vector Rows", eg a whole
  681. * column which is set to the result of
  682. * {=sin(B1:B9)}(9,1), so that each cell is
  683. * shown to have the contents
  684. * {=sin(B1:B9){9,1)[rownum][0]
  685. * In this sample file, the vector column
  686. * is C, and the data column is B.
  687. * <p>
  688. * Expected ExpPtg to be converted from Shared to Non-Shared...
  689. */
  690. @Disabled("For now, blows up with an exception from ExtPtg")
  691. @Test
  692. void test43623() throws Exception {
  693. try (HSSFWorkbook wb1 = openSampleWorkbook("43623.xls")) {
  694. assertEquals(1, wb1.getNumberOfSheets());
  695. HSSFSheet s1 = wb1.getSheetAt(0);
  696. HSSFCell c1 = s1.getRow(0).getCell(2);
  697. HSSFCell c2 = s1.getRow(1).getCell(2);
  698. HSSFCell c3 = s1.getRow(2).getCell(2);
  699. // These formula contents are a guess...
  700. assertEquals("{=sin(B1:B9){9,1)[0][0]", c1.getCellFormula());
  701. assertEquals("{=sin(B1:B9){9,1)[1][0]", c2.getCellFormula());
  702. assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula());
  703. // Save and re-open, ensure it still works
  704. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  705. HSSFSheet ns1 = wb2.getSheetAt(0);
  706. HSSFCell nc1 = ns1.getRow(0).getCell(2);
  707. HSSFCell nc2 = ns1.getRow(1).getCell(2);
  708. HSSFCell nc3 = ns1.getRow(2).getCell(2);
  709. assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula());
  710. assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula());
  711. assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula());
  712. }
  713. }
  714. }
  715. /**
  716. * People are all getting confused about the last
  717. * row and cell number
  718. */
  719. @Test
  720. void bug30635() throws IOException {
  721. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  722. HSSFSheet s = wb.createSheet();
  723. // No rows, first/last return -1
  724. assertEquals(-1, s.getFirstRowNum());
  725. assertEquals(-1, s.getLastRowNum());
  726. assertEquals(0, s.getPhysicalNumberOfRows());
  727. // One row, most things are 0, physical is 1
  728. s.createRow(0);
  729. assertEquals(0, s.getFirstRowNum());
  730. assertEquals(0, s.getLastRowNum());
  731. assertEquals(1, s.getPhysicalNumberOfRows());
  732. // And another, things change
  733. s.createRow(4);
  734. assertEquals(0, s.getFirstRowNum());
  735. assertEquals(4, s.getLastRowNum());
  736. assertEquals(2, s.getPhysicalNumberOfRows());
  737. // Now start on cells
  738. HSSFRow r = s.getRow(0);
  739. assertEquals(-1, r.getFirstCellNum());
  740. assertEquals(-1, r.getLastCellNum());
  741. assertEquals(0, r.getPhysicalNumberOfCells());
  742. // Add a cell, things move off -1
  743. r.createCell(0);
  744. assertEquals(0, r.getFirstCellNum());
  745. assertEquals(1, r.getLastCellNum()); // last cell # + 1
  746. assertEquals(1, r.getPhysicalNumberOfCells());
  747. r.createCell(1);
  748. assertEquals(0, r.getFirstCellNum());
  749. assertEquals(2, r.getLastCellNum()); // last cell # + 1
  750. assertEquals(2, r.getPhysicalNumberOfCells());
  751. r.createCell(4);
  752. assertEquals(0, r.getFirstCellNum());
  753. assertEquals(5, r.getLastCellNum()); // last cell # + 1
  754. assertEquals(3, r.getPhysicalNumberOfCells());
  755. }
  756. }
  757. /**
  758. * Data Tables - ptg 0x2
  759. */
  760. @Test
  761. void bug44958() throws Exception {
  762. try (HSSFWorkbook wb = openSampleWorkbook("44958.xls")) {
  763. HSSFSheet s;
  764. HSSFRow r;
  765. HSSFCell c;
  766. // Check the contents of the formulas
  767. // E4 to G9 of sheet 4 make up the table
  768. s = wb.getSheet("OneVariable Table Completed");
  769. r = s.getRow(3);
  770. c = r.getCell(4);
  771. assertEquals(CellType.FORMULA, c.getCellType());
  772. // TODO - check the formula once tables and
  773. // arrays are properly supported
  774. // E4 to H9 of sheet 5 make up the table
  775. s = wb.getSheet("TwoVariable Table Example");
  776. r = s.getRow(3);
  777. c = r.getCell(4);
  778. assertEquals(CellType.FORMULA, c.getCellType());
  779. // TODO - check the formula once tables and
  780. // arrays are properly supported
  781. }
  782. }
  783. /**
  784. * 45322: HSSFSheet.autoSizeColumn fails when style.getDataFormat() returns -1
  785. */
  786. @Test
  787. void bug45322() throws Exception {
  788. try (HSSFWorkbook wb = openSampleWorkbook("44958.xls")) {
  789. HSSFSheet sh = wb.getSheetAt(0);
  790. for (short i = 0; i < 30; i++) {
  791. sh.autoSizeColumn(i);
  792. assertNotEquals(0, sh.getColumnWidth(i));
  793. }
  794. }
  795. }
  796. /**
  797. * We used to add too many UncalcRecords to sheets
  798. * with diagrams on. Don't any more
  799. */
  800. @Test
  801. void bug45414() throws Exception {
  802. try (HSSFWorkbook wb1 = openSampleWorkbook("WithThreeCharts.xls")) {
  803. wb1.getSheetAt(0).setForceFormulaRecalculation(true);
  804. wb1.getSheetAt(1).setForceFormulaRecalculation(false);
  805. wb1.getSheetAt(2).setForceFormulaRecalculation(true);
  806. // Write out and back in again
  807. // This used to break
  808. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  809. // Check now set as it should be
  810. assertTrue(wb2.getSheetAt(0).getForceFormulaRecalculation());
  811. assertFalse(wb2.getSheetAt(1).getForceFormulaRecalculation());
  812. assertTrue(wb2.getSheetAt(2).getForceFormulaRecalculation());
  813. }
  814. }
  815. }
  816. /**
  817. * Very hidden sheets not displaying as such
  818. */
  819. @Test
  820. void bug45761() throws Exception {
  821. try (HSSFWorkbook wb1 = openSampleWorkbook("45761.xls")) {
  822. assertEquals(3, wb1.getNumberOfSheets());
  823. assertFalse(wb1.isSheetHidden(0));
  824. assertFalse(wb1.isSheetVeryHidden(0));
  825. assertTrue(wb1.isSheetHidden(1));
  826. assertFalse(wb1.isSheetVeryHidden(1));
  827. assertFalse(wb1.isSheetHidden(2));
  828. assertTrue(wb1.isSheetVeryHidden(2));
  829. // Change sheet 0 to be very hidden, and re-load
  830. wb1.setSheetVisibility(0, SheetVisibility.VERY_HIDDEN);
  831. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  832. assertFalse(wb2.isSheetHidden(0));
  833. assertTrue(wb2.isSheetVeryHidden(0));
  834. assertTrue(wb2.isSheetHidden(1));
  835. assertFalse(wb2.isSheetVeryHidden(1));
  836. assertFalse(wb2.isSheetHidden(2));
  837. assertTrue(wb2.isSheetVeryHidden(2));
  838. }
  839. }
  840. }
  841. /**
  842. * The resolution for bug 45777 assumed that the maximum text length in a header / footer
  843. * record was 256 bytes. This assumption appears to be wrong. Since the fix for bug 47244,
  844. * POI now supports header / footer text lengths beyond 256 bytes.
  845. */
  846. @Test
  847. void bug45777() throws Exception {
  848. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  849. HSSFSheet s = wb.createSheet();
  850. char[] cc248 = new char[248];
  851. Arrays.fill(cc248, 'x');
  852. String s248 = new String(cc248);
  853. String s249 = s248 + "1";
  854. String s250 = s248 + "12";
  855. String s251 = s248 + "123";
  856. assertEquals(248, s248.length());
  857. assertEquals(249, s249.length());
  858. assertEquals(250, s250.length());
  859. assertEquals(251, s251.length());
  860. // Try on headers
  861. s.getHeader().setCenter(s248);
  862. assertEquals(254, s.getHeader().getRawText().length());
  863. writeOutAndReadBack(wb).close();
  864. s.getHeader().setCenter(s251);
  865. assertEquals(257, s.getHeader().getRawText().length());
  866. writeOutAndReadBack(wb).close();
  867. // header can be more than 256 bytes
  868. s.getHeader().setCenter(s250); // 256 bytes required
  869. s.getHeader().setCenter(s251); // 257 bytes required
  870. // Now try on footers
  871. s.getFooter().setCenter(s248);
  872. assertEquals(254, s.getFooter().getRawText().length());
  873. writeOutAndReadBack(wb).close();
  874. s.getFooter().setCenter(s251);
  875. assertEquals(257, s.getFooter().getRawText().length());
  876. writeOutAndReadBack(wb).close();
  877. // footer can be more than 256 bytes
  878. s.getFooter().setCenter(s250); // 256 bytes required
  879. s.getFooter().setCenter(s251); // 257 bytes required
  880. }
  881. }
  882. /**
  883. * Charts with long titles
  884. */
  885. @Test
  886. void bug45784() throws Exception {
  887. simpleTest("45784.xls", wb -> assertEquals(1, wb.getNumberOfSheets()));
  888. }
  889. /**
  890. * Cell background colours
  891. */
  892. @Test
  893. void bug45492() throws Exception {
  894. try (HSSFWorkbook wb = openSampleWorkbook("45492.xls")) {
  895. HSSFSheet s = wb.getSheetAt(0);
  896. HSSFRow r = s.getRow(0);
  897. HSSFPalette p = wb.getCustomPalette();
  898. HSSFCell auto = r.getCell(0);
  899. HSSFCell grey = r.getCell(1);
  900. HSSFCell red = r.getCell(2);
  901. HSSFCell blue = r.getCell(3);
  902. HSSFCell green = r.getCell(4);
  903. assertEquals(64, auto.getCellStyle().getFillForegroundColor());
  904. assertEquals(64, auto.getCellStyle().getFillBackgroundColor());
  905. assertEquals("0:0:0", p.getColor(64).getHexString());
  906. assertEquals(22, grey.getCellStyle().getFillForegroundColor());
  907. assertEquals(64, grey.getCellStyle().getFillBackgroundColor());
  908. assertEquals("C0C0:C0C0:C0C0", p.getColor(22).getHexString());
  909. assertEquals(10, red.getCellStyle().getFillForegroundColor());
  910. assertEquals(64, red.getCellStyle().getFillBackgroundColor());
  911. assertEquals("FFFF:0:0", p.getColor(10).getHexString());
  912. assertEquals(12, blue.getCellStyle().getFillForegroundColor());
  913. assertEquals(64, blue.getCellStyle().getFillBackgroundColor());
  914. assertEquals("0:0:FFFF", p.getColor(12).getHexString());
  915. assertEquals(11, green.getCellStyle().getFillForegroundColor());
  916. assertEquals(64, green.getCellStyle().getFillBackgroundColor());
  917. assertEquals("0:FFFF:0", p.getColor(11).getHexString());
  918. }
  919. }
  920. /**
  921. * ContinueRecord after EOF
  922. */
  923. @Test
  924. void bug46137() throws Exception {
  925. simpleTest("46137.xls", wb -> assertEquals(7, wb.getNumberOfSheets()));
  926. }
  927. /**
  928. * Odd POIFS blocks issue:
  929. * block[ 44 ] already removed from org.apache.poi.poifs.storage.BlockListImpl.remove
  930. */
  931. @Test
  932. void bug45290() throws Exception {
  933. simpleTest("45290.xls", wb -> assertEquals(1, wb.getNumberOfSheets()));
  934. }
  935. /**
  936. * In POI-2.5 user reported exception when parsing a name with a custom VBA function:
  937. * =MY_VBA_FUNCTION("lskdjflsk")
  938. */
  939. @Test
  940. void bug30070() throws Exception {
  941. //contains custom VBA function 'Commission'
  942. try (HSSFWorkbook wb = openSampleWorkbook("30070.xls")) {
  943. HSSFSheet sh = wb.getSheetAt(0);
  944. HSSFCell cell = sh.getRow(0).getCell(1);
  945. //B1 uses VBA in the formula
  946. assertEquals("Commission(A1)", cell.getCellFormula());
  947. //name sales_1 refers to Commission(Sheet0!$A$1)
  948. int idx = wb.getNameIndex("sales_1");
  949. assertTrue(idx != -1);
  950. HSSFName name = wb.getNameAt(idx);
  951. assertEquals("Commission(Sheet0!$A$1)", name.getRefersToFormula());
  952. }
  953. }
  954. /**
  955. * The link formulas which is referring to other books cannot be taken (the bug existed prior to POI-3.2)
  956. * Expected:
  957. * <p>
  958. * [link_sub.xls]Sheet1!$A$1
  959. * [link_sub.xls]Sheet1!$A$2
  960. * [link_sub.xls]Sheet1!$A$3
  961. * <p>
  962. * POI-3.1 output:
  963. * <p>
  964. * Sheet1!$A$1
  965. * Sheet1!$A$2
  966. * Sheet1!$A$3
  967. */
  968. @Test
  969. void bug27364() throws Exception {
  970. try (HSSFWorkbook wb = openSampleWorkbook("27364.xls")) {
  971. HSSFSheet sheet = wb.getSheetAt(0);
  972. assertEquals("[link_sub.xls]Sheet1!$A$1", sheet.getRow(0).getCell(0).getCellFormula());
  973. assertEquals("[link_sub.xls]Sheet1!$A$2", sheet.getRow(1).getCell(0).getCellFormula());
  974. assertEquals("[link_sub.xls]Sheet1!$A$3", sheet.getRow(2).getCell(0).getCellFormula());
  975. }
  976. }
  977. /**
  978. * Similar to bug#27364:
  979. * HSSFCell.getCellFormula() fails with references to external workbooks
  980. */
  981. @Test
  982. void bug31661() throws Exception {
  983. try (HSSFWorkbook wb = openSampleWorkbook("31661.xls")) {
  984. HSSFSheet sheet = wb.getSheetAt(0);
  985. HSSFCell cell = sheet.getRow(11).getCell(10); //K11
  986. assertEquals("+'[GM Budget.xls]8085.4450'!$B$2", cell.getCellFormula());
  987. }
  988. }
  989. /**
  990. * Incorrect handling of non-ISO 8859-1 characters in Windows ANSII Code Page 1252
  991. */
  992. @Test
  993. void bug27394() throws Exception {
  994. try (HSSFWorkbook wb = openSampleWorkbook("27394.xls")) {
  995. assertEquals("\u0161\u017E", wb.getSheetName(0));
  996. assertEquals("\u0161\u017E\u010D\u0148\u0159", wb.getSheetName(1));
  997. HSSFSheet sheet = wb.getSheetAt(0);
  998. assertEquals("\u0161\u017E", sheet.getRow(0).getCell(0).getStringCellValue());
  999. assertEquals("\u0161\u017E\u010D\u0148\u0159", sheet.getRow(1).getCell(0).getStringCellValue());
  1000. }
  1001. }
  1002. /**
  1003. * Multiple calls of HSSFWorkbook.write result in corrupted xls
  1004. */
  1005. @Test
  1006. void bug32191() throws IOException {
  1007. try (HSSFWorkbook wb = openSampleWorkbook("27394.xls");
  1008. UnsynchronizedByteArrayOutputStream out1 = new UnsynchronizedByteArrayOutputStream();
  1009. UnsynchronizedByteArrayOutputStream out2 = new UnsynchronizedByteArrayOutputStream();
  1010. UnsynchronizedByteArrayOutputStream out3 = new UnsynchronizedByteArrayOutputStream()) {
  1011. wb.write(out1);
  1012. wb.write(out2);
  1013. wb.write(out3);
  1014. assertArrayEquals(out1.toByteArray(), out2.toByteArray());
  1015. assertArrayEquals(out2.toByteArray(), out3.toByteArray());
  1016. }
  1017. }
  1018. /**
  1019. * java.lang.NegativeArraySizeException reading long
  1020. * non-unicode data for a name record
  1021. */
  1022. @Test
  1023. void bug47034() throws Exception {
  1024. simpleTest("47034.xls", wb -> {
  1025. assertEquals(893, wb.getNumberOfNames());
  1026. assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300));
  1027. });
  1028. }
  1029. /**
  1030. * HSSFRichTextString.length() returns negative for really long strings.
  1031. * The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer than 32,767 characters
  1032. */
  1033. @Test
  1034. void bug46368() throws Exception {
  1035. try (HSSFWorkbook wb = openSampleWorkbook("46368.xls")) {
  1036. HSSFSheet s = wb.getSheetAt(0);
  1037. HSSFCell cell1 = s.getRow(0).getCell(0);
  1038. assertEquals(32770, cell1.getStringCellValue().length());
  1039. HSSFCell cell2 = s.getRow(2).getCell(0);
  1040. assertEquals(32766, cell2.getStringCellValue().length());
  1041. }
  1042. }
  1043. /**
  1044. * Short records on certain sheets with charts in them
  1045. */
  1046. @Test
  1047. void bug48180() throws Exception {
  1048. try (HSSFWorkbook wb = openSampleWorkbook("48180.xls")) {
  1049. HSSFSheet s = wb.getSheetAt(0);
  1050. HSSFCell cell1 = s.getRow(0).getCell(0);
  1051. assertEquals("test ", cell1.getStringCellValue());
  1052. HSSFCell cell2 = s.getRow(0).getCell(1);
  1053. assertEquals(1.0, cell2.getNumericCellValue(), 0.0);
  1054. }
  1055. }
  1056. /**
  1057. * Round trip a file with an unusual UnicodeString/ExtRst record parts
  1058. */
  1059. @Test
  1060. void bug47847() throws Exception {
  1061. try (HSSFWorkbook wb1 = openSampleWorkbook("47847.xls")) {
  1062. assertEquals(3, wb1.getNumberOfSheets());
  1063. // Find the SST record
  1064. UnicodeString withExt = wb1.getWorkbook().getSSTString(0);
  1065. UnicodeString withoutExt = wb1.getWorkbook().getSSTString(31);
  1066. assertEquals("O:Alloc:Qty", withExt.getString());
  1067. assertEquals(0x0004, (withExt.getOptionFlags() & 0x0004));
  1068. assertEquals("RT", withoutExt.getString());
  1069. assertEquals(0x0000, (withoutExt.getOptionFlags() & 0x0004));
  1070. // Something about continues...
  1071. // Write out and re-read
  1072. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  1073. assertEquals(3, wb2.getNumberOfSheets());
  1074. // Check it's the same now
  1075. withExt = wb2.getWorkbook().getSSTString(0);
  1076. withoutExt = wb2.getWorkbook().getSSTString(31);
  1077. assertEquals("O:Alloc:Qty", withExt.getString());
  1078. assertEquals(0x0004, (withExt.getOptionFlags() & 0x0004));
  1079. assertEquals("RT", withoutExt.getString());
  1080. assertEquals(0x0000, (withoutExt.getOptionFlags() & 0x0004));
  1081. }
  1082. }
  1083. }
  1084. /**
  1085. * Problem with cloning a sheet with a chart
  1086. * contained in it.
  1087. */
  1088. @Test
  1089. void bug49096() throws Exception {
  1090. try (HSSFWorkbook wb1 = openSampleWorkbook("49096.xls")) {
  1091. assertEquals(1, wb1.getNumberOfSheets());
  1092. assertNotNull(wb1.getSheetAt(0));
  1093. wb1.cloneSheet(0);
  1094. assertEquals(2, wb1.getNumberOfSheets());
  1095. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  1096. assertEquals(2, wb2.getNumberOfSheets());
  1097. }
  1098. }
  1099. }
  1100. /**
  1101. * Newly created sheets need to get a
  1102. * proper TabID, otherwise print setup
  1103. * gets confused on them.
  1104. * Also ensure that print setup refs are
  1105. * by reference not value
  1106. */
  1107. @Test
  1108. void bug46664() throws Exception {
  1109. try (HSSFWorkbook wb1 = new HSSFWorkbook()) {
  1110. HSSFSheet sheet = wb1.createSheet("new_sheet");
  1111. HSSFRow row = sheet.createRow((short) 0);
  1112. row.createCell(0).setCellValue(new HSSFRichTextString("Column A"));
  1113. row.createCell(1).setCellValue(new HSSFRichTextString("Column B"));
  1114. row.createCell(2).setCellValue(new HSSFRichTextString("Column C"));
  1115. row.createCell(3).setCellValue(new HSSFRichTextString("Column D"));
  1116. row.createCell(4).setCellValue(new HSSFRichTextString("Column E"));
  1117. row.createCell(5).setCellValue(new HSSFRichTextString("Column F"));
  1118. //set print area from column a to column c (on first row)
  1119. wb1.setPrintArea(
  1120. 0, //sheet index
  1121. 0, //start column
  1122. 2, //end column
  1123. 0, //start row
  1124. 0 //end row
  1125. );
  1126. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  1127. // Ensure the tab index
  1128. TabIdRecord tr = null;
  1129. for (org.apache.poi.hssf.record.Record r : wb2.getWorkbook().getRecords()) {
  1130. if (r instanceof TabIdRecord) {
  1131. tr = (TabIdRecord) r;
  1132. }
  1133. }
  1134. assertNotNull(tr);
  1135. assertEquals(1, tr.getTabIdSize());
  1136. assertEquals(0, tr.getTabIdAt(0));
  1137. // Ensure the print setup
  1138. assertEquals("new_sheet!$A$1:$C$1", wb2.getPrintArea(0));
  1139. HSSFName printArea = wb2.getName("Print_Area");
  1140. assertNotNull(printArea);
  1141. assertEquals("new_sheet!$A$1:$C$1", printArea.getRefersToFormula());
  1142. // Needs reference not value
  1143. NameRecord nr = wb2.getWorkbook().getNameRecord(
  1144. wb2.getNameIndex("Print_Area")
  1145. );
  1146. assertEquals("Print_Area", nr.getNameText());
  1147. assertEquals(1, nr.getNameDefinition().length);
  1148. assertEquals(
  1149. "new_sheet!$A$1:$C$1",
  1150. ((Area3DPtg) nr.getNameDefinition()[0]).toFormulaString(HSSFEvaluationWorkbook.create(wb2))
  1151. );
  1152. assertEquals('R', nr.getNameDefinition()[0].getRVAType());
  1153. }
  1154. }
  1155. }
  1156. /**
  1157. * Problems with formula references to
  1158. * sheets via URLs
  1159. */
  1160. @Test
  1161. void bug45970() throws Exception {
  1162. try (HSSFWorkbook wb1 = openSampleWorkbook("FormulaRefs.xls")) {
  1163. assertEquals(3, wb1.getNumberOfSheets());
  1164. HSSFSheet s = wb1.getSheetAt(0);
  1165. HSSFRow row;
  1166. row = s.getRow(0);
  1167. assertEquals(CellType.NUMERIC, row.getCell(1).getCellType());
  1168. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1169. row = s.getRow(1);
  1170. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1171. assertEquals("B1", row.getCell(1).getCellFormula());
  1172. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1173. row = s.getRow(2);
  1174. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1175. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1176. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1177. row = s.getRow(3);
  1178. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1179. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1180. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1181. row = s.getRow(4);
  1182. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1183. assertEquals("'[$http://gagravarr.org/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1184. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1185. // Link our new workbook
  1186. Workbook externalWb1 = new HSSFWorkbook();
  1187. externalWb1.createSheet("Sheet1");
  1188. assertEquals(4, wb1.linkExternalWorkbook("$http://gagravarr.org/FormulaRefs2.xls", externalWb1));
  1189. // Change 4
  1190. row.getCell(1).setCellFormula("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2");
  1191. row.getCell(1).setCellValue(123.0);
  1192. // Link our new workbook
  1193. Workbook externalWb2 = new HSSFWorkbook();
  1194. externalWb2.createSheet("Sheet1");
  1195. assertEquals(5, wb1.linkExternalWorkbook("$http://example.com/FormulaRefs.xls", externalWb2));
  1196. // Add 5
  1197. row = s.createRow(5);
  1198. row.createCell(1, CellType.FORMULA);
  1199. row.getCell(1).setCellFormula("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1");
  1200. row.getCell(1).setCellValue(234.0);
  1201. // Re-test
  1202. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  1203. s = wb2.getSheetAt(0);
  1204. row = s.getRow(0);
  1205. assertEquals(CellType.NUMERIC, row.getCell(1).getCellType());
  1206. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1207. row = s.getRow(1);
  1208. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1209. assertEquals("B1", row.getCell(1).getCellFormula());
  1210. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1211. row = s.getRow(2);
  1212. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1213. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1214. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1215. row = s.getRow(3);
  1216. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1217. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1218. assertEquals(112.0, row.getCell(1).getNumericCellValue(), 0);
  1219. row = s.getRow(4);
  1220. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1221. assertEquals("'[$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula());
  1222. assertEquals(123.0, row.getCell(1).getNumericCellValue(), 0);
  1223. row = s.getRow(5);
  1224. assertEquals(CellType.FORMULA, row.getCell(1).getCellType());
  1225. assertEquals("'[$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1226. assertEquals(234.0, row.getCell(1).getNumericCellValue(), 0);
  1227. }
  1228. }
  1229. }
  1230. /**
  1231. * Test for a file with NameRecord with NameCommentRecord comments
  1232. */
  1233. @Test
  1234. void bug49185() throws Exception {
  1235. try (HSSFWorkbook wb1 = openSampleWorkbook("49185.xls")) {
  1236. Name name = wb1.getName("foobarName");
  1237. assertNotNull(name);
  1238. assertEquals("This is a comment", name.getComment());
  1239. // Rename the name, comment comes with it
  1240. name.setNameName("ChangedName");
  1241. assertEquals("This is a comment", name.getComment());
  1242. // Save and re-check
  1243. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  1244. name = wb2.getName("ChangedName");
  1245. assertNotNull(name);
  1246. assertEquals("This is a comment", name.getComment());
  1247. // Now try to change it
  1248. name.setComment("Changed Comment");
  1249. assertEquals("Changed Comment", name.getComment());
  1250. // Save and re-check
  1251. try (HSSFWorkbook wb3 = writeOutAndReadBack(wb2)) {
  1252. name = wb3.getName("ChangedName");
  1253. assertNotNull(name);
  1254. assertEquals("Changed Comment", name.getComment());
  1255. }
  1256. }
  1257. }
  1258. }
  1259. /** Vertically aligned text */
  1260. @Test
  1261. void bug49524() throws Exception {
  1262. try (HSSFWorkbook wb1 = openSampleWorkbook("49524.xls")) {
  1263. Sheet s = wb1.getSheetAt(0);
  1264. Row r = s.getRow(0);
  1265. Cell rotated = r.getCell(0);
  1266. Cell normal = r.getCell(1);
  1267. // Check the current ones
  1268. assertEquals(0, normal.getCellStyle().getRotation());
  1269. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1270. // Add a new style, also rotated
  1271. CellStyle cs = wb1.createCellStyle();
  1272. cs.setRotation((short) 0xff);
  1273. Cell nc = r.createCell(2);
  1274. nc.setCellValue("New Rotated Text");
  1275. nc.setCellStyle(cs);
  1276. assertEquals(0xff, nc.getCellStyle().getRotation());
  1277. // Write out and read back
  1278. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  1279. // Re-check
  1280. s = wb2.getSheetAt(0);
  1281. r = s.getRow(0);
  1282. rotated = r.getCell(0);
  1283. normal = r.getCell(1);
  1284. nc = r.getCell(2);
  1285. assertEquals(0, normal.getCellStyle().getRotation());
  1286. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1287. assertEquals(0xff, nc.getCellStyle().getRotation());
  1288. }
  1289. }
  1290. }
  1291. /**
  1292. * Setting the user style name on custom styles
  1293. */
  1294. @Test
  1295. void bug49689() throws Exception {
  1296. try (HSSFWorkbook wb1 = new HSSFWorkbook()) {
  1297. HSSFSheet s = wb1.createSheet("Test");
  1298. HSSFRow r = s.createRow(0);
  1299. HSSFCell c = r.createCell(0);
  1300. HSSFCellStyle cs1 = wb1.createCellStyle();
  1301. HSSFCellStyle cs2 = wb1.createCellStyle();
  1302. HSSFCellStyle cs3 = wb1.createCellStyle();
  1303. assertEquals(21, cs1.getIndex());
  1304. cs1.setUserStyleName("Testing");
  1305. assertEquals(22, cs2.getIndex());
  1306. cs2.setUserStyleName("Testing 2");
  1307. assertEquals(23, cs3.getIndex());
  1308. cs3.setUserStyleName("Testing 3");
  1309. // Set one
  1310. c.setCellStyle(cs1);
  1311. // Write out and read back
  1312. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  1313. // Re-check
  1314. assertEquals("Testing", wb2.getCellStyleAt((short) 21).getUserStyleName());
  1315. assertEquals("Testing 2", wb2.getCellStyleAt((short) 22).getUserStyleName());
  1316. assertEquals("Testing 3", wb2.getCellStyleAt((short) 23).getUserStyleName());
  1317. }
  1318. }
  1319. }
  1320. @Test
  1321. void bug49751() throws Exception {
  1322. Set<String> exp = new HashSet<>(Arrays.asList(
  1323. "20% - Accent1", "20% - Accent2", "20% - Accent3", "20% - Accent4", "20% - Accent5",
  1324. "20% - Accent6", "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4",
  1325. "40% - Accent5", "40% - Accent6", "60% - Accent1", "60% - Accent2", "60% - Accent3",
  1326. "60% - Accent4", "60% - Accent5", "60% - Accent6", "Accent1", "Accent2", "Accent3",
  1327. "Accent4", "Accent5", "Accent6", "Bad", "Calculation", "Check Cell", "Explanatory Text",
  1328. "Good", "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Input", "Linked Cell",
  1329. "Neutral", "Note", "Output", "Title", "Total", "Warning Text"));
  1330. try (HSSFWorkbook wb = openSampleWorkbook("49751.xls")) {
  1331. Set<String> act = IntStream
  1332. .range(0, wb.getNumCellStyles())
  1333. .mapToObj(wb::getCellStyleAt)
  1334. .map(HSSFCellStyle::getUserStyleName)
  1335. .filter(Objects::nonNull)
  1336. .collect(Collectors.toSet());
  1337. assertEquals(exp, act);
  1338. }
  1339. }
  1340. /**
  1341. * Regression with the PageSettingsBlock
  1342. */
  1343. @Test
  1344. void bug49931() throws Exception {
  1345. simpleTest("49931.xls", wb -> {
  1346. assertEquals(1, wb.getNumberOfSheets());
  1347. assertEquals("Foo", wb.getSheetAt(0).getRow(0).getCell(0).getRichStringCellValue().toString());
  1348. });
  1349. }
  1350. /**
  1351. * Missing left/right/centre options on a footer
  1352. */
  1353. @Test
  1354. void bug48325() throws Exception {
  1355. try (HSSFWorkbook wb = openSampleWorkbook("48325.xls")) {
  1356. HSSFSheet sh = wb.getSheetAt(0);
  1357. HSSFFooter f = sh.getFooter();
  1358. // Will show as the center, as that is what excel does
  1359. // with an invalid footer lacking left/right/center details
  1360. assertEquals("", f.getLeft(), "Left text should be empty");
  1361. assertEquals("", f.getRight(), "Right text should be empty");
  1362. assertEquals("BlahBlah blah blah ", f.getCenter(),
  1363. "Center text should contain the illegal value");
  1364. }
  1365. }
  1366. /**
  1367. * Last row number when shifting rows
  1368. */
  1369. @Test
  1370. void bug50416LastRowNumber() throws IOException {
  1371. // Create the workbook with 1 sheet which contains 3 rows
  1372. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  1373. Sheet sheet = wb.createSheet("Bug50416");
  1374. Row row1 = sheet.createRow(0);
  1375. Cell cellA_1 = row1.createCell(0, CellType.STRING);
  1376. cellA_1.setCellValue("Cell A,1");
  1377. Row row2 = sheet.createRow(1);
  1378. Cell cellA_2 = row2.createCell(0, CellType.STRING);
  1379. cellA_2.setCellValue("Cell A,2");
  1380. Row row3 = sheet.createRow(2);
  1381. Cell cellA_3 = row3.createCell(0, CellType.STRING);
  1382. cellA_3.setCellValue("Cell A,3");
  1383. // Test the last Row number it currently correct
  1384. assertEquals(2, sheet.getLastRowNum());
  1385. // Shift the first row to the end
  1386. sheet.shiftRows(0, 0, 3);
  1387. assertEquals(3, sheet.getLastRowNum());
  1388. assertEquals(-1, sheet.getRow(0).getLastCellNum());
  1389. assertEquals("Cell A,2", sheet.getRow(1).getCell(0).getStringCellValue());
  1390. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1391. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1392. // Shift the 2nd row up to the first one
  1393. sheet.shiftRows(1, 1, -1);
  1394. assertEquals(3, sheet.getLastRowNum());
  1395. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1396. assertEquals(-1, sheet.getRow(1).getLastCellNum());
  1397. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1398. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1399. // Shift the 4th row up into the gap in the 3rd row
  1400. sheet.shiftRows(3, 3, -2);
  1401. assertEquals(2, sheet.getLastRowNum());
  1402. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1403. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1404. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1405. assertEquals(-1, sheet.getRow(3).getLastCellNum());
  1406. // Now zap the empty 4th row - won't do anything
  1407. sheet.removeRow(sheet.getRow(3));
  1408. // Test again the last row number which should be 2
  1409. assertEquals(2, sheet.getLastRowNum());
  1410. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1411. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1412. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1413. }
  1414. }
  1415. /**
  1416. * If you send a file between Excel and OpenOffice enough, something
  1417. * will turn the "General" format into "GENERAL"
  1418. */
  1419. @Test
  1420. void bug50756() throws Exception {
  1421. try (HSSFWorkbook wb = openSampleWorkbook("50756.xls")) {
  1422. HSSFSheet s = wb.getSheetAt(0);
  1423. HSSFRow r17 = s.getRow(16);
  1424. HSSFRow r18 = s.getRow(17);
  1425. HSSFDataFormatter df = new HSSFDataFormatter();
  1426. assertEquals(10.0, r17.getCell(1).getNumericCellValue(), 0);
  1427. assertEquals(20.0, r17.getCell(2).getNumericCellValue(), 0);
  1428. assertEquals(20.0, r17.getCell(3).getNumericCellValue(), 0);
  1429. assertEquals("GENERAL", r17.getCell(1).getCellStyle().getDataFormatString());
  1430. assertEquals("GENERAL", r17.getCell(2).getCellStyle().getDataFormatString());
  1431. assertEquals("GENERAL", r17.getCell(3).getCellStyle().getDataFormatString());
  1432. assertEquals("10", df.formatCellValue(r17.getCell(1)));
  1433. assertEquals("20", df.formatCellValue(r17.getCell(2)));
  1434. assertEquals("20", df.formatCellValue(r17.getCell(3)));
  1435. assertEquals(16.0, r18.getCell(1).getNumericCellValue(), 0);
  1436. assertEquals(35.0, r18.getCell(2).getNumericCellValue(), 0);
  1437. assertEquals(123.0, r18.getCell(3).getNumericCellValue(), 0);
  1438. assertEquals("GENERAL", r18.getCell(1).getCellStyle().getDataFormatString());
  1439. assertEquals("GENERAL", r18.getCell(2).getCellStyle().getDataFormatString());
  1440. assertEquals("GENERAL", r18.getCell(3).getCellStyle().getDataFormatString());
  1441. assertEquals("16", df.formatCellValue(r18.getCell(1)));
  1442. assertEquals("35", df.formatCellValue(r18.getCell(2)));
  1443. assertEquals("123", df.formatCellValue(r18.getCell(3)));
  1444. }
  1445. }
  1446. /**
  1447. * A protected sheet with comments, when written out by
  1448. * POI, ends up upsetting excel.
  1449. * TODO Identify the cause and add extra asserts for
  1450. * the bit excel cares about
  1451. */
  1452. @Test
  1453. void bug50833() throws Exception {
  1454. HSSFWorkbook wb1 = openSampleWorkbook("50833.xls");
  1455. HSSFSheet s = wb1.getSheetAt(0);
  1456. assertEquals("Sheet1", s.getSheetName());
  1457. assertFalse(s.getProtect());
  1458. HSSFCell c = s.getRow(0).getCell(0);
  1459. assertEquals("test cell value", c.getRichStringCellValue().getString());
  1460. HSSFComment cmt = c.getCellComment();
  1461. assertNotNull(cmt);
  1462. assertEquals("Robert Lawrence", cmt.getAuthor());
  1463. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1464. // Reload
  1465. HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
  1466. wb1.close();
  1467. s = wb2.getSheetAt(0);
  1468. c = s.getRow(0).getCell(0);
  1469. // Re-check the comment
  1470. cmt = c.getCellComment();
  1471. assertNotNull(cmt);
  1472. assertEquals("Robert Lawrence", cmt.getAuthor());
  1473. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1474. // TODO Identify what excel doesn't like, and check for that
  1475. wb2.close();
  1476. }
  1477. /**
  1478. * The spec says that ChartEndObjectRecord has 6 reserved
  1479. * bytes on the end, but we sometimes find files without...
  1480. */
  1481. @Test
  1482. void bug50939() throws Exception {
  1483. simpleTest("50939.xls", wb -> assertEquals(2, wb.getNumberOfSheets()));
  1484. }
  1485. @Test
  1486. void bug49219() throws Exception {
  1487. simpleTest("49219.xls", wb -> {
  1488. assertEquals(1, wb.getNumberOfSheets());
  1489. assertEquals("DGATE", wb.getSheetAt(0).getRow(1).getCell(0).getStringCellValue());
  1490. });
  1491. }
  1492. @Test
  1493. void bug48968() throws Exception {
  1494. TimeZone userTimeZone = LocaleUtil.getUserTimeZone();
  1495. LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
  1496. try {
  1497. HSSFWorkbook wb = openSampleWorkbook("48968.xls");
  1498. assertEquals(1, wb.getNumberOfSheets());
  1499. DataFormatter fmt = new DataFormatter();
  1500. // Check the dates
  1501. HSSFSheet s = wb.getSheetAt(0);
  1502. Cell cell_d20110325 = s.getRow(0).getCell(0);
  1503. Cell cell_d19000102 = s.getRow(11).getCell(0);
  1504. Cell cell_d19000100 = s.getRow(21).getCell(0);
  1505. assertEquals(s.getRow(0).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d20110325));
  1506. assertEquals(s.getRow(11).getCell(3).getStringCellValue(), fmt.formatCellValue(cell_d19000102));
  1507. // There is no such thing as 00/01/1900...
  1508. assertEquals("00/01/1900 06:14:24", s.getRow(21).getCell(3).getStringCellValue());
  1509. assertEquals("31/12/1899 06:14:24", fmt.formatCellValue(cell_d19000100));
  1510. // Check the cached values
  1511. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1512. assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue(), 0);
  1513. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1514. assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue(), 0);
  1515. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1516. assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue(), 0);
  1517. // Re-evaluate and check
  1518. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  1519. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1520. assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue(), 0);
  1521. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1522. assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue(), 0);
  1523. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1524. assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue(), 0);
  1525. // Push the time forward a bit and check
  1526. double date = s.getRow(0).getCell(0).getNumericCellValue();
  1527. s.getRow(0).getCell(0).setCellValue(date + 1.26);
  1528. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  1529. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1530. assertEquals(11.0 + 6.0, s.getRow(5).getCell(0).getNumericCellValue(), 0);
  1531. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1532. assertEquals(39.0 + 14.0 + 1, s.getRow(6).getCell(0).getNumericCellValue(), 0);
  1533. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1534. assertEquals(54.0 + 24.0 - 60, s.getRow(7).getCell(0).getNumericCellValue(), 0);
  1535. wb.close();
  1536. } finally {
  1537. LocaleUtil.setUserTimeZone(userTimeZone);
  1538. }
  1539. }
  1540. /**
  1541. * Mixture of Ascii and Unicode strings in a
  1542. * NameComment record
  1543. */
  1544. @Test
  1545. void bug51143() throws Exception {
  1546. simpleTest("51143.xls", wb -> assertEquals(1, wb.getNumberOfSheets()));
  1547. }
  1548. /**
  1549. * File with exactly 256 data blocks (+header block)
  1550. * shouldn't break on POIFS loading
  1551. */
  1552. @Test
  1553. void bug51461() throws Exception {
  1554. byte[] data = HSSFITestDataProvider.instance.getTestDataFileContent("51461.xls");
  1555. HSSFWorkbook wbPOIFS = new HSSFWorkbook(new POIFSFileSystem(
  1556. new ByteArrayInputStream(data)).getRoot(), false);
  1557. HSSFWorkbook wbPOIFS2 = new HSSFWorkbook(new POIFSFileSystem(
  1558. new ByteArrayInputStream(data)).getRoot(), false);
  1559. assertEquals(2, wbPOIFS.getNumberOfSheets());
  1560. assertEquals(2, wbPOIFS2.getNumberOfSheets());
  1561. }
  1562. @Test
  1563. void bug51535() throws Exception {
  1564. try (HSSFWorkbook wb = openSampleWorkbook("51535.xls")) {
  1565. assertEquals(3, wb.getNumberOfSheets());
  1566. // Check directly
  1567. HSSFSheet s = wb.getSheetAt(0);
  1568. assertEquals("Top Left Cell", s.getRow(0).getCell(0).getStringCellValue());
  1569. assertEquals("Top Right Cell", s.getRow(0).getCell(255).getStringCellValue());
  1570. assertEquals("Bottom Left Cell", s.getRow(65535).getCell(0).getStringCellValue());
  1571. assertEquals("Bottom Right Cell", s.getRow(65535).getCell(255).getStringCellValue());
  1572. // Extract and check
  1573. try (ExcelExtractor ex = new ExcelExtractor(wb)) {
  1574. String text = ex.getText();
  1575. assertContains(text, "Top Left Cell");
  1576. assertContains(text, "Top Right Cell");
  1577. assertContains(text, "Bottom Left Cell");
  1578. assertContains(text, "Bottom Right Cell");
  1579. }
  1580. }
  1581. }
  1582. /**
  1583. * Sum across multiple workbooks
  1584. * eg =SUM($Sheet2.A1:$Sheet3.A1)
  1585. */
  1586. @Test
  1587. void test48703() throws Exception {
  1588. try (HSSFWorkbook wb = openSampleWorkbook("48703.xls")) {
  1589. assertEquals(3, wb.getNumberOfSheets());
  1590. // Check reading the formula
  1591. Sheet sheet = wb.getSheetAt(0);
  1592. Row r = sheet.getRow(0);
  1593. Cell c = r.getCell(0);
  1594. assertEquals("SUM(Sheet2:Sheet3!A1)", c.getCellFormula());
  1595. assertEquals(4.0, c.getNumericCellValue(), 0);
  1596. // Check the evaluated result
  1597. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
  1598. eval.evaluateFormulaCell(c);
  1599. assertEquals(4.0, c.getNumericCellValue(), 0);
  1600. }
  1601. }
  1602. @Test
  1603. void bug49896() throws Exception {
  1604. String exp = "VLOOKUP(A2,'[C:Documents and Settings/Yegor/My Documents/csco.xls]Sheet1'!$A$2:$B$3,2,FALSE)"
  1605. .replace("/", File.separator);
  1606. simpleTest("49896.xls", wb -> assertEquals(exp, wb.getSheetAt(0).getRow(1).getCell(1).getCellFormula()));
  1607. }
  1608. @Test
  1609. void bug49529() throws Exception {
  1610. // user code reported in Bugzilla #49529
  1611. simpleTest("49529.xls", wb -> {
  1612. wb.getSheetAt(0).createDrawingPatriarch();
  1613. // prior to the fix the line below failed with
  1614. // java.lang.IllegalStateException: EOF - next record not available
  1615. wb.cloneSheet(0);
  1616. });
  1617. }
  1618. @Test
  1619. void bug49612_part() throws IOException {
  1620. try (HSSFWorkbook wb = openSampleWorkbook("49612.xls")) {
  1621. HSSFSheet sh = wb.getSheetAt(0);
  1622. HSSFRow row = sh.getRow(0);
  1623. HSSFCell c1 = row.getCell(2);
  1624. HSSFCell d1 = row.getCell(3);
  1625. HSSFCell e1 = row.getCell(2);
  1626. assertEquals("SUM(BOB+JIM)", c1.getCellFormula());
  1627. // Problem 1: See TestUnfixedBugs#test49612()
  1628. // Problem 2: TestUnfixedBugs#test49612()
  1629. // Problem 3: These used to fail, now pass
  1630. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
  1631. assertEquals(30.0, eval.evaluate(c1).getNumberValue(), 0.001, "evaluating c1");
  1632. assertEquals(30.0, eval.evaluate(d1).getNumberValue(), 0.001, "evaluating d1");
  1633. assertEquals(30.0, eval.evaluate(e1).getNumberValue(), 0.001, "evaluating e1");
  1634. }
  1635. }
  1636. @Test
  1637. void bug51675() throws Exception {
  1638. try (HSSFWorkbook wb = openSampleWorkbook("51675.xls")) {
  1639. HSSFSheet sh = wb.getSheetAt(0);
  1640. InternalSheet ish = HSSFTestHelper.getSheetForTest(sh);
  1641. PageSettingsBlock psb = (PageSettingsBlock) ish.getRecords().get(13);
  1642. List<Short> list = new ArrayList<>();
  1643. psb.visitContainedRecords(r -> list.add(r.getSid()));
  1644. assertEquals(UnknownRecord.BITMAP_00E9, list.get(list.size() - 1).intValue());
  1645. assertEquals(UnknownRecord.HEADER_FOOTER_089C, list.get(list.size() - 2).intValue());
  1646. }
  1647. }
  1648. @Test
  1649. void bug52272() throws IOException {
  1650. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  1651. HSSFSheet sh = wb.createSheet();
  1652. HSSFPatriarch p = sh.createDrawingPatriarch();
  1653. HSSFSimpleShape s = p.createSimpleShape(new HSSFClientAnchor());
  1654. s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
  1655. HSSFSheet sh2 = wb.cloneSheet(0);
  1656. assertNotNull(sh2.getDrawingPatriarch());
  1657. }
  1658. }
  1659. @Test
  1660. void bug53432() throws IOException {
  1661. try (HSSFWorkbook wb1 = new HSSFWorkbook()) {
  1662. wb1.addPicture(new byte[]{123, 22}, Workbook.PICTURE_TYPE_JPEG);
  1663. assertEquals(1, wb1.getAllPictures().size());
  1664. }
  1665. try (HSSFWorkbook wb1 = new HSSFWorkbook()) {
  1666. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  1667. assertEquals(0, wb2.getAllPictures().size());
  1668. wb2.addPicture(new byte[]{123, 22}, Workbook.PICTURE_TYPE_JPEG);
  1669. assertEquals(1, wb2.getAllPictures().size());
  1670. try (HSSFWorkbook wb3 = writeOutAndReadBack(wb2)) {
  1671. assertEquals(1, wb3.getAllPictures().size());
  1672. }
  1673. }
  1674. }
  1675. }
  1676. @Test
  1677. void bug46250() throws Exception {
  1678. try (HSSFWorkbook wb = openSampleWorkbook("46250.xls")) {
  1679. Sheet sh = wb.getSheet("Template");
  1680. Sheet cSh = wb.cloneSheet(wb.getSheetIndex(sh));
  1681. int sIdx = wb.getSheetIndex(cSh);
  1682. HSSFPatriarch patriarch = (HSSFPatriarch) cSh.createDrawingPatriarch();
  1683. HSSFTextbox tb = (HSSFTextbox) patriarch.getChildren().get(2);
  1684. tb.setString(new HSSFRichTextString("POI test"));
  1685. tb.setAnchor(new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 0, (short) 10, 10));
  1686. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb)) {
  1687. HSSFSheet sh2 = wb2.getSheetAt(sIdx);
  1688. assertNotNull(sh2);
  1689. assertNotNull(sh2.getDrawingPatriarch());
  1690. }
  1691. }
  1692. }
  1693. @Test
  1694. void bug53404() throws Exception {
  1695. try (HSSFWorkbook wb = openSampleWorkbook("53404.xls")) {
  1696. Sheet sheet = wb.getSheet("test-sheet");
  1697. int rowCount = sheet.getLastRowNum() + 1;
  1698. int newRows = 5;
  1699. Calendar cal = LocaleUtil.getLocaleCalendar();
  1700. for (int r = rowCount; r < rowCount + newRows; r++) {
  1701. Row row = sheet.createRow((short) r);
  1702. row.createCell(0).setCellValue(1.03 * (r + 7));
  1703. row.createCell(1).setCellValue(cal.getTime());
  1704. row.createCell(2).setCellValue(cal);
  1705. row.createCell(3).setCellValue(String.format(Locale.ROOT, "row:%d/col:%d", r, 3));
  1706. row.createCell(4).setCellValue(true);
  1707. row.createCell(5).setCellErrorValue(FormulaError.NUM.getCode());
  1708. row.createCell(6).setCellValue("added cells.");
  1709. }
  1710. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb)) {
  1711. Sheet sheet2 = wb2.getSheet("test-sheet");
  1712. Row row2 = sheet2.getRow(5);
  1713. assertNotNull(row2);
  1714. assertEquals(cal.getTime(),row2.getCell(2).getDateCellValue());
  1715. }
  1716. }
  1717. }
  1718. /**
  1719. * Row style information is 12 not 16 bits
  1720. */
  1721. @Test
  1722. void bug49237() throws Exception {
  1723. try (Workbook wb = openSampleWorkbook("49237.xls")) {
  1724. Sheet sheet = wb.getSheetAt(0);
  1725. Row row = sheet.getRow(0);
  1726. CellStyle rstyle = row.getRowStyle();
  1727. assertNotNull(rstyle);
  1728. assertEquals(BorderStyle.DOUBLE, rstyle.getBorderBottom());
  1729. }
  1730. }
  1731. /**
  1732. * POI does now support the RC4 CryptoAPI encryption header structure
  1733. */
  1734. @ParameterizedTest
  1735. @CsvSource({
  1736. "xor-encryption-abc.xls, abc",
  1737. "35897-type4.xls, freedom"
  1738. })
  1739. void bug35897(String file, String pass) throws Exception {
  1740. Biff8EncryptionKey.setCurrentUserPassword(pass);
  1741. try {
  1742. simpleTest(file, null);
  1743. } finally {
  1744. Biff8EncryptionKey.setCurrentUserPassword(null);
  1745. }
  1746. }
  1747. @Test
  1748. void bug56450() throws Exception {
  1749. try (HSSFWorkbook wb = openSampleWorkbook("56450.xls")) {
  1750. HSSFSheet sheet = wb.getSheetAt(0);
  1751. int comments = 0;
  1752. for (Row r : sheet) {
  1753. for (Cell c : r) {
  1754. if (c.getCellComment() != null) {
  1755. assertNotNull(c.getCellComment().getString().getString());
  1756. comments++;
  1757. }
  1758. }
  1759. }
  1760. assertEquals(0, comments);
  1761. }
  1762. }
  1763. /**
  1764. * Files initially created with Excel 2010 can have >3 CF rules
  1765. */
  1766. @Test
  1767. void bug56482() throws Exception {
  1768. try (HSSFWorkbook wb = openSampleWorkbook("56482.xls")) {
  1769. assertEquals(1, wb.getNumberOfSheets());
  1770. HSSFSheet sheet = wb.getSheetAt(0);
  1771. HSSFSheetConditionalFormatting cf = sheet.getSheetConditionalFormatting();
  1772. assertEquals(5, cf.getNumConditionalFormattings());
  1773. }
  1774. }
  1775. @Test
  1776. void bug56325() throws IOException {
  1777. try (HSSFWorkbook wb1 = openSampleWorkbook("56325.xls")) {
  1778. assertEquals(3, wb1.getNumberOfSheets());
  1779. wb1.removeSheetAt(0);
  1780. assertEquals(2, wb1.getNumberOfSheets());
  1781. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  1782. assertEquals(2, wb2.getNumberOfSheets());
  1783. wb2.removeSheetAt(0);
  1784. assertEquals(1, wb2.getNumberOfSheets());
  1785. wb2.removeSheetAt(0);
  1786. assertEquals(0, wb2.getNumberOfSheets());
  1787. try (HSSFWorkbook wb3 = writeOutAndReadBack(wb2)) {
  1788. assertEquals(0, wb3.getNumberOfSheets());
  1789. }
  1790. }
  1791. }
  1792. }
  1793. @Test
  1794. void bug56325a() throws IOException {
  1795. try (HSSFWorkbook wb1 = openSampleWorkbook("56325a.xls")) {
  1796. HSSFSheet sheet = wb1.cloneSheet(2);
  1797. wb1.setSheetName(3, "Clone 1");
  1798. sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3"));
  1799. wb1.setPrintArea(3, "$A$4:$C$10");
  1800. sheet = wb1.cloneSheet(2);
  1801. wb1.setSheetName(4, "Clone 2");
  1802. sheet.setRepeatingRows(CellRangeAddress.valueOf("2:3"));
  1803. wb1.setPrintArea(4, "$A$4:$C$10");
  1804. wb1.removeSheetAt(2);
  1805. try (Workbook wb2 = writeOutAndReadBack(wb1)) {
  1806. assertEquals(4, wb2.getNumberOfSheets());
  1807. }
  1808. }
  1809. }
  1810. /**
  1811. * Formulas which reference named ranges, either in other
  1812. * sheets, or workbook scoped but in other workbooks.
  1813. * Used to fail with
  1814. * java.lang.RuntimeException: Unexpected eval class (org.apache.poi.ss.formula.eval.NameXEval)
  1815. */
  1816. @Test
  1817. void bug56737() throws IOException {
  1818. try (Workbook wb = openSampleWorkbook("56737.xls")) {
  1819. // Check the named range definitions
  1820. Name nSheetScope = wb.getName("NR_To_A1");
  1821. Name nWBScope = wb.getName("NR_Global_B2");
  1822. assertNotNull(nSheetScope);
  1823. assertNotNull(nWBScope);
  1824. assertEquals("Defines!$A$1", nSheetScope.getRefersToFormula());
  1825. assertEquals("Defines!$B$2", nWBScope.getRefersToFormula());
  1826. // Check the different kinds of formulas
  1827. Sheet s = wb.getSheetAt(0);
  1828. Cell cRefSName = s.getRow(1).getCell(3);
  1829. Cell cRefWName = s.getRow(2).getCell(3);
  1830. assertEquals("Defines!NR_To_A1", cRefSName.getCellFormula());
  1831. // TODO Correct this, so that the filename is shown too, see bug #56742
  1832. // This is what Excel itself shows
  1833. //assertEquals("'56737.xls'!NR_Global_B2", cRefWName.getCellFormula());
  1834. // TODO This isn't right, but it's what we currently generate....
  1835. assertEquals("NR_Global_B2", cRefWName.getCellFormula());
  1836. // Try to evaluate them
  1837. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  1838. assertEquals("Test A1", eval.evaluate(cRefSName).getStringValue());
  1839. assertEquals(142, (int) eval.evaluate(cRefWName).getNumberValue());
  1840. // Try to evaluate everything
  1841. eval.evaluateAll();
  1842. }
  1843. }
  1844. /**
  1845. * ClassCastException in HSSFOptimiser - StyleRecord cannot be cast to
  1846. * ExtendedFormatRecord when removing un-used styles
  1847. */
  1848. @Test
  1849. void bug54443() throws Exception {
  1850. try (HSSFWorkbook workbook = new HSSFWorkbook()) {
  1851. HSSFCellStyle style = workbook.createCellStyle();
  1852. HSSFCellStyle newStyle = workbook.createCellStyle();
  1853. HSSFSheet mySheet = workbook.createSheet();
  1854. HSSFRow row = mySheet.createRow(0);
  1855. HSSFCell cell = row.createCell(0);
  1856. // Use style
  1857. cell.setCellStyle(style);
  1858. // Switch to newStyle, style is now un-used
  1859. cell.setCellStyle(newStyle);
  1860. // Optimize
  1861. int nbrOfCellStyles = workbook.getNumCellStyles();
  1862. HSSFOptimiser.optimiseCellStyles(workbook);
  1863. assertEquals(nbrOfCellStyles-1, workbook.getNumCellStyles());
  1864. }
  1865. }
  1866. /**
  1867. * Intersection formula ranges, eg =(C2:D3 D3:E4)
  1868. */
  1869. @Test
  1870. void bug52111() throws Exception {
  1871. try (Workbook wb = openSampleWorkbook("Intersection-52111.xls")) {
  1872. Sheet s = wb.getSheetAt(0);
  1873. assertFormula(wb, s.getRow(2).getCell(0), "(C2:D3 D3:E4)", "4.0");
  1874. assertFormula(wb, s.getRow(6).getCell(0), "Tabelle2!E:E Tabelle2!$A11:$IV11", "5.0");
  1875. assertFormula(wb, s.getRow(8).getCell(0), "Tabelle2!E:F Tabelle2!$A11:$IV12", null);
  1876. }
  1877. }
  1878. @Test
  1879. void bug42016() throws Exception {
  1880. try (Workbook wb = openSampleWorkbook("42016.xls")) {
  1881. Sheet s = wb.getSheetAt(0);
  1882. for (int row = 0; row < 7; row++) {
  1883. assertEquals("A$1+B$1", s.getRow(row).getCell(2).getCellFormula());
  1884. }
  1885. }
  1886. }
  1887. /**
  1888. * Unexpected record type (org.apache.poi.hssf.record.ColumnInfoRecord)
  1889. */
  1890. @Test
  1891. void bug53984() throws Exception {
  1892. try (Workbook wb = openSampleWorkbook("53984.xls")) {
  1893. Sheet s = wb.getSheetAt(0);
  1894. assertEquals("International Communication Services SA", s.getRow(2).getCell(0).getStringCellValue());
  1895. assertEquals("Saudi Arabia-Riyadh", s.getRow(210).getCell(0).getStringCellValue());
  1896. }
  1897. }
  1898. /**
  1899. * Read, write, read for formulas point to cells in other files.
  1900. * See bug46670() for the main test, this just
  1901. * covers reading an existing file and checking it.
  1902. *
  1903. * See base-test-class for some related tests that still fail
  1904. */
  1905. @Test
  1906. void bug46670_existing() throws Exception {
  1907. // Expected values
  1908. String refLocal = "'[refs" + File.separator + "airport.xls]Sheet1'!$A$2";
  1909. String refHttp = "'[9http://www.principlesofeconometrics.com/excel/airline.xls]Sheet1'!$A$2";
  1910. // Check we can read them correctly
  1911. simpleTest("46670_local.xls",
  1912. wb -> assertEquals(refLocal, wb.getSheetAt(0).getRow(0).getCell(0).getCellFormula()));
  1913. simpleTest("46670_http.xls",
  1914. wb -> assertEquals(refHttp, wb.getSheetAt(0).getRow(0).getCell(0).getCellFormula()));
  1915. // Now try to set them to the same values, and ensure that
  1916. // they end up as they did before, even with a save and re-load
  1917. try (HSSFWorkbook wb3 = openSampleWorkbook("46670_local.xls")) {
  1918. Sheet s = wb3.getSheetAt(0);
  1919. Cell c = s.getRow(0).getCell(0);
  1920. c.setCellFormula(refLocal);
  1921. assertEquals(refLocal, c.getCellFormula());
  1922. try (HSSFWorkbook wb4 = writeOutAndReadBack(wb3)) {
  1923. s = wb4.getSheetAt(0);
  1924. assertEquals(refLocal, s.getRow(0).getCell(0).getCellFormula());
  1925. }
  1926. }
  1927. try (HSSFWorkbook wb5 = openSampleWorkbook("46670_http.xls")) {
  1928. Sheet s = wb5.getSheetAt(0);
  1929. Cell c = s.getRow(0).getCell(0);
  1930. c.setCellFormula(refHttp);
  1931. assertEquals(refHttp, c.getCellFormula());
  1932. try (Workbook wb6 = writeOutAndReadBack(wb5)) {
  1933. s = wb6.getSheetAt(0);
  1934. assertEquals(refHttp, s.getRow(0).getCell(0).getCellFormula());
  1935. }
  1936. }
  1937. }
  1938. @Test
  1939. void test57163() throws IOException {
  1940. simpleTest("57163.xls", wb -> {
  1941. while (wb.getNumberOfSheets() > 1) {
  1942. wb.removeSheetAt(1);
  1943. }
  1944. });
  1945. }
  1946. @Test
  1947. void test48043() throws IOException {
  1948. try (HSSFWorkbook wb = openSampleWorkbook("56325a.xls")) {
  1949. wb.removeSheetAt(2);
  1950. wb.removeSheetAt(1);
  1951. //Sheet s = wb.createSheet("sheetname");
  1952. Sheet s = wb.getSheetAt(0);
  1953. Row row = s.createRow(0);
  1954. Cell cell = row.createCell(0);
  1955. cell.setCellFormula(
  1956. "IF(AND(ISBLANK(A10)," +
  1957. "ISBLANK(B10)),\"\"," +
  1958. "CONCATENATE(A10,\"-\",B10))");
  1959. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  1960. eval.evaluateAll();
  1961. try (Workbook wbBack = writeOutAndReadBack(wb)) {
  1962. assertNotNull(wbBack);
  1963. }
  1964. }
  1965. }
  1966. @Test
  1967. void test57925() throws IOException {
  1968. try (Workbook wb = openSampleWorkbook("57925.xls")) {
  1969. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  1970. DataFormatter df = new DataFormatter();
  1971. for (Sheet sheet : wb) {
  1972. for (Row row : sheet) {
  1973. for (Cell cell : row) {
  1974. assertNotNull(df.formatCellValue(cell));
  1975. }
  1976. }
  1977. }
  1978. }
  1979. }
  1980. @Test
  1981. void test46515() throws IOException {
  1982. try (Workbook wb = openSampleWorkbook("46515.xls")) {
  1983. // Get structure from webservice
  1984. String urlString = "https://poi.apache.org/components/spreadsheet/images/calendar.jpg";
  1985. URL structURL = new URL(urlString);
  1986. BufferedImage bimage;
  1987. try {
  1988. bimage = ImageIO.read(structURL);
  1989. } catch (IOException e) {
  1990. Assumptions.assumeFalse(true, "Downloading a jpg from poi.apache.org should work");
  1991. return;
  1992. }
  1993. // Convert BufferedImage to byte[]
  1994. UnsynchronizedByteArrayOutputStream imageBAOS = new UnsynchronizedByteArrayOutputStream();
  1995. ImageIO.write(bimage, "jpeg", imageBAOS);
  1996. imageBAOS.flush();
  1997. byte[] imageBytes = imageBAOS.toByteArray();
  1998. imageBAOS.close();
  1999. // Pop structure into Structure HSSFSheet
  2000. int pict = wb.addPicture(imageBytes, HSSFWorkbook.PICTURE_TYPE_JPEG);
  2001. Sheet sheet = wb.getSheet("Structure");
  2002. assertNotNull(sheet, "Did not find sheet");
  2003. HSSFPatriarch patriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
  2004. HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 10, 22);
  2005. anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE);
  2006. patriarch.createPicture(anchor, pict);
  2007. }
  2008. }
  2009. @Test
  2010. void test55668() throws IOException {
  2011. try (Workbook wb = openSampleWorkbook("55668.xls")) {
  2012. Sheet sheet = wb.getSheetAt(0);
  2013. Row row = sheet.getRow(0);
  2014. Cell cell = row.getCell(0);
  2015. assertEquals(CellType.FORMULA, cell.getCellType());
  2016. assertEquals("IF(TRUE,\"\",\"\")", cell.getCellFormula());
  2017. assertEquals("", cell.getStringCellValue());
  2018. //noinspection deprecation
  2019. cell.setCellType(CellType.STRING);
  2020. assertEquals(CellType.BLANK, cell.getCellType());
  2021. assertEquals("", cell.getStringCellValue());
  2022. assertThrows(IllegalStateException.class, cell::getCellFormula);
  2023. }
  2024. }
  2025. @Test
  2026. void test55982() throws IOException {
  2027. simpleTest("55982.xls", wb -> assertNotNull(wb.cloneSheet(1)));
  2028. }
  2029. /**
  2030. * Test generator of ids for the CommonObjectDataSubRecord record.
  2031. */
  2032. @Test
  2033. void test51332() {
  2034. HSSFClientAnchor anchor = new HSSFClientAnchor();
  2035. HSSFSimpleShape shape;
  2036. CommonObjectDataSubRecord cmo;
  2037. shape = new HSSFTextbox(null, anchor);
  2038. shape.setShapeId(1025);
  2039. cmo = (CommonObjectDataSubRecord) shape.getObjRecord().getSubRecords().get(0);
  2040. assertEquals(1, cmo.getObjectId());
  2041. shape = new HSSFPicture(null, anchor);
  2042. shape.setShapeId(1026);
  2043. cmo = (CommonObjectDataSubRecord) shape.getObjRecord().getSubRecords().get(0);
  2044. assertEquals(2, cmo.getObjectId());
  2045. shape = new HSSFComment(null, anchor);
  2046. shape.setShapeId(1027);
  2047. cmo = (CommonObjectDataSubRecord) shape.getObjRecord().getSubRecords().get(0);
  2048. assertEquals(1027, cmo.getObjectId());
  2049. }
  2050. // As of POI 3.15 beta 2, LibreOffice does not display the diagonal border while it does display the bottom border
  2051. // I have not checked Excel to know if this is a LibreOffice or a POI problem.
  2052. @Test
  2053. void test53564() throws IOException {
  2054. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  2055. HSSFSheet sheet = wb.createSheet("Page 1");
  2056. final short BLUE = 30;
  2057. HSSFSheetConditionalFormatting scf = sheet.getSheetConditionalFormatting();
  2058. HSSFConditionalFormattingRule rule = scf.createConditionalFormattingRule(ComparisonOperator.GT, "10");
  2059. HSSFBorderFormatting bord = rule.createBorderFormatting();
  2060. bord.setBorderDiagonal(BorderStyle.THICK);
  2061. assertEquals(BorderStyle.THICK, bord.getBorderDiagonal());
  2062. bord.setBackwardDiagonalOn(true);
  2063. assertTrue(bord.isBackwardDiagonalOn());
  2064. bord.setForwardDiagonalOn(true);
  2065. assertTrue(bord.isForwardDiagonalOn());
  2066. bord.setDiagonalBorderColor(BLUE);
  2067. assertEquals(BLUE, bord.getDiagonalBorderColor());
  2068. // Create the bottom border style so we know what a border is supposed to look like
  2069. bord.setBorderBottom(BorderStyle.THICK);
  2070. assertEquals(BorderStyle.THICK, bord.getBorderBottom());
  2071. bord.setBottomBorderColor(BLUE);
  2072. assertEquals(BLUE, bord.getBottomBorderColor());
  2073. CellRangeAddress[] A2_D4 = {new CellRangeAddress(1, 3, 0, 3)};
  2074. scf.addConditionalFormatting(A2_D4, rule);
  2075. // Set a cell value within the conditional formatting range whose rule would resolve to True.
  2076. Cell C3 = sheet.createRow(2).createCell(2);
  2077. C3.setCellValue(30.0);
  2078. }
  2079. }
  2080. @Test
  2081. void test61287() throws IOException {
  2082. try (HSSFWorkbook wb = openSampleWorkbook("61287.xls");
  2083. ExcelExtractor ex = new ExcelExtractor(wb)) {
  2084. String text = ex.getText();
  2085. assertContains(text, "\u8D44\u4EA7\u8D1F\u503A\u8868");
  2086. }
  2087. }
  2088. @Test
  2089. void test61300() throws Exception {
  2090. try (POIFSFileSystem poifs = new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("61300.xls"))) {
  2091. DocumentEntry entry =
  2092. (DocumentEntry) poifs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
  2093. RuntimeException ex = assertThrows(
  2094. RuntimeException.class,
  2095. () -> new PropertySet(new DocumentInputStream(entry))
  2096. );
  2097. assertEquals("Can't read negative number of bytes", ex.getMessage());
  2098. }
  2099. }
  2100. @Test
  2101. void test51262() throws IOException {
  2102. try (HSSFWorkbook wb = openSampleWorkbook("51262.xls")) {
  2103. Sheet sheet = wb.getSheetAt(0);
  2104. Row row = sheet.getRow(2);
  2105. Cell cell = row.getCell(1);
  2106. CellStyle style = cell.getCellStyle();
  2107. assertEquals(26, style.getFontIndex());
  2108. row = sheet.getRow(3);
  2109. cell = row.getCell(1);
  2110. style = cell.getCellStyle();
  2111. assertEquals(28, style.getFontIndex());
  2112. // check the two fonts
  2113. HSSFFont font = wb.getFontAt(26);
  2114. assertTrue(font.getBold());
  2115. assertEquals(10, font.getFontHeightInPoints());
  2116. assertEquals("\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF", font.getFontName());
  2117. font = wb.getFontAt(28);
  2118. assertTrue(font.getBold());
  2119. assertEquals(10, font.getFontHeightInPoints());
  2120. assertEquals("\uFF2D\uFF33 \uFF30\u30B4\u30B7\u30C3\u30AF", font.getFontName());
  2121. }
  2122. }
  2123. @Test
  2124. void test60460() throws IOException {
  2125. try (final Workbook wb = openSampleWorkbook("60460.xls")) {
  2126. assertEquals(2, wb.getAllNames().size());
  2127. Name rangedName = wb.getAllNames().get(0);
  2128. assertFalse(rangedName.isFunctionName());
  2129. assertEquals("'[\\\\HEPPC3\\gt$\\Teaching\\Syn\\physyn.xls]#REF'!$AK$70:$AL$70",
  2130. // replace '/' to make test work equally on Windows and Linux
  2131. rangedName.getRefersToFormula().replace("/", "\\"));
  2132. rangedName = wb.getAllNames().get(1);
  2133. assertFalse(rangedName.isFunctionName());
  2134. assertEquals("Questionnaire!$A$1:$L$65", rangedName.getRefersToFormula());
  2135. }
  2136. }
  2137. @Test
  2138. void test63819() throws IOException {
  2139. LocaleUtil.setUserLocale(Locale.UK);
  2140. try {
  2141. simpleTest("63819.xls", null);
  2142. } finally {
  2143. LocaleUtil.resetUserLocale();
  2144. }
  2145. }
  2146. /**
  2147. * Test that VALUE behaves properly as array function and its result is handled by aggregate function
  2148. */
  2149. @Test
  2150. void testValueAsArrayFunction() throws IOException {
  2151. try (final Workbook wb = openSampleWorkbook("TestValueAsArrayFunction.xls")) {
  2152. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  2153. Sheet sheet = wb.getSheetAt(0);
  2154. Row row = sheet.getRow(0);
  2155. Cell cell = row.getCell(0);
  2156. assertEquals(6.0, cell.getNumericCellValue(), 0.0);
  2157. }
  2158. }
  2159. // a simple test which rewrites the file once and evaluates its formulas
  2160. @ParameterizedTest
  2161. @CsvSource({
  2162. "15228.xls", "13796.xls", "14460.xls", "14330-1.xls", "14330-2.xls", "22742.xls", "12561-1.xls", "12561-2.xls",
  2163. "12843-1.xls", "12843-2.xls", "13224.xls", "19599-1.xls", "19599-2.xls", "32822.xls", "15573.xls",
  2164. "33082.xls", "34775.xls", "37630.xls", "25183.xls", "26100.xls", "27933.xls", "29675.xls", "29982.xls",
  2165. "31749.xls", "37376.xls", "SimpleWithAutofilter.xls", "44201.xls", "37684-1.xls", "37684-2.xls",
  2166. "41139.xls", "ex42564-21435.xls", "ex42564-21503.xls", "28774.xls", "44891.xls", "44235.xls", "36947.xls",
  2167. "39634.xls", "47701.xls", "48026.xls", "47251.xls", "47251_1.xls", "50020.xls", "50426.xls", "50779_1.xls",
  2168. "50779_2.xls", "51670.xls", "54016.xls", "57456.xls", "53109.xls", "com.aida-tour.www_SPO_files_maldives%20august%20october.xls",
  2169. "named-cell-in-formula-test.xls", "named-cell-test.xls", "bug55505.xls", "SUBSTITUTE.xls", "64261.xls"
  2170. })
  2171. void simpleTest(String fileName) throws IOException {
  2172. simpleTest(fileName, null);
  2173. }
  2174. @SuppressWarnings("unchecked")
  2175. @ParameterizedTest
  2176. @CsvSource({
  2177. "46904.xls, org.apache.poi.hssf.OldExcelFormatException, The supplied spreadsheet seems to be Excel",
  2178. "51832.xls, org.apache.poi.EncryptedDocumentException, Default password is invalid for salt/verifier/verifierHash"
  2179. })
  2180. void simpleTest(String fileName, String exClazz, String exMessage) throws ClassNotFoundException {
  2181. Class<? extends Exception> ex = (Class<? extends Exception>)Class.forName(exClazz);
  2182. Exception e = assertThrows(ex, () -> simpleTest(fileName, null));
  2183. assertTrue(e.getMessage().startsWith(exMessage));
  2184. }
  2185. private void simpleTest(String fileName, Consumer<HSSFWorkbook> addTest) throws IOException {
  2186. try (HSSFWorkbook wb1 = openSampleWorkbook(fileName)) {
  2187. if (addTest != null) {
  2188. addTest.accept(wb1);
  2189. }
  2190. HSSFSheet s = wb1.getSheetAt(0);
  2191. HSSFRow r = s.createRow(10_000);
  2192. HSSFCell c = r.createCell(0);
  2193. c.setCellValue(10);
  2194. HSSFSheet tmpSheet = wb1.createSheet("POITESTSHEET");
  2195. tmpSheet.createRow(10).createCell(10).setCellValue("Test");
  2196. wb1.removeSheetAt(wb1.getSheetIndex(tmpSheet));
  2197. simpleTestHelper(wb1, fileName);
  2198. try (HSSFWorkbook wb2 = writeOutAndReadBack(wb1)) {
  2199. if (addTest != null) {
  2200. addTest.accept(wb2);
  2201. }
  2202. double act = wb2.getSheetAt(0).getRow(10_000).getCell(0).getNumericCellValue();
  2203. assertEquals(10, act, 0);
  2204. simpleTestHelper(wb2, fileName);
  2205. }
  2206. }
  2207. }
  2208. private void simpleTestHelper(HSSFWorkbook wb, String fileName) {
  2209. List<String> files = new ArrayList<>();
  2210. files.add(fileName);
  2211. files.addAll(SIMPLE_REFS.keySet());
  2212. List<HSSFFormulaEvaluator> evals = new ArrayList<>();
  2213. evals.add(wb.getCreationHelper().createFormulaEvaluator());
  2214. evals.addAll(SIMPLE_REFS.values());
  2215. try {
  2216. HSSFFormulaEvaluator.setupEnvironment(files.toArray(new String[0]), evals.toArray(new HSSFFormulaEvaluator[0]));
  2217. evals.get(0).evaluateAll();
  2218. } catch (RuntimeException e) {
  2219. throw new RuntimeException("While handling files " + files + " and evals " + evals, e);
  2220. }
  2221. }
  2222. @Test
  2223. void test52447() throws IOException {
  2224. try (Workbook wb = openSampleWorkbook("52447.xls")) {
  2225. assertNotNull(wb);
  2226. }
  2227. }
  2228. }