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.

InternalWorkbook.java 87KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393
  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.model;
  16. import java.security.AccessControlException;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.LinkedHashMap;
  20. import java.util.List;
  21. import java.util.Locale;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import org.apache.poi.ddf.EscherBSERecord;
  25. import org.apache.poi.ddf.EscherBoolProperty;
  26. import org.apache.poi.ddf.EscherContainerRecord;
  27. import org.apache.poi.ddf.EscherDgRecord;
  28. import org.apache.poi.ddf.EscherDggRecord;
  29. import org.apache.poi.ddf.EscherOptRecord;
  30. import org.apache.poi.ddf.EscherProperties;
  31. import org.apache.poi.ddf.EscherRGBProperty;
  32. import org.apache.poi.ddf.EscherRecord;
  33. import org.apache.poi.ddf.EscherSpRecord;
  34. import org.apache.poi.ddf.EscherSplitMenuColorsRecord;
  35. import org.apache.poi.hssf.record.BOFRecord;
  36. import org.apache.poi.hssf.record.BackupRecord;
  37. import org.apache.poi.hssf.record.BookBoolRecord;
  38. import org.apache.poi.hssf.record.BoundSheetRecord;
  39. import org.apache.poi.hssf.record.CodepageRecord;
  40. import org.apache.poi.hssf.record.CountryRecord;
  41. import org.apache.poi.hssf.record.DSFRecord;
  42. import org.apache.poi.hssf.record.DateWindow1904Record;
  43. import org.apache.poi.hssf.record.DrawingGroupRecord;
  44. import org.apache.poi.hssf.record.EOFRecord;
  45. import org.apache.poi.hssf.record.EscherAggregate;
  46. import org.apache.poi.hssf.record.ExtSSTRecord;
  47. import org.apache.poi.hssf.record.ExtendedFormatRecord;
  48. import org.apache.poi.hssf.record.ExternSheetRecord;
  49. import org.apache.poi.hssf.record.FileSharingRecord;
  50. import org.apache.poi.hssf.record.FnGroupCountRecord;
  51. import org.apache.poi.hssf.record.FontRecord;
  52. import org.apache.poi.hssf.record.FormatRecord;
  53. import org.apache.poi.hssf.record.HideObjRecord;
  54. import org.apache.poi.hssf.record.HyperlinkRecord;
  55. import org.apache.poi.hssf.record.InterfaceEndRecord;
  56. import org.apache.poi.hssf.record.InterfaceHdrRecord;
  57. import org.apache.poi.hssf.record.MMSRecord;
  58. import org.apache.poi.hssf.record.NameCommentRecord;
  59. import org.apache.poi.hssf.record.NameRecord;
  60. import org.apache.poi.hssf.record.PaletteRecord;
  61. import org.apache.poi.hssf.record.PasswordRecord;
  62. import org.apache.poi.hssf.record.PasswordRev4Record;
  63. import org.apache.poi.hssf.record.PrecisionRecord;
  64. import org.apache.poi.hssf.record.ProtectRecord;
  65. import org.apache.poi.hssf.record.ProtectionRev4Record;
  66. import org.apache.poi.hssf.record.RecalcIdRecord;
  67. import org.apache.poi.hssf.record.Record;
  68. import org.apache.poi.hssf.record.RefreshAllRecord;
  69. import org.apache.poi.hssf.record.SSTRecord;
  70. import org.apache.poi.hssf.record.StyleRecord;
  71. import org.apache.poi.hssf.record.SupBookRecord;
  72. import org.apache.poi.hssf.record.TabIdRecord;
  73. import org.apache.poi.hssf.record.UseSelFSRecord;
  74. import org.apache.poi.hssf.record.WindowOneRecord;
  75. import org.apache.poi.hssf.record.WindowProtectRecord;
  76. import org.apache.poi.hssf.record.WriteAccessRecord;
  77. import org.apache.poi.hssf.record.WriteProtectRecord;
  78. import org.apache.poi.hssf.record.common.UnicodeString;
  79. import org.apache.poi.ss.formula.ptg.NameXPtg;
  80. import org.apache.poi.ss.formula.FormulaShifter;
  81. import org.apache.poi.ss.formula.udf.UDFFinder;
  82. import org.apache.poi.ss.formula.ptg.Ptg;
  83. import org.apache.poi.hssf.util.HSSFColor;
  84. import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalName;
  85. import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
  86. import org.apache.poi.ss.usermodel.BuiltinFormats;
  87. import org.apache.poi.util.Internal;
  88. import org.apache.poi.util.POILogFactory;
  89. import org.apache.poi.util.POILogger;
  90. /**
  91. * Low level model implementation of a Workbook. Provides creational methods
  92. * for settings and objects contained in the workbook object.
  93. * <P>
  94. * This file contains the low level binary records starting at the workbook's BOF and
  95. * ending with the workbook's EOF. Use HSSFWorkbook for a high level representation.
  96. * <P>
  97. * The structures of the highlevel API use references to this to perform most of their
  98. * operations. Its probably unwise to use these low level structures directly unless you
  99. * really know what you're doing. I recommend you read the Microsoft Excel 97 Developer's
  100. * Kit (Microsoft Press) and the documentation at http://sc.openoffice.org/excelfileformat.pdf
  101. * before even attempting to use this.
  102. *
  103. *
  104. * @author Luc Girardin (luc dot girardin at macrofocus dot com)
  105. * @author Sergei Kozello (sergeikozello at mail.ru)
  106. * @author Shawn Laubach (slaubach at apache dot org) (Data Formats)
  107. * @author Andrew C. Oliver (acoliver at apache dot org)
  108. * @author Brian Sanders (bsanders at risklabs dot com) - custom palette
  109. * @author Dan Sherman (dsherman at isisph.com)
  110. * @author Glen Stampoultzis (glens at apache.org)
  111. * @see org.apache.poi.hssf.usermodel.HSSFWorkbook
  112. */
  113. @Internal
  114. public final class InternalWorkbook {
  115. /**
  116. * Excel silently truncates long sheet names to 31 chars.
  117. * This constant is used to ensure uniqueness in the first 31 chars
  118. */
  119. private static final int MAX_SENSITIVE_SHEET_NAME_LEN = 31;
  120. private static final POILogger log = POILogFactory.getLogger(InternalWorkbook.class);
  121. private static final int DEBUG = POILogger.DEBUG;
  122. /**
  123. * constant used to set the "codepage" wherever "codepage" is set in records
  124. * (which is duplicated in more than one record)
  125. */
  126. private final static short CODEPAGE = 0x04B0;
  127. /**
  128. * this contains the Worksheet record objects
  129. */
  130. private final WorkbookRecordList records;
  131. /**
  132. * this contains a reference to the SSTRecord so that new stings can be added
  133. * to it.
  134. */
  135. protected SSTRecord sst;
  136. private LinkTable linkTable; // optionally occurs if there are references in the document. (4.10.3)
  137. /**
  138. * holds the "boundsheet" records (aka bundlesheet) so that they can have their
  139. * reference to their "BOF" marker
  140. */
  141. private final List<BoundSheetRecord> boundsheets;
  142. private final List<FormatRecord> formats;
  143. private final List<HyperlinkRecord> hyperlinks;
  144. /** the number of extended format records */
  145. private int numxfs;
  146. /** the number of font records */
  147. private int numfonts;
  148. /** holds the max format id */
  149. private int maxformatid;
  150. /** whether 1904 date windowing is being used */
  151. private boolean uses1904datewindowing;
  152. private DrawingManager2 drawingManager;
  153. private List<EscherBSERecord> escherBSERecords;
  154. private WindowOneRecord windowOne;
  155. private FileSharingRecord fileShare;
  156. private WriteAccessRecord writeAccess;
  157. private WriteProtectRecord writeProtect;
  158. /**
  159. * Hold the {@link NameCommentRecord}s indexed by the name of the {@link NameRecord} to which they apply.
  160. */
  161. private final Map<String, NameCommentRecord> commentRecords;
  162. private InternalWorkbook() {
  163. records = new WorkbookRecordList();
  164. boundsheets = new ArrayList<BoundSheetRecord>();
  165. formats = new ArrayList<FormatRecord>();
  166. hyperlinks = new ArrayList<HyperlinkRecord>();
  167. numxfs = 0;
  168. numfonts = 0;
  169. maxformatid = -1;
  170. uses1904datewindowing = false;
  171. escherBSERecords = new ArrayList<EscherBSERecord>();
  172. commentRecords = new LinkedHashMap<String, NameCommentRecord>();
  173. }
  174. /**
  175. * read support for low level
  176. * API. Pass in an array of Record objects, A Workbook
  177. * object is constructed and passed back with all of its initialization set
  178. * to the passed in records and references to those records held. Unlike Sheet
  179. * workbook does not use an offset (its assumed to be 0) since its first in a file.
  180. * If you need an offset then construct a new array with a 0 offset or write your
  181. * own ;-p.
  182. *
  183. * @param recs an array of Record objects
  184. * @return Workbook object
  185. */
  186. public static InternalWorkbook createWorkbook(List<Record> recs) {
  187. if (log.check( POILogger.DEBUG ))
  188. log.log(DEBUG, "Workbook (readfile) created with reclen=",
  189. Integer.valueOf(recs.size()));
  190. InternalWorkbook retval = new InternalWorkbook();
  191. List<Record> records = new ArrayList<Record>(recs.size() / 3);
  192. retval.records.setRecords(records);
  193. int k;
  194. for (k = 0; k < recs.size(); k++) {
  195. Record rec = recs.get(k);
  196. if (rec.getSid() == EOFRecord.sid) {
  197. records.add(rec);
  198. if (log.check( POILogger.DEBUG ))
  199. log.log(DEBUG, "found workbook eof record at " + k);
  200. break;
  201. }
  202. switch (rec.getSid()) {
  203. case BoundSheetRecord.sid :
  204. if (log.check( POILogger.DEBUG ))
  205. log.log(DEBUG, "found boundsheet record at " + k);
  206. retval.boundsheets.add((BoundSheetRecord) rec);
  207. retval.records.setBspos( k );
  208. break;
  209. case SSTRecord.sid :
  210. if (log.check( POILogger.DEBUG ))
  211. log.log(DEBUG, "found sst record at " + k);
  212. retval.sst = ( SSTRecord ) rec;
  213. break;
  214. case FontRecord.sid :
  215. if (log.check( POILogger.DEBUG ))
  216. log.log(DEBUG, "found font record at " + k);
  217. retval.records.setFontpos( k );
  218. retval.numfonts++;
  219. break;
  220. case ExtendedFormatRecord.sid :
  221. if (log.check( POILogger.DEBUG ))
  222. log.log(DEBUG, "found XF record at " + k);
  223. retval.records.setXfpos( k );
  224. retval.numxfs++;
  225. break;
  226. case TabIdRecord.sid :
  227. if (log.check( POILogger.DEBUG ))
  228. log.log(DEBUG, "found tabid record at " + k);
  229. retval.records.setTabpos( k );
  230. break;
  231. case ProtectRecord.sid :
  232. if (log.check( POILogger.DEBUG ))
  233. log.log(DEBUG, "found protect record at " + k);
  234. retval.records.setProtpos( k );
  235. break;
  236. case BackupRecord.sid :
  237. if (log.check( POILogger.DEBUG ))
  238. log.log(DEBUG, "found backup record at " + k);
  239. retval.records.setBackuppos( k );
  240. break;
  241. case ExternSheetRecord.sid :
  242. throw new RuntimeException("Extern sheet is part of LinkTable");
  243. case NameRecord.sid :
  244. case SupBookRecord.sid :
  245. // LinkTable can start with either of these
  246. if (log.check( POILogger.DEBUG ))
  247. log.log(DEBUG, "found SupBook record at " + k);
  248. retval.linkTable = new LinkTable(recs, k, retval.records, retval.commentRecords);
  249. k+=retval.linkTable.getRecordCount() - 1;
  250. continue;
  251. case FormatRecord.sid :
  252. if (log.check( POILogger.DEBUG ))
  253. log.log(DEBUG, "found format record at " + k);
  254. retval.formats.add((FormatRecord) rec);
  255. retval.maxformatid = retval.maxformatid >= ((FormatRecord)rec).getIndexCode() ? retval.maxformatid : ((FormatRecord)rec).getIndexCode();
  256. break;
  257. case DateWindow1904Record.sid :
  258. if (log.check( POILogger.DEBUG ))
  259. log.log(DEBUG, "found datewindow1904 record at " + k);
  260. retval.uses1904datewindowing = ((DateWindow1904Record)rec).getWindowing() == 1;
  261. break;
  262. case PaletteRecord.sid:
  263. if (log.check( POILogger.DEBUG ))
  264. log.log(DEBUG, "found palette record at " + k);
  265. retval.records.setPalettepos( k );
  266. break;
  267. case WindowOneRecord.sid:
  268. if (log.check( POILogger.DEBUG ))
  269. log.log(DEBUG, "found WindowOneRecord at " + k);
  270. retval.windowOne = (WindowOneRecord) rec;
  271. break;
  272. case WriteAccessRecord.sid:
  273. if (log.check( POILogger.DEBUG ))
  274. log.log(DEBUG, "found WriteAccess at " + k);
  275. retval.writeAccess = (WriteAccessRecord) rec;
  276. break;
  277. case WriteProtectRecord.sid:
  278. if (log.check( POILogger.DEBUG ))
  279. log.log(DEBUG, "found WriteProtect at " + k);
  280. retval.writeProtect = (WriteProtectRecord) rec;
  281. break;
  282. case FileSharingRecord.sid:
  283. if (log.check( POILogger.DEBUG ))
  284. log.log(DEBUG, "found FileSharing at " + k);
  285. retval.fileShare = (FileSharingRecord) rec;
  286. break;
  287. case NameCommentRecord.sid:
  288. final NameCommentRecord ncr = (NameCommentRecord) rec;
  289. if (log.check( POILogger.DEBUG ))
  290. log.log(DEBUG, "found NameComment at " + k);
  291. retval.commentRecords.put(ncr.getNameText(), ncr);
  292. default :
  293. }
  294. records.add(rec);
  295. }
  296. //What if we dont have any ranges and supbooks
  297. // if (retval.records.supbookpos == 0) {
  298. // retval.records.supbookpos = retval.records.bspos + 1;
  299. // retval.records.namepos = retval.records.supbookpos + 1;
  300. // }
  301. // Look for other interesting values that
  302. // follow the EOFRecord
  303. for ( ; k < recs.size(); k++) {
  304. Record rec = recs.get(k);
  305. switch (rec.getSid()) {
  306. case HyperlinkRecord.sid:
  307. retval.hyperlinks.add((HyperlinkRecord)rec);
  308. break;
  309. }
  310. }
  311. if (retval.windowOne == null) {
  312. retval.windowOne = createWindowOne();
  313. }
  314. if (log.check( POILogger.DEBUG ))
  315. log.log(DEBUG, "exit create workbook from existing file function");
  316. return retval;
  317. }
  318. /**
  319. * Creates an empty workbook object with three blank sheets and all the empty
  320. * fields. Use this to create a workbook from scratch.
  321. */
  322. public static InternalWorkbook createWorkbook()
  323. {
  324. if (log.check( POILogger.DEBUG ))
  325. log.log( DEBUG, "creating new workbook from scratch" );
  326. InternalWorkbook retval = new InternalWorkbook();
  327. List<Record> records = new ArrayList<Record>( 30 );
  328. retval.records.setRecords(records);
  329. List<FormatRecord> formats = retval.formats;
  330. records.add(createBOF());
  331. records.add(new InterfaceHdrRecord(CODEPAGE));
  332. records.add(createMMS());
  333. records.add(InterfaceEndRecord.instance);
  334. records.add(createWriteAccess());
  335. records.add(createCodepage());
  336. records.add(createDSF());
  337. records.add(createTabId());
  338. retval.records.setTabpos(records.size() - 1);
  339. records.add(createFnGroupCount());
  340. records.add(createWindowProtect());
  341. records.add(createProtect());
  342. retval.records.setProtpos(records.size() - 1);
  343. records.add(createPassword());
  344. records.add(createProtectionRev4());
  345. records.add(createPasswordRev4());
  346. retval.windowOne = createWindowOne();
  347. records.add(retval.windowOne);
  348. records.add(createBackup());
  349. retval.records.setBackuppos(records.size() - 1);
  350. records.add(createHideObj());
  351. records.add(createDateWindow1904());
  352. records.add(createPrecision());
  353. records.add(createRefreshAll());
  354. records.add(createBookBool());
  355. records.add(createFont());
  356. records.add(createFont());
  357. records.add(createFont());
  358. records.add(createFont());
  359. retval.records.setFontpos( records.size() - 1 ); // last font record position
  360. retval.numfonts = 4;
  361. // set up format records
  362. for (int i = 0; i <= 7; i++) {
  363. FormatRecord rec = createFormat(i);
  364. retval.maxformatid = retval.maxformatid >= rec.getIndexCode() ? retval.maxformatid : rec.getIndexCode();
  365. formats.add(rec);
  366. records.add(rec);
  367. }
  368. for (int k = 0; k < 21; k++) {
  369. records.add(retval.createExtendedFormat(k));
  370. retval.numxfs++;
  371. }
  372. retval.records.setXfpos( records.size() - 1 );
  373. for (int k = 0; k < 6; k++) {
  374. records.add(retval.createStyle(k));
  375. }
  376. records.add(retval.createUseSelFS());
  377. int nBoundSheets = 1; // now just do 1
  378. for (int k = 0; k < nBoundSheets; k++) {
  379. BoundSheetRecord bsr = createBoundSheet(k);
  380. records.add(bsr);
  381. retval.boundsheets.add(bsr);
  382. retval.records.setBspos(records.size() - 1);
  383. }
  384. records.add( retval.createCountry() );
  385. for ( int k = 0; k < nBoundSheets; k++ ) {
  386. retval.getOrCreateLinkTable().checkExternSheet(k);
  387. }
  388. retval.sst = new SSTRecord();
  389. records.add(retval.sst);
  390. records.add(retval.createExtendedSST());
  391. records.add(EOFRecord.instance);
  392. if (log.check( POILogger.DEBUG ))
  393. log.log( DEBUG, "exit create new workbook from scratch" );
  394. return retval;
  395. }
  396. /**Retrieves the Builtin NameRecord that matches the name and index
  397. * There shouldn't be too many names to make the sequential search too slow
  398. * @param name byte representation of the builtin name to match
  399. * @param sheetNumber 1-based sheet number
  400. * @return null if no builtin NameRecord matches
  401. */
  402. public NameRecord getSpecificBuiltinRecord(byte name, int sheetNumber)
  403. {
  404. return getOrCreateLinkTable().getSpecificBuiltinRecord(name, sheetNumber);
  405. }
  406. /**
  407. * Removes the specified Builtin NameRecord that matches the name and index
  408. * @param name byte representation of the builtin to match
  409. * @param sheetIndex zero-based sheet reference
  410. */
  411. public void removeBuiltinRecord(byte name, int sheetIndex) {
  412. linkTable.removeBuiltinRecord(name, sheetIndex);
  413. // TODO - do we need "this.records.remove(...);" similar to that in this.removeName(int namenum) {}?
  414. }
  415. public int getNumRecords() {
  416. return records.size();
  417. }
  418. /**
  419. * gets the font record at the given index in the font table. Remember
  420. * "There is No Four" (someone at M$ must have gone to Rocky Horror one too
  421. * many times)
  422. *
  423. * @param idx the index to look at (0 or greater but NOT 4)
  424. * @return FontRecord located at the given index
  425. */
  426. public FontRecord getFontRecordAt(int idx) {
  427. int index = idx;
  428. if (index > 4) {
  429. index -= 1; // adjust for "There is no 4"
  430. }
  431. if (index > (numfonts - 1)) {
  432. throw new ArrayIndexOutOfBoundsException(
  433. "There are only " + numfonts
  434. + " font records, you asked for " + idx);
  435. }
  436. FontRecord retval =
  437. ( FontRecord ) records.get((records.getFontpos() - (numfonts - 1)) + index);
  438. return retval;
  439. }
  440. /**
  441. * Retrieves the index of the given font
  442. */
  443. public int getFontIndex(FontRecord font) {
  444. for(int i=0; i<=numfonts; i++) {
  445. FontRecord thisFont =
  446. ( FontRecord ) records.get((records.getFontpos() - (numfonts - 1)) + i);
  447. if(thisFont == font) {
  448. // There is no 4!
  449. if(i > 3) {
  450. return (i+1);
  451. }
  452. return i;
  453. }
  454. }
  455. throw new IllegalArgumentException("Could not find that font!");
  456. }
  457. /**
  458. * creates a new font record and adds it to the "font table". This causes the
  459. * boundsheets to move down one, extended formats to move down (so this function moves
  460. * those pointers as well)
  461. *
  462. * @return FontRecord that was just created
  463. */
  464. public FontRecord createNewFont() {
  465. FontRecord rec = createFont();
  466. records.add(records.getFontpos()+1, rec);
  467. records.setFontpos( records.getFontpos() + 1 );
  468. numfonts++;
  469. return rec;
  470. }
  471. /**
  472. * Removes the given font record from the
  473. * file's list. This will make all
  474. * subsequent font indicies drop by one,
  475. * so you'll need to update those yourself!
  476. */
  477. public void removeFontRecord(FontRecord rec) {
  478. records.remove(rec); // this updates FontPos for us
  479. numfonts--;
  480. }
  481. /**
  482. * gets the number of font records
  483. *
  484. * @return number of font records in the "font table"
  485. */
  486. public int getNumberOfFontRecords() {
  487. return numfonts;
  488. }
  489. /**
  490. * Sets the BOF for a given sheet
  491. *
  492. * @param sheetIndex the number of the sheet to set the positing of the bof for
  493. * @param pos the actual bof position
  494. */
  495. public void setSheetBof(int sheetIndex, int pos) {
  496. if (log.check( POILogger.DEBUG ))
  497. log.log(DEBUG, "setting bof for sheetnum =", Integer.valueOf(sheetIndex),
  498. " at pos=", Integer.valueOf(pos));
  499. checkSheets(sheetIndex);
  500. getBoundSheetRec(sheetIndex)
  501. .setPositionOfBof(pos);
  502. }
  503. private BoundSheetRecord getBoundSheetRec(int sheetIndex) {
  504. return boundsheets.get(sheetIndex);
  505. }
  506. /**
  507. * Returns the position of the backup record.
  508. */
  509. public BackupRecord getBackupRecord() {
  510. return ( BackupRecord ) records.get(records.getBackuppos());
  511. }
  512. /**
  513. * sets the name for a given sheet. If the boundsheet record doesn't exist and
  514. * its only one more than we have, go ahead and create it. If it's > 1 more than
  515. * we have, except
  516. *
  517. * @param sheetnum the sheet number (0 based)
  518. * @param sheetname the name for the sheet
  519. */
  520. public void setSheetName(int sheetnum, String sheetname) {
  521. checkSheets(sheetnum);
  522. BoundSheetRecord sheet = boundsheets.get(sheetnum);
  523. sheet.setSheetname(sheetname);
  524. }
  525. /**
  526. * Determines whether a workbook contains the provided sheet name. For the purpose of
  527. * comparison, long names are truncated to 31 chars.
  528. *
  529. * @param name the name to test (case insensitive match)
  530. * @param excludeSheetIdx the sheet to exclude from the check or -1 to include all sheets in the check.
  531. * @return true if the sheet contains the name, false otherwise.
  532. */
  533. public boolean doesContainsSheetName(String name, int excludeSheetIdx) {
  534. String aName = name;
  535. if (aName.length() > MAX_SENSITIVE_SHEET_NAME_LEN) {
  536. aName = aName.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN);
  537. }
  538. for (int i = 0; i < boundsheets.size(); i++) {
  539. BoundSheetRecord boundSheetRecord = getBoundSheetRec(i);
  540. if (excludeSheetIdx == i) {
  541. continue;
  542. }
  543. String bName = boundSheetRecord.getSheetname();
  544. if (bName.length() > MAX_SENSITIVE_SHEET_NAME_LEN) {
  545. bName = bName.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN);
  546. }
  547. if (aName.equalsIgnoreCase(bName)) {
  548. return true;
  549. }
  550. }
  551. return false;
  552. }
  553. /**
  554. * sets the order of appearance for a given sheet.
  555. *
  556. * @param sheetname the name of the sheet to reorder
  557. * @param pos the position that we want to insert the sheet into (0 based)
  558. */
  559. public void setSheetOrder(String sheetname, int pos ) {
  560. int sheetNumber = getSheetIndex(sheetname);
  561. //remove the sheet that needs to be reordered and place it in the spot we want
  562. boundsheets.add(pos, boundsheets.remove(sheetNumber));
  563. }
  564. /**
  565. * gets the name for a given sheet.
  566. *
  567. * @param sheetIndex the sheet number (0 based)
  568. * @return sheetname the name for the sheet
  569. */
  570. public String getSheetName(int sheetIndex) {
  571. return getBoundSheetRec(sheetIndex).getSheetname();
  572. }
  573. /**
  574. * Gets the hidden flag for a given sheet.
  575. * Note that a sheet could instead be
  576. * set to be very hidden, which is different
  577. * ({@link #isSheetVeryHidden(int)})
  578. *
  579. * @param sheetnum the sheet number (0 based)
  580. * @return True if sheet is hidden
  581. */
  582. public boolean isSheetHidden(int sheetnum) {
  583. return getBoundSheetRec(sheetnum).isHidden();
  584. }
  585. /**
  586. * Gets the very hidden flag for a given sheet.
  587. * This is different from the normal
  588. * hidden flag
  589. * ({@link #isSheetHidden(int)})
  590. *
  591. * @param sheetnum the sheet number (0 based)
  592. * @return True if sheet is very hidden
  593. */
  594. public boolean isSheetVeryHidden(int sheetnum) {
  595. return getBoundSheetRec(sheetnum).isVeryHidden();
  596. }
  597. /**
  598. * Hide or unhide a sheet
  599. *
  600. * @param sheetnum The sheet number
  601. * @param hidden True to mark the sheet as hidden, false otherwise
  602. */
  603. public void setSheetHidden(int sheetnum, boolean hidden) {
  604. getBoundSheetRec(sheetnum).setHidden(hidden);
  605. }
  606. /**
  607. * Hide or unhide a sheet.
  608. * 0 = not hidden
  609. * 1 = hidden
  610. * 2 = very hidden.
  611. *
  612. * @param sheetnum The sheet number
  613. * @param hidden 0 for not hidden, 1 for hidden, 2 for very hidden
  614. */
  615. public void setSheetHidden(int sheetnum, int hidden) {
  616. BoundSheetRecord bsr = getBoundSheetRec(sheetnum);
  617. boolean h = false;
  618. boolean vh = false;
  619. if(hidden == 0) {
  620. } else if(hidden == 1) {
  621. h = true;
  622. } else if(hidden == 2) {
  623. vh = true;
  624. } else {
  625. throw new IllegalArgumentException("Invalid hidden flag " + hidden + " given, must be 0, 1 or 2");
  626. }
  627. bsr.setHidden(h);
  628. bsr.setVeryHidden(vh);
  629. }
  630. /**
  631. * get the sheet's index
  632. * @param name sheet name
  633. * @return sheet index or -1 if it was not found.
  634. */
  635. public int getSheetIndex(String name) {
  636. int retval = -1;
  637. for (int k = 0; k < boundsheets.size(); k++) {
  638. String sheet = getSheetName(k);
  639. if (sheet.equalsIgnoreCase(name)) {
  640. retval = k;
  641. break;
  642. }
  643. }
  644. return retval;
  645. }
  646. /**
  647. * if we're trying to address one more sheet than we have, go ahead and add it! if we're
  648. * trying to address >1 more than we have throw an exception!
  649. */
  650. private void checkSheets(int sheetnum) {
  651. if ((boundsheets.size()) <= sheetnum) { // if we're short one add another..
  652. if ((boundsheets.size() + 1) <= sheetnum) {
  653. throw new RuntimeException("Sheet number out of bounds!");
  654. }
  655. BoundSheetRecord bsr = createBoundSheet(sheetnum);
  656. records.add(records.getBspos()+1, bsr);
  657. records.setBspos( records.getBspos() + 1 );
  658. boundsheets.add(bsr);
  659. getOrCreateLinkTable().checkExternSheet(sheetnum);
  660. fixTabIdRecord();
  661. } else {
  662. // Ensure we have enough tab IDs
  663. // Can be a few short if new sheets were added
  664. if(records.getTabpos() > 0) {
  665. TabIdRecord tir = ( TabIdRecord ) records.get(records.getTabpos());
  666. if(tir._tabids.length < boundsheets.size()) {
  667. fixTabIdRecord();
  668. }
  669. }
  670. }
  671. }
  672. /**
  673. * @param sheetIndex zero based sheet index
  674. */
  675. public void removeSheet(int sheetIndex) {
  676. if (boundsheets.size() > sheetIndex) {
  677. records.remove(records.getBspos() - (boundsheets.size() - 1) + sheetIndex);
  678. boundsheets.remove(sheetIndex);
  679. fixTabIdRecord();
  680. }
  681. // Within NameRecords, it's ok to have the formula
  682. // part point at deleted sheets. It's also ok to
  683. // have the ExternSheetNumber point at deleted
  684. // sheets.
  685. // However, the sheet index must be adjusted, or
  686. // excel will break. (Sheet index is either 0 for
  687. // global, or 1 based index to sheet)
  688. int sheetNum1Based = sheetIndex + 1;
  689. for(int i=0; i<getNumNames(); i++) {
  690. NameRecord nr = getNameRecord(i);
  691. if(nr.getSheetNumber() == sheetNum1Based) {
  692. // Excel re-writes these to point to no sheet
  693. nr.setSheetNumber(0);
  694. } else if(nr.getSheetNumber() > sheetNum1Based) {
  695. // Bump down by one, so still points
  696. // at the same sheet
  697. nr.setSheetNumber(nr.getSheetNumber()-1);
  698. }
  699. }
  700. }
  701. /**
  702. * make the tabid record look like the current situation.
  703. *
  704. */
  705. private void fixTabIdRecord() {
  706. TabIdRecord tir = ( TabIdRecord ) records.get(records.getTabpos());
  707. short[] tia = new short[ boundsheets.size() ];
  708. for (short k = 0; k < tia.length; k++) {
  709. tia[ k ] = k;
  710. }
  711. tir.setTabIdArray(tia);
  712. }
  713. /**
  714. * returns the number of boundsheet objects contained in this workbook.
  715. *
  716. * @return number of BoundSheet records
  717. */
  718. public int getNumSheets() {
  719. if (log.check( POILogger.DEBUG ))
  720. log.log(DEBUG, "getNumSheets=", Integer.valueOf(boundsheets.size()));
  721. return boundsheets.size();
  722. }
  723. /**
  724. * get the number of ExtendedFormat records contained in this workbook.
  725. *
  726. * @return int count of ExtendedFormat records
  727. */
  728. public int getNumExFormats() {
  729. if (log.check( POILogger.DEBUG ))
  730. log.log(DEBUG, "getXF=", Integer.valueOf(numxfs));
  731. return numxfs;
  732. }
  733. /**
  734. * gets the ExtendedFormatRecord at the given 0-based index
  735. *
  736. * @param index of the Extended format record (0-based)
  737. * @return ExtendedFormatRecord at the given index
  738. */
  739. public ExtendedFormatRecord getExFormatAt(int index) {
  740. int xfptr = records.getXfpos() - (numxfs - 1);
  741. xfptr += index;
  742. ExtendedFormatRecord retval =
  743. ( ExtendedFormatRecord ) records.get(xfptr);
  744. return retval;
  745. }
  746. /**
  747. * Removes the given ExtendedFormatRecord record from the
  748. * file's list. This will make all
  749. * subsequent font indicies drop by one,
  750. * so you'll need to update those yourself!
  751. */
  752. public void removeExFormatRecord(ExtendedFormatRecord rec) {
  753. records.remove(rec); // this updates XfPos for us
  754. numxfs--;
  755. }
  756. /**
  757. * creates a new Cell-type Extneded Format Record and adds it to the end of
  758. * ExtendedFormatRecords collection
  759. *
  760. * @return ExtendedFormatRecord that was created
  761. */
  762. public ExtendedFormatRecord createCellXF() {
  763. ExtendedFormatRecord xf = createExtendedFormat();
  764. records.add(records.getXfpos()+1, xf);
  765. records.setXfpos( records.getXfpos() + 1 );
  766. numxfs++;
  767. return xf;
  768. }
  769. /**
  770. * Returns the StyleRecord for the given
  771. * xfIndex, or null if that ExtendedFormat doesn't
  772. * have a Style set.
  773. */
  774. public StyleRecord getStyleRecord(int xfIndex) {
  775. // Style records always follow after
  776. // the ExtendedFormat records
  777. for(int i=records.getXfpos(); i<records.size(); i++) {
  778. Record r = records.get(i);
  779. if(r instanceof ExtendedFormatRecord) {
  780. continue;
  781. }
  782. if(!(r instanceof StyleRecord)) {
  783. continue;
  784. }
  785. StyleRecord sr = (StyleRecord)r;
  786. if(sr.getXFIndex() == xfIndex) {
  787. return sr;
  788. }
  789. }
  790. return null;
  791. }
  792. /**
  793. * Creates a new StyleRecord, for the given Extended
  794. * Format index, and adds it onto the end of the
  795. * records collection
  796. */
  797. public StyleRecord createStyleRecord(int xfIndex) {
  798. // Style records always follow after
  799. // the ExtendedFormat records
  800. StyleRecord newSR = new StyleRecord();
  801. newSR.setXFIndex(xfIndex);
  802. // Find the spot
  803. int addAt = -1;
  804. for(int i=records.getXfpos(); i<records.size() &&
  805. addAt == -1; i++) {
  806. Record r = records.get(i);
  807. if(r instanceof ExtendedFormatRecord ||
  808. r instanceof StyleRecord) {
  809. // Keep going
  810. } else {
  811. addAt = i;
  812. }
  813. }
  814. if(addAt == -1) {
  815. throw new IllegalStateException("No XF Records found!");
  816. }
  817. records.add(addAt, newSR);
  818. return newSR;
  819. }
  820. /**
  821. * Adds a string to the SST table and returns its index (if its a duplicate
  822. * just returns its index and update the counts) ASSUMES compressed unicode
  823. * (meaning 8bit)
  824. *
  825. * @param string the string to be added to the SSTRecord
  826. *
  827. * @return index of the string within the SSTRecord
  828. */
  829. public int addSSTString(UnicodeString string) {
  830. if (log.check( POILogger.DEBUG ))
  831. log.log(DEBUG, "insert to sst string='", string);
  832. if (sst == null) {
  833. insertSST();
  834. }
  835. return sst.addString(string);
  836. }
  837. /**
  838. * given an index into the SST table, this function returns the corresponding String value
  839. * @return String containing the SST String
  840. */
  841. public UnicodeString getSSTString(int str) {
  842. if (sst == null) {
  843. insertSST();
  844. }
  845. UnicodeString retval = sst.getString(str);
  846. if (log.check( POILogger.DEBUG ))
  847. log.log(DEBUG, "Returning SST for index=", Integer.valueOf(str),
  848. " String= ", retval);
  849. return retval;
  850. }
  851. /**
  852. * use this function to add a Shared String Table to an existing sheet (say
  853. * generated by a different java api) without an sst....
  854. * @see #createExtendedSST()
  855. * @see org.apache.poi.hssf.record.SSTRecord
  856. */
  857. public void insertSST() {
  858. if (log.check( POILogger.DEBUG ))
  859. log.log(DEBUG, "creating new SST via insertSST!");
  860. sst = new SSTRecord();
  861. records.add(records.size() - 1, createExtendedSST());
  862. records.add(records.size() - 2, sst);
  863. }
  864. /**
  865. * Serializes all records int the worksheet section into a big byte array. Use
  866. * this to write the Workbook out.
  867. *
  868. * @return byte array containing the HSSF-only portions of the POIFS file.
  869. */
  870. // GJS: Not used so why keep it.
  871. // public byte [] serialize() {
  872. // log.log(DEBUG, "Serializing Workbook!");
  873. // byte[] retval = null;
  874. //
  875. //// ArrayList bytes = new ArrayList(records.size());
  876. // int arraysize = getSize();
  877. // int pos = 0;
  878. //
  879. // retval = new byte[ arraysize ];
  880. // for (int k = 0; k < records.size(); k++) {
  881. //
  882. // Record record = records.get(k);
  883. //// Let's skip RECALCID records, as they are only use for optimization
  884. // if(record.getSid() != RecalcIdRecord.sid || ((RecalcIdRecord)record).isNeeded()) {
  885. // pos += record.serialize(pos, retval); // rec.length;
  886. // }
  887. // }
  888. // log.log(DEBUG, "Exiting serialize workbook");
  889. // return retval;
  890. // }
  891. /**
  892. * Serializes all records int the worksheet section into a big byte array. Use
  893. * this to write the Workbook out.
  894. * @param offset of the data to be written
  895. * @param data array of bytes to write this to
  896. */
  897. public int serialize( int offset, byte[] data )
  898. {
  899. if (log.check( POILogger.DEBUG ))
  900. log.log( DEBUG, "Serializing Workbook with offsets" );
  901. int pos = 0;
  902. SSTRecord sst = null;
  903. int sstPos = 0;
  904. boolean wroteBoundSheets = false;
  905. for ( int k = 0; k < records.size(); k++ )
  906. {
  907. Record record = records.get( k );
  908. // Let's skip RECALCID records, as they are only use for optimization
  909. if ( record.getSid() != RecalcIdRecord.sid || ( (RecalcIdRecord) record ).isNeeded() )
  910. {
  911. int len = 0;
  912. if (record instanceof SSTRecord)
  913. {
  914. sst = (SSTRecord)record;
  915. sstPos = pos;
  916. }
  917. if (record.getSid() == ExtSSTRecord.sid && sst != null)
  918. {
  919. record = sst.createExtSSTRecord(sstPos + offset);
  920. }
  921. if (record instanceof BoundSheetRecord) {
  922. if(!wroteBoundSheets) {
  923. for (int i = 0; i < boundsheets.size(); i++) {
  924. len+= getBoundSheetRec(i)
  925. .serialize(pos+offset+len, data);
  926. }
  927. wroteBoundSheets = true;
  928. }
  929. } else {
  930. len = record.serialize( pos + offset, data );
  931. }
  932. ///// DEBUG BEGIN /////
  933. // if (len != record.getRecordSize())
  934. // throw new IllegalStateException("Record size does not match serialized bytes. Serialized size = " + len + " but getRecordSize() returns " + record.getRecordSize());
  935. ///// DEBUG END /////
  936. pos += len; // rec.length;
  937. }
  938. }
  939. if (log.check( POILogger.DEBUG ))
  940. log.log( DEBUG, "Exiting serialize workbook" );
  941. return pos;
  942. }
  943. public int getSize()
  944. {
  945. int retval = 0;
  946. SSTRecord sst = null;
  947. for ( int k = 0; k < records.size(); k++ )
  948. {
  949. Record record = records.get( k );
  950. // Let's skip RECALCID records, as they are only use for optimization
  951. if ( record.getSid() != RecalcIdRecord.sid || ( (RecalcIdRecord) record ).isNeeded() )
  952. {
  953. if (record instanceof SSTRecord)
  954. sst = (SSTRecord)record;
  955. if (record.getSid() == ExtSSTRecord.sid && sst != null)
  956. retval += sst.calcExtSSTRecordSize();
  957. else
  958. retval += record.getRecordSize();
  959. }
  960. }
  961. return retval;
  962. }
  963. private static BOFRecord createBOF() {
  964. BOFRecord retval = new BOFRecord();
  965. retval.setVersion(( short ) 0x600);
  966. retval.setType(BOFRecord.TYPE_WORKBOOK);
  967. retval.setBuild(( short ) 0x10d3);
  968. retval.setBuildYear(( short ) 1996);
  969. retval.setHistoryBitMask(0x41); // was c1 before verify
  970. retval.setRequiredVersion(0x6);
  971. return retval;
  972. }
  973. private static MMSRecord createMMS() {
  974. MMSRecord retval = new MMSRecord();
  975. retval.setAddMenuCount(( byte ) 0);
  976. retval.setDelMenuCount(( byte ) 0);
  977. return retval;
  978. }
  979. /**
  980. * creates the WriteAccess record containing the logged in user's name
  981. */
  982. private static WriteAccessRecord createWriteAccess() {
  983. WriteAccessRecord retval = new WriteAccessRecord();
  984. try {
  985. retval.setUsername(System.getProperty("user.name"));
  986. } catch (AccessControlException e) {
  987. // AccessControlException can occur in a restricted context
  988. // (client applet/jws application or restricted security server)
  989. retval.setUsername("POI");
  990. }
  991. return retval;
  992. }
  993. private static CodepageRecord createCodepage() {
  994. CodepageRecord retval = new CodepageRecord();
  995. retval.setCodepage(CODEPAGE);
  996. return retval;
  997. }
  998. private static DSFRecord createDSF() {
  999. return new DSFRecord(false); // we don't even support double stream files
  1000. }
  1001. /**
  1002. * creates the TabId record containing an array
  1003. */
  1004. private static TabIdRecord createTabId() {
  1005. return new TabIdRecord();
  1006. }
  1007. /**
  1008. * creates the FnGroupCount record containing the Magic number constant of 14.
  1009. */
  1010. private static FnGroupCountRecord createFnGroupCount() {
  1011. FnGroupCountRecord retval = new FnGroupCountRecord();
  1012. retval.setCount(( short ) 14);
  1013. return retval;
  1014. }
  1015. /**
  1016. * @return a new WindowProtect record with protect set to false.
  1017. */
  1018. private static WindowProtectRecord createWindowProtect() {
  1019. // by default even when we support it we won't
  1020. // want it to be protected
  1021. return new WindowProtectRecord(false);
  1022. }
  1023. /**
  1024. * @return a new Protect record with protect set to false.
  1025. */
  1026. private static ProtectRecord createProtect() {
  1027. // by default even when we support it we won't
  1028. // want it to be protected
  1029. return new ProtectRecord(false);
  1030. }
  1031. /**
  1032. * @return a new Password record with password set to 0x0000 (no password).
  1033. */
  1034. private static PasswordRecord createPassword() {
  1035. return new PasswordRecord(0x0000); // no password by default!
  1036. }
  1037. /**
  1038. * @return a new ProtectionRev4 record with protect set to false.
  1039. */
  1040. private static ProtectionRev4Record createProtectionRev4() {
  1041. return new ProtectionRev4Record(false);
  1042. }
  1043. /**
  1044. * @return a new PasswordRev4 record with password set to 0.
  1045. */
  1046. private static PasswordRev4Record createPasswordRev4() {
  1047. return new PasswordRev4Record(0x0000);
  1048. }
  1049. /**
  1050. * creates the WindowOne record with the following magic values: <P>
  1051. * horizontal hold - 0x168 <P>
  1052. * vertical hold - 0x10e <P>
  1053. * width - 0x3a5c <P>
  1054. * height - 0x23be <P>
  1055. * options - 0x38 <P>
  1056. * selected tab - 0 <P>
  1057. * displayed tab - 0 <P>
  1058. * num selected tab- 0 <P>
  1059. * tab width ratio - 0x258 <P>
  1060. */
  1061. private static WindowOneRecord createWindowOne() {
  1062. WindowOneRecord retval = new WindowOneRecord();
  1063. retval.setHorizontalHold(( short ) 0x168);
  1064. retval.setVerticalHold(( short ) 0x10e);
  1065. retval.setWidth(( short ) 0x3a5c);
  1066. retval.setHeight(( short ) 0x23be);
  1067. retval.setOptions(( short ) 0x38);
  1068. retval.setActiveSheetIndex( 0x0);
  1069. retval.setFirstVisibleTab(0x0);
  1070. retval.setNumSelectedTabs(( short ) 1);
  1071. retval.setTabWidthRatio(( short ) 0x258);
  1072. return retval;
  1073. }
  1074. /**
  1075. * creates the Backup record with backup set to 0. (loose the data, who cares)
  1076. */
  1077. private static BackupRecord createBackup() {
  1078. BackupRecord retval = new BackupRecord();
  1079. retval.setBackup(( short ) 0); // by default DONT save backups of files...just loose data
  1080. return retval;
  1081. }
  1082. /**
  1083. * creates the HideObj record with hide object set to 0. (don't hide)
  1084. */
  1085. private static HideObjRecord createHideObj() {
  1086. HideObjRecord retval = new HideObjRecord();
  1087. retval.setHideObj(( short ) 0); // by default set hide object off
  1088. return retval;
  1089. }
  1090. /**
  1091. * creates the DateWindow1904 record with windowing set to 0. (don't window)
  1092. */
  1093. private static DateWindow1904Record createDateWindow1904() {
  1094. DateWindow1904Record retval = new DateWindow1904Record();
  1095. retval.setWindowing(( short ) 0); // don't EVER use 1904 date windowing...tick tock..
  1096. return retval;
  1097. }
  1098. /**
  1099. * creates the Precision record with precision set to true. (full precision)
  1100. */
  1101. private static PrecisionRecord createPrecision() {
  1102. PrecisionRecord retval = new PrecisionRecord();
  1103. retval.setFullPrecision(true); // always use real numbers in calculations!
  1104. return retval;
  1105. }
  1106. /**
  1107. * @return a new RefreshAll record with refreshAll set to false. (do not refresh all calcs)
  1108. */
  1109. private static RefreshAllRecord createRefreshAll() {
  1110. return new RefreshAllRecord(false);
  1111. }
  1112. /**
  1113. * creates the BookBool record with saveLinkValues set to 0. (don't save link values)
  1114. */
  1115. private static BookBoolRecord createBookBool() {
  1116. BookBoolRecord retval = new BookBoolRecord();
  1117. retval.setSaveLinkValues(( short ) 0);
  1118. return retval;
  1119. }
  1120. /**
  1121. * creates a Font record with the following magic values: <P>
  1122. * fontheight = 0xc8<P>
  1123. * attributes = 0x0<P>
  1124. * color palette index = 0x7fff<P>
  1125. * bold weight = 0x190<P>
  1126. * Font Name Length = 5 <P>
  1127. * Font Name = Arial <P>
  1128. */
  1129. private static FontRecord createFont() {
  1130. FontRecord retval = new FontRecord();
  1131. retval.setFontHeight(( short ) 0xc8);
  1132. retval.setAttributes(( short ) 0x0);
  1133. retval.setColorPaletteIndex(( short ) 0x7fff);
  1134. retval.setBoldWeight(( short ) 0x190);
  1135. retval.setFontName("Arial");
  1136. return retval;
  1137. }
  1138. /**
  1139. * Creates a FormatRecord object
  1140. * @param id the number of the format record to create (meaning its position in
  1141. * a file as M$ Excel would create it.)
  1142. */
  1143. private static FormatRecord createFormat(int id) {
  1144. // we'll need multiple editions for
  1145. // the different formats
  1146. switch (id) {
  1147. case 0: return new FormatRecord(5, BuiltinFormats.getBuiltinFormat(5));
  1148. case 1: return new FormatRecord(6, BuiltinFormats.getBuiltinFormat(6));
  1149. case 2: return new FormatRecord(7, BuiltinFormats.getBuiltinFormat(7));
  1150. case 3: return new FormatRecord(8, BuiltinFormats.getBuiltinFormat(8));
  1151. case 4: return new FormatRecord(0x2a, BuiltinFormats.getBuiltinFormat(0x2a));
  1152. case 5: return new FormatRecord(0x29, BuiltinFormats.getBuiltinFormat(0x29));
  1153. case 6: return new FormatRecord(0x2c, BuiltinFormats.getBuiltinFormat(0x2c));
  1154. case 7: return new FormatRecord(0x2b, BuiltinFormats.getBuiltinFormat(0x2b));
  1155. }
  1156. throw new IllegalArgumentException("Unexpected id " + id);
  1157. }
  1158. /**
  1159. * Creates an ExtendedFormatRecord object
  1160. * @param id the number of the extended format record to create (meaning its position in
  1161. * a file as MS Excel would create it.)
  1162. */
  1163. private static ExtendedFormatRecord createExtendedFormat(int id) { // we'll need multiple editions
  1164. ExtendedFormatRecord retval = new ExtendedFormatRecord();
  1165. switch (id) {
  1166. case 0 :
  1167. retval.setFontIndex(( short ) 0);
  1168. retval.setFormatIndex(( short ) 0);
  1169. retval.setCellOptions(( short ) 0xfffffff5);
  1170. retval.setAlignmentOptions(( short ) 0x20);
  1171. retval.setIndentionOptions(( short ) 0);
  1172. retval.setBorderOptions(( short ) 0);
  1173. retval.setPaletteOptions(( short ) 0);
  1174. retval.setAdtlPaletteOptions(( short ) 0);
  1175. retval.setFillPaletteOptions(( short ) 0x20c0);
  1176. break;
  1177. case 1 :
  1178. retval.setFontIndex(( short ) 1);
  1179. retval.setFormatIndex(( short ) 0);
  1180. retval.setCellOptions(( short ) 0xfffffff5);
  1181. retval.setAlignmentOptions(( short ) 0x20);
  1182. retval.setIndentionOptions(( short ) 0xfffff400);
  1183. retval.setBorderOptions(( short ) 0);
  1184. retval.setPaletteOptions(( short ) 0);
  1185. retval.setAdtlPaletteOptions(( short ) 0);
  1186. retval.setFillPaletteOptions(( short ) 0x20c0);
  1187. break;
  1188. case 2 :
  1189. retval.setFontIndex(( short ) 1);
  1190. retval.setFormatIndex(( short ) 0);
  1191. retval.setCellOptions(( short ) 0xfffffff5);
  1192. retval.setAlignmentOptions(( short ) 0x20);
  1193. retval.setIndentionOptions(( short ) 0xfffff400);
  1194. retval.setBorderOptions(( short ) 0);
  1195. retval.setPaletteOptions(( short ) 0);
  1196. retval.setAdtlPaletteOptions(( short ) 0);
  1197. retval.setFillPaletteOptions(( short ) 0x20c0);
  1198. break;
  1199. case 3 :
  1200. retval.setFontIndex(( short ) 2);
  1201. retval.setFormatIndex(( short ) 0);
  1202. retval.setCellOptions(( short ) 0xfffffff5);
  1203. retval.setAlignmentOptions(( short ) 0x20);
  1204. retval.setIndentionOptions(( short ) 0xfffff400);
  1205. retval.setBorderOptions(( short ) 0);
  1206. retval.setPaletteOptions(( short ) 0);
  1207. retval.setAdtlPaletteOptions(( short ) 0);
  1208. retval.setFillPaletteOptions(( short ) 0x20c0);
  1209. break;
  1210. case 4 :
  1211. retval.setFontIndex(( short ) 2);
  1212. retval.setFormatIndex(( short ) 0);
  1213. retval.setCellOptions(( short ) 0xfffffff5);
  1214. retval.setAlignmentOptions(( short ) 0x20);
  1215. retval.setIndentionOptions(( short ) 0xfffff400);
  1216. retval.setBorderOptions(( short ) 0);
  1217. retval.setPaletteOptions(( short ) 0);
  1218. retval.setAdtlPaletteOptions(( short ) 0);
  1219. retval.setFillPaletteOptions(( short ) 0x20c0);
  1220. break;
  1221. case 5 :
  1222. retval.setFontIndex(( short ) 0);
  1223. retval.setFormatIndex(( short ) 0);
  1224. retval.setCellOptions(( short ) 0xfffffff5);
  1225. retval.setAlignmentOptions(( short ) 0x20);
  1226. retval.setIndentionOptions(( short ) 0xfffff400);
  1227. retval.setBorderOptions(( short ) 0);
  1228. retval.setPaletteOptions(( short ) 0);
  1229. retval.setAdtlPaletteOptions(( short ) 0);
  1230. retval.setFillPaletteOptions(( short ) 0x20c0);
  1231. break;
  1232. case 6 :
  1233. retval.setFontIndex(( short ) 0);
  1234. retval.setFormatIndex(( short ) 0);
  1235. retval.setCellOptions(( short ) 0xfffffff5);
  1236. retval.setAlignmentOptions(( short ) 0x20);
  1237. retval.setIndentionOptions(( short ) 0xfffff400);
  1238. retval.setBorderOptions(( short ) 0);
  1239. retval.setPaletteOptions(( short ) 0);
  1240. retval.setAdtlPaletteOptions(( short ) 0);
  1241. retval.setFillPaletteOptions(( short ) 0x20c0);
  1242. break;
  1243. case 7 :
  1244. retval.setFontIndex(( short ) 0);
  1245. retval.setFormatIndex(( short ) 0);
  1246. retval.setCellOptions(( short ) 0xfffffff5);
  1247. retval.setAlignmentOptions(( short ) 0x20);
  1248. retval.setIndentionOptions(( short ) 0xfffff400);
  1249. retval.setBorderOptions(( short ) 0);
  1250. retval.setPaletteOptions(( short ) 0);
  1251. retval.setAdtlPaletteOptions(( short ) 0);
  1252. retval.setFillPaletteOptions(( short ) 0x20c0);
  1253. break;
  1254. case 8 :
  1255. retval.setFontIndex(( short ) 0);
  1256. retval.setFormatIndex(( short ) 0);
  1257. retval.setCellOptions(( short ) 0xfffffff5);
  1258. retval.setAlignmentOptions(( short ) 0x20);
  1259. retval.setIndentionOptions(( short ) 0xfffff400);
  1260. retval.setBorderOptions(( short ) 0);
  1261. retval.setPaletteOptions(( short ) 0);
  1262. retval.setAdtlPaletteOptions(( short ) 0);
  1263. retval.setFillPaletteOptions(( short ) 0x20c0);
  1264. break;
  1265. case 9 :
  1266. retval.setFontIndex(( short ) 0);
  1267. retval.setFormatIndex(( short ) 0);
  1268. retval.setCellOptions(( short ) 0xfffffff5);
  1269. retval.setAlignmentOptions(( short ) 0x20);
  1270. retval.setIndentionOptions(( short ) 0xfffff400);
  1271. retval.setBorderOptions(( short ) 0);
  1272. retval.setPaletteOptions(( short ) 0);
  1273. retval.setAdtlPaletteOptions(( short ) 0);
  1274. retval.setFillPaletteOptions(( short ) 0x20c0);
  1275. break;
  1276. case 10 :
  1277. retval.setFontIndex(( short ) 0);
  1278. retval.setFormatIndex(( short ) 0);
  1279. retval.setCellOptions(( short ) 0xfffffff5);
  1280. retval.setAlignmentOptions(( short ) 0x20);
  1281. retval.setIndentionOptions(( short ) 0xfffff400);
  1282. retval.setBorderOptions(( short ) 0);
  1283. retval.setPaletteOptions(( short ) 0);
  1284. retval.setAdtlPaletteOptions(( short ) 0);
  1285. retval.setFillPaletteOptions(( short ) 0x20c0);
  1286. break;
  1287. case 11 :
  1288. retval.setFontIndex(( short ) 0);
  1289. retval.setFormatIndex(( short ) 0);
  1290. retval.setCellOptions(( short ) 0xfffffff5);
  1291. retval.setAlignmentOptions(( short ) 0x20);
  1292. retval.setIndentionOptions(( short ) 0xfffff400);
  1293. retval.setBorderOptions(( short ) 0);
  1294. retval.setPaletteOptions(( short ) 0);
  1295. retval.setAdtlPaletteOptions(( short ) 0);
  1296. retval.setFillPaletteOptions(( short ) 0x20c0);
  1297. break;
  1298. case 12 :
  1299. retval.setFontIndex(( short ) 0);
  1300. retval.setFormatIndex(( short ) 0);
  1301. retval.setCellOptions(( short ) 0xfffffff5);
  1302. retval.setAlignmentOptions(( short ) 0x20);
  1303. retval.setIndentionOptions(( short ) 0xfffff400);
  1304. retval.setBorderOptions(( short ) 0);
  1305. retval.setPaletteOptions(( short ) 0);
  1306. retval.setAdtlPaletteOptions(( short ) 0);
  1307. retval.setFillPaletteOptions(( short ) 0x20c0);
  1308. break;
  1309. case 13 :
  1310. retval.setFontIndex(( short ) 0);
  1311. retval.setFormatIndex(( short ) 0);
  1312. retval.setCellOptions(( short ) 0xfffffff5);
  1313. retval.setAlignmentOptions(( short ) 0x20);
  1314. retval.setIndentionOptions(( short ) 0xfffff400);
  1315. retval.setBorderOptions(( short ) 0);
  1316. retval.setPaletteOptions(( short ) 0);
  1317. retval.setAdtlPaletteOptions(( short ) 0);
  1318. retval.setFillPaletteOptions(( short ) 0x20c0);
  1319. break;
  1320. case 14 :
  1321. retval.setFontIndex(( short ) 0);
  1322. retval.setFormatIndex(( short ) 0);
  1323. retval.setCellOptions(( short ) 0xfffffff5);
  1324. retval.setAlignmentOptions(( short ) 0x20);
  1325. retval.setIndentionOptions(( short ) 0xfffff400);
  1326. retval.setBorderOptions(( short ) 0);
  1327. retval.setPaletteOptions(( short ) 0);
  1328. retval.setAdtlPaletteOptions(( short ) 0);
  1329. retval.setFillPaletteOptions(( short ) 0x20c0);
  1330. break;
  1331. // cell records
  1332. case 15 :
  1333. retval.setFontIndex(( short ) 0);
  1334. retval.setFormatIndex(( short ) 0);
  1335. retval.setCellOptions(( short ) 0x1);
  1336. retval.setAlignmentOptions(( short ) 0x20);
  1337. retval.setIndentionOptions(( short ) 0x0);
  1338. retval.setBorderOptions(( short ) 0);
  1339. retval.setPaletteOptions(( short ) 0);
  1340. retval.setAdtlPaletteOptions(( short ) 0);
  1341. retval.setFillPaletteOptions(( short ) 0x20c0);
  1342. break;
  1343. // style
  1344. case 16 :
  1345. retval.setFontIndex(( short ) 1);
  1346. retval.setFormatIndex(( short ) 0x2b);
  1347. retval.setCellOptions(( short ) 0xfffffff5);
  1348. retval.setAlignmentOptions(( short ) 0x20);
  1349. retval.setIndentionOptions(( short ) 0xfffff800);
  1350. retval.setBorderOptions(( short ) 0);
  1351. retval.setPaletteOptions(( short ) 0);
  1352. retval.setAdtlPaletteOptions(( short ) 0);
  1353. retval.setFillPaletteOptions(( short ) 0x20c0);
  1354. break;
  1355. case 17 :
  1356. retval.setFontIndex(( short ) 1);
  1357. retval.setFormatIndex(( short ) 0x29);
  1358. retval.setCellOptions(( short ) 0xfffffff5);
  1359. retval.setAlignmentOptions(( short ) 0x20);
  1360. retval.setIndentionOptions(( short ) 0xfffff800);
  1361. retval.setBorderOptions(( short ) 0);
  1362. retval.setPaletteOptions(( short ) 0);
  1363. retval.setAdtlPaletteOptions(( short ) 0);
  1364. retval.setFillPaletteOptions(( short ) 0x20c0);
  1365. break;
  1366. case 18 :
  1367. retval.setFontIndex(( short ) 1);
  1368. retval.setFormatIndex(( short ) 0x2c);
  1369. retval.setCellOptions(( short ) 0xfffffff5);
  1370. retval.setAlignmentOptions(( short ) 0x20);
  1371. retval.setIndentionOptions(( short ) 0xfffff800);
  1372. retval.setBorderOptions(( short ) 0);
  1373. retval.setPaletteOptions(( short ) 0);
  1374. retval.setAdtlPaletteOptions(( short ) 0);
  1375. retval.setFillPaletteOptions(( short ) 0x20c0);
  1376. break;
  1377. case 19 :
  1378. retval.setFontIndex(( short ) 1);
  1379. retval.setFormatIndex(( short ) 0x2a);
  1380. retval.setCellOptions(( short ) 0xfffffff5);
  1381. retval.setAlignmentOptions(( short ) 0x20);
  1382. retval.setIndentionOptions(( short ) 0xfffff800);
  1383. retval.setBorderOptions(( short ) 0);
  1384. retval.setPaletteOptions(( short ) 0);
  1385. retval.setAdtlPaletteOptions(( short ) 0);
  1386. retval.setFillPaletteOptions(( short ) 0x20c0);
  1387. break;
  1388. case 20 :
  1389. retval.setFontIndex(( short ) 1);
  1390. retval.setFormatIndex(( short ) 0x9);
  1391. retval.setCellOptions(( short ) 0xfffffff5);
  1392. retval.setAlignmentOptions(( short ) 0x20);
  1393. retval.setIndentionOptions(( short ) 0xfffff800);
  1394. retval.setBorderOptions(( short ) 0);
  1395. retval.setPaletteOptions(( short ) 0);
  1396. retval.setAdtlPaletteOptions(( short ) 0);
  1397. retval.setFillPaletteOptions(( short ) 0x20c0);
  1398. break;
  1399. // unused from this point down
  1400. case 21 :
  1401. retval.setFontIndex(( short ) 5);
  1402. retval.setFormatIndex(( short ) 0x0);
  1403. retval.setCellOptions(( short ) 0x1);
  1404. retval.setAlignmentOptions(( short ) 0x20);
  1405. retval.setIndentionOptions(( short ) 0x800);
  1406. retval.setBorderOptions(( short ) 0);
  1407. retval.setPaletteOptions(( short ) 0);
  1408. retval.setAdtlPaletteOptions(( short ) 0);
  1409. retval.setFillPaletteOptions(( short ) 0x20c0);
  1410. break;
  1411. case 22 :
  1412. retval.setFontIndex(( short ) 6);
  1413. retval.setFormatIndex(( short ) 0x0);
  1414. retval.setCellOptions(( short ) 0x1);
  1415. retval.setAlignmentOptions(( short ) 0x20);
  1416. retval.setIndentionOptions(( short ) 0x5c00);
  1417. retval.setBorderOptions(( short ) 0);
  1418. retval.setPaletteOptions(( short ) 0);
  1419. retval.setAdtlPaletteOptions(( short ) 0);
  1420. retval.setFillPaletteOptions(( short ) 0x20c0);
  1421. break;
  1422. case 23 :
  1423. retval.setFontIndex(( short ) 0);
  1424. retval.setFormatIndex(( short ) 0x31);
  1425. retval.setCellOptions(( short ) 0x1);
  1426. retval.setAlignmentOptions(( short ) 0x20);
  1427. retval.setIndentionOptions(( short ) 0x5c00);
  1428. retval.setBorderOptions(( short ) 0);
  1429. retval.setPaletteOptions(( short ) 0);
  1430. retval.setAdtlPaletteOptions(( short ) 0);
  1431. retval.setFillPaletteOptions(( short ) 0x20c0);
  1432. break;
  1433. case 24 :
  1434. retval.setFontIndex(( short ) 0);
  1435. retval.setFormatIndex(( short ) 0x8);
  1436. retval.setCellOptions(( short ) 0x1);
  1437. retval.setAlignmentOptions(( short ) 0x20);
  1438. retval.setIndentionOptions(( short ) 0x5c00);
  1439. retval.setBorderOptions(( short ) 0);
  1440. retval.setPaletteOptions(( short ) 0);
  1441. retval.setAdtlPaletteOptions(( short ) 0);
  1442. retval.setFillPaletteOptions(( short ) 0x20c0);
  1443. break;
  1444. case 25 :
  1445. retval.setFontIndex(( short ) 6);
  1446. retval.setFormatIndex(( short ) 0x8);
  1447. retval.setCellOptions(( short ) 0x1);
  1448. retval.setAlignmentOptions(( short ) 0x20);
  1449. retval.setIndentionOptions(( short ) 0x5c00);
  1450. retval.setBorderOptions(( short ) 0);
  1451. retval.setPaletteOptions(( short ) 0);
  1452. retval.setAdtlPaletteOptions(( short ) 0);
  1453. retval.setFillPaletteOptions(( short ) 0x20c0);
  1454. break;
  1455. }
  1456. return retval;
  1457. }
  1458. /**
  1459. * creates an default cell type ExtendedFormatRecord object.
  1460. * @return ExtendedFormatRecord with intial defaults (cell-type)
  1461. */
  1462. private static ExtendedFormatRecord createExtendedFormat() {
  1463. ExtendedFormatRecord retval = new ExtendedFormatRecord();
  1464. retval.setFontIndex(( short ) 0);
  1465. retval.setFormatIndex(( short ) 0x0);
  1466. retval.setCellOptions(( short ) 0x1);
  1467. retval.setAlignmentOptions(( short ) 0x20);
  1468. retval.setIndentionOptions(( short ) 0);
  1469. retval.setBorderOptions(( short ) 0);
  1470. retval.setPaletteOptions(( short ) 0);
  1471. retval.setAdtlPaletteOptions(( short ) 0);
  1472. retval.setFillPaletteOptions(( short ) 0x20c0);
  1473. retval.setTopBorderPaletteIdx(HSSFColor.BLACK.index);
  1474. retval.setBottomBorderPaletteIdx(HSSFColor.BLACK.index);
  1475. retval.setLeftBorderPaletteIdx(HSSFColor.BLACK.index);
  1476. retval.setRightBorderPaletteIdx(HSSFColor.BLACK.index);
  1477. return retval;
  1478. }
  1479. /**
  1480. * Creates a StyleRecord object
  1481. * @param id the number of the style record to create (meaning its position in
  1482. * a file as MS Excel would create it.
  1483. */
  1484. private static StyleRecord createStyle(int id) { // we'll need multiple editions
  1485. StyleRecord retval = new StyleRecord();
  1486. switch (id) {
  1487. case 0 :
  1488. retval.setXFIndex(0x010);
  1489. retval.setBuiltinStyle(3);
  1490. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1491. break;
  1492. case 1 :
  1493. retval.setXFIndex(0x011);
  1494. retval.setBuiltinStyle(6);
  1495. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1496. break;
  1497. case 2 :
  1498. retval.setXFIndex(0x012);
  1499. retval.setBuiltinStyle(4);
  1500. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1501. break;
  1502. case 3 :
  1503. retval.setXFIndex(0x013);
  1504. retval.setBuiltinStyle(7);
  1505. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1506. break;
  1507. case 4 :
  1508. retval.setXFIndex(0x000);
  1509. retval.setBuiltinStyle(0);
  1510. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1511. break;
  1512. case 5 :
  1513. retval.setXFIndex(0x014);
  1514. retval.setBuiltinStyle(5);
  1515. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1516. break;
  1517. }
  1518. return retval;
  1519. }
  1520. /**
  1521. * Creates a palette record initialized to the default palette
  1522. */
  1523. private static PaletteRecord createPalette() {
  1524. return new PaletteRecord();
  1525. }
  1526. /**
  1527. * @return a new UseSelFS object with the use natural language flag set to 0 (false)
  1528. */
  1529. private static UseSelFSRecord createUseSelFS() {
  1530. return new UseSelFSRecord(false);
  1531. }
  1532. /**
  1533. * create a "bound sheet" or "bundlesheet" (depending who you ask) record
  1534. * Always sets the sheet's bof to 0. You'll need to set that yourself.
  1535. * @param id either sheet 0,1 or 2.
  1536. * @return record containing a BoundSheetRecord
  1537. * @see org.apache.poi.hssf.record.BoundSheetRecord
  1538. * @see org.apache.poi.hssf.record.Record
  1539. */
  1540. private static BoundSheetRecord createBoundSheet(int id) {
  1541. return new BoundSheetRecord("Sheet" + (id+1));
  1542. }
  1543. /**
  1544. * Creates the Country record with the default country set to 1
  1545. * and current country set to 7 in case of russian locale ("ru_RU") and 1 otherwise
  1546. */
  1547. private static CountryRecord createCountry() {
  1548. CountryRecord retval = new CountryRecord();
  1549. retval.setDefaultCountry(( short ) 1);
  1550. // from Russia with love ;)
  1551. if ( Locale.getDefault().toString().equals( "ru_RU" ) ) {
  1552. retval.setCurrentCountry(( short ) 7);
  1553. }
  1554. else {
  1555. retval.setCurrentCountry(( short ) 1);
  1556. }
  1557. return retval;
  1558. }
  1559. /**
  1560. * Creates the ExtendedSST record with numstrings per bucket set to 0x8. HSSF
  1561. * doesn't yet know what to do with this thing, but we create it with nothing in
  1562. * it hardly just to make Excel happy and our sheets look like Excel's
  1563. */
  1564. private static ExtSSTRecord createExtendedSST() {
  1565. ExtSSTRecord retval = new ExtSSTRecord();
  1566. retval.setNumStringsPerBucket(( short ) 0x8);
  1567. return retval;
  1568. }
  1569. /**
  1570. * lazy initialization
  1571. * Note - creating the link table causes creation of 1 EXTERNALBOOK and 1 EXTERNALSHEET record
  1572. */
  1573. private LinkTable getOrCreateLinkTable() {
  1574. if(linkTable == null) {
  1575. linkTable = new LinkTable((short) getNumSheets(), records);
  1576. }
  1577. return linkTable;
  1578. }
  1579. /** finds the sheet name by his extern sheet index
  1580. * @param externSheetIndex extern sheet index
  1581. * @return sheet name.
  1582. */
  1583. public String findSheetNameFromExternSheet(int externSheetIndex){
  1584. int indexToSheet = linkTable.getIndexToInternalSheet(externSheetIndex);
  1585. if (indexToSheet < 0) {
  1586. // TODO - what does '-1' mean here?
  1587. //error check, bail out gracefully!
  1588. return "";
  1589. }
  1590. if (indexToSheet >= boundsheets.size()) {
  1591. // Not sure if this can ever happen (See bug 45798)
  1592. return ""; // Seems to be what excel would do in this case
  1593. }
  1594. return getSheetName(indexToSheet);
  1595. }
  1596. public ExternalSheet getExternalSheet(int externSheetIndex) {
  1597. String[] extNames = linkTable.getExternalBookAndSheetName(externSheetIndex);
  1598. if (extNames == null) {
  1599. return null;
  1600. }
  1601. return new ExternalSheet(extNames[0], extNames[1]);
  1602. }
  1603. public ExternalName getExternalName(int externSheetIndex, int externNameIndex) {
  1604. String nameName = linkTable.resolveNameXText(externSheetIndex, externNameIndex);
  1605. if(nameName == null) {
  1606. return null;
  1607. }
  1608. int ix = linkTable.resolveNameXIx(externSheetIndex, externNameIndex);
  1609. return new ExternalName(nameName, externNameIndex, ix);
  1610. }
  1611. /**
  1612. * Finds the sheet index for a particular external sheet number.
  1613. * @param externSheetNumber The external sheet number to convert
  1614. * @return The index to the sheet found.
  1615. */
  1616. public int getSheetIndexFromExternSheetIndex(int externSheetNumber)
  1617. {
  1618. return linkTable.getSheetIndexFromExternSheetIndex(externSheetNumber);
  1619. }
  1620. /** returns the extern sheet number for specific sheet number ,
  1621. * if this sheet doesn't exist in extern sheet , add it
  1622. * @param sheetNumber sheet number
  1623. * @return index to extern sheet
  1624. */
  1625. public short checkExternSheet(int sheetNumber){
  1626. return (short)getOrCreateLinkTable().checkExternSheet(sheetNumber);
  1627. }
  1628. public int getExternalSheetIndex(String workbookName, String sheetName) {
  1629. return getOrCreateLinkTable().getExternalSheetIndex(workbookName, sheetName);
  1630. }
  1631. /** gets the total number of names
  1632. * @return number of names
  1633. */
  1634. public int getNumNames(){
  1635. if(linkTable == null) {
  1636. return 0;
  1637. }
  1638. return linkTable.getNumNames();
  1639. }
  1640. /** gets the name record
  1641. * @param index name index
  1642. * @return name record
  1643. */
  1644. public NameRecord getNameRecord(int index){
  1645. return linkTable.getNameRecord(index);
  1646. }
  1647. /** gets the name comment record
  1648. * @param nameRecord name record who's comment is required.
  1649. * @return name comment record or <code>null</code> if there isn't one for the given name.
  1650. */
  1651. public NameCommentRecord getNameCommentRecord(final NameRecord nameRecord){
  1652. return commentRecords.get(nameRecord.getNameText());
  1653. }
  1654. /** creates new name
  1655. * @return new name record
  1656. */
  1657. public NameRecord createName(){
  1658. return addName(new NameRecord());
  1659. }
  1660. /** creates new name
  1661. * @return new name record
  1662. */
  1663. public NameRecord addName(NameRecord name)
  1664. {
  1665. LinkTable linkTable = getOrCreateLinkTable();
  1666. linkTable.addName(name);
  1667. return name;
  1668. }
  1669. /**
  1670. * Generates a NameRecord to represent a built-in region
  1671. * @return a new NameRecord
  1672. */
  1673. public NameRecord createBuiltInName(byte builtInName, int sheetNumber) {
  1674. if (sheetNumber < 0 || sheetNumber+1 > Short.MAX_VALUE) {
  1675. throw new IllegalArgumentException("Sheet number ["+sheetNumber+"]is not valid ");
  1676. }
  1677. NameRecord name = new NameRecord(builtInName, sheetNumber);
  1678. if(linkTable.nameAlreadyExists(name)) {
  1679. throw new RuntimeException("Builtin (" + builtInName
  1680. + ") already exists for sheet (" + sheetNumber + ")");
  1681. }
  1682. addName(name);
  1683. return name;
  1684. }
  1685. /** removes the name
  1686. * @param nameIndex name index
  1687. */
  1688. public void removeName(int nameIndex){
  1689. if (linkTable.getNumNames() > nameIndex) {
  1690. int idx = findFirstRecordLocBySid(NameRecord.sid);
  1691. records.remove(idx + nameIndex);
  1692. linkTable.removeName(nameIndex);
  1693. }
  1694. }
  1695. /**
  1696. * If a {@link NameCommentRecord} is added or the name it references
  1697. * is renamed, then this will update the lookup cache for it.
  1698. */
  1699. public void updateNameCommentRecordCache(final NameCommentRecord commentRecord) {
  1700. if(commentRecords.containsValue(commentRecord)) {
  1701. for(Entry<String,NameCommentRecord> entry : commentRecords.entrySet()) {
  1702. if(entry.getValue().equals(commentRecord)) {
  1703. commentRecords.remove(entry.getKey());
  1704. break;
  1705. }
  1706. }
  1707. }
  1708. commentRecords.put(commentRecord.getNameText(), commentRecord);
  1709. }
  1710. /**
  1711. * Returns a format index that matches the passed in format. It does not tie into HSSFDataFormat.
  1712. * @param format the format string
  1713. * @param createIfNotFound creates a new format if format not found
  1714. * @return the format id of a format that matches or -1 if none found and createIfNotFound
  1715. */
  1716. public short getFormat(String format, boolean createIfNotFound) {
  1717. for (FormatRecord r : formats) {
  1718. if (r.getFormatString().equals(format)) {
  1719. return (short)r.getIndexCode();
  1720. }
  1721. }
  1722. if (createIfNotFound) {
  1723. return (short)createFormat(format);
  1724. }
  1725. return -1;
  1726. }
  1727. /**
  1728. * Returns the list of FormatRecords in the workbook.
  1729. * @return ArrayList of FormatRecords in the notebook
  1730. */
  1731. public List<FormatRecord> getFormats() {
  1732. return formats;
  1733. }
  1734. /**
  1735. * Creates a FormatRecord, inserts it, and returns the index code.
  1736. * @param formatString the format string
  1737. * @return the index code of the format record.
  1738. * @see org.apache.poi.hssf.record.FormatRecord
  1739. * @see org.apache.poi.hssf.record.Record
  1740. */
  1741. public int createFormat(String formatString) {
  1742. maxformatid = maxformatid >= 0xa4 ? maxformatid + 1 : 0xa4; //Starting value from M$ empircal study.
  1743. FormatRecord rec = new FormatRecord(maxformatid, formatString);
  1744. int pos = 0;
  1745. while ( pos < records.size() && records.get( pos ).getSid() != FormatRecord.sid )
  1746. pos++;
  1747. pos += formats.size();
  1748. formats.add( rec );
  1749. records.add( pos, rec );
  1750. return maxformatid;
  1751. }
  1752. /**
  1753. * Returns the first occurance of a record matching a particular sid.
  1754. */
  1755. public Record findFirstRecordBySid(short sid) {
  1756. for (Record record : records) {
  1757. if (record.getSid() == sid) {
  1758. return record;
  1759. }
  1760. }
  1761. return null;
  1762. }
  1763. /**
  1764. * Returns the index of a record matching a particular sid.
  1765. * @param sid The sid of the record to match
  1766. * @return The index of -1 if no match made.
  1767. */
  1768. public int findFirstRecordLocBySid(short sid) {
  1769. int index = 0;
  1770. for (Record record : records) {
  1771. if (record.getSid() == sid) {
  1772. return index;
  1773. }
  1774. index ++;
  1775. }
  1776. return -1;
  1777. }
  1778. /**
  1779. * Returns the next occurance of a record matching a particular sid.
  1780. */
  1781. public Record findNextRecordBySid(short sid, int pos) {
  1782. int matches = 0;
  1783. for (Record record : records) {
  1784. if (record.getSid() == sid) {
  1785. if (matches++ == pos)
  1786. return record;
  1787. }
  1788. }
  1789. return null;
  1790. }
  1791. public List<HyperlinkRecord> getHyperlinks()
  1792. {
  1793. return hyperlinks;
  1794. }
  1795. public List<Record> getRecords() {
  1796. return records.getRecords();
  1797. }
  1798. /**
  1799. * Whether date windowing is based on 1/2/1904 or 1/1/1900.
  1800. * Some versions of Excel (Mac) can save workbooks using 1904 date windowing.
  1801. *
  1802. * @return true if using 1904 date windowing
  1803. */
  1804. public boolean isUsing1904DateWindowing() {
  1805. return uses1904datewindowing;
  1806. }
  1807. /**
  1808. * Returns the custom palette in use for this workbook; if a custom palette record
  1809. * does not exist, then it is created.
  1810. */
  1811. public PaletteRecord getCustomPalette()
  1812. {
  1813. PaletteRecord palette;
  1814. int palettePos = records.getPalettepos();
  1815. if (palettePos != -1) {
  1816. Record rec = records.get(palettePos);
  1817. if (rec instanceof PaletteRecord) {
  1818. palette = (PaletteRecord) rec;
  1819. } else throw new RuntimeException("InternalError: Expected PaletteRecord but got a '"+rec+"'");
  1820. }
  1821. else
  1822. {
  1823. palette = createPalette();
  1824. //Add the palette record after the bof which is always the first record
  1825. records.add(1, palette);
  1826. records.setPalettepos(1);
  1827. }
  1828. return palette;
  1829. }
  1830. /**
  1831. * Finds the primary drawing group, if one already exists
  1832. */
  1833. public void findDrawingGroup() {
  1834. if(drawingManager != null) {
  1835. // We already have it!
  1836. return;
  1837. }
  1838. // Need to find a DrawingGroupRecord that
  1839. // contains a EscherDggRecord
  1840. for(Record r : records) {
  1841. if(r instanceof DrawingGroupRecord) {
  1842. DrawingGroupRecord dg = (DrawingGroupRecord)r;
  1843. dg.processChildRecords();
  1844. EscherContainerRecord cr =
  1845. dg.getEscherContainer();
  1846. if(cr == null) {
  1847. continue;
  1848. }
  1849. EscherDggRecord dgg = null;
  1850. for(Iterator<EscherRecord> it = cr.getChildIterator(); it.hasNext();) {
  1851. Object er = it.next();
  1852. if(er instanceof EscherDggRecord) {
  1853. dgg = (EscherDggRecord)er;
  1854. }
  1855. }
  1856. if(dgg != null) {
  1857. drawingManager = new DrawingManager2(dgg);
  1858. return;
  1859. }
  1860. }
  1861. }
  1862. // Look for the DrawingGroup record
  1863. int dgLoc = findFirstRecordLocBySid(DrawingGroupRecord.sid);
  1864. // If there is one, does it have a EscherDggRecord?
  1865. if(dgLoc != -1) {
  1866. DrawingGroupRecord dg = (DrawingGroupRecord)records.get(dgLoc);
  1867. EscherDggRecord dgg = null;
  1868. for(EscherRecord er : dg.getEscherRecords()) {
  1869. if(er instanceof EscherDggRecord) {
  1870. dgg = (EscherDggRecord)er;
  1871. }
  1872. }
  1873. if(dgg != null) {
  1874. drawingManager = new DrawingManager2(dgg);
  1875. }
  1876. }
  1877. }
  1878. /**
  1879. * Creates a primary drawing group record. If it already
  1880. * exists then it's modified.
  1881. */
  1882. public void createDrawingGroup() {
  1883. if (drawingManager == null) {
  1884. EscherContainerRecord dggContainer = new EscherContainerRecord();
  1885. EscherDggRecord dgg = new EscherDggRecord();
  1886. EscherOptRecord opt = new EscherOptRecord();
  1887. EscherSplitMenuColorsRecord splitMenuColors = new EscherSplitMenuColorsRecord();
  1888. dggContainer.setRecordId((short) 0xF000);
  1889. dggContainer.setOptions((short) 0x000F);
  1890. dgg.setRecordId(EscherDggRecord.RECORD_ID);
  1891. dgg.setOptions((short)0x0000);
  1892. dgg.setShapeIdMax(1024);
  1893. dgg.setNumShapesSaved(0);
  1894. dgg.setDrawingsSaved(0);
  1895. dgg.setFileIdClusters(new EscherDggRecord.FileIdCluster[] {} );
  1896. drawingManager = new DrawingManager2(dgg);
  1897. EscherContainerRecord bstoreContainer = null;
  1898. if (escherBSERecords.size() > 0)
  1899. {
  1900. bstoreContainer = new EscherContainerRecord();
  1901. bstoreContainer.setRecordId( EscherContainerRecord.BSTORE_CONTAINER );
  1902. bstoreContainer.setOptions( (short) ( (escherBSERecords.size() << 4) | 0xF ) );
  1903. for (EscherRecord escherRecord : escherBSERecords) {
  1904. bstoreContainer.addChildRecord( escherRecord );
  1905. }
  1906. }
  1907. opt.setRecordId((short) 0xF00B);
  1908. opt.setOptions((short) 0x0033);
  1909. opt.addEscherProperty( new EscherBoolProperty(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 524296) );
  1910. opt.addEscherProperty( new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, 0x08000041) );
  1911. opt.addEscherProperty( new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, 134217792) );
  1912. splitMenuColors.setRecordId((short) 0xF11E);
  1913. splitMenuColors.setOptions((short) 0x0040);
  1914. splitMenuColors.setColor1(0x0800000D);
  1915. splitMenuColors.setColor2(0x0800000C);
  1916. splitMenuColors.setColor3(0x08000017);
  1917. splitMenuColors.setColor4(0x100000F7);
  1918. dggContainer.addChildRecord(dgg);
  1919. if (bstoreContainer != null)
  1920. dggContainer.addChildRecord( bstoreContainer );
  1921. dggContainer.addChildRecord(opt);
  1922. dggContainer.addChildRecord(splitMenuColors);
  1923. int dgLoc = findFirstRecordLocBySid(DrawingGroupRecord.sid);
  1924. if (dgLoc == -1) {
  1925. DrawingGroupRecord drawingGroup = new DrawingGroupRecord();
  1926. drawingGroup.addEscherRecord(dggContainer);
  1927. int loc = findFirstRecordLocBySid(CountryRecord.sid);
  1928. getRecords().add(loc+1, drawingGroup);
  1929. } else {
  1930. DrawingGroupRecord drawingGroup = new DrawingGroupRecord();
  1931. drawingGroup.addEscherRecord(dggContainer);
  1932. getRecords().set(dgLoc, drawingGroup);
  1933. }
  1934. }
  1935. }
  1936. public WindowOneRecord getWindowOne() {
  1937. return windowOne;
  1938. }
  1939. public EscherBSERecord getBSERecord(int pictureIndex) {
  1940. return escherBSERecords.get(pictureIndex-1);
  1941. }
  1942. public int addBSERecord(EscherBSERecord e) {
  1943. createDrawingGroup();
  1944. // maybe we don't need that as an instance variable anymore
  1945. escherBSERecords.add( e );
  1946. int dgLoc = findFirstRecordLocBySid(DrawingGroupRecord.sid);
  1947. DrawingGroupRecord drawingGroup = (DrawingGroupRecord) getRecords().get( dgLoc );
  1948. EscherContainerRecord dggContainer = (EscherContainerRecord) drawingGroup.getEscherRecord( 0 );
  1949. EscherContainerRecord bstoreContainer;
  1950. if (dggContainer.getChild( 1 ).getRecordId() == EscherContainerRecord.BSTORE_CONTAINER )
  1951. {
  1952. bstoreContainer = (EscherContainerRecord) dggContainer.getChild( 1 );
  1953. }
  1954. else
  1955. {
  1956. bstoreContainer = new EscherContainerRecord();
  1957. bstoreContainer.setRecordId( EscherContainerRecord.BSTORE_CONTAINER );
  1958. List<EscherRecord> childRecords = dggContainer.getChildRecords();
  1959. childRecords.add(1, bstoreContainer);
  1960. dggContainer.setChildRecords(childRecords);
  1961. }
  1962. bstoreContainer.setOptions( (short) ( (escherBSERecords.size() << 4) | 0xF ) );
  1963. bstoreContainer.addChildRecord( e );
  1964. return escherBSERecords.size();
  1965. }
  1966. public DrawingManager2 getDrawingManager()
  1967. {
  1968. return drawingManager;
  1969. }
  1970. public WriteProtectRecord getWriteProtect() {
  1971. if (writeProtect == null) {
  1972. writeProtect = new WriteProtectRecord();
  1973. int i = 0;
  1974. for (i = 0;
  1975. i < records.size() && !(records.get(i) instanceof BOFRecord);
  1976. i++) {
  1977. }
  1978. records.add(i+1, writeProtect);
  1979. }
  1980. return this.writeProtect;
  1981. }
  1982. public WriteAccessRecord getWriteAccess() {
  1983. if (writeAccess == null) {
  1984. writeAccess = createWriteAccess();
  1985. int i = 0;
  1986. for (i = 0;
  1987. i < records.size() && !(records.get(i) instanceof InterfaceEndRecord);
  1988. i++) {
  1989. }
  1990. records.add(i+1, writeAccess);
  1991. }
  1992. return writeAccess;
  1993. }
  1994. public FileSharingRecord getFileSharing() {
  1995. if (fileShare == null) {
  1996. fileShare = new FileSharingRecord();
  1997. int i = 0;
  1998. for (i = 0;
  1999. i < records.size() && !(records.get(i) instanceof WriteAccessRecord);
  2000. i++) {
  2001. }
  2002. records.add(i+1, fileShare);
  2003. }
  2004. return fileShare;
  2005. }
  2006. /**
  2007. * is the workbook protected with a password (not encrypted)?
  2008. */
  2009. public boolean isWriteProtected() {
  2010. if (fileShare == null) {
  2011. return false;
  2012. }
  2013. FileSharingRecord frec = getFileSharing();
  2014. return frec.getReadOnly() == 1;
  2015. }
  2016. /**
  2017. * protect a workbook with a password (not encypted, just sets writeprotect
  2018. * flags and the password.
  2019. * @param password to set
  2020. */
  2021. public void writeProtectWorkbook( String password, String username ) {
  2022. int protIdx = -1;
  2023. FileSharingRecord frec = getFileSharing();
  2024. WriteAccessRecord waccess = getWriteAccess();
  2025. WriteProtectRecord wprotect = getWriteProtect();
  2026. frec.setReadOnly((short)1);
  2027. frec.setPassword(FileSharingRecord.hashPassword(password));
  2028. frec.setUsername(username);
  2029. waccess.setUsername(username);
  2030. }
  2031. /**
  2032. * removes the write protect flag
  2033. */
  2034. public void unwriteProtectWorkbook() {
  2035. records.remove(fileShare);
  2036. records.remove(writeProtect);
  2037. fileShare = null;
  2038. writeProtect = null;
  2039. }
  2040. /**
  2041. * @param refIndex Index to REF entry in EXTERNSHEET record in the Link Table
  2042. * @param definedNameIndex zero-based to DEFINEDNAME or EXTERNALNAME record
  2043. * @return the string representation of the defined or external name
  2044. */
  2045. public String resolveNameXText(int refIndex, int definedNameIndex) {
  2046. return linkTable.resolveNameXText(refIndex, definedNameIndex);
  2047. }
  2048. /**
  2049. *
  2050. * @param name the name of an external function, typically a name of a UDF
  2051. * @param udf locator of user-defiend functions to resolve names of VBA and Add-In functions
  2052. * @return the external name or null
  2053. */
  2054. public NameXPtg getNameXPtg(String name, UDFFinder udf) {
  2055. LinkTable lnk = getOrCreateLinkTable();
  2056. NameXPtg xptg = lnk.getNameXPtg(name);
  2057. if(xptg == null && udf.findFunction(name) != null) {
  2058. // the name was not found in the list of external names
  2059. // check if the Workbook's UDFFinder is aware about it and register the name if it is
  2060. xptg = lnk.addNameXPtg(name);
  2061. }
  2062. return xptg;
  2063. }
  2064. /**
  2065. * Check if the cloned sheet has drawings. If yes, then allocate a new drawing group ID and
  2066. * re-generate shape IDs
  2067. *
  2068. * @param sheet the cloned sheet
  2069. */
  2070. public void cloneDrawings(InternalSheet sheet){
  2071. findDrawingGroup();
  2072. if(drawingManager == null) {
  2073. //this workbook does not have drawings
  2074. return;
  2075. }
  2076. //check if the cloned sheet has drawings
  2077. int aggLoc = sheet.aggregateDrawingRecords(drawingManager, false);
  2078. if(aggLoc != -1) {
  2079. EscherAggregate agg = (EscherAggregate) sheet.findFirstRecordBySid(EscherAggregate.sid);
  2080. EscherContainerRecord escherContainer = agg.getEscherContainer();
  2081. if (escherContainer == null) {
  2082. return;
  2083. }
  2084. EscherDggRecord dgg = drawingManager.getDgg();
  2085. //register a new drawing group for the cloned sheet
  2086. int dgId = drawingManager.findNewDrawingGroupId();
  2087. dgg.addCluster( dgId, 0 );
  2088. dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
  2089. EscherDgRecord dg = null;
  2090. for(Iterator<EscherRecord> it = escherContainer.getChildIterator(); it.hasNext();) {
  2091. EscherRecord er = it.next();
  2092. if(er instanceof EscherDgRecord) {
  2093. dg = (EscherDgRecord)er;
  2094. //update id of the drawing in the cloned sheet
  2095. dg.setOptions( (short) ( dgId << 4 ) );
  2096. } else if (er instanceof EscherContainerRecord){
  2097. //recursively find shape records and re-generate shapeId
  2098. List<EscherRecord> spRecords = new ArrayList<EscherRecord>();
  2099. EscherContainerRecord cp = (EscherContainerRecord)er;
  2100. cp.getRecordsById(EscherSpRecord.RECORD_ID, spRecords);
  2101. for(Iterator<EscherRecord> spIt = spRecords.iterator(); spIt.hasNext();) {
  2102. EscherSpRecord sp = (EscherSpRecord)spIt.next();
  2103. int shapeId = drawingManager.allocateShapeId((short)dgId, dg);
  2104. //allocateShapeId increments the number of shapes. roll back to the previous value
  2105. dg.setNumShapes(dg.getNumShapes()-1);
  2106. sp.setShapeId(shapeId);
  2107. }
  2108. }
  2109. }
  2110. }
  2111. }
  2112. /**
  2113. * Updates named ranges due to moving of cells
  2114. */
  2115. public void updateNamesAfterCellShift(FormulaShifter shifter) {
  2116. for (int i = 0 ; i < getNumNames() ; ++i){
  2117. NameRecord nr = getNameRecord(i);
  2118. Ptg[] ptgs = nr.getNameDefinition();
  2119. if (shifter.adjustFormula(ptgs, nr.getSheetNumber())) {
  2120. nr.setNameDefinition(ptgs);
  2121. }
  2122. }
  2123. }
  2124. }