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

TestBugs.java 72KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  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 java.io.ByteArrayOutputStream;
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.text.DateFormat;
  22. import java.text.SimpleDateFormat;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Calendar;
  26. import java.util.Date;
  27. import java.util.Iterator;
  28. import java.util.List;
  29. import java.util.Locale;
  30. import java.util.TimeZone;
  31. import junit.framework.AssertionFailedError;
  32. import org.apache.poi.hssf.HSSFITestDataProvider;
  33. import org.apache.poi.hssf.HSSFTestDataSamples;
  34. import org.apache.poi.hssf.OldExcelFormatException;
  35. import org.apache.poi.hssf.model.InternalWorkbook;
  36. import org.apache.poi.hssf.record.CellValueRecordInterface;
  37. import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
  38. import org.apache.poi.hssf.record.NameRecord;
  39. import org.apache.poi.hssf.record.Record;
  40. import org.apache.poi.hssf.record.TabIdRecord;
  41. import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
  42. import org.apache.poi.hssf.record.common.UnicodeString;
  43. import org.apache.poi.ss.formula.ptg.Area3DPtg;
  44. import org.apache.poi.ss.formula.ptg.DeletedArea3DPtg;
  45. import org.apache.poi.ss.formula.ptg.Ptg;
  46. import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
  47. import org.apache.poi.ss.usermodel.Cell;
  48. import org.apache.poi.ss.usermodel.CellStyle;
  49. import org.apache.poi.ss.usermodel.DateUtil;
  50. import org.apache.poi.ss.usermodel.Name;
  51. import org.apache.poi.ss.usermodel.Row;
  52. import org.apache.poi.ss.usermodel.Sheet;
  53. import org.apache.poi.util.TempFile;
  54. /**
  55. * Testcases for bugs entered in bugzilla
  56. * the Test name contains the bugzilla bug id
  57. *
  58. * <b>YK: If a bug can be tested in terms of common ss interfaces,
  59. * define the test in the base class {@link BaseTestBugzillaIssues}</b>
  60. *
  61. * @author Avik Sengupta
  62. * @author Yegor Kozlov
  63. */
  64. public final class TestBugs extends BaseTestBugzillaIssues {
  65. public TestBugs() {
  66. super(HSSFITestDataProvider.instance);
  67. }
  68. private static HSSFWorkbook openSample(String sampleFileName) {
  69. return HSSFITestDataProvider.instance.openSampleWorkbook(sampleFileName);
  70. }
  71. private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
  72. return HSSFITestDataProvider.instance.writeOutAndReadBack(original);
  73. }
  74. private static void writeTestOutputFileForViewing(HSSFWorkbook wb, String simpleFileName) {
  75. if (true) { // set to false to output test files
  76. return;
  77. }
  78. System.setProperty("poi.keep.tmp.files", "true");
  79. File file;
  80. try {
  81. file = TempFile.createTempFile(simpleFileName + "#", ".xls");
  82. FileOutputStream out = new FileOutputStream(file);
  83. wb.write(out);
  84. out.close();
  85. } catch (IOException e) {
  86. throw new RuntimeException(e);
  87. }
  88. if (!file.exists()) {
  89. throw new RuntimeException("File was not written");
  90. }
  91. System.out.println("Open file '" + file.getAbsolutePath() + "' in Excel");
  92. }
  93. /** Test reading AND writing a complicated workbook
  94. *Test opening resulting sheet in excel*/
  95. public void test15228() {
  96. HSSFWorkbook wb = openSample("15228.xls");
  97. HSSFSheet s = wb.getSheetAt(0);
  98. HSSFRow r = s.createRow(0);
  99. HSSFCell c = r.createCell(0);
  100. c.setCellValue(10);
  101. writeTestOutputFileForViewing(wb, "test15228");
  102. }
  103. public void test13796() {
  104. HSSFWorkbook wb = openSample("13796.xls");
  105. HSSFSheet s = wb.getSheetAt(0);
  106. HSSFRow r = s.createRow(0);
  107. HSSFCell c = r.createCell(0);
  108. c.setCellValue(10);
  109. writeOutAndReadBack(wb);
  110. }
  111. /** test hyperlinks
  112. * open resulting file in excel, and check that there is a link to Google
  113. */
  114. public void test15353() {
  115. HSSFWorkbook wb = new HSSFWorkbook();
  116. HSSFSheet sheet = wb.createSheet("My sheet");
  117. HSSFRow row = sheet.createRow( 0 );
  118. HSSFCell cell = row.createCell( 0 );
  119. cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
  120. writeOutAndReadBack(wb);
  121. }
  122. /** test reading of a formula with a name and a cell ref in one
  123. **/
  124. public void test14460() {
  125. HSSFWorkbook wb = openSample("14460.xls");
  126. wb.getSheetAt(0);
  127. }
  128. public void test14330() {
  129. HSSFWorkbook wb = openSample("14330-1.xls");
  130. wb.getSheetAt(0);
  131. wb = openSample("14330-2.xls");
  132. wb.getSheetAt(0);
  133. }
  134. private static void setCellText(HSSFCell cell, String text) {
  135. cell.setCellValue(new HSSFRichTextString(text));
  136. }
  137. /** test rewriting a file with large number of unique strings
  138. *open resulting file in Excel to check results!*/
  139. public void test15375() {
  140. HSSFWorkbook wb = openSample("15375.xls");
  141. HSSFSheet sheet = wb.getSheetAt(0);
  142. HSSFRow row = sheet.getRow(5);
  143. HSSFCell cell = row.getCell(3);
  144. if (cell == null)
  145. cell = row.createCell(3);
  146. // Write test
  147. cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  148. setCellText(cell, "a test");
  149. // change existing numeric cell value
  150. HSSFRow oRow = sheet.getRow(14);
  151. HSSFCell oCell = oRow.getCell(4);
  152. oCell.setCellValue(75);
  153. oCell = oRow.getCell(5);
  154. setCellText(oCell, "0.3");
  155. writeTestOutputFileForViewing(wb, "test15375");
  156. }
  157. /**
  158. * test writing a file with large number of unique strings,
  159. * open resulting file in Excel to check results!
  160. */
  161. public void test15375_2() {
  162. baseTest15375(6000);
  163. }
  164. /**Double byte strings*/
  165. public void test15556() {
  166. HSSFWorkbook wb = openSample("15556.xls");
  167. HSSFSheet sheet = wb.getSheetAt(0);
  168. HSSFRow row = sheet.getRow(45);
  169. assertNotNull("Read row fine!" , row);
  170. }
  171. /**Double byte strings */
  172. public void test22742() {
  173. openSample("22742.xls");
  174. }
  175. /**Double byte strings */
  176. public void test12561_1() {
  177. openSample("12561-1.xls");
  178. }
  179. /** Double byte strings */
  180. public void test12561_2() {
  181. openSample("12561-2.xls");
  182. }
  183. /** Double byte strings
  184. File supplied by jubeson*/
  185. public void test12843_1() {
  186. openSample("12843-1.xls");
  187. }
  188. /** Double byte strings
  189. File supplied by Paul Chung*/
  190. public void test12843_2() {
  191. openSample("12843-2.xls");
  192. }
  193. /** Reference to Name*/
  194. public void test13224() {
  195. openSample("13224.xls");
  196. }
  197. /** Illegal argument exception - cannot store duplicate value in Map*/
  198. public void test19599() {
  199. openSample("19599-1.xls");
  200. openSample("19599-2.xls");
  201. }
  202. public void test24215() {
  203. HSSFWorkbook wb = openSample("24215.xls");
  204. for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets();sheetIndex++) {
  205. HSSFSheet sheet = wb.getSheetAt(sheetIndex);
  206. int rows = sheet.getLastRowNum();
  207. for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
  208. HSSFRow row = sheet.getRow(rowIndex);
  209. int cells = row.getLastCellNum();
  210. for (int cellIndex = 0; cellIndex < cells; cellIndex++) {
  211. row.getCell(cellIndex);
  212. }
  213. }
  214. }
  215. }
  216. /**Tests read and write of Unicode strings in formula results
  217. * bug and testcase submitted by Sompop Kumnoonsate
  218. * The file contains THAI unicode characters.
  219. */
  220. public void testUnicodeStringFormulaRead() {
  221. HSSFWorkbook w = openSample("25695.xls");
  222. HSSFCell a1 = w.getSheetAt(0).getRow(0).getCell(0);
  223. HSSFCell a2 = w.getSheetAt(0).getRow(0).getCell(1);
  224. HSSFCell b1 = w.getSheetAt(0).getRow(1).getCell(0);
  225. HSSFCell b2 = w.getSheetAt(0).getRow(1).getCell(1);
  226. HSSFCell c1 = w.getSheetAt(0).getRow(2).getCell(0);
  227. HSSFCell c2 = w.getSheetAt(0).getRow(2).getCell(1);
  228. HSSFCell d1 = w.getSheetAt(0).getRow(3).getCell(0);
  229. HSSFCell d2 = w.getSheetAt(0).getRow(3).getCell(1);
  230. if (false) {
  231. // THAI code page
  232. System.out.println("a1="+unicodeString(a1));
  233. System.out.println("a2="+unicodeString(a2));
  234. // US code page
  235. System.out.println("b1="+unicodeString(b1));
  236. System.out.println("b2="+unicodeString(b2));
  237. // THAI+US
  238. System.out.println("c1="+unicodeString(c1));
  239. System.out.println("c2="+unicodeString(c2));
  240. // US+THAI
  241. System.out.println("d1="+unicodeString(d1));
  242. System.out.println("d2="+unicodeString(d2));
  243. }
  244. confirmSameCellText(a1, a2);
  245. confirmSameCellText(b1, b2);
  246. confirmSameCellText(c1, c2);
  247. confirmSameCellText(d1, d2);
  248. HSSFWorkbook rw = writeOutAndReadBack(w);
  249. HSSFCell ra1 = rw.getSheetAt(0).getRow(0).getCell(0);
  250. HSSFCell ra2 = rw.getSheetAt(0).getRow(0).getCell(1);
  251. HSSFCell rb1 = rw.getSheetAt(0).getRow(1).getCell(0);
  252. HSSFCell rb2 = rw.getSheetAt(0).getRow(1).getCell(1);
  253. HSSFCell rc1 = rw.getSheetAt(0).getRow(2).getCell(0);
  254. HSSFCell rc2 = rw.getSheetAt(0).getRow(2).getCell(1);
  255. HSSFCell rd1 = rw.getSheetAt(0).getRow(3).getCell(0);
  256. HSSFCell rd2 = rw.getSheetAt(0).getRow(3).getCell(1);
  257. confirmSameCellText(a1, ra1);
  258. confirmSameCellText(b1, rb1);
  259. confirmSameCellText(c1, rc1);
  260. confirmSameCellText(d1, rd1);
  261. confirmSameCellText(a1, ra2);
  262. confirmSameCellText(b1, rb2);
  263. confirmSameCellText(c1, rc2);
  264. confirmSameCellText(d1, rd2);
  265. }
  266. private static void confirmSameCellText(HSSFCell a, HSSFCell b) {
  267. assertEquals(a.getRichStringCellValue().getString(), b.getRichStringCellValue().getString());
  268. }
  269. private static String unicodeString(HSSFCell cell) {
  270. String ss = cell.getRichStringCellValue().getString();
  271. char s[] = ss.toCharArray();
  272. StringBuffer sb = new StringBuffer();
  273. for (int x=0;x<s.length;x++) {
  274. sb.append("\\u").append(Integer.toHexString(s[x]));
  275. }
  276. return sb.toString();
  277. }
  278. /** Error in opening wb*/
  279. public void test32822() {
  280. openSample("32822.xls");
  281. }
  282. /**fail to read wb with chart */
  283. public void test15573() {
  284. openSample("15573.xls");
  285. }
  286. /**names and macros */
  287. public void test27852() {
  288. HSSFWorkbook wb = openSample("27852.xls");
  289. for(int i = 0 ; i < wb.getNumberOfNames(); i++){
  290. HSSFName name = wb.getNameAt(i);
  291. name.getNameName();
  292. if (name.isFunctionName()) {
  293. continue;
  294. }
  295. name.getRefersToFormula();
  296. }
  297. }
  298. public void test33082() {
  299. openSample("33082.xls");
  300. }
  301. public void test34775() {
  302. try {
  303. openSample("34775.xls");
  304. } catch (NullPointerException e) {
  305. throw new AssertionFailedError("identified bug 34775");
  306. }
  307. }
  308. /** Error when reading then writing ArrayValues in NameRecord's*/
  309. public void test37630() {
  310. HSSFWorkbook wb = openSample("37630.xls");
  311. writeOutAndReadBack(wb);
  312. }
  313. /**
  314. * Bug 25183: org.apache.poi.hssf.usermodel.HSSFSheet.setPropertiesFromSheet
  315. */
  316. public void test25183() {
  317. HSSFWorkbook wb = openSample("25183.xls");
  318. writeOutAndReadBack(wb);
  319. }
  320. /**
  321. * Bug 26100: 128-character message in IF statement cell causes HSSFWorkbook open failure
  322. */
  323. public void test26100() {
  324. HSSFWorkbook wb = openSample("26100.xls");
  325. writeOutAndReadBack(wb);
  326. }
  327. /**
  328. * Bug 27933: Unable to use a template (xls) file containing a wmf graphic
  329. */
  330. public void test27933() {
  331. HSSFWorkbook wb = openSample("27933.xls");
  332. writeOutAndReadBack(wb);
  333. }
  334. /**
  335. * Bug 29206: NPE on HSSFSheet.getRow for blank rows
  336. */
  337. public void test29206() {
  338. //the first check with blank workbook
  339. HSSFWorkbook wb = openSample("Simple.xls");
  340. HSSFSheet sheet = wb.createSheet();
  341. for(int i = 1; i < 400; i++) {
  342. HSSFRow row = sheet.getRow(i);
  343. if(row != null) {
  344. row.getCell(0);
  345. }
  346. }
  347. }
  348. /**
  349. * Bug 29675: POI 2.5 final corrupts output when starting workbook has a graphic
  350. */
  351. public void test29675() {
  352. HSSFWorkbook wb = openSample("29675.xls");
  353. writeOutAndReadBack(wb);
  354. }
  355. /**
  356. * Bug 29942: Importing Excel files that have been created by Open Office on Linux
  357. */
  358. public void test29942() {
  359. HSSFWorkbook wb = openSample("29942.xls");
  360. HSSFSheet sheet = wb.getSheetAt(0);
  361. int count = 0;
  362. for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
  363. HSSFRow row = sheet.getRow(i);
  364. if (row != null) {
  365. HSSFCell cell = row .getCell(0);
  366. assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCellType());
  367. count++;
  368. }
  369. }
  370. assertEquals(85, count); //should read 85 rows
  371. writeOutAndReadBack(wb);
  372. }
  373. /**
  374. * Bug 29982: Unable to read spreadsheet when dropdown list cell is selected -
  375. * Unable to construct record instance
  376. */
  377. public void test29982() {
  378. HSSFWorkbook wb = openSample("29982.xls");
  379. writeOutAndReadBack(wb);
  380. }
  381. /**
  382. * Bug 30540: HSSFSheet.setRowBreak throws NullPointerException
  383. */
  384. public void test30540() {
  385. HSSFWorkbook wb = openSample("30540.xls");
  386. HSSFSheet s = wb.getSheetAt(0);
  387. s.setRowBreak(1);
  388. writeOutAndReadBack(wb);
  389. }
  390. /**
  391. * Bug 31749: {Need help urgently}[This is critical] workbook.write() corrupts the file......?
  392. */
  393. public void test31749() {
  394. HSSFWorkbook wb = openSample("31749.xls");
  395. writeOutAndReadBack(wb);
  396. }
  397. /**
  398. * Bug 31979: {urgent help needed .....}poi library does not support form objects properly.
  399. */
  400. public void test31979() {
  401. HSSFWorkbook wb = openSample("31979.xls");
  402. writeOutAndReadBack(wb);
  403. }
  404. /**
  405. * Bug 35564: HSSFCell.java: NullPtrExc in isGridsPrinted() and getProtect()
  406. * when HSSFWorkbook is created from file
  407. */
  408. public void test35564() {
  409. HSSFWorkbook wb = openSample("35564.xls");
  410. HSSFSheet sheet = wb.getSheetAt( 0 );
  411. assertEquals(false, sheet.isGridsPrinted());
  412. assertEquals(false, sheet.getProtect());
  413. writeOutAndReadBack(wb);
  414. }
  415. /**
  416. * Bug 35565: HSSFCell.java: NullPtrExc in getColumnBreaks() when HSSFWorkbook is created from file
  417. */
  418. public void test35565() {
  419. HSSFWorkbook wb = openSample("35565.xls");
  420. HSSFSheet sheet = wb.getSheetAt( 0 );
  421. assertNotNull(sheet);
  422. writeOutAndReadBack(wb);
  423. }
  424. /**
  425. * Bug 37376: Cannot open the saved Excel file if checkbox controls exceed certain limit
  426. */
  427. public void test37376() {
  428. HSSFWorkbook wb = openSample("37376.xls");
  429. writeOutAndReadBack(wb);
  430. }
  431. /**
  432. * Bug 40285: CellIterator Skips First Column
  433. */
  434. public void test40285() {
  435. HSSFWorkbook wb = openSample("40285.xls");
  436. HSSFSheet sheet = wb.getSheetAt( 0 );
  437. int rownum = 0;
  438. for (Iterator it = sheet.rowIterator(); it.hasNext(); rownum++) {
  439. HSSFRow row = (HSSFRow)it.next();
  440. assertEquals(rownum, row.getRowNum());
  441. int cellNum = 0;
  442. for (Iterator it2 = row.cellIterator(); it2.hasNext(); cellNum++) {
  443. HSSFCell cell = (HSSFCell)it2.next();
  444. assertEquals(cellNum, cell.getColumnIndex());
  445. }
  446. }
  447. }
  448. /**
  449. * Test bug 38266: NPE when adding a row break
  450. *
  451. * User's diagnosis:
  452. * 1. Manually (i.e., not using POI) create an Excel Workbook, making sure it
  453. * contains a sheet that doesn't have any row breaks
  454. * 2. Using POI, create a new HSSFWorkbook from the template in step #1
  455. * 3. Try adding a row break (via sheet.setRowBreak()) to the sheet mentioned in step #1
  456. * 4. Get a NullPointerException
  457. */
  458. public void test38266() {
  459. String[] files = {"Simple.xls", "SimpleMultiCell.xls", "duprich1.xls"};
  460. for (int i = 0; i < files.length; i++) {
  461. HSSFWorkbook wb = openSample(files[i]);
  462. HSSFSheet sheet = wb.getSheetAt( 0 );
  463. int[] breaks = sheet.getRowBreaks();
  464. assertEquals(0, breaks.length);
  465. //add 3 row breaks
  466. for (int j = 1; j <= 3; j++) {
  467. sheet.setRowBreak(j*20);
  468. }
  469. }
  470. }
  471. public void test40738() {
  472. HSSFWorkbook wb = openSample("SimpleWithAutofilter.xls");
  473. writeOutAndReadBack(wb);
  474. }
  475. /**
  476. * Bug 44200: Sheet not cloneable when Note added to excel cell
  477. */
  478. public void test44200() {
  479. HSSFWorkbook wb = openSample("44200.xls");
  480. wb.cloneSheet(0);
  481. writeOutAndReadBack(wb);
  482. }
  483. /**
  484. * Bug 44201: Sheet not cloneable when validation added to excel cell
  485. */
  486. public void test44201() {
  487. HSSFWorkbook wb = openSample("44201.xls");
  488. writeOutAndReadBack(wb);
  489. }
  490. /**
  491. * Bug 37684 : Unhandled Continue Record Error
  492. */
  493. public void test37684 () {
  494. HSSFWorkbook wb = openSample("37684-1.xls");
  495. writeOutAndReadBack(wb);
  496. wb = openSample("37684-2.xls");
  497. writeOutAndReadBack(wb);
  498. }
  499. /**
  500. * Bug 41139: Constructing HSSFWorkbook is failed,threw threw ArrayIndexOutOfBoundsException for creating UnknownRecord
  501. */
  502. public void test41139() {
  503. HSSFWorkbook wb = openSample("41139.xls");
  504. writeOutAndReadBack(wb);
  505. }
  506. /**
  507. * Bug 41546: Constructing HSSFWorkbook is failed,
  508. * Unknown Ptg in Formula: 0x1a (26)
  509. */
  510. public void test41546() {
  511. HSSFWorkbook wb = openSample("41546.xls");
  512. assertEquals(1, wb.getNumberOfSheets());
  513. wb = writeOutAndReadBack(wb);
  514. assertEquals(1, wb.getNumberOfSheets());
  515. }
  516. /**
  517. * Bug 42564: Some files from Access were giving a RecordFormatException
  518. * when reading the BOFRecord
  519. */
  520. public void test42564() {
  521. HSSFWorkbook wb = openSample("ex42564-21435.xls");
  522. writeOutAndReadBack(wb);
  523. }
  524. /**
  525. * Bug 42564: Some files from Access also have issues
  526. * with the NameRecord, once you get past the BOFRecord
  527. * issue.
  528. */
  529. public void test42564Alt() {
  530. HSSFWorkbook wb = openSample("ex42564-21503.xls");
  531. writeOutAndReadBack(wb);
  532. }
  533. /**
  534. * Bug 42618: RecordFormatException reading a file containing
  535. * =CHOOSE(2,A2,A3,A4)
  536. */
  537. public void test42618() {
  538. HSSFWorkbook wb = openSample("SimpleWithChoose.xls");
  539. wb = writeOutAndReadBack(wb);
  540. // Check we detect the string properly too
  541. HSSFSheet s = wb.getSheetAt(0);
  542. // Textual value
  543. HSSFRow r1 = s.getRow(0);
  544. HSSFCell c1 = r1.getCell(1);
  545. assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString());
  546. // Formula Value
  547. HSSFRow r2 = s.getRow(1);
  548. HSSFCell c2 = r2.getCell(1);
  549. assertEquals(25, (int)c2.getNumericCellValue());
  550. try {
  551. assertEquals("CHOOSE(2,A2,A3,A4)", c2.getCellFormula());
  552. } catch (IllegalStateException e) {
  553. if (e.getMessage().startsWith("Too few arguments")
  554. && e.getMessage().indexOf("ConcatPtg") > 0) {
  555. throw new AssertionFailedError("identified bug 44306");
  556. }
  557. }
  558. }
  559. /**
  560. * Something up with the FileSharingRecord
  561. */
  562. public void test43251() {
  563. // Used to blow up with an IllegalArgumentException
  564. // when creating a FileSharingRecord
  565. HSSFWorkbook wb;
  566. try {
  567. wb = openSample("43251.xls");
  568. } catch (IllegalArgumentException e) {
  569. throw new AssertionFailedError("identified bug 43251");
  570. }
  571. assertEquals(1, wb.getNumberOfSheets());
  572. }
  573. /**
  574. * Crystal reports generates files with short
  575. * StyleRecords, which is against the spec
  576. */
  577. public void test44471() {
  578. // Used to blow up with an ArrayIndexOutOfBounds
  579. // when creating a StyleRecord
  580. HSSFWorkbook wb;
  581. try {
  582. wb = openSample("OddStyleRecord.xls");
  583. } catch (ArrayIndexOutOfBoundsException e) {
  584. throw new AssertionFailedError("Identified bug 44471");
  585. }
  586. assertEquals(1, wb.getNumberOfSheets());
  587. }
  588. /**
  589. * Files with "read only recommended" were giving
  590. * grief on the FileSharingRecord
  591. */
  592. public void test44536() {
  593. // Used to blow up with an IllegalArgumentException
  594. // when creating a FileSharingRecord
  595. HSSFWorkbook wb = openSample("ReadOnlyRecommended.xls");
  596. // Check read only advised
  597. assertEquals(3, wb.getNumberOfSheets());
  598. assertTrue(wb.isWriteProtected());
  599. // But also check that another wb isn't
  600. wb = openSample("SimpleWithChoose.xls");
  601. assertFalse(wb.isWriteProtected());
  602. }
  603. /**
  604. * Some files were having problems with the DVRecord,
  605. * probably due to dropdowns
  606. */
  607. public void test44593() {
  608. // Used to blow up with an IllegalArgumentException
  609. // when creating a DVRecord
  610. // Now won't, but no idea if this means we have
  611. // rubbish in the DVRecord or not...
  612. HSSFWorkbook wb;
  613. try {
  614. wb = openSample("44593.xls");
  615. } catch (IllegalArgumentException e) {
  616. throw new AssertionFailedError("Identified bug 44593");
  617. }
  618. assertEquals(2, wb.getNumberOfSheets());
  619. }
  620. /**
  621. * Used to give problems due to trying to read a zero
  622. * length string, but that's now properly handled
  623. */
  624. public void test44643() {
  625. // Used to blow up with an IllegalArgumentException
  626. HSSFWorkbook wb;
  627. try {
  628. wb = openSample("44643.xls");
  629. } catch (IllegalArgumentException e) {
  630. throw new AssertionFailedError("identified bug 44643");
  631. }
  632. assertEquals(1, wb.getNumberOfSheets());
  633. }
  634. /**
  635. * User reported the wrong number of rows from the
  636. * iterator, but we can't replicate that
  637. */
  638. public void test44693() {
  639. HSSFWorkbook wb = openSample("44693.xls");
  640. HSSFSheet s = wb.getSheetAt(0);
  641. // Rows are 1 to 713
  642. assertEquals(0, s.getFirstRowNum());
  643. assertEquals(712, s.getLastRowNum());
  644. assertEquals(713, s.getPhysicalNumberOfRows());
  645. // Now check the iterator
  646. int rowsSeen = 0;
  647. for(Iterator i = s.rowIterator(); i.hasNext(); ) {
  648. HSSFRow r = (HSSFRow)i.next();
  649. assertNotNull(r);
  650. rowsSeen++;
  651. }
  652. assertEquals(713, rowsSeen);
  653. }
  654. /**
  655. * Bug 28774: Excel will crash when opening xls-files with images.
  656. */
  657. public void test28774() {
  658. HSSFWorkbook wb = openSample("28774.xls");
  659. assertTrue("no errors reading sample xls", true);
  660. writeOutAndReadBack(wb);
  661. assertTrue("no errors writing sample xls", true);
  662. }
  663. /**
  664. * Had a problem apparently, not sure what as it
  665. * works just fine...
  666. */
  667. public void test44891() {
  668. HSSFWorkbook wb = openSample("44891.xls");
  669. assertTrue("no errors reading sample xls", true);
  670. writeOutAndReadBack(wb);
  671. assertTrue("no errors writing sample xls", true);
  672. }
  673. /**
  674. * Bug 44235: Ms Excel can't open save as excel file
  675. *
  676. * Works fine with poi-3.1-beta1.
  677. */
  678. public void test44235() {
  679. HSSFWorkbook wb = openSample("44235.xls");
  680. assertTrue("no errors reading sample xls", true);
  681. writeOutAndReadBack(wb);
  682. assertTrue("no errors writing sample xls", true);
  683. }
  684. public void test36947() {
  685. HSSFWorkbook wb = openSample("36947.xls");
  686. assertTrue("no errors reading sample xls", true);
  687. writeOutAndReadBack(wb);
  688. assertTrue("no errors writing sample xls", true);
  689. }
  690. public void test39634() {
  691. HSSFWorkbook wb = openSample("39634.xls");
  692. assertTrue("no errors reading sample xls", true);
  693. writeOutAndReadBack(wb);
  694. assertTrue("no errors writing sample xls", true);
  695. }
  696. /**
  697. * Problems with extracting check boxes from
  698. * HSSFObjectData
  699. * @throws Exception
  700. */
  701. public void test44840() {
  702. HSSFWorkbook wb = openSample("WithCheckBoxes.xls");
  703. // Take a look at the embedded objects
  704. List objects = wb.getAllEmbeddedObjects();
  705. assertEquals(1, objects.size());
  706. HSSFObjectData obj = (HSSFObjectData)objects.get(0);
  707. assertNotNull(obj);
  708. // Peek inside the underlying record
  709. EmbeddedObjectRefSubRecord rec = obj.findObjectRecord();
  710. assertNotNull(rec);
  711. // assertEquals(32, rec.field_1_stream_id_offset);
  712. assertEquals(0, rec.getStreamId().intValue()); // WRONG!
  713. assertEquals("Forms.CheckBox.1", rec.getOLEClassName());
  714. assertEquals(12, rec.getObjectData().length);
  715. // Doesn't have a directory
  716. assertFalse(obj.hasDirectoryEntry());
  717. assertNotNull(obj.getObjectData());
  718. assertEquals(12, obj.getObjectData().length);
  719. assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName());
  720. try {
  721. obj.getDirectory();
  722. fail();
  723. } catch(FileNotFoundException e) {
  724. // expected during successful test
  725. } catch (IOException e) {
  726. throw new RuntimeException(e);
  727. }
  728. }
  729. /**
  730. * Test that we can delete sheets without
  731. * breaking the build in named ranges
  732. * used for printing stuff.
  733. */
  734. public void test30978() {
  735. HSSFWorkbook wb = openSample("30978-alt.xls");
  736. assertEquals(1, wb.getNumberOfNames());
  737. assertEquals(3, wb.getNumberOfSheets());
  738. // Check all names fit within range, and use
  739. // DeletedArea3DPtg
  740. InternalWorkbook w = wb.getWorkbook();
  741. for(int i=0; i<w.getNumNames(); i++) {
  742. NameRecord r = w.getNameRecord(i);
  743. assertTrue(r.getSheetNumber() <= wb.getNumberOfSheets());
  744. Ptg[] nd = r.getNameDefinition();
  745. assertEquals(1, nd.length);
  746. assertTrue(nd[0] instanceof DeletedArea3DPtg);
  747. }
  748. // Delete the 2nd sheet
  749. wb.removeSheetAt(1);
  750. // Re-check
  751. assertEquals(1, wb.getNumberOfNames());
  752. assertEquals(2, wb.getNumberOfSheets());
  753. for(int i=0; i<w.getNumNames(); i++) {
  754. NameRecord r = w.getNameRecord(i);
  755. assertTrue(r.getSheetNumber() <= wb.getNumberOfSheets());
  756. Ptg[] nd = r.getNameDefinition();
  757. assertEquals(1, nd.length);
  758. assertTrue(nd[0] instanceof DeletedArea3DPtg);
  759. }
  760. // Save and re-load
  761. wb = writeOutAndReadBack(wb);
  762. w = wb.getWorkbook();
  763. assertEquals(1, wb.getNumberOfNames());
  764. assertEquals(2, wb.getNumberOfSheets());
  765. for(int i=0; i<w.getNumNames(); i++) {
  766. NameRecord r = w.getNameRecord(i);
  767. assertTrue(r.getSheetNumber() <= wb.getNumberOfSheets());
  768. Ptg[] nd = r.getNameDefinition();
  769. assertEquals(1, nd.length);
  770. assertTrue(nd[0] instanceof DeletedArea3DPtg);
  771. }
  772. }
  773. /**
  774. * Test that fonts get added properly
  775. */
  776. public void test45338() {
  777. HSSFWorkbook wb = new HSSFWorkbook();
  778. assertEquals(4, wb.getNumberOfFonts());
  779. HSSFSheet s = wb.createSheet();
  780. s.createRow(0);
  781. s.createRow(1);
  782. s.getRow(0).createCell(0);
  783. s.getRow(1).createCell(0);
  784. assertEquals(4, wb.getNumberOfFonts());
  785. HSSFFont f1 = wb.getFontAt((short)0);
  786. assertEquals(400, f1.getBoldweight());
  787. // Check that asking for the same font
  788. // multiple times gives you the same thing.
  789. // Otherwise, our tests wouldn't work!
  790. assertEquals(
  791. wb.getFontAt((short)0),
  792. wb.getFontAt((short)0)
  793. );
  794. assertEquals(
  795. wb.getFontAt((short)2),
  796. wb.getFontAt((short)2)
  797. );
  798. assertTrue(
  799. wb.getFontAt((short)0)
  800. !=
  801. wb.getFontAt((short)2)
  802. );
  803. // Look for a new font we have
  804. // yet to add
  805. assertNull(
  806. wb.findFont(
  807. (short)11, (short)123, (short)22,
  808. "Thingy", false, true, (short)2, (byte)2
  809. )
  810. );
  811. HSSFFont nf = wb.createFont();
  812. assertEquals(5, wb.getNumberOfFonts());
  813. assertEquals(5, nf.getIndex());
  814. assertEquals(nf, wb.getFontAt((short)5));
  815. nf.setBoldweight((short)11);
  816. nf.setColor((short)123);
  817. nf.setFontHeight((short)22);
  818. nf.setFontName("Thingy");
  819. nf.setItalic(false);
  820. nf.setStrikeout(true);
  821. nf.setTypeOffset((short)2);
  822. nf.setUnderline((byte)2);
  823. assertEquals(5, wb.getNumberOfFonts());
  824. assertEquals(nf, wb.getFontAt((short)5));
  825. // Find it now
  826. assertNotNull(
  827. wb.findFont(
  828. (short)11, (short)123, (short)22,
  829. "Thingy", false, true, (short)2, (byte)2
  830. )
  831. );
  832. assertEquals(
  833. 5,
  834. wb.findFont(
  835. (short)11, (short)123, (short)22,
  836. "Thingy", false, true, (short)2, (byte)2
  837. ).getIndex()
  838. );
  839. assertEquals(nf,
  840. wb.findFont(
  841. (short)11, (short)123, (short)22,
  842. "Thingy", false, true, (short)2, (byte)2
  843. )
  844. );
  845. }
  846. /**
  847. * From the mailing list - ensure we can handle a formula
  848. * containing a zip code, eg ="70164"
  849. */
  850. public void testZipCodeFormulas() {
  851. HSSFWorkbook wb = new HSSFWorkbook();
  852. HSSFSheet s = wb.createSheet();
  853. s.createRow(0);
  854. HSSFCell c1 = s.getRow(0).createCell(0);
  855. HSSFCell c2 = s.getRow(0).createCell(1);
  856. HSSFCell c3 = s.getRow(0).createCell(2);
  857. // As number and string
  858. c1.setCellFormula("70164");
  859. c2.setCellFormula("\"70164\"");
  860. c3.setCellFormula("\"90210\"");
  861. // Check the formulas
  862. assertEquals("70164", c1.getCellFormula());
  863. assertEquals("\"70164\"", c2.getCellFormula());
  864. // And check the values - blank
  865. confirmCachedValue(0.0, c1);
  866. confirmCachedValue(0.0, c2);
  867. confirmCachedValue(0.0, c3);
  868. // Try changing the cached value on one of the string
  869. // formula cells, so we can see it updates properly
  870. c3.setCellValue(new HSSFRichTextString("test"));
  871. confirmCachedValue("test", c3);
  872. try {
  873. c3.getNumericCellValue();
  874. throw new AssertionFailedError("exception should have been thrown");
  875. } catch (IllegalStateException e) {
  876. assertEquals("Cannot get a numeric value from a text formula cell", e.getMessage());
  877. }
  878. // Now evaluate, they should all be changed
  879. HSSFFormulaEvaluator eval = new HSSFFormulaEvaluator(wb);
  880. eval.evaluateFormulaCell(c1);
  881. eval.evaluateFormulaCell(c2);
  882. eval.evaluateFormulaCell(c3);
  883. // Check that the cells now contain
  884. // the correct values
  885. confirmCachedValue(70164.0, c1);
  886. confirmCachedValue("70164", c2);
  887. confirmCachedValue("90210", c3);
  888. // Write and read
  889. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  890. HSSFSheet ns = nwb.getSheetAt(0);
  891. HSSFCell nc1 = ns.getRow(0).getCell(0);
  892. HSSFCell nc2 = ns.getRow(0).getCell(1);
  893. HSSFCell nc3 = ns.getRow(0).getCell(2);
  894. // Re-check
  895. confirmCachedValue(70164.0, nc1);
  896. confirmCachedValue("70164", nc2);
  897. confirmCachedValue("90210", nc3);
  898. CellValueRecordInterface[] cvrs = ns.getSheet().getValueRecords();
  899. for (int i = 0; i < cvrs.length; i++) {
  900. CellValueRecordInterface cvr = cvrs[i];
  901. if(cvr instanceof FormulaRecordAggregate) {
  902. FormulaRecordAggregate fr = (FormulaRecordAggregate)cvr;
  903. if(i == 0) {
  904. assertEquals(70164.0, fr.getFormulaRecord().getValue(), 0.0001);
  905. assertNull(fr.getStringRecord());
  906. } else if (i == 1) {
  907. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  908. assertNotNull(fr.getStringRecord());
  909. assertEquals("70164", fr.getStringRecord().getString());
  910. } else {
  911. assertEquals(0.0, fr.getFormulaRecord().getValue(), 0.0001);
  912. assertNotNull(fr.getStringRecord());
  913. assertEquals("90210", fr.getStringRecord().getString());
  914. }
  915. }
  916. }
  917. assertEquals(3, cvrs.length);
  918. }
  919. private static void confirmCachedValue(double expectedValue, HSSFCell cell) {
  920. assertEquals(HSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  921. assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cell.getCachedFormulaResultType());
  922. assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
  923. }
  924. private static void confirmCachedValue(String expectedValue, HSSFCell cell) {
  925. assertEquals(HSSFCell.CELL_TYPE_FORMULA, cell.getCellType());
  926. assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCachedFormulaResultType());
  927. assertEquals(expectedValue, cell.getRichStringCellValue().getString());
  928. }
  929. /**
  930. * Problem with "Vector Rows", eg a whole
  931. * column which is set to the result of
  932. * {=sin(B1:B9)}(9,1), so that each cell is
  933. * shown to have the contents
  934. * {=sin(B1:B9){9,1)[rownum][0]
  935. * In this sample file, the vector column
  936. * is C, and the data column is B.
  937. *
  938. * For now, blows up with an exception from ExtPtg
  939. * Expected ExpPtg to be converted from Shared to Non-Shared...
  940. */
  941. public void DISABLEDtest43623() {
  942. HSSFWorkbook wb = openSample("43623.xls");
  943. assertEquals(1, wb.getNumberOfSheets());
  944. HSSFSheet s1 = wb.getSheetAt(0);
  945. HSSFCell c1 = s1.getRow(0).getCell(2);
  946. HSSFCell c2 = s1.getRow(1).getCell(2);
  947. HSSFCell c3 = s1.getRow(2).getCell(2);
  948. // These formula contents are a guess...
  949. assertEquals("{=sin(B1:B9){9,1)[0][0]", c1.getCellFormula());
  950. assertEquals("{=sin(B1:B9){9,1)[1][0]", c2.getCellFormula());
  951. assertEquals("{=sin(B1:B9){9,1)[2][0]", c3.getCellFormula());
  952. // Save and re-open, ensure it still works
  953. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  954. HSSFSheet ns1 = nwb.getSheetAt(0);
  955. HSSFCell nc1 = ns1.getRow(0).getCell(2);
  956. HSSFCell nc2 = ns1.getRow(1).getCell(2);
  957. HSSFCell nc3 = ns1.getRow(2).getCell(2);
  958. assertEquals("{=sin(B1:B9){9,1)[0][0]", nc1.getCellFormula());
  959. assertEquals("{=sin(B1:B9){9,1)[1][0]", nc2.getCellFormula());
  960. assertEquals("{=sin(B1:B9){9,1)[2][0]", nc3.getCellFormula());
  961. }
  962. /**
  963. * People are all getting confused about the last
  964. * row and cell number
  965. */
  966. public void test30635() {
  967. HSSFWorkbook wb = new HSSFWorkbook();
  968. HSSFSheet s = wb.createSheet();
  969. // No rows, everything is 0
  970. assertEquals(0, s.getFirstRowNum());
  971. assertEquals(0, s.getLastRowNum());
  972. assertEquals(0, s.getPhysicalNumberOfRows());
  973. // One row, most things are 0, physical is 1
  974. s.createRow(0);
  975. assertEquals(0, s.getFirstRowNum());
  976. assertEquals(0, s.getLastRowNum());
  977. assertEquals(1, s.getPhysicalNumberOfRows());
  978. // And another, things change
  979. s.createRow(4);
  980. assertEquals(0, s.getFirstRowNum());
  981. assertEquals(4, s.getLastRowNum());
  982. assertEquals(2, s.getPhysicalNumberOfRows());
  983. // Now start on cells
  984. HSSFRow r = s.getRow(0);
  985. assertEquals(-1, r.getFirstCellNum());
  986. assertEquals(-1, r.getLastCellNum());
  987. assertEquals(0, r.getPhysicalNumberOfCells());
  988. // Add a cell, things move off -1
  989. r.createCell(0);
  990. assertEquals(0, r.getFirstCellNum());
  991. assertEquals(1, r.getLastCellNum()); // last cell # + 1
  992. assertEquals(1, r.getPhysicalNumberOfCells());
  993. r.createCell(1);
  994. assertEquals(0, r.getFirstCellNum());
  995. assertEquals(2, r.getLastCellNum()); // last cell # + 1
  996. assertEquals(2, r.getPhysicalNumberOfCells());
  997. r.createCell(4);
  998. assertEquals(0, r.getFirstCellNum());
  999. assertEquals(5, r.getLastCellNum()); // last cell # + 1
  1000. assertEquals(3, r.getPhysicalNumberOfCells());
  1001. }
  1002. /**
  1003. * Data Tables - ptg 0x2
  1004. */
  1005. public void test44958() {
  1006. HSSFWorkbook wb = openSample("44958.xls");
  1007. HSSFSheet s;
  1008. HSSFRow r;
  1009. HSSFCell c;
  1010. // Check the contents of the formulas
  1011. // E4 to G9 of sheet 4 make up the table
  1012. s = wb.getSheet("OneVariable Table Completed");
  1013. r = s.getRow(3);
  1014. c = r.getCell(4);
  1015. assertEquals(HSSFCell.CELL_TYPE_FORMULA, c.getCellType());
  1016. // TODO - check the formula once tables and
  1017. // arrays are properly supported
  1018. // E4 to H9 of sheet 5 make up the table
  1019. s = wb.getSheet("TwoVariable Table Example");
  1020. r = s.getRow(3);
  1021. c = r.getCell(4);
  1022. assertEquals(HSSFCell.CELL_TYPE_FORMULA, c.getCellType());
  1023. // TODO - check the formula once tables and
  1024. // arrays are properly supported
  1025. }
  1026. /**
  1027. * 45322: HSSFSheet.autoSizeColumn fails when style.getDataFormat() returns -1
  1028. */
  1029. public void test45322() {
  1030. HSSFWorkbook wb = openSample("44958.xls");
  1031. HSSFSheet sh = wb.getSheetAt(0);
  1032. for(short i=0; i < 30; i++) sh.autoSizeColumn(i);
  1033. }
  1034. /**
  1035. * We used to add too many UncalcRecords to sheets
  1036. * with diagrams on. Don't any more
  1037. */
  1038. public void test45414() {
  1039. HSSFWorkbook wb = openSample("WithThreeCharts.xls");
  1040. wb.getSheetAt(0).setForceFormulaRecalculation(true);
  1041. wb.getSheetAt(1).setForceFormulaRecalculation(false);
  1042. wb.getSheetAt(2).setForceFormulaRecalculation(true);
  1043. // Write out and back in again
  1044. // This used to break
  1045. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  1046. // Check now set as it should be
  1047. assertTrue(nwb.getSheetAt(0).getForceFormulaRecalculation());
  1048. assertFalse(nwb.getSheetAt(1).getForceFormulaRecalculation());
  1049. assertTrue(nwb.getSheetAt(2).getForceFormulaRecalculation());
  1050. }
  1051. /**
  1052. * Very hidden sheets not displaying as such
  1053. */
  1054. public void test45761() {
  1055. HSSFWorkbook wb = openSample("45761.xls");
  1056. assertEquals(3, wb.getNumberOfSheets());
  1057. assertFalse(wb.isSheetHidden(0));
  1058. assertFalse(wb.isSheetVeryHidden(0));
  1059. assertTrue(wb.isSheetHidden(1));
  1060. assertFalse(wb.isSheetVeryHidden(1));
  1061. assertFalse(wb.isSheetHidden(2));
  1062. assertTrue(wb.isSheetVeryHidden(2));
  1063. // Change 0 to be very hidden, and re-load
  1064. wb.setSheetHidden(0, HSSFWorkbook.SHEET_STATE_VERY_HIDDEN);
  1065. HSSFWorkbook nwb = writeOutAndReadBack(wb);
  1066. assertFalse(nwb.isSheetHidden(0));
  1067. assertTrue(nwb.isSheetVeryHidden(0));
  1068. assertTrue(nwb.isSheetHidden(1));
  1069. assertFalse(nwb.isSheetVeryHidden(1));
  1070. assertFalse(nwb.isSheetHidden(2));
  1071. assertTrue(nwb.isSheetVeryHidden(2));
  1072. }
  1073. /**
  1074. * The resolution for bug 45777 assumed that the maximum text length in a header / footer
  1075. * record was 256 bytes. This assumption appears to be wrong. Since the fix for bug 47244,
  1076. * POI now supports header / footer text lengths beyond 256 bytes.
  1077. */
  1078. public void test45777() {
  1079. HSSFWorkbook wb = new HSSFWorkbook();
  1080. HSSFSheet s = wb.createSheet();
  1081. char[] cc248 = new char[248];
  1082. Arrays.fill(cc248, 'x');
  1083. String s248 = new String(cc248);
  1084. String s249 = s248 + "1";
  1085. String s250 = s248 + "12";
  1086. String s251 = s248 + "123";
  1087. assertEquals(248, s248.length());
  1088. assertEquals(249, s249.length());
  1089. assertEquals(250, s250.length());
  1090. assertEquals(251, s251.length());
  1091. // Try on headers
  1092. s.getHeader().setCenter(s248);
  1093. assertEquals(254, s.getHeader().getRawText().length());
  1094. writeOutAndReadBack(wb);
  1095. s.getHeader().setCenter(s251);
  1096. assertEquals(257, s.getHeader().getRawText().length());
  1097. writeOutAndReadBack(wb);
  1098. try {
  1099. s.getHeader().setCenter(s250); // 256 bytes required
  1100. } catch(IllegalArgumentException e) {
  1101. throw new AssertionFailedError("Identified bug 47244b - header can be more than 256 bytes");
  1102. }
  1103. try {
  1104. s.getHeader().setCenter(s251); // 257 bytes required
  1105. } catch(IllegalArgumentException e) {
  1106. throw new AssertionFailedError("Identified bug 47244b - header can be more than 256 bytes");
  1107. }
  1108. // Now try on footers
  1109. s.getFooter().setCenter(s248);
  1110. assertEquals(254, s.getFooter().getRawText().length());
  1111. writeOutAndReadBack(wb);
  1112. s.getFooter().setCenter(s251);
  1113. assertEquals(257, s.getFooter().getRawText().length());
  1114. writeOutAndReadBack(wb);
  1115. try {
  1116. s.getFooter().setCenter(s250); // 256 bytes required
  1117. } catch(IllegalArgumentException e) {
  1118. throw new AssertionFailedError("Identified bug 47244b - footer can be more than 256 bytes");
  1119. }
  1120. try {
  1121. s.getFooter().setCenter(s251); // 257 bytes required
  1122. } catch(IllegalArgumentException e) {
  1123. throw new AssertionFailedError("Identified bug 47244b - footer can be more than 256 bytes");
  1124. }
  1125. }
  1126. /**
  1127. * Charts with long titles
  1128. */
  1129. public void test45784() {
  1130. // This used to break
  1131. HSSFWorkbook wb = openSample("45784.xls");
  1132. assertEquals(1, wb.getNumberOfSheets());
  1133. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  1134. }
  1135. /**
  1136. * Cell background colours
  1137. */
  1138. public void test45492() {
  1139. HSSFWorkbook wb = openSample("45492.xls");
  1140. HSSFSheet s = wb.getSheetAt(0);
  1141. HSSFRow r = s.getRow(0);
  1142. HSSFPalette p = wb.getCustomPalette();
  1143. HSSFCell auto = r.getCell(0);
  1144. HSSFCell grey = r.getCell(1);
  1145. HSSFCell red = r.getCell(2);
  1146. HSSFCell blue = r.getCell(3);
  1147. HSSFCell green = r.getCell(4);
  1148. assertEquals(64, auto.getCellStyle().getFillForegroundColor());
  1149. assertEquals(64, auto.getCellStyle().getFillBackgroundColor());
  1150. assertEquals("0:0:0", p.getColor(64).getHexString());
  1151. assertEquals(22, grey.getCellStyle().getFillForegroundColor());
  1152. assertEquals(64, grey.getCellStyle().getFillBackgroundColor());
  1153. assertEquals("C0C0:C0C0:C0C0", p.getColor(22).getHexString());
  1154. assertEquals(10, red.getCellStyle().getFillForegroundColor());
  1155. assertEquals(64, red.getCellStyle().getFillBackgroundColor());
  1156. assertEquals("FFFF:0:0", p.getColor(10).getHexString());
  1157. assertEquals(12, blue.getCellStyle().getFillForegroundColor());
  1158. assertEquals(64, blue.getCellStyle().getFillBackgroundColor());
  1159. assertEquals("0:0:FFFF", p.getColor(12).getHexString());
  1160. assertEquals(11, green.getCellStyle().getFillForegroundColor());
  1161. assertEquals(64, green.getCellStyle().getFillBackgroundColor());
  1162. assertEquals("0:FFFF:0", p.getColor(11).getHexString());
  1163. }
  1164. /**
  1165. * ContinueRecord after EOF
  1166. */
  1167. public void test46137() {
  1168. // This used to break
  1169. HSSFWorkbook wb = openSample("46137.xls");
  1170. assertEquals(7, wb.getNumberOfSheets());
  1171. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  1172. }
  1173. /**
  1174. * Odd POIFS blocks issue:
  1175. * block[ 44 ] already removed from org.apache.poi.poifs.storage.BlockListImpl.remove
  1176. */
  1177. public void test45290() {
  1178. HSSFWorkbook wb = openSample("45290.xls");
  1179. assertEquals(1, wb.getNumberOfSheets());
  1180. }
  1181. /**
  1182. * In POI-2.5 user reported exception when parsing a name with a custom VBA function:
  1183. * =MY_VBA_FUNCTION("lskdjflsk")
  1184. */
  1185. public void test30070() {
  1186. HSSFWorkbook wb = openSample("30070.xls"); //contains custom VBA function 'Commission'
  1187. HSSFSheet sh = wb.getSheetAt(0);
  1188. HSSFCell cell = sh.getRow(0).getCell(1);
  1189. //B1 uses VBA in the formula
  1190. assertEquals("Commission(A1)", cell.getCellFormula());
  1191. //name sales_1 refers to Commission(Sheet0!$A$1)
  1192. int idx = wb.getNameIndex("sales_1");
  1193. assertTrue(idx != -1);
  1194. HSSFName name = wb.getNameAt(idx);
  1195. assertEquals("Commission(Sheet0!$A$1)", name.getRefersToFormula());
  1196. }
  1197. /**
  1198. * The link formulas which is referring to other books cannot be taken (the bug existed prior to POI-3.2)
  1199. * Expected:
  1200. *
  1201. * [link_sub.xls]Sheet1!$A$1
  1202. * [link_sub.xls]Sheet1!$A$2
  1203. * [link_sub.xls]Sheet1!$A$3
  1204. *
  1205. * POI-3.1 output:
  1206. *
  1207. * Sheet1!$A$1
  1208. * Sheet1!$A$2
  1209. * Sheet1!$A$3
  1210. *
  1211. */
  1212. public void test27364() {
  1213. HSSFWorkbook wb = openSample("27364.xls");
  1214. HSSFSheet sheet = wb.getSheetAt(0);
  1215. assertEquals("[link_sub.xls]Sheet1!$A$1", sheet.getRow(0).getCell(0).getCellFormula());
  1216. assertEquals("[link_sub.xls]Sheet1!$A$2", sheet.getRow(1).getCell(0).getCellFormula());
  1217. assertEquals("[link_sub.xls]Sheet1!$A$3", sheet.getRow(2).getCell(0).getCellFormula());
  1218. }
  1219. /**
  1220. * Similar to bug#27364:
  1221. * HSSFCell.getCellFormula() fails with references to external workbooks
  1222. */
  1223. public void test31661() {
  1224. HSSFWorkbook wb = openSample("31661.xls");
  1225. HSSFSheet sheet = wb.getSheetAt(0);
  1226. HSSFCell cell = sheet.getRow(11).getCell(10); //K11
  1227. assertEquals("+'[GM Budget.xls]8085.4450'!$B$2", cell.getCellFormula());
  1228. }
  1229. /**
  1230. * Incorrect handling of non-ISO 8859-1 characters in Windows ANSII Code Page 1252
  1231. */
  1232. public void test27394() {
  1233. HSSFWorkbook wb = openSample("27394.xls");
  1234. assertEquals("\u0161\u017E", wb.getSheetName(0));
  1235. assertEquals("\u0161\u017E\u010D\u0148\u0159", wb.getSheetName(1));
  1236. HSSFSheet sheet = wb.getSheetAt(0);
  1237. assertEquals("\u0161\u017E", sheet.getRow(0).getCell(0).getStringCellValue());
  1238. assertEquals("\u0161\u017E\u010D\u0148\u0159", sheet.getRow(1).getCell(0).getStringCellValue());
  1239. }
  1240. /**
  1241. * Multiple calls of HSSFWorkbook.write result in corrupted xls
  1242. */
  1243. public void test32191() throws IOException {
  1244. HSSFWorkbook wb = openSample("27394.xls");
  1245. ByteArrayOutputStream out = new ByteArrayOutputStream();
  1246. wb.write(out);
  1247. out.close();
  1248. int size1 = out.size();
  1249. out = new ByteArrayOutputStream();
  1250. wb.write(out);
  1251. out.close();
  1252. int size2 = out.size();
  1253. assertEquals(size1, size2);
  1254. out = new ByteArrayOutputStream();
  1255. wb.write(out);
  1256. out.close();
  1257. int size3 = out.size();
  1258. assertEquals(size2, size3);
  1259. }
  1260. /**
  1261. * java.io.IOException: block[ 0 ] already removed
  1262. * (is an excel 95 file though)
  1263. */
  1264. public void test46904() {
  1265. try {
  1266. openSample("46904.xls");
  1267. fail();
  1268. } catch(OldExcelFormatException e) {
  1269. assertTrue(e.getMessage().startsWith(
  1270. "The supplied spreadsheet seems to be Excel"
  1271. ));
  1272. }
  1273. }
  1274. /**
  1275. * java.lang.NegativeArraySizeException reading long
  1276. * non-unicode data for a name record
  1277. */
  1278. public void test47034() {
  1279. HSSFWorkbook wb = openSample("47034.xls");
  1280. assertEquals(893, wb.getNumberOfNames());
  1281. assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300));
  1282. }
  1283. /**
  1284. * HSSFRichTextString.length() returns negative for really long strings.
  1285. * The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer than 32,767 characters
  1286. */
  1287. public void test46368() {
  1288. HSSFWorkbook wb = openSample("46368.xls");
  1289. HSSFSheet s = wb.getSheetAt(0);
  1290. HSSFCell cell1 = s.getRow(0).getCell(0);
  1291. assertEquals(32770, cell1.getStringCellValue().length());
  1292. HSSFCell cell2 = s.getRow(2).getCell(0);
  1293. assertEquals(32766, cell2.getStringCellValue().length());
  1294. }
  1295. /**
  1296. * Short records on certain sheets with charts in them
  1297. */
  1298. public void test48180() {
  1299. HSSFWorkbook wb = openSample("48180.xls");
  1300. HSSFSheet s = wb.getSheetAt(0);
  1301. HSSFCell cell1 = s.getRow(0).getCell(0);
  1302. assertEquals("test ", cell1.getStringCellValue().toString());
  1303. HSSFCell cell2 = s.getRow(0).getCell(1);
  1304. assertEquals(1.0, cell2.getNumericCellValue(), 0.0);
  1305. }
  1306. /**
  1307. * POI 3.5 beta 7 can not read excel file contain list box (Form Control)
  1308. */
  1309. public void test47701() {
  1310. openSample("47701.xls");
  1311. }
  1312. public void test48026() {
  1313. openSample("48026.xls");
  1314. }
  1315. public void test47251() {
  1316. openSample("47251.xls");
  1317. }
  1318. /**
  1319. * Round trip a file with an unusual UnicodeString/ExtRst record parts
  1320. */
  1321. public void test47847() throws Exception {
  1322. HSSFWorkbook wb = openSample("47847.xls");
  1323. assertEquals(3, wb.getNumberOfSheets());
  1324. // Find the SST record
  1325. UnicodeString withExt = wb.getWorkbook().getSSTString(0);
  1326. UnicodeString withoutExt = wb.getWorkbook().getSSTString(31);
  1327. assertEquals("O:Alloc:Qty", withExt.getString());
  1328. assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);
  1329. assertEquals("RT", withoutExt.getString());
  1330. assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000);
  1331. // Something about continues...
  1332. // Write out and re-read
  1333. wb = writeOutAndReadBack(wb);
  1334. assertEquals(3, wb.getNumberOfSheets());
  1335. // Check it's the same now
  1336. withExt = wb.getWorkbook().getSSTString(0);
  1337. withoutExt = wb.getWorkbook().getSSTString(31);
  1338. assertEquals("O:Alloc:Qty", withExt.getString());
  1339. assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004);
  1340. assertEquals("RT", withoutExt.getString());
  1341. assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000);
  1342. }
  1343. /**
  1344. * Problem with cloning a sheet with a chart
  1345. * contained in it.
  1346. */
  1347. public void test49096() throws Exception {
  1348. HSSFWorkbook wb = openSample("49096.xls");
  1349. assertEquals(1, wb.getNumberOfSheets());
  1350. assertNotNull(wb.getSheetAt(0));
  1351. wb.cloneSheet(0);
  1352. assertEquals(2, wb.getNumberOfSheets());
  1353. wb = writeOutAndReadBack(wb);
  1354. assertEquals(2, wb.getNumberOfSheets());
  1355. }
  1356. /**
  1357. * Newly created sheets need to get a
  1358. * proper TabID, otherwise print setup
  1359. * gets confused on them.
  1360. * Also ensure that print setup refs are
  1361. * by reference not value
  1362. */
  1363. public void test46664() throws Exception {
  1364. HSSFWorkbook wb = new HSSFWorkbook();
  1365. HSSFSheet sheet = wb.createSheet("new_sheet");
  1366. HSSFRow row = sheet.createRow((short)0);
  1367. row.createCell(0).setCellValue(new HSSFRichTextString("Column A"));
  1368. row.createCell(1).setCellValue(new HSSFRichTextString("Column B"));
  1369. row.createCell(2).setCellValue(new HSSFRichTextString("Column C"));
  1370. row.createCell(3).setCellValue(new HSSFRichTextString("Column D"));
  1371. row.createCell(4).setCellValue(new HSSFRichTextString("Column E"));
  1372. row.createCell(5).setCellValue(new HSSFRichTextString("Column F"));
  1373. //set print area from column a to column c (on first row)
  1374. wb.setPrintArea(
  1375. 0, //sheet index
  1376. 0, //start column
  1377. 2, //end column
  1378. 0, //start row
  1379. 0 //end row
  1380. );
  1381. wb = writeOutAndReadBack(wb);
  1382. // Ensure the tab index
  1383. TabIdRecord tr = null;
  1384. for(Record r : wb.getWorkbook().getRecords()) {
  1385. if(r instanceof TabIdRecord) {
  1386. tr = (TabIdRecord)r;
  1387. }
  1388. }
  1389. assertNotNull(tr);
  1390. assertEquals(1, tr._tabids.length);
  1391. assertEquals(0, tr._tabids[0]);
  1392. // Ensure the print setup
  1393. assertEquals("new_sheet!$A$1:$C$1", wb.getPrintArea(0));
  1394. assertEquals("new_sheet!$A$1:$C$1", wb.getName("Print_Area").getRefersToFormula());
  1395. // Needs reference not value
  1396. NameRecord nr = wb.getWorkbook().getNameRecord(
  1397. wb.getNameIndex("Print_Area")
  1398. );
  1399. assertEquals("Print_Area", nr.getNameText());
  1400. assertEquals(1, nr.getNameDefinition().length);
  1401. assertEquals(
  1402. "new_sheet!$A$1:$C$1",
  1403. ((Area3DPtg)nr.getNameDefinition()[0]).toFormulaString(HSSFEvaluationWorkbook.create(wb))
  1404. );
  1405. assertEquals('R', nr.getNameDefinition()[0].getRVAType());
  1406. }
  1407. /**
  1408. * Problems with formula references to
  1409. * sheets via URLs
  1410. */
  1411. public void test45970() throws Exception {
  1412. HSSFWorkbook wb = openSample("FormulaRefs.xls");
  1413. assertEquals(3, wb.getNumberOfSheets());
  1414. HSSFSheet s = wb.getSheetAt(0);
  1415. HSSFRow row;
  1416. row = s.getRow(0);
  1417. assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
  1418. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1419. row = s.getRow(1);
  1420. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1421. assertEquals("B1", row.getCell(1).getCellFormula());
  1422. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1423. row = s.getRow(2);
  1424. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1425. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1426. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1427. row = s.getRow(3);
  1428. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1429. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1430. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1431. row = s.getRow(4);
  1432. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1433. assertEquals("'[\u0005$http://gagravarr.org/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1434. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1435. // Change 4
  1436. row.getCell(1).setCellFormula("'[\u0005$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2");
  1437. row.getCell(1).setCellValue(123.0);
  1438. // Add 5
  1439. row = s.createRow(5);
  1440. row.createCell(1, Cell.CELL_TYPE_FORMULA);
  1441. row.getCell(1).setCellFormula("'[\u0005$http://example.com/FormulaRefs.xls]Sheet1'!B1");
  1442. row.getCell(1).setCellValue(234.0);
  1443. // Re-test
  1444. wb = writeOutAndReadBack(wb);
  1445. s = wb.getSheetAt(0);
  1446. row = s.getRow(0);
  1447. assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
  1448. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1449. row = s.getRow(1);
  1450. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1451. assertEquals("B1", row.getCell(1).getCellFormula());
  1452. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1453. row = s.getRow(2);
  1454. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1455. assertEquals("Sheet1!B1", row.getCell(1).getCellFormula());
  1456. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1457. row = s.getRow(3);
  1458. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1459. assertEquals("[Formulas2.xls]Sheet1!B2", row.getCell(1).getCellFormula());
  1460. assertEquals(112.0, row.getCell(1).getNumericCellValue());
  1461. // TODO - Fix these so they work...
  1462. if(1==2) {
  1463. row = s.getRow(4);
  1464. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1465. assertEquals("'[\u0005$http://gagravarr.org/FormulaRefs2.xls]Sheet1'!B2", row.getCell(1).getCellFormula());
  1466. assertEquals(123.0, row.getCell(1).getNumericCellValue());
  1467. row = s.getRow(5);
  1468. assertEquals(Cell.CELL_TYPE_FORMULA, row.getCell(1).getCellType());
  1469. assertEquals("'[\u0005$http://example.com/FormulaRefs.xls]Sheet1'!B1", row.getCell(1).getCellFormula());
  1470. assertEquals(234.0, row.getCell(1).getNumericCellValue());
  1471. }
  1472. }
  1473. /**
  1474. * Test for a file with NameRecord with NameCommentRecord comments
  1475. */
  1476. public void test49185() throws Exception {
  1477. HSSFWorkbook wb = openSample("49185.xls");
  1478. Name name = wb.getName("foobarName");
  1479. assertEquals("This is a comment", name.getComment());
  1480. // Rename the name, comment comes with it
  1481. name.setNameName("ChangedName");
  1482. assertEquals("This is a comment", name.getComment());
  1483. // Save and re-check
  1484. wb = writeOutAndReadBack(wb);
  1485. name = wb.getName("ChangedName");
  1486. assertEquals("This is a comment", name.getComment());
  1487. // Now try to change it
  1488. name.setComment("Changed Comment");
  1489. assertEquals("Changed Comment", name.getComment());
  1490. // Save and re-check
  1491. wb = writeOutAndReadBack(wb);
  1492. name = wb.getName("ChangedName");
  1493. assertEquals("Changed Comment", name.getComment());
  1494. }
  1495. /**
  1496. * Vertically aligned text
  1497. */
  1498. public void test49524() throws Exception {
  1499. HSSFWorkbook wb = openSample("49524.xls");
  1500. Sheet s = wb.getSheetAt(0);
  1501. Row r = s.getRow(0);
  1502. Cell rotated = r.getCell(0);
  1503. Cell normal = r.getCell(1);
  1504. // Check the current ones
  1505. assertEquals(0, normal.getCellStyle().getRotation());
  1506. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1507. // Add a new style, also rotated
  1508. CellStyle cs = wb.createCellStyle();
  1509. cs.setRotation((short)0xff);
  1510. Cell nc = r.createCell(2);
  1511. nc.setCellValue("New Rotated Text");
  1512. nc.setCellStyle(cs);
  1513. assertEquals(0xff, nc.getCellStyle().getRotation());
  1514. // Write out and read back
  1515. wb = writeOutAndReadBack(wb);
  1516. // Re-check
  1517. s = wb.getSheetAt(0);
  1518. r = s.getRow(0);
  1519. rotated = r.getCell(0);
  1520. normal = r.getCell(1);
  1521. nc = r.getCell(2);
  1522. assertEquals(0, normal.getCellStyle().getRotation());
  1523. assertEquals(0xff, rotated.getCellStyle().getRotation());
  1524. assertEquals(0xff, nc.getCellStyle().getRotation());
  1525. }
  1526. /**
  1527. * Setting the user style name on custom styles
  1528. */
  1529. public void test49689() throws Exception {
  1530. HSSFWorkbook wb = new HSSFWorkbook();
  1531. HSSFSheet s = wb.createSheet("Test");
  1532. HSSFRow r = s.createRow(0);
  1533. HSSFCell c = r.createCell(0);
  1534. HSSFCellStyle cs1 = wb.createCellStyle();
  1535. HSSFCellStyle cs2 = wb.createCellStyle();
  1536. HSSFCellStyle cs3 = wb.createCellStyle();
  1537. assertEquals(21, cs1.getIndex());
  1538. cs1.setUserStyleName("Testing");
  1539. assertEquals(22, cs2.getIndex());
  1540. cs2.setUserStyleName("Testing 2");
  1541. assertEquals(23, cs3.getIndex());
  1542. cs3.setUserStyleName("Testing 3");
  1543. // Set one
  1544. c.setCellStyle(cs1);
  1545. // Write out and read back
  1546. wb = writeOutAndReadBack(wb);
  1547. // Re-check
  1548. assertEquals("Testing", wb.getCellStyleAt((short)21).getUserStyleName());
  1549. assertEquals("Testing 2", wb.getCellStyleAt((short)22).getUserStyleName());
  1550. assertEquals("Testing 3", wb.getCellStyleAt((short)23).getUserStyleName());
  1551. }
  1552. public void test49751() {
  1553. HSSFWorkbook wb = openSample("49751.xls");
  1554. short numCellStyles = wb.getNumCellStyles();
  1555. List<String> namedStyles = Arrays.asList(
  1556. "20% - Accent1", "20% - Accent2", "20% - Accent3", "20% - Accent4", "20% - Accent5",
  1557. "20% - Accent6", "40% - Accent1", "40% - Accent2", "40% - Accent3", "40% - Accent4",
  1558. "40% - Accent5", "40% - Accent6", "60% - Accent1", "60% - Accent2", "60% - Accent3",
  1559. "60% - Accent4", "60% - Accent5", "60% - Accent6", "Accent1", "Accent2", "Accent3",
  1560. "Accent4", "Accent5", "Accent6", "Bad", "Calculation", "Check Cell", "Explanatory Text",
  1561. "Good", "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Input", "Linked Cell",
  1562. "Neutral", "Note", "Output", "Title", "Total", "Warning Text");
  1563. List<String> collecteddStyles = new ArrayList<String>();
  1564. for (short i = 0; i < numCellStyles; i++) {
  1565. HSSFCellStyle cellStyle = wb.getCellStyleAt(i);
  1566. String styleName = cellStyle.getUserStyleName();
  1567. if (styleName != null) {
  1568. collecteddStyles.add(styleName);
  1569. }
  1570. }
  1571. assertTrue(namedStyles.containsAll(collecteddStyles));
  1572. }
  1573. /**
  1574. * Regression with the PageSettingsBlock
  1575. */
  1576. public void test49931() throws Exception {
  1577. HSSFWorkbook wb = openSample("49931.xls");
  1578. assertEquals(1, wb.getNumberOfSheets());
  1579. assertEquals("Foo", wb.getSheetAt(0).getRow(0).getCell(0).getRichStringCellValue().toString());
  1580. }
  1581. /**
  1582. * Missing left/right/centre options on a footer
  1583. */
  1584. public void test48325() throws Exception {
  1585. HSSFWorkbook wb = openSample("48325.xls");
  1586. HSSFSheet sh = wb.getSheetAt(0);
  1587. HSSFFooter f = sh.getFooter();
  1588. // Will show as the centre, as that is what excel does
  1589. // with an invalid footer lacking left/right/centre details
  1590. assertEquals("Left text should be empty", "", f.getLeft());
  1591. assertEquals("Right text should be empty", "", f.getRight());
  1592. assertEquals(
  1593. "Center text should contain the illegal value",
  1594. "BlahBlah blah blah ", f.getCenter()
  1595. );
  1596. }
  1597. /**
  1598. * IllegalStateException received when creating Data validation in sheet with macro
  1599. */
  1600. public void test50020() throws Exception {
  1601. HSSFWorkbook wb = openSample("50020.xls");
  1602. writeOutAndReadBack(wb);
  1603. }
  1604. public void test50426() throws Exception {
  1605. HSSFWorkbook wb = openSample("50426.xls");
  1606. writeOutAndReadBack(wb);
  1607. }
  1608. /**
  1609. * Last row number when shifting rows
  1610. */
  1611. public void test50416LastRowNumber() {
  1612. // Create the workbook with 1 sheet which contains 3 rows
  1613. HSSFWorkbook workbook = new HSSFWorkbook();
  1614. Sheet sheet = workbook.createSheet("Bug50416");
  1615. Row row1 = sheet.createRow(0);
  1616. Cell cellA_1 = row1.createCell(0,Cell.CELL_TYPE_STRING);
  1617. cellA_1.setCellValue("Cell A,1");
  1618. Row row2 = sheet.createRow(1);
  1619. Cell cellA_2 = row2.createCell(0,Cell.CELL_TYPE_STRING);
  1620. cellA_2.setCellValue("Cell A,2");
  1621. Row row3 = sheet.createRow(2);
  1622. Cell cellA_3 = row3.createCell(0,Cell.CELL_TYPE_STRING);
  1623. cellA_3.setCellValue("Cell A,3");
  1624. // Test the last Row number it currently correct
  1625. assertEquals(2, sheet.getLastRowNum());
  1626. // Shift the first row to the end
  1627. sheet.shiftRows(0, 0, 3);
  1628. assertEquals(3, sheet.getLastRowNum());
  1629. assertEquals(-1, sheet.getRow(0).getLastCellNum());
  1630. assertEquals("Cell A,2", sheet.getRow(1).getCell(0).getStringCellValue());
  1631. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1632. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1633. // Shift the 2nd row up to the first one
  1634. sheet.shiftRows(1, 1, -1);
  1635. assertEquals(3, sheet.getLastRowNum());
  1636. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1637. assertEquals(-1, sheet.getRow(1).getLastCellNum());
  1638. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1639. assertEquals("Cell A,1", sheet.getRow(3).getCell(0).getStringCellValue());
  1640. // Shift the 4th row up into the gap in the 3rd row
  1641. sheet.shiftRows(3, 3, -2);
  1642. assertEquals(2, sheet.getLastRowNum());
  1643. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1644. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1645. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1646. assertEquals(-1, sheet.getRow(3).getLastCellNum());
  1647. // Now zap the empty 4th row - won't do anything
  1648. sheet.removeRow(sheet.getRow(3));
  1649. // Test again the last row number which should be 2
  1650. assertEquals(2, sheet.getLastRowNum());
  1651. assertEquals("Cell A,2", sheet.getRow(0).getCell(0).getStringCellValue());
  1652. assertEquals("Cell A,1", sheet.getRow(1).getCell(0).getStringCellValue());
  1653. assertEquals("Cell A,3", sheet.getRow(2).getCell(0).getStringCellValue());
  1654. }
  1655. /**
  1656. * If you send a file between Excel and OpenOffice enough, something
  1657. * will turn the "General" format into "GENERAL"
  1658. */
  1659. public void test50756() throws Exception {
  1660. HSSFWorkbook wb = openSample("50756.xls");
  1661. HSSFSheet s = wb.getSheetAt(0);
  1662. HSSFRow r17 = s.getRow(16);
  1663. HSSFRow r18 = s.getRow(17);
  1664. HSSFDataFormatter df = new HSSFDataFormatter();
  1665. assertEquals(10.0, r17.getCell(1).getNumericCellValue());
  1666. assertEquals(20.0, r17.getCell(2).getNumericCellValue());
  1667. assertEquals(20.0, r17.getCell(3).getNumericCellValue());
  1668. assertEquals("GENERAL", r17.getCell(1).getCellStyle().getDataFormatString());
  1669. assertEquals("GENERAL", r17.getCell(2).getCellStyle().getDataFormatString());
  1670. assertEquals("GENERAL", r17.getCell(3).getCellStyle().getDataFormatString());
  1671. assertEquals("10", df.formatCellValue(r17.getCell(1)));
  1672. assertEquals("20", df.formatCellValue(r17.getCell(2)));
  1673. assertEquals("20", df.formatCellValue(r17.getCell(3)));
  1674. assertEquals(16.0, r18.getCell(1).getNumericCellValue());
  1675. assertEquals(35.0, r18.getCell(2).getNumericCellValue());
  1676. assertEquals(123.0, r18.getCell(3).getNumericCellValue());
  1677. assertEquals("GENERAL", r18.getCell(1).getCellStyle().getDataFormatString());
  1678. assertEquals("GENERAL", r18.getCell(2).getCellStyle().getDataFormatString());
  1679. assertEquals("GENERAL", r18.getCell(3).getCellStyle().getDataFormatString());
  1680. assertEquals("16", df.formatCellValue(r18.getCell(1)));
  1681. assertEquals("35", df.formatCellValue(r18.getCell(2)));
  1682. assertEquals("123", df.formatCellValue(r18.getCell(3)));
  1683. }
  1684. /**
  1685. * A protected sheet with comments, when written out by
  1686. * POI, ends up upsetting excel.
  1687. * TODO Identify the cause and add extra asserts for
  1688. * the bit excel cares about
  1689. */
  1690. public void test50833() throws Exception {
  1691. HSSFWorkbook wb = openSample("50833.xls");
  1692. HSSFSheet s = wb.getSheetAt(0);
  1693. assertEquals("Sheet1", s.getSheetName());
  1694. assertEquals(false, s.getProtect());
  1695. HSSFCell c = s.getRow(0).getCell(0);
  1696. assertEquals("test cell value", c.getRichStringCellValue().getString());
  1697. HSSFComment cmt = c.getCellComment();
  1698. assertNotNull(cmt);
  1699. assertEquals("Robert Lawrence", cmt.getAuthor());
  1700. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1701. // Reload
  1702. wb = writeOutAndReadBack(wb);
  1703. s = wb.getSheetAt(0);
  1704. c = s.getRow(0).getCell(0);
  1705. // Re-check the comment
  1706. cmt = c.getCellComment();
  1707. assertNotNull(cmt);
  1708. assertEquals("Robert Lawrence", cmt.getAuthor());
  1709. assertEquals("Robert Lawrence:\ntest comment", cmt.getString().getString());
  1710. // TODO Identify what excel doesn't like, and check for that
  1711. }
  1712. public void test50779() throws Exception {
  1713. HSSFWorkbook wb1 = openSample("50779_1.xls");
  1714. writeOutAndReadBack(wb1);
  1715. HSSFWorkbook wb2 = openSample("50779_2.xls");
  1716. writeOutAndReadBack(wb2);
  1717. }
  1718. /**
  1719. * The spec says that ChartEndObjectRecord has 6 reserved
  1720. * bytes on the end, but we sometimes find files without...
  1721. */
  1722. public void test50939() throws Exception {
  1723. HSSFWorkbook wb = openSample("50939.xls");
  1724. assertEquals(2, wb.getNumberOfSheets());
  1725. }
  1726. public void test49219() throws Exception {
  1727. HSSFWorkbook wb = openSample("49219.xls");
  1728. assertEquals(1, wb.getNumberOfSheets());
  1729. assertEquals("DGATE", wb.getSheetAt(0).getRow(1).getCell(0).getStringCellValue());
  1730. }
  1731. public void test48968() throws Exception {
  1732. HSSFWorkbook wb = openSample("48968.xls");
  1733. assertEquals(1, wb.getNumberOfSheets());
  1734. // Check the dates
  1735. HSSFSheet s = wb.getSheetAt(0);
  1736. Date d20110325 = s.getRow(0).getCell(0).getDateCellValue();
  1737. Date d19000102 = s.getRow(11).getCell(0).getDateCellValue();
  1738. Date d19000100 = s.getRow(21).getCell(0).getDateCellValue();
  1739. assertEquals(s.getRow(0).getCell(3).getStringCellValue(), timeToUTC(d20110325));
  1740. assertEquals(s.getRow(11).getCell(3).getStringCellValue(), timeToUTC(d19000102));
  1741. // There is no such thing as 00/01/1900...
  1742. assertEquals("00/01/1900 06:14:24", s.getRow(21).getCell(3).getStringCellValue());
  1743. assertEquals("31/12/1899 06:14:24", timeToUTC(d19000100));
  1744. // Check the cached values
  1745. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1746. assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue());
  1747. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1748. assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue());
  1749. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1750. assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue());
  1751. // Re-evaulate and check
  1752. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  1753. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1754. assertEquals(11.0, s.getRow(5).getCell(0).getNumericCellValue());
  1755. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1756. assertEquals(39.0, s.getRow(6).getCell(0).getNumericCellValue());
  1757. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1758. assertEquals(54.0, s.getRow(7).getCell(0).getNumericCellValue());
  1759. // Push the time forward a bit and check
  1760. double date = s.getRow(0).getCell(0).getNumericCellValue();
  1761. s.getRow(0).getCell(0).setCellValue(date + 1.26);
  1762. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  1763. assertEquals("HOUR(A1)", s.getRow(5).getCell(0).getCellFormula());
  1764. assertEquals(11.0+6.0, s.getRow(5).getCell(0).getNumericCellValue());
  1765. assertEquals("MINUTE(A1)", s.getRow(6).getCell(0).getCellFormula());
  1766. assertEquals(39.0+14.0+1, s.getRow(6).getCell(0).getNumericCellValue());
  1767. assertEquals("SECOND(A1)", s.getRow(7).getCell(0).getCellFormula());
  1768. assertEquals(54.0+24.0-60, s.getRow(7).getCell(0).getNumericCellValue());
  1769. }
  1770. private String timeToUTC(Date d) {
  1771. SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.UK);
  1772. fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
  1773. return fmt.format(d);
  1774. }
  1775. }