Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import static org.apache.poi.xssf.usermodel.helpers.XSSFPasswordHelper.setPassword;
  18. import static org.apache.poi.xssf.usermodel.helpers.XSSFPasswordHelper.validatePassword;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.util.*;
  23. import javax.xml.namespace.QName;
  24. import javax.xml.stream.XMLStreamException;
  25. import javax.xml.stream.XMLStreamReader;
  26. import org.apache.logging.log4j.LogManager;
  27. import org.apache.logging.log4j.Logger;
  28. import org.apache.poi.ooxml.POIXMLDocumentPart;
  29. import org.apache.poi.ooxml.POIXMLException;
  30. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  31. import org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException;
  32. import org.apache.poi.openxml4j.opc.*;
  33. import org.apache.poi.poifs.crypt.HashAlgorithm;
  34. import org.apache.poi.ss.SpreadsheetVersion;
  35. import org.apache.poi.ss.formula.FormulaShifter;
  36. import org.apache.poi.ss.formula.SheetNameFormatter;
  37. import org.apache.poi.ss.usermodel.*;
  38. import org.apache.poi.ss.util.AreaReference;
  39. import org.apache.poi.ss.util.CellAddress;
  40. import org.apache.poi.ss.util.CellRangeAddress;
  41. import org.apache.poi.ss.util.CellRangeAddressList;
  42. import org.apache.poi.ss.util.CellReference;
  43. import org.apache.poi.ss.util.PaneInformation;
  44. import org.apache.poi.ss.util.SSCellRange;
  45. import org.apache.poi.ss.util.SheetUtil;
  46. import org.apache.poi.util.Beta;
  47. import org.apache.poi.util.Internal;
  48. import org.apache.poi.util.Removal;
  49. import org.apache.poi.util.Units;
  50. import org.apache.poi.xssf.model.Comments;
  51. import org.apache.poi.xssf.usermodel.XSSFPivotTable.PivotTableReferenceConfigurator;
  52. import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
  53. import org.apache.poi.xssf.usermodel.helpers.XSSFColumnShifter;
  54. import org.apache.poi.xssf.usermodel.helpers.XSSFIgnoredErrorHelper;
  55. import org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter;
  56. import org.apache.xmlbeans.XmlCursor;
  57. import org.apache.xmlbeans.XmlException;
  58. import org.apache.xmlbeans.XmlObject;
  59. import org.apache.xmlbeans.XmlOptions;
  60. import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
  61. /**
  62. * High level representation of a SpreadsheetML worksheet.
  63. *
  64. * <p>
  65. * Sheets are the central structures within a workbook, and are where a user does most of his spreadsheet work.
  66. * The most common type of sheet is the worksheet, which is represented as a grid of cells. Worksheet cells can
  67. * contain text, numbers, dates, and formulas. Cells can also be formatted.
  68. * </p>
  69. */
  70. public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetExtensions {
  71. private static final Logger LOG = LogManager.getLogger(XSSFSheet.class);
  72. private static final double DEFAULT_ROW_HEIGHT = 15.0;
  73. private static final double DEFAULT_MARGIN_HEADER = 0.3;
  74. private static final double DEFAULT_MARGIN_FOOTER = 0.3;
  75. private static final double DEFAULT_MARGIN_TOP = 0.75;
  76. private static final double DEFAULT_MARGIN_BOTTOM = 0.75;
  77. private static final double DEFAULT_MARGIN_LEFT = 0.7;
  78. private static final double DEFAULT_MARGIN_RIGHT = 0.7;
  79. //TODO make the two variable below private!
  80. protected CTSheet sheet;
  81. protected CTWorksheet worksheet;
  82. private final SortedMap<Integer, XSSFRow> _rows = new TreeMap<>();
  83. private List<XSSFHyperlink> hyperlinks;
  84. private ColumnHelper columnHelper;
  85. private Comments sheetComments;
  86. /**
  87. * cache of master shared formulas in this sheet.
  88. * Master shared formula is the first formula in a group of shared formulas is saved in the f element.
  89. */
  90. private Map<Integer, CTCellFormula> sharedFormulas;
  91. private SortedMap<String,XSSFTable> tables;
  92. private List<CellRangeAddress> arrayFormulas;
  93. private final XSSFDataValidationHelper dataValidationHelper;
  94. private XSSFVMLDrawing xssfvmlDrawing;
  95. private CellRangeAddress dimensionOverride;
  96. /**
  97. * Creates new XSSFSheet - called by XSSFWorkbook to create a sheet from scratch.
  98. *
  99. * @see XSSFWorkbook#createSheet()
  100. */
  101. protected XSSFSheet() {
  102. super();
  103. dataValidationHelper = new XSSFDataValidationHelper(this);
  104. onDocumentCreate();
  105. }
  106. /**
  107. * Creates an XSSFSheet representing the given package part and relationship.
  108. * Should only be called by XSSFWorkbook when reading in an existing file.
  109. *
  110. * @param part - The package part that holds xml data representing this sheet.
  111. *
  112. * @since POI 3.14-Beta1
  113. */
  114. protected XSSFSheet(PackagePart part) {
  115. super(part);
  116. dataValidationHelper = new XSSFDataValidationHelper(this);
  117. }
  118. /**
  119. * Returns the parent XSSFWorkbook
  120. *
  121. * @return the parent XSSFWorkbook
  122. */
  123. @Override
  124. public XSSFWorkbook getWorkbook() {
  125. return (XSSFWorkbook)getParent();
  126. }
  127. /**
  128. * Initialize worksheet data when reading in an exisiting file.
  129. */
  130. @Override
  131. protected void onDocumentRead() {
  132. try (InputStream stream = getPackagePart().getInputStream()) {
  133. read(stream);
  134. } catch (IOException e){
  135. throw new POIXMLException(e);
  136. }
  137. }
  138. protected void read(InputStream is) throws IOException {
  139. try {
  140. worksheet = WorksheetDocument.Factory.parse(is, DEFAULT_XML_OPTIONS).getWorksheet();
  141. } catch (XmlException e){
  142. throw new POIXMLException(e);
  143. }
  144. initRows(worksheet);
  145. columnHelper = new ColumnHelper(worksheet);
  146. // Look for bits we're interested in
  147. for(RelationPart rp : getRelationParts()){
  148. POIXMLDocumentPart p = rp.getDocumentPart();
  149. if(p instanceof Comments) {
  150. sheetComments = (Comments)p;
  151. sheetComments.setSheet(this);
  152. }
  153. if(p instanceof XSSFTable) {
  154. tables.put( rp.getRelationship().getId(), (XSSFTable)p );
  155. }
  156. if(p instanceof XSSFPivotTable) {
  157. getWorkbook().getPivotTables().add((XSSFPivotTable) p);
  158. }
  159. }
  160. // Process external hyperlinks for the sheet, if there are any
  161. initHyperlinks();
  162. }
  163. /**
  164. * Initialize worksheet data when creating a new sheet.
  165. */
  166. @Override
  167. protected void onDocumentCreate(){
  168. worksheet = newSheet();
  169. initRows(worksheet);
  170. columnHelper = new ColumnHelper(worksheet);
  171. hyperlinks = new ArrayList<>();
  172. }
  173. private void initRows(CTWorksheet worksheetParam) {
  174. if (worksheetParam.getSheetData() == null || worksheetParam.getSheetData().getRowArray() == null) {
  175. throw new IllegalArgumentException("Had empty sheet data when initializing the sheet");
  176. }
  177. _rows.clear();
  178. tables = new TreeMap<>();
  179. sharedFormulas = new HashMap<>();
  180. arrayFormulas = new ArrayList<>();
  181. for (CTRow row : worksheetParam.getSheetData().getRowArray()) {
  182. XSSFRow r = new XSSFRow(row, this);
  183. // Performance optimization: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  184. //noinspection UnnecessaryBoxing
  185. final Integer rownumI = Integer.valueOf(r.getRowNum()); // NOSONAR
  186. _rows.put(rownumI, r);
  187. }
  188. }
  189. /**
  190. * Read hyperlink relations, link them with CTHyperlink beans in this worksheet
  191. * and initialize the internal array of XSSFHyperlink objects
  192. */
  193. private void initHyperlinks() {
  194. hyperlinks = new ArrayList<>();
  195. if(!worksheet.isSetHyperlinks()) {
  196. return;
  197. }
  198. try {
  199. PackageRelationshipCollection hyperRels =
  200. getPackagePart().getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation());
  201. // Turn each one into a XSSFHyperlink
  202. for(CTHyperlink hyperlink : worksheet.getHyperlinks().getHyperlinkArray()) {
  203. PackageRelationship hyperRel = null;
  204. if(hyperlink.getId() != null) {
  205. hyperRel = hyperRels.getRelationshipByID(hyperlink.getId());
  206. }
  207. hyperlinks.add( new XSSFHyperlink(hyperlink, hyperRel) );
  208. }
  209. } catch (InvalidFormatException e){
  210. throw new POIXMLException(e);
  211. }
  212. }
  213. /**
  214. * Create a new CTWorksheet instance with all values set to defaults
  215. *
  216. * @return a new instance
  217. */
  218. private static CTWorksheet newSheet(){
  219. CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
  220. CTSheetFormatPr ctFormat = worksheet.addNewSheetFormatPr();
  221. ctFormat.setDefaultRowHeight(DEFAULT_ROW_HEIGHT);
  222. CTSheetView ctView = worksheet.addNewSheetViews().addNewSheetView();
  223. ctView.setWorkbookViewId(0);
  224. worksheet.addNewDimension().setRef("A1");
  225. worksheet.addNewSheetData();
  226. CTPageMargins ctMargins = worksheet.addNewPageMargins();
  227. ctMargins.setBottom(DEFAULT_MARGIN_BOTTOM);
  228. ctMargins.setFooter(DEFAULT_MARGIN_FOOTER);
  229. ctMargins.setHeader(DEFAULT_MARGIN_HEADER);
  230. ctMargins.setLeft(DEFAULT_MARGIN_LEFT);
  231. ctMargins.setRight(DEFAULT_MARGIN_RIGHT);
  232. ctMargins.setTop(DEFAULT_MARGIN_TOP);
  233. return worksheet;
  234. }
  235. /**
  236. * Provide access to the CTWorksheet bean holding this sheet's data
  237. *
  238. * @return the CTWorksheet bean holding this sheet's data
  239. */
  240. @Internal
  241. public CTWorksheet getCTWorksheet() {
  242. return this.worksheet;
  243. }
  244. public ColumnHelper getColumnHelper() {
  245. return columnHelper;
  246. }
  247. /**
  248. * Returns the name of this sheet
  249. *
  250. * @return the name of this sheet
  251. */
  252. @Override
  253. public String getSheetName() {
  254. return sheet.getName();
  255. }
  256. /**
  257. * Adds a merged region of cells on a sheet.
  258. *
  259. * @param region to merge
  260. * @return index of this region
  261. * @throws IllegalArgumentException if region contains fewer than 2 cells
  262. * @throws IllegalStateException if region intersects with a multi-cell array formula
  263. * @throws IllegalStateException if region intersects with an existing region on this sheet
  264. */
  265. @Override
  266. public int addMergedRegion(CellRangeAddress region) {
  267. return addMergedRegion(region, true);
  268. }
  269. /**
  270. * Adds a merged region of cells (hence those cells form one).
  271. * Skips validation. It is possible to create overlapping merged regions
  272. * or create a merged region that intersects a multi-cell array formula
  273. * with this formula, which may result in a corrupt workbook.
  274. *
  275. * To check for merged regions overlapping array formulas or other merged regions
  276. * after addMergedRegionUnsafe has been called, call {@link #validateMergedRegions()}, which runs in O(n^2) time.
  277. *
  278. * @param region to merge
  279. * @return index of this region
  280. * @throws IllegalArgumentException if region contains fewer than 2 cells
  281. */
  282. @Override
  283. public int addMergedRegionUnsafe(CellRangeAddress region) {
  284. return addMergedRegion(region, false);
  285. }
  286. /**
  287. * Adds a merged region of cells (hence those cells form one).
  288. * If validate is true, check to make sure adding the merged region to the sheet doesn't create a corrupt workbook
  289. * If validate is false, skips the expensive merged region checks, but may produce a corrupt workbook.
  290. *
  291. * @param region to merge
  292. * @param validate whether to validate merged region
  293. * @return 0-based index of this region
  294. * @throws IllegalArgumentException if region contains fewer than 2 cells (this check is inexpensive and is performed regardless of {@code validate})
  295. * @throws IllegalStateException if region intersects with a multi-cell array formula
  296. * @throws IllegalStateException if region intersects with an existing region on this sheet
  297. */
  298. private int addMergedRegion(CellRangeAddress region, boolean validate) {
  299. if (region.getNumberOfCells() < 2) {
  300. throw new IllegalArgumentException("Merged region " + region.formatAsString() + " must contain 2 or more cells");
  301. }
  302. region.validate(SpreadsheetVersion.EXCEL2007);
  303. if (validate) {
  304. // throw IllegalStateException if the argument CellRangeAddress intersects with
  305. // a multi-cell array formula defined in this sheet
  306. validateArrayFormulas(region);
  307. // Throw IllegalStateException if the argument CellRangeAddress intersects with
  308. // a merged region already in this sheet
  309. validateMergedRegions(region);
  310. }
  311. CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells();
  312. CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell();
  313. ctMergeCell.setRef(region.formatAsString());
  314. long count = ctMergeCells.getCount();
  315. if (count == 0) {
  316. count=ctMergeCells.sizeOfMergeCellArray();
  317. } else {
  318. count++;
  319. }
  320. // also adjust the number of merged regions overall
  321. ctMergeCells.setCount(count);
  322. return Math.toIntExact(count-1);
  323. }
  324. /**
  325. * Verify that the candidate region does not intersect with an existing multi-cell array formula in this sheet
  326. *
  327. * @param region a region that is validated.
  328. * @throws IllegalStateException if candidate region intersects an existing array formula in this sheet
  329. */
  330. private void validateArrayFormulas(CellRangeAddress region) {
  331. // FIXME: this may be faster if it looped over array formulas directly rather than looping over each cell in
  332. // the region and searching if that cell belongs to an array formula
  333. int firstRow = region.getFirstRow();
  334. int firstColumn = region.getFirstColumn();
  335. int lastRow = region.getLastRow();
  336. int lastColumn = region.getLastColumn();
  337. // for each cell in sheet, if cell belongs to an array formula, check if merged region intersects array formula cells
  338. for (int rowIn = firstRow; rowIn <= lastRow; rowIn++) {
  339. XSSFRow row = getRow(rowIn);
  340. if (row == null) {
  341. continue;
  342. }
  343. for (int colIn = firstColumn; colIn <= lastColumn; colIn++) {
  344. XSSFCell cell = row.getCell(colIn);
  345. if (cell == null) {
  346. continue;
  347. }
  348. if (cell.isPartOfArrayFormulaGroup()) {
  349. CellRangeAddress arrayRange = cell.getArrayFormulaRange();
  350. if (arrayRange.getNumberOfCells() > 1 && region.intersects(arrayRange)) {
  351. String msg = "The range " + region.formatAsString() + " intersects with a multi-cell array formula. " +
  352. "You cannot merge cells of an array.";
  353. throw new IllegalStateException(msg);
  354. }
  355. }
  356. }
  357. }
  358. }
  359. /**
  360. * Verify that none of the merged regions intersect a multi-cell array formula in this sheet
  361. *
  362. * @throws IllegalStateException if candidate region intersects an existing array formula in this sheet
  363. */
  364. private void checkForMergedRegionsIntersectingArrayFormulas() {
  365. for (CellRangeAddress region : getMergedRegions()) {
  366. validateArrayFormulas(region);
  367. }
  368. }
  369. /**
  370. * Verify that candidate region does not intersect with an existing merged region in this sheet
  371. *
  372. * @param candidateRegion the range of cells to verify
  373. * @throws IllegalStateException if candidate region intersects an existing merged region in this sheet (or candidateRegion is already merged in this sheet)
  374. */
  375. private void validateMergedRegions(CellRangeAddress candidateRegion) {
  376. for (final CellRangeAddress existingRegion : getMergedRegions()) {
  377. if (existingRegion.intersects(candidateRegion)) {
  378. throw new IllegalStateException("Cannot add merged region " + candidateRegion.formatAsString() +
  379. " to sheet because it overlaps with an existing merged region (" + existingRegion.formatAsString() + ").");
  380. }
  381. }
  382. }
  383. /**
  384. * Verify that no merged regions intersect another merged region in this sheet.
  385. *
  386. * @throws IllegalStateException if at least one region intersects with another merged region in this sheet
  387. */
  388. private void checkForIntersectingMergedRegions() {
  389. final List<CellRangeAddress> regions = getMergedRegions();
  390. final int size = regions.size();
  391. for (int i=0; i < size; i++) {
  392. final CellRangeAddress region = regions.get(i);
  393. for (final CellRangeAddress other : regions.subList(i+1, regions.size())) {
  394. if (region.intersects(other)) {
  395. String msg = "The range " + region.formatAsString() +
  396. " intersects with another merged region " +
  397. other.formatAsString() + " in this sheet";
  398. throw new IllegalStateException(msg);
  399. }
  400. }
  401. }
  402. }
  403. /**
  404. * Verify that merged regions do not intersect multi-cell array formulas and
  405. * no merged regions intersect another merged region in this sheet.
  406. *
  407. * @throws IllegalStateException if region intersects with a multi-cell array formula
  408. * @throws IllegalStateException if at least one region intersects with another merged region in this sheet
  409. */
  410. @Override
  411. public void validateMergedRegions() {
  412. checkForMergedRegionsIntersectingArrayFormulas();
  413. checkForIntersectingMergedRegions();
  414. }
  415. /**
  416. * Adjusts the column width to fit the contents.
  417. *
  418. * This process can be relatively slow on large sheets, so this should
  419. * normally only be called once per column, at the end of your
  420. * processing.
  421. *
  422. * @param column the column index
  423. */
  424. @Override
  425. public void autoSizeColumn(int column) {
  426. autoSizeColumn(column, false);
  427. }
  428. /**
  429. * Adjusts the column width to fit the contents.
  430. * <p>
  431. * This process can be relatively slow on large sheets, so this should
  432. * normally only be called once per column, at the end of your
  433. * processing.
  434. * </p>
  435. * You can specify whether the content of merged cells should be considered or ignored.
  436. * Default is to ignore merged cells.
  437. *
  438. * @param column the column index
  439. * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column
  440. */
  441. @Override
  442. public void autoSizeColumn(int column, boolean useMergedCells) {
  443. double width = SheetUtil.getColumnWidth(this, column, useMergedCells);
  444. if (width != -1) {
  445. width *= 256;
  446. int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters
  447. if (width > maxColumnWidth) {
  448. width = maxColumnWidth;
  449. }
  450. setColumnWidth(column, Math.toIntExact(Math.round(width)));
  451. columnHelper.setColBestFit(column, true);
  452. }
  453. }
  454. /**
  455. * Return the sheet's existing drawing, or null if there isn't yet one.
  456. *
  457. * Use {@link #createDrawingPatriarch()} to get or create
  458. *
  459. * @return a SpreadsheetML drawing
  460. */
  461. @Override
  462. public XSSFDrawing getDrawingPatriarch() {
  463. CTDrawing ctDrawing = getCTDrawing();
  464. if (ctDrawing != null) {
  465. // Search the referenced drawing in the list of the sheet's relations
  466. for (RelationPart rp : getRelationParts()){
  467. POIXMLDocumentPart p = rp.getDocumentPart();
  468. if (p instanceof XSSFDrawing) {
  469. XSSFDrawing dr = (XSSFDrawing)p;
  470. String drId = rp.getRelationship().getId();
  471. if (drId.equals(ctDrawing.getId())){
  472. return dr;
  473. }
  474. }
  475. }
  476. LOG.atError().log("Can't find drawing with id={} in the list of the sheet's relationships", ctDrawing.getId());
  477. }
  478. return null;
  479. }
  480. /**
  481. * Create a new SpreadsheetML drawing. If this sheet already contains a drawing - return that.
  482. *
  483. * @return a SpreadsheetML drawing
  484. */
  485. @Override
  486. public XSSFDrawing createDrawingPatriarch() {
  487. XSSFDrawing existingDrawing = getDrawingPatriarch();
  488. if (existingDrawing != null) {
  489. return existingDrawing;
  490. }
  491. // Default drawingNumber = #drawings.size() + 1
  492. int drawingNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.DRAWINGS.getContentType()).size() + 1;
  493. drawingNumber = getNextPartNumber(XSSFRelation.DRAWINGS, drawingNumber);
  494. RelationPart rp = createRelationship(XSSFRelation.DRAWINGS, getWorkbook().getXssfFactory(), drawingNumber, false);
  495. XSSFDrawing drawing = rp.getDocumentPart();
  496. String relId = rp.getRelationship().getId();
  497. //add CT_Drawing element which indicates that this sheet contains drawing components built on the drawingML platform.
  498. //The relationship Id references the part containing the drawingML definitions.
  499. CTDrawing ctDrawing = worksheet.addNewDrawing();
  500. ctDrawing.setId(relId);
  501. // Return the newly created drawing
  502. return drawing;
  503. }
  504. /**
  505. * Get VML drawing for this sheet (aka 'legacy' drawing).
  506. *
  507. * @param autoCreate if true, then a new VML drawing part is created
  508. *
  509. * @return the VML drawing of {@code null} if the drawing was not found and autoCreate=false
  510. */
  511. @Override
  512. public XSSFVMLDrawing getVMLDrawing(boolean autoCreate) {
  513. if (xssfvmlDrawing == null) {
  514. XSSFVMLDrawing drawing = null;
  515. CTLegacyDrawing ctDrawing = getCTLegacyDrawing();
  516. if(ctDrawing == null) {
  517. if(autoCreate) {
  518. int drawingNumber = getNextPartNumber(XSSFRelation.VML_DRAWINGS,
  519. getPackagePart().getPackage().getPartsByContentType(XSSFRelation.VML_DRAWINGS.getContentType()).size());
  520. RelationPart rp = createRelationship(XSSFRelation.VML_DRAWINGS, getWorkbook().getXssfFactory(), drawingNumber, false);
  521. drawing = rp.getDocumentPart();
  522. String relId = rp.getRelationship().getId();
  523. //add CTLegacyDrawing element which indicates that this sheet contains drawing components built on the drawingML platform.
  524. //The relationship Id references the part containing the drawing definitions.
  525. ctDrawing = worksheet.addNewLegacyDrawing();
  526. ctDrawing.setId(relId);
  527. }
  528. } else {
  529. //search the referenced drawing in the list of the sheet's relations
  530. final String id = ctDrawing.getId();
  531. for (RelationPart rp : getRelationParts()){
  532. POIXMLDocumentPart p = rp.getDocumentPart();
  533. if(p instanceof XSSFVMLDrawing) {
  534. XSSFVMLDrawing dr = (XSSFVMLDrawing)p;
  535. String drId = rp.getRelationship().getId();
  536. if (drId.equals(id)) {
  537. drawing = dr;
  538. break;
  539. }
  540. // do not break here since drawing has not been found yet (see bug 52425)
  541. }
  542. }
  543. if(drawing == null){
  544. LOG.atError().log("Can't find VML drawing with id={} in the list of the sheet's relationships", id);
  545. }
  546. }
  547. xssfvmlDrawing = drawing;
  548. }
  549. return xssfvmlDrawing;
  550. }
  551. protected CTDrawing getCTDrawing() {
  552. return worksheet.getDrawing();
  553. }
  554. protected CTLegacyDrawing getCTLegacyDrawing() {
  555. return worksheet.getLegacyDrawing();
  556. }
  557. /**
  558. * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
  559. * @param colSplit Horizontal position of split.
  560. * @param rowSplit Vertical position of split.
  561. */
  562. @Override
  563. public void createFreezePane(int colSplit, int rowSplit) {
  564. createFreezePane( colSplit, rowSplit, colSplit, rowSplit );
  565. }
  566. /**
  567. * Creates a split (freezepane). Any existing freezepane or split pane is overwritten.
  568. *
  569. * <p>
  570. * If both colSplit and rowSplit are zero then the existing freeze pane is removed
  571. * </p>
  572. *
  573. * @param colSplit Horizontal position of split.
  574. * @param rowSplit Vertical position of split.
  575. * @param leftmostColumn Left column visible in right pane.
  576. * @param topRow Top row visible in bottom pane
  577. */
  578. @Override
  579. public void createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow) {
  580. final boolean removeSplit = colSplit == 0 && rowSplit == 0;
  581. final CTSheetView ctView = getDefaultSheetView(!removeSplit);
  582. if (ctView != null) {
  583. ctView.setSelectionArray(null);
  584. }
  585. // If both colSplit and rowSplit are zero then the existing freeze pane is removed
  586. if (removeSplit) {
  587. if (ctView != null && ctView.isSetPane()) {
  588. ctView.unsetPane();
  589. }
  590. return;
  591. }
  592. assert(ctView != null);
  593. final CTPane pane = (ctView.isSetPane()) ? ctView.getPane() : ctView.addNewPane();
  594. assert(pane != null);
  595. if (colSplit > 0) {
  596. pane.setXSplit(colSplit);
  597. } else if (pane.isSetXSplit()) {
  598. pane.unsetXSplit();
  599. }
  600. if (rowSplit > 0) {
  601. pane.setYSplit(rowSplit);
  602. } else if(pane.isSetYSplit()) {
  603. pane.unsetYSplit();
  604. }
  605. STPane.Enum activePane = STPane.BOTTOM_RIGHT;
  606. int pRow = topRow, pCol = leftmostColumn;
  607. if (rowSplit == 0) {
  608. pRow = 0;
  609. activePane = STPane.TOP_RIGHT;
  610. } else if (colSplit == 0) {
  611. pCol = 0;
  612. activePane = STPane.BOTTOM_LEFT;
  613. }
  614. pane.setState(STPaneState.FROZEN);
  615. pane.setTopLeftCell(new CellReference(pRow, pCol).formatAsString());
  616. pane.setActivePane(activePane);
  617. ctView.addNewSelection().setPane(activePane);
  618. }
  619. /**
  620. * Create a new row within the sheet and return the high level representation
  621. *
  622. * Note: If a row already exists at this position, it is removed/overwritten and
  623. * any existing cell is removed!
  624. *
  625. * @param rownum row number
  626. * @return High level {@link XSSFRow} object representing a row in the sheet
  627. * @see #removeRow(Row)
  628. */
  629. @Override
  630. public XSSFRow createRow(int rownum) {
  631. // Performance optimization: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  632. //noinspection UnnecessaryBoxing
  633. final Integer rownumI = Integer.valueOf(rownum); // NOSONAR
  634. CTRow ctRow;
  635. XSSFRow prev = _rows.get(rownumI);
  636. if(prev != null){
  637. // the Cells in an existing row are invalidated on-purpose, in order to clean up correctly, we
  638. // need to call the remove, so things like ArrayFormulas and CalculationChain updates are done
  639. // correctly.
  640. // We remove the cell this way as the internal cell-list is changed by the remove call and
  641. // thus would cause ConcurrentModificationException otherwise
  642. while(prev.getFirstCellNum() != -1) {
  643. prev.removeCell(prev.getCell(prev.getFirstCellNum()));
  644. }
  645. ctRow = prev.getCTRow();
  646. ctRow.set(CTRow.Factory.newInstance());
  647. } else {
  648. if(_rows.isEmpty() || rownum > _rows.lastKey()) {
  649. // we can append the new row at the end
  650. ctRow = worksheet.getSheetData().addNewRow();
  651. } else {
  652. // get number of rows where row index < rownum
  653. // --> this tells us where our row should go
  654. int idx = _rows.headMap(rownumI).size();
  655. ctRow = worksheet.getSheetData().insertNewRow(idx);
  656. }
  657. }
  658. XSSFRow r = new XSSFRow(ctRow, this);
  659. r.setRowNum(rownum);
  660. _rows.put(rownumI, r);
  661. return r;
  662. }
  663. /**
  664. * Creates a split pane. Any existing freezepane or split pane is overwritten.
  665. * @param xSplitPos Horizontal position of split (in 1/20th of a point).
  666. * @param ySplitPos Vertical position of split (in 1/20th of a point).
  667. * @param topRow Top row visible in bottom pane
  668. * @param leftmostColumn Left column visible in right pane.
  669. * @param activePane Active pane. One of: PANE_LOWER_RIGHT,
  670. * PANE_UPPER_RIGHT, PANE_LOWER_LEFT, PANE_UPPER_LEFT (but there is a
  671. * <a href="https://bz.apache.org/bugzilla/show_bug.cgi?id=66173">bug</a>, so add 1)
  672. * @see #PANE_LOWER_LEFT
  673. * @see #PANE_LOWER_RIGHT
  674. * @see #PANE_UPPER_LEFT
  675. * @see #PANE_UPPER_RIGHT
  676. * @deprecated use {@link #createSplitPane(int, int, int, int, PaneType)}
  677. */
  678. @Override
  679. @Deprecated
  680. @Removal(version = "7.0.0")
  681. public void createSplitPane(int xSplitPos, int ySplitPos, int leftmostColumn, int topRow, int activePane) {
  682. createFreezePane(xSplitPos, ySplitPos, leftmostColumn, topRow);
  683. if (xSplitPos > 0 || ySplitPos > 0) {
  684. final CTPane pane = getPane(true);
  685. pane.setState(STPaneState.SPLIT);
  686. pane.setActivePane(STPane.Enum.forInt(activePane));
  687. }
  688. }
  689. /**
  690. * Creates a split pane. Any existing freezepane or split pane is overwritten.
  691. * @param xSplitPos Horizontal position of split (in 1/20th of a point).
  692. * @param ySplitPos Vertical position of split (in 1/20th of a point).
  693. * @param topRow Top row visible in bottom pane
  694. * @param leftmostColumn Left column visible in right pane.
  695. * @param activePane Active pane.
  696. * @see PaneType
  697. * @since POI 5.2.3
  698. */
  699. @Override
  700. public void createSplitPane(int xSplitPos, int ySplitPos, int leftmostColumn, int topRow, PaneType activePane) {
  701. createFreezePane(xSplitPos, ySplitPos, leftmostColumn, topRow);
  702. if (xSplitPos > 0 || ySplitPos > 0) {
  703. final CTPane pane = getPane(true);
  704. pane.setState(STPaneState.SPLIT);
  705. STPane.Enum stPaneEnum;
  706. switch (activePane) {
  707. case LOWER_RIGHT:
  708. stPaneEnum = STPane.BOTTOM_RIGHT;
  709. break;
  710. case UPPER_RIGHT:
  711. stPaneEnum = STPane.TOP_RIGHT;
  712. break;
  713. case LOWER_LEFT:
  714. stPaneEnum = STPane.BOTTOM_LEFT;
  715. break;
  716. case UPPER_LEFT:
  717. default:
  718. stPaneEnum = STPane.TOP_LEFT;
  719. break;
  720. }
  721. pane.setActivePane(stPaneEnum);
  722. }
  723. }
  724. /**
  725. * Return cell comment at row, column, if one exists. Otherwise returns null.
  726. *
  727. * @param address the location of the cell comment
  728. * @return the cell comment, if one exists. Otherwise, return null.
  729. */
  730. @Override
  731. public XSSFComment getCellComment(CellAddress address) {
  732. if (sheetComments == null) {
  733. return null;
  734. }
  735. return sheetComments.findCellComment(address);
  736. }
  737. /**
  738. * Returns all cell comments on this sheet.
  739. * @return A map of each Comment in the sheet, keyed on the cell address where
  740. * the comment is located.
  741. */
  742. @Override
  743. public Map<CellAddress, XSSFComment> getCellComments() {
  744. if (sheetComments == null) {
  745. return Collections.emptyMap();
  746. }
  747. // the cell comments in sheetComments.getCellComments() do not have the client anchors set
  748. Map<CellAddress, XSSFComment> map = new HashMap<>();
  749. for(Iterator<CellAddress> iter = sheetComments.getCellAddresses(); iter.hasNext(); ) {
  750. CellAddress address = iter.next();
  751. map.put(address, getCellComment(address));
  752. }
  753. return map;
  754. }
  755. /**
  756. * Get a Hyperlink in this sheet anchored at row, column
  757. *
  758. * @param row The row where the hyperlink is anchored
  759. * @param column The column where the hyperlink is anchored
  760. * @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns null
  761. */
  762. @Override
  763. public XSSFHyperlink getHyperlink(int row, int column) {
  764. return getHyperlink(new CellAddress(row, column));
  765. }
  766. /**
  767. * Get a Hyperlink in this sheet located in a cell specified by {code addr}
  768. *
  769. * @param addr The address of the cell containing the hyperlink
  770. * @return hyperlink if there is a hyperlink anchored at {@code addr}; otherwise returns {@code null}
  771. * @since POI 3.15 beta 3
  772. */
  773. @Override
  774. public XSSFHyperlink getHyperlink(CellAddress addr) {
  775. for (XSSFHyperlink hyperlink : getHyperlinkList()) {
  776. if (addr.getRow() >= hyperlink.getFirstRow() && addr.getRow() <= hyperlink.getLastRow()
  777. && addr.getColumn() >= hyperlink.getFirstColumn() && addr.getColumn() <= hyperlink.getLastColumn()) {
  778. return hyperlink;
  779. }
  780. }
  781. return null;
  782. }
  783. /**
  784. * Get a list of Hyperlinks in this sheet
  785. *
  786. * @return Hyperlinks for the sheet
  787. */
  788. @Override
  789. public List<XSSFHyperlink> getHyperlinkList() {
  790. return Collections.unmodifiableList(hyperlinks);
  791. }
  792. private int[] getBreaks(CTPageBreak ctPageBreak) {
  793. CTBreak[] brkArray = ctPageBreak.getBrkArray();
  794. int[] breaks = new int[brkArray.length];
  795. for (int i = 0 ; i < brkArray.length ; i++) {
  796. breaks[i] = Math.toIntExact(brkArray[i].getId() - 1);
  797. }
  798. return breaks;
  799. }
  800. private void removeBreak(int index, CTPageBreak ctPageBreak) {
  801. int index1 = index + 1;
  802. CTBreak[] brkArray = ctPageBreak.getBrkArray();
  803. for (int i = 0 ; i < brkArray.length ; i++) {
  804. if (brkArray[i].getId() == index1) {
  805. ctPageBreak.removeBrk(i);
  806. // TODO: check if we can break here, i.e. if a page can have more than 1 break on the same id
  807. }
  808. }
  809. }
  810. /**
  811. * Vertical page break information used for print layout view, page layout view, drawing print breaks
  812. * in normal view, and for printing the worksheet.
  813. *
  814. * @return column indexes of all the vertical page breaks, never {@code null}
  815. */
  816. @Override
  817. public int[] getColumnBreaks() {
  818. return worksheet.isSetColBreaks() ? getBreaks(worksheet.getColBreaks()) : new int[0];
  819. }
  820. /**
  821. * Get the actual column width (in units of 1/256th of a character width )
  822. *
  823. * <p>
  824. * Note, the returned value is always greater that {@link #getDefaultColumnWidth()} because the latter does not include margins.
  825. * Actual column width measured as the number of characters of the maximum digit width of the
  826. * numbers 0, 1, 2, ..., 9 as rendered in the normal style's font. There are 4 pixels of margin
  827. * padding (two on each side), plus 1 pixel padding for the gridlines.
  828. * </p>
  829. *
  830. * @param columnIndex - the column to set (0-based)
  831. * @return width - the width in units of 1/256th of a character width
  832. */
  833. @Override
  834. public int getColumnWidth(int columnIndex) {
  835. CTCol col = columnHelper.getColumn(columnIndex, false);
  836. double width = col == null || !col.isSetWidth() ? getDefaultColumnWidth() : col.getWidth();
  837. return Math.toIntExact(Math.round(width*256));
  838. }
  839. /**
  840. * Get the actual column width in pixels
  841. *
  842. * <p>
  843. * Please note, that this method works correctly only for workbooks
  844. * with the default font size (Calibri 11pt for .xlsx).
  845. * </p>
  846. */
  847. @Override
  848. public float getColumnWidthInPixels(int columnIndex) {
  849. float widthIn256 = getColumnWidth(columnIndex);
  850. return (float)(widthIn256/256.0*Units.DEFAULT_CHARACTER_WIDTH);
  851. }
  852. /**
  853. * Get the default column width for the sheet (if the columns do not define their own width) in
  854. * characters.
  855. * <p>
  856. * Note, this value is different from {@link #getColumnWidth(int)}. The latter is always greater and includes
  857. * 4 pixels of margin padding (two on each side), plus 1 pixel padding for the gridlines.
  858. * </p>
  859. * @return column width, default value is 8
  860. */
  861. @Override
  862. public int getDefaultColumnWidth() {
  863. CTSheetFormatPr pr = worksheet.getSheetFormatPr();
  864. return pr == null ? 8 : Math.toIntExact(pr.getBaseColWidth());
  865. }
  866. /**
  867. * Get the default row height for the sheet (if the rows do not define their own height) in
  868. * twips (1/20 of a point)
  869. *
  870. * @return default row height
  871. */
  872. @Override
  873. public short getDefaultRowHeight() {
  874. return (short)(getDefaultRowHeightInPoints() * Font.TWIPS_PER_POINT);
  875. }
  876. /**
  877. * Get the default row height for the sheet measured in point size (if the rows do not define their own height).
  878. *
  879. * @return default row height in points
  880. */
  881. @Override
  882. public float getDefaultRowHeightInPoints() {
  883. CTSheetFormatPr pr = worksheet.getSheetFormatPr();
  884. return (float)(pr == null ? 0 : pr.getDefaultRowHeight());
  885. }
  886. private CTSheetFormatPr getSheetTypeSheetFormatPr() {
  887. return worksheet.isSetSheetFormatPr() ?
  888. worksheet.getSheetFormatPr() :
  889. worksheet.addNewSheetFormatPr();
  890. }
  891. /**
  892. * Returns the CellStyle that applies to the given
  893. * (0 based) column, or null if no style has been
  894. * set for that column
  895. */
  896. @Override
  897. public CellStyle getColumnStyle(int column) {
  898. int idx = columnHelper.getColDefaultStyle(column);
  899. return getWorkbook().getCellStyleAt(idx == -1 ? 0 : idx);
  900. }
  901. /**
  902. * Sets whether the worksheet is displayed from right to left instead of from left to right.
  903. *
  904. * @param value true for right to left, false otherwise.
  905. */
  906. @Override
  907. public void setRightToLeft(boolean value) {
  908. final CTSheetView dsv = getDefaultSheetView(true);
  909. assert(dsv != null);
  910. dsv.setRightToLeft(value);
  911. }
  912. /**
  913. * Whether the text is displayed in right-to-left mode in the window
  914. *
  915. * @return whether the text is displayed in right-to-left mode in the window
  916. */
  917. @Override
  918. public boolean isRightToLeft() {
  919. final CTSheetView dsv = getDefaultSheetView(false);
  920. return (dsv != null && dsv.getRightToLeft());
  921. }
  922. /**
  923. * Get whether to display the guts or not,
  924. * default value is true
  925. *
  926. * @return boolean - guts or no guts
  927. */
  928. @Override
  929. public boolean getDisplayGuts() {
  930. CTSheetPr sheetPr = getSheetTypeSheetPr();
  931. CTOutlinePr outlinePr = sheetPr.getOutlinePr() == null ? CTOutlinePr.Factory.newInstance() : sheetPr.getOutlinePr();
  932. return outlinePr.getShowOutlineSymbols();
  933. }
  934. /**
  935. * Set whether to display the guts or not
  936. *
  937. * @param value - guts or no guts
  938. */
  939. @Override
  940. public void setDisplayGuts(boolean value) {
  941. CTSheetPr sheetPr = getSheetTypeSheetPr();
  942. CTOutlinePr outlinePr = sheetPr.getOutlinePr() == null ? sheetPr.addNewOutlinePr() : sheetPr.getOutlinePr();
  943. outlinePr.setShowOutlineSymbols(value);
  944. }
  945. /**
  946. * Gets the flag indicating whether the window should show 0 (zero) in cells containing zero value.
  947. * When false, cells with zero value appear blank instead of showing the number zero.
  948. *
  949. * @return whether all zero values on the worksheet are displayed (defaults to true)
  950. */
  951. @Override
  952. public boolean isDisplayZeros(){
  953. final CTSheetView dsv = getDefaultSheetView(false);
  954. return (dsv == null) || dsv.getShowZeros();
  955. }
  956. /**
  957. * Set whether the window should show 0 (zero) in cells containing zero value.
  958. * When false, cells with zero value appear blank instead of showing the number zero.
  959. *
  960. * @param value whether to display or hide all zero values on the worksheet
  961. */
  962. @Override
  963. public void setDisplayZeros(boolean value){
  964. final CTSheetView view = getDefaultSheetView(true);
  965. assert(view != null);
  966. view.setShowZeros(value);
  967. }
  968. /**
  969. * Gets the first row on the sheet
  970. *
  971. * @return the number of the first logical row on the sheet, zero based
  972. */
  973. @Override
  974. public int getFirstRowNum() {
  975. return _rows.isEmpty() ? -1 : _rows.firstKey();
  976. }
  977. /**
  978. * Flag indicating whether the Fit to Page print option is enabled.
  979. *
  980. * @return {@code true}
  981. */
  982. @Override
  983. public boolean getFitToPage() {
  984. CTSheetPr sheetPr = getSheetTypeSheetPr();
  985. CTPageSetUpPr psSetup = (sheetPr == null || !sheetPr.isSetPageSetUpPr()) ?
  986. CTPageSetUpPr.Factory.newInstance() : sheetPr.getPageSetUpPr();
  987. return psSetup.getFitToPage();
  988. }
  989. private CTSheetPr getSheetTypeSheetPr() {
  990. if (worksheet.getSheetPr() == null) {
  991. worksheet.setSheetPr(CTSheetPr.Factory.newInstance());
  992. }
  993. return worksheet.getSheetPr();
  994. }
  995. private CTHeaderFooter getSheetTypeHeaderFooter() {
  996. if (worksheet.getHeaderFooter() == null) {
  997. worksheet.setHeaderFooter(CTHeaderFooter.Factory.newInstance());
  998. }
  999. return worksheet.getHeaderFooter();
  1000. }
  1001. /**
  1002. * Returns the default footer for the sheet,
  1003. * creating one as needed.
  1004. * You may also want to look at
  1005. * {@link #getFirstFooter()},
  1006. * {@link #getOddFooter()} and
  1007. * {@link #getEvenFooter()}
  1008. */
  1009. @Override
  1010. public Footer getFooter() {
  1011. // The default footer is an odd footer
  1012. return getOddFooter();
  1013. }
  1014. /**
  1015. * Returns the default header for the sheet,
  1016. * creating one as needed.
  1017. * You may also want to look at
  1018. * {@link #getFirstHeader()},
  1019. * {@link #getOddHeader()} and
  1020. * {@link #getEvenHeader()}
  1021. */
  1022. @Override
  1023. public Header getHeader() {
  1024. // The default header is an odd header
  1025. return getOddHeader();
  1026. }
  1027. /**
  1028. * Returns the odd footer. Used on all pages unless
  1029. * other footers also present, when used on only
  1030. * odd pages.
  1031. */
  1032. public Footer getOddFooter() {
  1033. return new XSSFOddFooter(getSheetTypeHeaderFooter());
  1034. }
  1035. /**
  1036. * Returns the even footer. Not there by default, but
  1037. * when set, used on even pages.
  1038. */
  1039. public Footer getEvenFooter() {
  1040. return new XSSFEvenFooter(getSheetTypeHeaderFooter());
  1041. }
  1042. /**
  1043. * Returns the first page footer. Not there by
  1044. * default, but when set, used on the first page.
  1045. */
  1046. public Footer getFirstFooter() {
  1047. return new XSSFFirstFooter(getSheetTypeHeaderFooter());
  1048. }
  1049. /**
  1050. * Returns the odd header. Used on all pages unless
  1051. * other headers also present, when used on only
  1052. * odd pages.
  1053. */
  1054. public Header getOddHeader() {
  1055. return new XSSFOddHeader(getSheetTypeHeaderFooter());
  1056. }
  1057. /**
  1058. * Returns the even header. Not there by default, but
  1059. * when set, used on even pages.
  1060. */
  1061. public Header getEvenHeader() {
  1062. return new XSSFEvenHeader(getSheetTypeHeaderFooter());
  1063. }
  1064. /**
  1065. * Returns the first page header. Not there by
  1066. * default, but when set, used on the first page.
  1067. */
  1068. public Header getFirstHeader() {
  1069. return new XSSFFirstHeader(getSheetTypeHeaderFooter());
  1070. }
  1071. /**
  1072. * Determine whether printed output for this sheet will be horizontally centered.
  1073. */
  1074. @Override
  1075. public boolean getHorizontallyCenter() {
  1076. CTPrintOptions opts = worksheet.getPrintOptions();
  1077. return opts != null && opts.getHorizontalCentered();
  1078. }
  1079. @Override
  1080. public int getLastRowNum() {
  1081. // _rows.getLastKey() (O(logN)) or caching last row (O(1))?
  1082. // A test with 1_000_000 rows shows that querying getLastRowNum with lastKey() implementation takes ~40 ms,
  1083. // and ~1.2 ms with cached implementation. 40 ms is negligible compared to the time of evaluation a million
  1084. // cells, and the lastKey implementation is much more elegant and less error prone than caching.
  1085. return _rows.isEmpty() ? -1 : _rows.lastKey();
  1086. }
  1087. @Override
  1088. public short getLeftCol() {
  1089. String cellRef = worksheet.getSheetViews().getSheetViewArray(0).getTopLeftCell();
  1090. if(cellRef == null) {
  1091. return 0;
  1092. }
  1093. CellReference cellReference = new CellReference(cellRef);
  1094. return cellReference.getCol();
  1095. }
  1096. /**
  1097. * Gets the size of the margin in inches.
  1098. *
  1099. * @param margin which margin to get
  1100. * @return the size of the margin
  1101. * @see Sheet#LeftMargin
  1102. * @see Sheet#RightMargin
  1103. * @see Sheet#TopMargin
  1104. * @see Sheet#BottomMargin
  1105. * @see Sheet#HeaderMargin
  1106. * @see Sheet#FooterMargin
  1107. * @deprecated use {@link #getMargin(PageMargin)}
  1108. */
  1109. @Override
  1110. @Deprecated
  1111. @Removal(version = "7.0.0")
  1112. public double getMargin(short margin) {
  1113. return getMargin(PageMargin.getByShortValue(margin));
  1114. }
  1115. /**
  1116. * Gets the size of the margin in inches.
  1117. *
  1118. * @param margin which margin to get
  1119. * @return the size of the margin
  1120. * @since POI 5.2.3
  1121. */
  1122. @Override
  1123. public double getMargin(PageMargin margin) {
  1124. if (!worksheet.isSetPageMargins()) {
  1125. return 0;
  1126. }
  1127. CTPageMargins pageMargins = worksheet.getPageMargins();
  1128. switch (margin) {
  1129. case LEFT:
  1130. return pageMargins.getLeft();
  1131. case RIGHT:
  1132. return pageMargins.getRight();
  1133. case TOP:
  1134. return pageMargins.getTop();
  1135. case BOTTOM:
  1136. return pageMargins.getBottom();
  1137. case HEADER:
  1138. return pageMargins.getHeader();
  1139. case FOOTER:
  1140. return pageMargins.getFooter();
  1141. default :
  1142. throw new IllegalArgumentException("Unknown margin constant: " + margin);
  1143. }
  1144. }
  1145. /**
  1146. * Sets the size of the margin in inches.
  1147. *
  1148. * @param margin which margin to set
  1149. * @param size the size of the margin
  1150. * @see Sheet#LeftMargin
  1151. * @see Sheet#RightMargin
  1152. * @see Sheet#TopMargin
  1153. * @see Sheet#BottomMargin
  1154. * @see Sheet#HeaderMargin
  1155. * @see Sheet#FooterMargin
  1156. * @deprecated use {@link #setMargin(PageMargin, double)} instead
  1157. */
  1158. @Override
  1159. @Deprecated
  1160. @Removal(version = "7.0.0")
  1161. public void setMargin(short margin, double size) {
  1162. final PageMargin pageMargin = PageMargin.getByShortValue(margin);
  1163. if (pageMargin == null) {
  1164. throw new IllegalArgumentException( "Unknown margin constant: " + margin );
  1165. }
  1166. setMargin(pageMargin, size);
  1167. }
  1168. /**
  1169. * Sets the size of the margin in inches.
  1170. *
  1171. * @param margin which margin to set
  1172. * @param size the size of the margin
  1173. * @since POI 5.2.3
  1174. */
  1175. @Override
  1176. public void setMargin(PageMargin margin, double size) {
  1177. CTPageMargins pageMargins = worksheet.isSetPageMargins() ?
  1178. worksheet.getPageMargins() : worksheet.addNewPageMargins();
  1179. switch (margin) {
  1180. case LEFT:
  1181. pageMargins.setLeft(size);
  1182. break;
  1183. case RIGHT:
  1184. pageMargins.setRight(size);
  1185. break;
  1186. case TOP:
  1187. pageMargins.setTop(size);
  1188. break;
  1189. case BOTTOM:
  1190. pageMargins.setBottom(size);
  1191. break;
  1192. case HEADER:
  1193. pageMargins.setHeader(size);
  1194. break;
  1195. case FOOTER:
  1196. pageMargins.setFooter(size);
  1197. break;
  1198. default:
  1199. throw new IllegalArgumentException( "Unknown margin constant: " + margin );
  1200. }
  1201. }
  1202. /**
  1203. * Returns the merged region at the specified index. If you want multiple
  1204. * regions, it is faster to call {@link #getMergedRegions()} than to call
  1205. * this each time.
  1206. *
  1207. * @return the merged region at the specified index
  1208. */
  1209. @Override
  1210. public CellRangeAddress getMergedRegion(int index) {
  1211. CTMergeCells ctMergeCells = worksheet.getMergeCells();
  1212. if(ctMergeCells == null) {
  1213. throw new IllegalStateException("This worksheet does not contain merged regions");
  1214. }
  1215. CTMergeCell ctMergeCell = ctMergeCells.getMergeCellArray(index);
  1216. String ref = ctMergeCell.getRef();
  1217. return CellRangeAddress.valueOf(ref);
  1218. }
  1219. /**
  1220. * Returns the list of merged regions. If you want multiple regions, this is
  1221. * faster than calling {@link #getMergedRegion(int)} each time.
  1222. *
  1223. * @return the list of merged regions
  1224. */
  1225. @Override
  1226. public List<CellRangeAddress> getMergedRegions() {
  1227. List<CellRangeAddress> addresses = new ArrayList<>();
  1228. CTMergeCells ctMergeCells = worksheet.getMergeCells();
  1229. if(ctMergeCells == null) {
  1230. return addresses;
  1231. }
  1232. for(CTMergeCell ctMergeCell : ctMergeCells.getMergeCellArray()) {
  1233. String ref = ctMergeCell.getRef();
  1234. addresses.add(CellRangeAddress.valueOf(ref));
  1235. }
  1236. return addresses;
  1237. }
  1238. /**
  1239. * Returns the number of merged regions defined in this worksheet
  1240. *
  1241. * @return number of merged regions in this worksheet
  1242. */
  1243. @Override
  1244. public int getNumMergedRegions() {
  1245. CTMergeCells ctMergeCells = worksheet.getMergeCells();
  1246. return ctMergeCells == null ? 0 : ctMergeCells.sizeOfMergeCellArray();
  1247. }
  1248. public int getNumHyperlinks() {
  1249. return hyperlinks.size();
  1250. }
  1251. /**
  1252. * Returns the information regarding the currently configured pane (split or freeze).
  1253. *
  1254. * @return null if no pane configured, or the pane information.
  1255. */
  1256. @Override
  1257. public PaneInformation getPaneInformation() {
  1258. final CTPane pane = getPane(false);
  1259. // no pane configured
  1260. if(pane == null) {
  1261. return null;
  1262. }
  1263. short row = 0, col = 0;
  1264. if (pane.isSetTopLeftCell()) {
  1265. final CellReference cellRef = new CellReference(pane.getTopLeftCell());
  1266. row = (short)cellRef.getRow();
  1267. col = cellRef.getCol();
  1268. }
  1269. final short x = (short)pane.getXSplit();
  1270. final short y = (short)pane.getYSplit();
  1271. final byte active = (byte)(pane.getActivePane().intValue() - 1);
  1272. final boolean frozen = pane.getState() == STPaneState.FROZEN;
  1273. return new PaneInformation(x, y, row, col, active, frozen);
  1274. }
  1275. /**
  1276. * Returns the number of physically defined rows (NOT the number of rows in the sheet)
  1277. *
  1278. * @return the number of physically defined rows
  1279. */
  1280. @Override
  1281. public int getPhysicalNumberOfRows() {
  1282. return _rows.size();
  1283. }
  1284. /**
  1285. * Gets the print setup object.
  1286. *
  1287. * @return The user model for the print setup object.
  1288. */
  1289. @Override
  1290. public XSSFPrintSetup getPrintSetup() {
  1291. return new XSSFPrintSetup(worksheet);
  1292. }
  1293. /**
  1294. * Answer whether protection is enabled or disabled
  1295. *
  1296. * @return true =&gt; protection enabled; false =&gt; protection disabled
  1297. */
  1298. @Override
  1299. public boolean getProtect() {
  1300. return isSheetLocked();
  1301. }
  1302. /**
  1303. * Enables sheet protection and sets the password for the sheet.
  1304. * Also sets some attributes on the {@link CTSheetProtection} that correspond to
  1305. * the default values used by Excel
  1306. *
  1307. * @param password to set for protection. Pass {@code null} to remove protection
  1308. */
  1309. @Override
  1310. public void protectSheet(String password) {
  1311. if (password != null) {
  1312. CTSheetProtection sheetProtection = safeGetProtectionField();
  1313. setSheetPassword(password, null); // defaults to xor password
  1314. sheetProtection.setSheet(true);
  1315. sheetProtection.setScenarios(true);
  1316. sheetProtection.setObjects(true);
  1317. } else {
  1318. worksheet.unsetSheetProtection();
  1319. }
  1320. }
  1321. /**
  1322. * Sets the sheet password.
  1323. *
  1324. * @param password if null, the password will be removed
  1325. * @param hashAlgo if null, the password will be set as XOR password (Excel 2010 and earlier)
  1326. * otherwise the given algorithm is used for calculating the hash password (Excel 2013)
  1327. */
  1328. public void setSheetPassword(String password, HashAlgorithm hashAlgo) {
  1329. if (password == null && !isSheetProtectionEnabled()) {
  1330. return;
  1331. }
  1332. setPassword(safeGetProtectionField(), password, hashAlgo, null);
  1333. }
  1334. /**
  1335. * Validate the password against the stored hash, the hashing method will be determined
  1336. * by the existing password attributes
  1337. * @return true, if the hashes match (... though original password may differ ...)
  1338. */
  1339. public boolean validateSheetPassword(String password) {
  1340. if (!isSheetProtectionEnabled()) {
  1341. return (password == null);
  1342. }
  1343. return validatePassword(safeGetProtectionField(), password, null);
  1344. }
  1345. /**
  1346. * Returns the logical row ( 0-based). If you ask for a row that is not
  1347. * defined you get a null. This is to say row 4 represents the fifth row on a sheet.
  1348. *
  1349. * @param rownum row to get
  1350. * @return {@code XSSFRow} representing the rownumber or {@code null} if its not defined on the sheet
  1351. */
  1352. @Override
  1353. public XSSFRow getRow(int rownum) {
  1354. // Performance optimization: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  1355. //noinspection UnnecessaryBoxing
  1356. final Integer rownumI = Integer.valueOf(rownum); // NOSONAR
  1357. return _rows.get(rownumI);
  1358. }
  1359. /**
  1360. * returns all rows between startRow and endRow, inclusive.
  1361. * Rows between startRow and endRow that haven't been created are not included
  1362. * in result unless createRowIfMissing is true
  1363. *
  1364. * @param startRowNum the first row number in this sheet to return
  1365. * @param endRowNum the last row number in this sheet to return
  1366. * @param createRowIfMissing If missing rows should be created.
  1367. * @return All rows between startRow and endRow, inclusive. If createRowIfMissing is false,
  1368. * only previously existing rows are returned, otherwise empty rows are added as necessary
  1369. * @throws IllegalArgumentException if startRowNum and endRowNum are not in ascending order
  1370. */
  1371. private List<XSSFRow> getRows(int startRowNum, int endRowNum, boolean createRowIfMissing) {
  1372. if (startRowNum > endRowNum) {
  1373. throw new IllegalArgumentException("getRows: startRowNum must be less than or equal to endRowNum");
  1374. }
  1375. final List<XSSFRow> rows = new ArrayList<>();
  1376. if (createRowIfMissing) {
  1377. for (int i = startRowNum; i <= endRowNum; i++) {
  1378. XSSFRow row = getRow(i);
  1379. if (row == null) {
  1380. row = createRow(i);
  1381. }
  1382. rows.add(row);
  1383. }
  1384. }
  1385. else {
  1386. // Performance optimization: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  1387. //noinspection UnnecessaryBoxing
  1388. final Integer startI = Integer.valueOf(startRowNum); // NOSONAR
  1389. //noinspection UnnecessaryBoxing
  1390. final Integer endI = Integer.valueOf(endRowNum+1); // NOSONAR
  1391. final Collection<XSSFRow> inclusive = _rows.subMap(startI, endI).values();
  1392. rows.addAll(inclusive);
  1393. }
  1394. return rows;
  1395. }
  1396. /**
  1397. * Horizontal page break information used for print layout view, page layout view, drawing print breaks in normal
  1398. * view, and for printing the worksheet.
  1399. *
  1400. * @return row indexes of all the horizontal page breaks, never {@code null}
  1401. */
  1402. @Override
  1403. public int[] getRowBreaks() {
  1404. return worksheet.isSetRowBreaks() ? getBreaks(worksheet.getRowBreaks()) : new int[0];
  1405. }
  1406. /**
  1407. * Flag indicating whether summary rows appear below detail in an outline, when applying an outline.
  1408. *
  1409. * <p>
  1410. * When true a summary row is inserted below the detailed data being summarized and a
  1411. * new outline level is established on that row.
  1412. * </p>
  1413. * <p>
  1414. * When false a summary row is inserted above the detailed data being summarized and a new outline level
  1415. * is established on that row.
  1416. * </p>
  1417. * @return {@code true} if row summaries appear below detail in the outline
  1418. */
  1419. @Override
  1420. public boolean getRowSumsBelow() {
  1421. CTSheetPr sheetPr = worksheet.getSheetPr();
  1422. CTOutlinePr outlinePr = (sheetPr != null && sheetPr.isSetOutlinePr())
  1423. ? sheetPr.getOutlinePr() : null;
  1424. return outlinePr == null || outlinePr.getSummaryBelow();
  1425. }
  1426. /**
  1427. * Flag indicating whether summary rows appear below detail in an outline, when applying an outline.
  1428. *
  1429. * <p>
  1430. * When true a summary row is inserted below the detailed data being summarized and a
  1431. * new outline level is established on that row.
  1432. * </p>
  1433. * <p>
  1434. * When false a summary row is inserted above the detailed data being summarized and a new outline level
  1435. * is established on that row.
  1436. * </p>
  1437. * @param value {@code true} if row summaries appear below detail in the outline
  1438. */
  1439. @Override
  1440. public void setRowSumsBelow(boolean value) {
  1441. ensureOutlinePr().setSummaryBelow(value);
  1442. }
  1443. /**
  1444. * Flag indicating whether summary columns appear to the right of detail in an outline, when applying an outline.
  1445. *
  1446. * <p>
  1447. * When true a summary column is inserted to the right of the detailed data being summarized
  1448. * and a new outline level is established on that column.
  1449. * </p>
  1450. * <p>
  1451. * When false a summary column is inserted to the left of the detailed data being
  1452. * summarized and a new outline level is established on that column.
  1453. * </p>
  1454. * @return {@code true} if col summaries appear right of the detail in the outline
  1455. */
  1456. @Override
  1457. public boolean getRowSumsRight() {
  1458. CTSheetPr sheetPr = worksheet.getSheetPr();
  1459. CTOutlinePr outlinePr = (sheetPr != null && sheetPr.isSetOutlinePr())
  1460. ? sheetPr.getOutlinePr() : CTOutlinePr.Factory.newInstance();
  1461. return outlinePr.getSummaryRight();
  1462. }
  1463. /**
  1464. * Flag indicating whether summary columns appear to the right of detail in an outline, when applying an outline.
  1465. *
  1466. * <p>
  1467. * When true a summary column is inserted to the right of the detailed data being summarized
  1468. * and a new outline level is established on that column.
  1469. * </p>
  1470. * <p>
  1471. * When false a summary column is inserted to the left of the detailed data being
  1472. * summarized and a new outline level is established on that column.
  1473. * </p>
  1474. * @param value {@code true} if col summaries appear right of the detail in the outline
  1475. */
  1476. @Override
  1477. public void setRowSumsRight(boolean value) {
  1478. ensureOutlinePr().setSummaryRight(value);
  1479. }
  1480. /**
  1481. * Ensure CTWorksheet.CTSheetPr.CTOutlinePr
  1482. */
  1483. private CTOutlinePr ensureOutlinePr(){
  1484. CTSheetPr sheetPr = worksheet.isSetSheetPr() ? worksheet.getSheetPr() : worksheet.addNewSheetPr();
  1485. return sheetPr.isSetOutlinePr() ? sheetPr.getOutlinePr() : sheetPr.addNewOutlinePr();
  1486. }
  1487. /**
  1488. * A flag indicating whether scenarios are locked when the sheet is protected.
  1489. *
  1490. * @return true =&gt; protection enabled; false =&gt; protection disabled
  1491. */
  1492. @Override
  1493. public boolean getScenarioProtect() {
  1494. return worksheet.isSetSheetProtection() && worksheet.getSheetProtection().getScenarios();
  1495. }
  1496. /**
  1497. * The top row in the visible view when the sheet is
  1498. * first viewed after opening it in a viewer
  1499. *
  1500. * @return integer indicating the rownum (0 based) of the top row
  1501. */
  1502. @Override
  1503. public short getTopRow() {
  1504. final CTSheetView dsv = getDefaultSheetView(false);
  1505. final String cellRef = (dsv == null) ? null : dsv.getTopLeftCell();
  1506. if(cellRef == null) {
  1507. return 0;
  1508. }
  1509. return (short) new CellReference(cellRef).getRow();
  1510. }
  1511. /**
  1512. * Determine whether printed output for this sheet will be vertically centered.
  1513. *
  1514. * @return whether printed output for this sheet will be vertically centered.
  1515. */
  1516. @Override
  1517. public boolean getVerticallyCenter() {
  1518. CTPrintOptions opts = worksheet.getPrintOptions();
  1519. return opts != null && opts.getVerticalCentered();
  1520. }
  1521. /**
  1522. * Group between (0 based) columns
  1523. */
  1524. @Override
  1525. public void groupColumn(int fromColumn, int toColumn) {
  1526. groupColumn1Based(fromColumn+1, toColumn+1);
  1527. }
  1528. private void groupColumn1Based(int fromColumn, int toColumn) {
  1529. CTCols ctCols=worksheet.getColsArray(0);
  1530. CTCol ctCol=CTCol.Factory.newInstance();
  1531. // copy attributes, as they might be removed by merging with the new column
  1532. // TODO: check if this fix is really necessary or if the sweeping algorithm
  1533. // in addCleanColIntoCols needs to be adapted ...
  1534. CTCol fixCol_before = this.columnHelper.getColumn1Based(toColumn, false);
  1535. if (fixCol_before != null) {
  1536. fixCol_before = (CTCol)fixCol_before.copy();
  1537. }
  1538. ctCol.setMin(fromColumn);
  1539. ctCol.setMax(toColumn);
  1540. this.columnHelper.addCleanColIntoCols(ctCols, ctCol);
  1541. CTCol fixCol_after = this.columnHelper.getColumn1Based(toColumn, false);
  1542. if (fixCol_before != null && fixCol_after != null) {
  1543. this.columnHelper.setColumnAttributes(fixCol_before, fixCol_after);
  1544. }
  1545. for(int index=fromColumn;index<=toColumn;index++){
  1546. CTCol col=columnHelper.getColumn1Based(index, false);
  1547. //col must exist
  1548. short outlineLevel=col.getOutlineLevel();
  1549. col.setOutlineLevel((short)(outlineLevel+1));
  1550. index = Math.toIntExact(col.getMax());
  1551. }
  1552. worksheet.setColsArray(0,ctCols);
  1553. setSheetFormatPrOutlineLevelCol();
  1554. }
  1555. /**
  1556. * Do not leave the width attribute undefined (see #52186).
  1557. */
  1558. private void setColWidthAttribute(CTCols ctCols) {
  1559. for (CTCol col : ctCols.getColArray()) {
  1560. if (!col.isSetWidth()) {
  1561. col.setWidth(getDefaultColumnWidth());
  1562. col.setCustomWidth(false);
  1563. }
  1564. }
  1565. }
  1566. /**
  1567. * Tie a range of cell together so that they can be collapsed or expanded
  1568. *
  1569. * @param fromRow start row (0-based)
  1570. * @param toRow end row (0-based)
  1571. */
  1572. @Override
  1573. public void groupRow(int fromRow, int toRow) {
  1574. for (int i = fromRow; i <= toRow; i++) {
  1575. XSSFRow xrow = getRow(i);
  1576. if (xrow == null) {
  1577. xrow = createRow(i);
  1578. }
  1579. CTRow ctrow = xrow.getCTRow();
  1580. short outlineLevel = ctrow.getOutlineLevel();
  1581. ctrow.setOutlineLevel((short) (outlineLevel + 1));
  1582. }
  1583. setSheetFormatPrOutlineLevelRow();
  1584. }
  1585. private short getMaxOutlineLevelRows(){
  1586. int outlineLevel = 0;
  1587. for (XSSFRow xrow : _rows.values()) {
  1588. outlineLevel = Math.max(outlineLevel, xrow.getCTRow().getOutlineLevel());
  1589. }
  1590. return (short) outlineLevel;
  1591. }
  1592. private short getMaxOutlineLevelCols() {
  1593. CTCols ctCols = worksheet.getColsArray(0);
  1594. int outlineLevel = 0;
  1595. for (CTCol col : ctCols.getColArray()) {
  1596. outlineLevel = Math.max(outlineLevel, col.getOutlineLevel());
  1597. }
  1598. return (short) outlineLevel;
  1599. }
  1600. /**
  1601. * Determines if there is a page break at the indicated column
  1602. */
  1603. @Override
  1604. public boolean isColumnBroken(int column) {
  1605. for (int colBreak : getColumnBreaks()) {
  1606. if (colBreak == column) {
  1607. return true;
  1608. }
  1609. }
  1610. return false;
  1611. }
  1612. /**
  1613. * Get the hidden state for a given column.
  1614. *
  1615. * @param columnIndex - the column to set (0-based)
  1616. * @return hidden - {@code false} if the column is visible
  1617. */
  1618. @Override
  1619. public boolean isColumnHidden(int columnIndex) {
  1620. CTCol col = columnHelper.getColumn(columnIndex, false);
  1621. return col != null && col.getHidden();
  1622. }
  1623. /**
  1624. * Gets the flag indicating whether this sheet should display formulas.
  1625. *
  1626. * @return {@code true} if this sheet should display formulas.
  1627. */
  1628. @Override
  1629. public boolean isDisplayFormulas() {
  1630. final CTSheetView dsv = getDefaultSheetView(false);
  1631. return dsv != null && dsv.getShowFormulas();
  1632. }
  1633. /**
  1634. * Gets the flag indicating whether this sheet displays the lines
  1635. * between rows and columns to make editing and reading easier.
  1636. *
  1637. * @return {@code true} (default) if this sheet displays gridlines.
  1638. * @see #isPrintGridlines() to check if printing of gridlines is turned on or off
  1639. */
  1640. @Override
  1641. public boolean isDisplayGridlines() {
  1642. final CTSheetView dsv = getDefaultSheetView(false);
  1643. return (dsv == null) || dsv.getShowGridLines();
  1644. }
  1645. /**
  1646. * Sets the flag indicating whether this sheet should display the lines
  1647. * between rows and columns to make editing and reading easier.
  1648. * To turn printing of gridlines use {@link #setPrintGridlines(boolean)}
  1649. *
  1650. *
  1651. * @param show {@code true} if this sheet should display gridlines.
  1652. * @see #setPrintGridlines(boolean)
  1653. */
  1654. @Override
  1655. public void setDisplayGridlines(boolean show) {
  1656. final CTSheetView dsv = getDefaultSheetView(true);
  1657. assert(dsv != null);
  1658. dsv.setShowGridLines(show);
  1659. }
  1660. /**
  1661. * Gets the flag indicating whether this sheet should display row and column headings.
  1662. * <p>
  1663. * Row heading are the row numbers to the side of the sheet
  1664. * </p>
  1665. * <p>
  1666. * Column heading are the letters or numbers that appear above the columns of the sheet
  1667. * </p>
  1668. *
  1669. * @return {@code true} (default) if this sheet should display row and column headings.
  1670. */
  1671. @Override
  1672. public boolean isDisplayRowColHeadings() {
  1673. final CTSheetView dsv = getDefaultSheetView(false);
  1674. return (dsv == null) || dsv.getShowRowColHeaders();
  1675. }
  1676. /**
  1677. * Sets the flag indicating whether this sheet should display row and column headings.
  1678. * <p>
  1679. * Row heading are the row numbers to the side of the sheet
  1680. * </p>
  1681. * <p>
  1682. * Column heading are the letters or numbers that appear above the columns of the sheet
  1683. * </p>
  1684. *
  1685. * @param show {@code true} if this sheet should display row and column headings.
  1686. */
  1687. @Override
  1688. public void setDisplayRowColHeadings(boolean show) {
  1689. final CTSheetView dsv = getDefaultSheetView(true);
  1690. assert(dsv != null);
  1691. dsv.setShowRowColHeaders(show);
  1692. }
  1693. /**
  1694. * Returns whether gridlines are printed.
  1695. *
  1696. * @return whether gridlines are printed
  1697. */
  1698. @Override
  1699. public boolean isPrintGridlines() {
  1700. CTPrintOptions opts = worksheet.getPrintOptions();
  1701. return opts != null && opts.getGridLines();
  1702. }
  1703. /**
  1704. * Turns on or off the printing of gridlines.
  1705. *
  1706. * @param value boolean to turn on or off the printing of gridlines
  1707. */
  1708. @Override
  1709. public void setPrintGridlines(boolean value) {
  1710. CTPrintOptions opts = worksheet.isSetPrintOptions() ?
  1711. worksheet.getPrintOptions() : worksheet.addNewPrintOptions();
  1712. opts.setGridLines(value);
  1713. }
  1714. /**
  1715. * Returns whether row and column headings are printed.
  1716. *
  1717. * @return whether row and column headings are printed
  1718. */
  1719. @Override
  1720. public boolean isPrintRowAndColumnHeadings() {
  1721. CTPrintOptions opts = worksheet.getPrintOptions();
  1722. return opts != null && opts.getHeadings();
  1723. }
  1724. /**
  1725. * Turns on or off the printing of row and column headings.
  1726. *
  1727. * @param value boolean to turn on or off the printing of row and column headings
  1728. */
  1729. @Override
  1730. public void setPrintRowAndColumnHeadings(boolean value) {
  1731. CTPrintOptions opts = worksheet.isSetPrintOptions() ?
  1732. worksheet.getPrintOptions() : worksheet.addNewPrintOptions();
  1733. opts.setHeadings(value);
  1734. }
  1735. /**
  1736. * Tests if there is a page break at the indicated row
  1737. *
  1738. * @param row index of the row to test
  1739. * @return {@code true} if there is a page break at the indicated row
  1740. */
  1741. @Override
  1742. public boolean isRowBroken(int row) {
  1743. for (int rowBreak : getRowBreaks()) {
  1744. if (rowBreak == row) {
  1745. return true;
  1746. }
  1747. }
  1748. return false;
  1749. }
  1750. private void setBreak(int id, CTPageBreak ctPgBreak, int lastIndex) {
  1751. CTBreak brk = ctPgBreak.addNewBrk();
  1752. // this is id of the element which is 1-based: <row r="1" ... >
  1753. brk.setId(id + 1L);
  1754. brk.setMan(true);
  1755. // end column of the break
  1756. brk.setMax(lastIndex);
  1757. int nPageBreaks = ctPgBreak.sizeOfBrkArray();
  1758. ctPgBreak.setCount(nPageBreaks);
  1759. ctPgBreak.setManualBreakCount(nPageBreaks);
  1760. }
  1761. /**
  1762. * Sets a page break at the indicated row
  1763. * Breaks occur above the specified row and left of the specified column inclusive.
  1764. *
  1765. * For example, {@code sheet.setColumnBreak(2);} breaks the sheet into two parts
  1766. * with columns A,B,C in the first and D,E,... in the second. Simuilar, {@code sheet.setRowBreak(2);}
  1767. * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part
  1768. * and rows starting with rownum=4 in the second.
  1769. *
  1770. * @param row the row to break, inclusive
  1771. */
  1772. @Override
  1773. public void setRowBreak(int row) {
  1774. if (!isRowBroken(row)) {
  1775. CTPageBreak pgBreak = worksheet.isSetRowBreaks() ? worksheet.getRowBreaks() : worksheet.addNewRowBreaks();
  1776. setBreak(row, pgBreak, SpreadsheetVersion.EXCEL2007.getLastColumnIndex());
  1777. }
  1778. }
  1779. /**
  1780. * Removes a page break at the indicated column
  1781. */
  1782. @Override
  1783. public void removeColumnBreak(int column) {
  1784. if (worksheet.isSetColBreaks()) {
  1785. removeBreak(column, worksheet.getColBreaks());
  1786. } // else no breaks
  1787. }
  1788. /**
  1789. * Removes a merged region of cells (hence letting them free)
  1790. *
  1791. * @param index of the region to unmerge
  1792. */
  1793. @Override
  1794. public void removeMergedRegion(int index) {
  1795. if (!worksheet.isSetMergeCells()) {
  1796. return;
  1797. }
  1798. CTMergeCells ctMergeCells = worksheet.getMergeCells();
  1799. int size = ctMergeCells.sizeOfMergeCellArray();
  1800. assert(0 <= index && index < size);
  1801. if (size > 1) {
  1802. ctMergeCells.removeMergeCell(index);
  1803. } else {
  1804. worksheet.unsetMergeCells();
  1805. }
  1806. }
  1807. /**
  1808. * Removes a number of merged regions of cells (hence letting them free)
  1809. *
  1810. * This method can be used to bulk-remove merged regions in a way
  1811. * much faster than calling removeMergedRegion() for every single
  1812. * merged region.
  1813. *
  1814. * @param indices A set of the regions to unmerge
  1815. */
  1816. @Override
  1817. public void removeMergedRegions(Collection<Integer> indices) {
  1818. if (!worksheet.isSetMergeCells()) {
  1819. return;
  1820. }
  1821. CTMergeCells ctMergeCells = worksheet.getMergeCells();
  1822. List<CTMergeCell> newMergeCells = new ArrayList<>(ctMergeCells.sizeOfMergeCellArray());
  1823. int idx = 0;
  1824. for (CTMergeCell mc : ctMergeCells.getMergeCellArray()) {
  1825. if (!indices.contains(idx++)) {
  1826. newMergeCells.add(mc);
  1827. }
  1828. }
  1829. if (newMergeCells.isEmpty()) {
  1830. worksheet.unsetMergeCells();
  1831. } else {
  1832. CTMergeCell[] newMergeCellsArray = new CTMergeCell[newMergeCells.size()];
  1833. ctMergeCells.setMergeCellArray(newMergeCells.toArray(newMergeCellsArray));
  1834. }
  1835. }
  1836. /**
  1837. * Remove a row from this sheet. All cells contained in the row are removed as well
  1838. *
  1839. * @param row the row to remove.
  1840. */
  1841. @Override
  1842. public void removeRow(Row row) {
  1843. if (row.getSheet() != this) {
  1844. throw new IllegalArgumentException("Specified row does not belong to this sheet");
  1845. }
  1846. // collect cells into a temporary array to avoid ConcurrentModificationException
  1847. ArrayList<XSSFCell> cellsToDelete = new ArrayList<>();
  1848. for (Cell cell : row) {
  1849. cellsToDelete.add((XSSFCell)cell);
  1850. }
  1851. for (XSSFCell cell : cellsToDelete) {
  1852. row.removeCell(cell);
  1853. }
  1854. final int rowNum = row.getRowNum();
  1855. // Performance optimization: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  1856. //noinspection UnnecessaryBoxing
  1857. final Integer rowNumI = Integer.valueOf(rowNum); // NOSONAR
  1858. // this is not the physical row number!
  1859. final int idx = _rows.headMap(rowNumI).size();
  1860. _rows.remove(rowNumI);
  1861. worksheet.getSheetData().removeRow(idx);
  1862. // also remove any comment located in that row
  1863. if(sheetComments != null) {
  1864. for (CellAddress ref : getCellComments().keySet()) {
  1865. if (ref.getRow() == rowNum) {
  1866. sheetComments.removeComment(ref);
  1867. }
  1868. }
  1869. }
  1870. }
  1871. /**
  1872. * Removes the page break at the indicated row
  1873. */
  1874. @Override
  1875. public void removeRowBreak(int row) {
  1876. if (worksheet.isSetRowBreaks()) {
  1877. removeBreak(row, worksheet.getRowBreaks());
  1878. } // else no breaks
  1879. }
  1880. /**
  1881. * Control if Excel should be asked to recalculate all formulas on this sheet
  1882. * when the workbook is opened.
  1883. *
  1884. * <p>
  1885. * Calculating the formula values with {@link FormulaEvaluator} is the
  1886. * recommended solution, but this may be used for certain cases where
  1887. * evaluation in POI is not possible.
  1888. * </p>
  1889. *
  1890. * <p>
  1891. * It is recommended to force recalcuation of formulas on workbook level using
  1892. * {@link Workbook#setForceFormulaRecalculation(boolean)}
  1893. * to ensure that all cross-worksheet formuals and external dependencies are updated.
  1894. * </p>
  1895. * @param value true if the application will perform a full recalculation of
  1896. * this worksheet values when the workbook is opened
  1897. *
  1898. * @see Workbook#setForceFormulaRecalculation(boolean)
  1899. */
  1900. @Override
  1901. public void setForceFormulaRecalculation(boolean value) {
  1902. CTCalcPr calcPr = getWorkbook().getCTWorkbook().getCalcPr();
  1903. if(worksheet.isSetSheetCalcPr()) {
  1904. // Change the current setting
  1905. CTSheetCalcPr calc = worksheet.getSheetCalcPr();
  1906. calc.setFullCalcOnLoad(value);
  1907. }
  1908. else if(value) {
  1909. // Add the Calc block and set it
  1910. CTSheetCalcPr calc = worksheet.addNewSheetCalcPr();
  1911. calc.setFullCalcOnLoad(value);
  1912. }
  1913. if(value && calcPr != null && calcPr.getCalcMode() == STCalcMode.MANUAL) {
  1914. calcPr.setCalcMode(STCalcMode.AUTO);
  1915. }
  1916. }
  1917. /**
  1918. * Whether Excel will be asked to recalculate all formulas of this sheet
  1919. * when the workbook is opened.
  1920. *
  1921. * Note: This just returns if the sheet has the recalculate flag set and
  1922. * will still return false even if recalculation is enabled on workbook-level.
  1923. *
  1924. * @return true if the Sheet has the recalculate-flag set.
  1925. */
  1926. @Override
  1927. public boolean getForceFormulaRecalculation() {
  1928. if(worksheet.isSetSheetCalcPr()) {
  1929. CTSheetCalcPr calc = worksheet.getSheetCalcPr();
  1930. return calc.getFullCalcOnLoad();
  1931. }
  1932. return false;
  1933. }
  1934. /**
  1935. * @return an iterator of the PHYSICAL rows. Meaning the 3rd element may not
  1936. * be the third row if say for instance the second row is undefined.
  1937. * Call getRowNum() on each row if you care which one it is.
  1938. */
  1939. @Override
  1940. @SuppressWarnings("unchecked")
  1941. public Iterator<Row> rowIterator() {
  1942. return (Iterator<Row>)(Iterator<? extends Row>) _rows.values().iterator();
  1943. }
  1944. /**
  1945. * @return a spliterator of the PHYSICAL rows. Meaning the 3rd element may not
  1946. * be the third row if say for instance the second row is undefined.
  1947. * Call getRowNum() on each row if you care which one it is.
  1948. *
  1949. * @since POI 5.2.0
  1950. */
  1951. @Override
  1952. @SuppressWarnings("unchecked")
  1953. public Spliterator<Row> spliterator() {
  1954. return (Spliterator<Row>)(Spliterator<? extends Row>) _rows.values().spliterator();
  1955. }
  1956. /**
  1957. * Flag indicating whether the sheet displays Automatic Page Breaks.
  1958. *
  1959. * @return {@code true} if the sheet displays Automatic Page Breaks.
  1960. */
  1961. @Override
  1962. public boolean getAutobreaks() {
  1963. CTSheetPr sheetPr = getSheetTypeSheetPr();
  1964. CTPageSetUpPr psSetup = (sheetPr == null || !sheetPr.isSetPageSetUpPr()) ?
  1965. CTPageSetUpPr.Factory.newInstance() : sheetPr.getPageSetUpPr();
  1966. return psSetup.getAutoPageBreaks();
  1967. }
  1968. /**
  1969. * Flag indicating whether the sheet displays Automatic Page Breaks.
  1970. *
  1971. * @param value {@code true} if the sheet displays Automatic Page Breaks.
  1972. */
  1973. @Override
  1974. public void setAutobreaks(boolean value) {
  1975. CTSheetPr sheetPr = getSheetTypeSheetPr();
  1976. CTPageSetUpPr psSetup = sheetPr.isSetPageSetUpPr() ? sheetPr.getPageSetUpPr() : sheetPr.addNewPageSetUpPr();
  1977. psSetup.setAutoPageBreaks(value);
  1978. }
  1979. /**
  1980. * Sets a page break at the indicated column.
  1981. * Breaks occur above the specified row and left of the specified column inclusive.
  1982. *
  1983. * For example, {@code sheet.setColumnBreak(2);} breaks the sheet into two parts
  1984. * with columns A,B,C in the first and D,E,... in the second. Simuilar, {@code sheet.setRowBreak(2);}
  1985. * breaks the sheet into two parts with first three rows (rownum=1...3) in the first part
  1986. * and rows starting with rownum=4 in the second.
  1987. *
  1988. * @param column the column to break, inclusive
  1989. */
  1990. @Override
  1991. public void setColumnBreak(int column) {
  1992. if (!isColumnBroken(column)) {
  1993. CTPageBreak pgBreak = worksheet.isSetColBreaks() ? worksheet.getColBreaks() : worksheet.addNewColBreaks();
  1994. setBreak(column, pgBreak, SpreadsheetVersion.EXCEL2007.getLastRowIndex());
  1995. }
  1996. }
  1997. @Override
  1998. public void setColumnGroupCollapsed(int columnNumber, boolean collapsed) {
  1999. if (collapsed) {
  2000. collapseColumn(columnNumber);
  2001. } else {
  2002. expandColumn(columnNumber);
  2003. }
  2004. }
  2005. private void collapseColumn(int columnNumber) {
  2006. CTCols cols = worksheet.getColsArray(0);
  2007. CTCol col = columnHelper.getColumn(columnNumber, false);
  2008. int colInfoIx = columnHelper.getIndexOfColumn(cols, col);
  2009. if (colInfoIx == -1) {
  2010. return;
  2011. }
  2012. // Find the start of the group.
  2013. int groupStartColInfoIx = findStartOfColumnOutlineGroup(colInfoIx);
  2014. CTCol columnInfo = cols.getColArray(groupStartColInfoIx);
  2015. // Hide all the columns until the end of the group
  2016. int lastColMax = setGroupHidden(groupStartColInfoIx, columnInfo
  2017. .getOutlineLevel(), true);
  2018. // write collapse field
  2019. setColumn(lastColMax + 1, 0, null, null, Boolean.TRUE);
  2020. }
  2021. private void setColumn(int targetColumnIx, Integer style,
  2022. Integer level, Boolean hidden, Boolean collapsed) {
  2023. CTCols cols = worksheet.getColsArray(0);
  2024. CTCol ci = null;
  2025. for (CTCol tci : cols.getColArray()) {
  2026. long tciMin = tci.getMin();
  2027. long tciMax = tci.getMax();
  2028. if (tciMin >= targetColumnIx && tciMax <= targetColumnIx) {
  2029. ci = tci;
  2030. break;
  2031. }
  2032. if (tciMin > targetColumnIx) {
  2033. // call column infos after k are for later columns
  2034. break; // exit now so k will be the correct insert pos
  2035. }
  2036. }
  2037. if (ci == null) {
  2038. // okay so there ISN'T a column info record that covers this column
  2039. // so lets create one!
  2040. CTCol nci = CTCol.Factory.newInstance();
  2041. nci.setMin(targetColumnIx);
  2042. nci.setMax(targetColumnIx);
  2043. unsetCollapsed(collapsed, nci);
  2044. this.columnHelper.addCleanColIntoCols(cols, nci);
  2045. return;
  2046. }
  2047. boolean styleChanged = style != null && ci.getStyle() != style;
  2048. boolean levelChanged = level != null && ci.getOutlineLevel() != level;
  2049. boolean hiddenChanged = hidden != null && ci.getHidden() != hidden;
  2050. boolean collapsedChanged = collapsed != null && ci.getCollapsed() != collapsed;
  2051. boolean columnChanged = levelChanged || hiddenChanged || collapsedChanged || styleChanged;
  2052. if (!columnChanged) {
  2053. // do nothing...nothing changed.
  2054. return;
  2055. }
  2056. long ciMin = ci.getMin();
  2057. long ciMax = ci.getMax();
  2058. if (ciMin == targetColumnIx && ciMax == targetColumnIx) {
  2059. // ColumnInfo ci for a single column, the target column
  2060. unsetCollapsed(collapsed, ci);
  2061. return;
  2062. }
  2063. if (ciMin == targetColumnIx || ciMax == targetColumnIx) {
  2064. // The target column is at either end of the multi-column ColumnInfo ci
  2065. // we'll just divide the info and create a new one
  2066. if (ciMin == targetColumnIx) {
  2067. ci.setMin(targetColumnIx + 1L);
  2068. } else {
  2069. ci.setMax(targetColumnIx - 1L);
  2070. }
  2071. CTCol nci = columnHelper.cloneCol(cols, ci);
  2072. nci.setMin(targetColumnIx);
  2073. unsetCollapsed(collapsed, nci);
  2074. this.columnHelper.addCleanColIntoCols(cols, nci);
  2075. } else {
  2076. // split to 3 records
  2077. CTCol ciMid = columnHelper.cloneCol(cols, ci);
  2078. CTCol ciEnd = columnHelper.cloneCol(cols, ci);
  2079. int lastcolumn = Math.toIntExact(ciMax);
  2080. ci.setMax(targetColumnIx - 1L);
  2081. ciMid.setMin(targetColumnIx);
  2082. ciMid.setMax(targetColumnIx);
  2083. unsetCollapsed(collapsed, ciMid);
  2084. this.columnHelper.addCleanColIntoCols(cols, ciMid);
  2085. ciEnd.setMin(targetColumnIx + 1L);
  2086. ciEnd.setMax(lastcolumn);
  2087. this.columnHelper.addCleanColIntoCols(cols, ciEnd);
  2088. }
  2089. }
  2090. private void unsetCollapsed(Boolean collapsed, CTCol ci) {
  2091. if (collapsed != null && collapsed) {
  2092. ci.setCollapsed(true);
  2093. } else {
  2094. ci.unsetCollapsed();
  2095. }
  2096. }
  2097. /**
  2098. * Sets all adjacent columns of the same outline level to the specified
  2099. * hidden status.
  2100. *
  2101. * @param pIdx
  2102. * the col info index of the start of the outline group
  2103. * @return the column index of the last column in the outline group
  2104. */
  2105. private int setGroupHidden(int pIdx, int level, boolean hidden) {
  2106. CTCols cols = worksheet.getColsArray(0);
  2107. int idx = pIdx;
  2108. CTCol[] colArray = cols.getColArray();
  2109. CTCol columnInfo = colArray[idx];
  2110. while (idx < colArray.length) {
  2111. columnInfo.setHidden(hidden);
  2112. if (idx + 1 < colArray.length) {
  2113. CTCol nextColumnInfo = colArray[idx + 1];
  2114. if (!isAdjacentBefore(columnInfo, nextColumnInfo)) {
  2115. break;
  2116. }
  2117. if (nextColumnInfo.getOutlineLevel() < level) {
  2118. break;
  2119. }
  2120. columnInfo = nextColumnInfo;
  2121. }
  2122. idx++;
  2123. }
  2124. return Math.toIntExact(columnInfo.getMax());
  2125. }
  2126. private boolean isAdjacentBefore(CTCol col, CTCol otherCol) {
  2127. return col.getMax() == otherCol.getMin() - 1;
  2128. }
  2129. private int findStartOfColumnOutlineGroup(int pIdx) {
  2130. // Find the start of the group.
  2131. CTCols cols = worksheet.getColsArray(0);
  2132. CTCol[] colArray = cols.getColArray();
  2133. CTCol columnInfo = colArray[pIdx];
  2134. int level = columnInfo.getOutlineLevel();
  2135. int idx = pIdx;
  2136. while (idx != 0) {
  2137. CTCol prevColumnInfo = colArray[idx - 1];
  2138. if (!isAdjacentBefore(prevColumnInfo, columnInfo)) {
  2139. break;
  2140. }
  2141. if (prevColumnInfo.getOutlineLevel() < level) {
  2142. break;
  2143. }
  2144. idx--;
  2145. columnInfo = prevColumnInfo;
  2146. }
  2147. return idx;
  2148. }
  2149. private int findEndOfColumnOutlineGroup(int colInfoIndex) {
  2150. CTCols cols = worksheet.getColsArray(0);
  2151. // Find the end of the group.
  2152. CTCol[] colArray = cols.getColArray();
  2153. CTCol columnInfo = colArray[colInfoIndex];
  2154. int level = columnInfo.getOutlineLevel();
  2155. int idx = colInfoIndex;
  2156. int lastIdx = colArray.length - 1;
  2157. while (idx < lastIdx) {
  2158. CTCol nextColumnInfo = colArray[idx + 1];
  2159. if (!isAdjacentBefore(columnInfo, nextColumnInfo)) {
  2160. break;
  2161. }
  2162. if (nextColumnInfo.getOutlineLevel() < level) {
  2163. break;
  2164. }
  2165. idx++;
  2166. columnInfo = nextColumnInfo;
  2167. }
  2168. return idx;
  2169. }
  2170. private void expandColumn(int columnIndex) {
  2171. CTCols cols = worksheet.getColsArray(0);
  2172. CTCol col = columnHelper.getColumn(columnIndex, false);
  2173. int colInfoIx = columnHelper.getIndexOfColumn(cols, col);
  2174. int idx = col == null ? -1 : findColInfoIdx(Math.toIntExact(col.getMax()), colInfoIx);
  2175. if (idx == -1) {
  2176. return;
  2177. }
  2178. // If it is already expanded do nothing.
  2179. if (!isColumnGroupCollapsed(idx)) {
  2180. return;
  2181. }
  2182. // Find the start/end of the group.
  2183. int startIdx = findStartOfColumnOutlineGroup(idx);
  2184. int endIdx = findEndOfColumnOutlineGroup(idx);
  2185. // expand:
  2186. // collapsed bit must be unset
  2187. // hidden bit gets unset _if_ surrounding groups are expanded you can
  2188. // determine
  2189. // this by looking at the hidden bit of the enclosing group. You will
  2190. // have
  2191. // to look at the start and the end of the current group to determine
  2192. // which
  2193. // is the enclosing group
  2194. // hidden bit only is altered for this outline level. ie. don't
  2195. // uncollapse contained groups
  2196. CTCol[] colArray = cols.getColArray();
  2197. CTCol columnInfo = colArray[endIdx];
  2198. if (!isColumnGroupHiddenByParent(idx)) {
  2199. short outlineLevel = columnInfo.getOutlineLevel();
  2200. boolean nestedGroup = false;
  2201. for (int i = startIdx; i <= endIdx; i++) {
  2202. CTCol ci = colArray[i];
  2203. if (outlineLevel == ci.getOutlineLevel()) {
  2204. ci.unsetHidden();
  2205. if (nestedGroup) {
  2206. nestedGroup = false;
  2207. ci.setCollapsed(true);
  2208. }
  2209. } else {
  2210. nestedGroup = true;
  2211. }
  2212. }
  2213. }
  2214. // Write collapse flag (stored in a single col info record after this
  2215. // outline group)
  2216. setColumn(Math.toIntExact(columnInfo.getMax() + 1), null, null,
  2217. Boolean.FALSE, Boolean.FALSE);
  2218. }
  2219. private boolean isColumnGroupHiddenByParent(int idx) {
  2220. CTCols cols = worksheet.getColsArray(0);
  2221. // Look out outline details of end
  2222. int endLevel = 0;
  2223. boolean endHidden = false;
  2224. int endOfOutlineGroupIdx = findEndOfColumnOutlineGroup(idx);
  2225. CTCol[] colArray = cols.getColArray();
  2226. if (endOfOutlineGroupIdx < (colArray.length - 1)) {
  2227. CTCol nextInfo = colArray[endOfOutlineGroupIdx + 1];
  2228. if (isAdjacentBefore(colArray[endOfOutlineGroupIdx], nextInfo)) {
  2229. endLevel = nextInfo.getOutlineLevel();
  2230. endHidden = nextInfo.getHidden();
  2231. }
  2232. }
  2233. // Look out outline details of start
  2234. int startLevel = 0;
  2235. boolean startHidden = false;
  2236. int startOfOutlineGroupIdx = findStartOfColumnOutlineGroup(idx);
  2237. if (startOfOutlineGroupIdx > 0) {
  2238. CTCol prevInfo = colArray[startOfOutlineGroupIdx - 1];
  2239. if (isAdjacentBefore(prevInfo, colArray[startOfOutlineGroupIdx])) {
  2240. startLevel = prevInfo.getOutlineLevel();
  2241. startHidden = prevInfo.getHidden();
  2242. }
  2243. }
  2244. if (endLevel > startLevel) {
  2245. return endHidden;
  2246. }
  2247. return startHidden;
  2248. }
  2249. private int findColInfoIdx(int columnValue, int fromColInfoIdx) {
  2250. CTCols cols = worksheet.getColsArray(0);
  2251. if (columnValue < 0) {
  2252. throw new IllegalArgumentException(
  2253. "column parameter out of range: " + columnValue);
  2254. }
  2255. if (fromColInfoIdx < 0) {
  2256. throw new IllegalArgumentException(
  2257. "fromIdx parameter out of range: " + fromColInfoIdx);
  2258. }
  2259. CTCol[] colArray = cols.getColArray();
  2260. for (int k = fromColInfoIdx; k < colArray.length; k++) {
  2261. CTCol ci = colArray[k];
  2262. if (containsColumn(ci, columnValue)) {
  2263. return k;
  2264. }
  2265. if (ci.getMin() > fromColInfoIdx) {
  2266. break;
  2267. }
  2268. }
  2269. return -1;
  2270. }
  2271. private boolean containsColumn(CTCol col, int columnIndex) {
  2272. return col.getMin() <= columnIndex && columnIndex <= col.getMax();
  2273. }
  2274. /**
  2275. * 'Collapsed' state is stored in a single column col info record
  2276. * immediately after the outline group
  2277. *
  2278. * @param idx The column-index to check
  2279. * @return a boolean represented if the column is collapsed
  2280. */
  2281. private boolean isColumnGroupCollapsed(int idx) {
  2282. CTCols cols = worksheet.getColsArray(0);
  2283. CTCol[] colArray = cols.getColArray();
  2284. int endOfOutlineGroupIdx = findEndOfColumnOutlineGroup(idx);
  2285. int nextColInfoIx = endOfOutlineGroupIdx + 1;
  2286. if (nextColInfoIx >= colArray.length) {
  2287. return false;
  2288. }
  2289. CTCol nextColInfo = colArray[nextColInfoIx];
  2290. CTCol col = colArray[endOfOutlineGroupIdx];
  2291. if (!isAdjacentBefore(col, nextColInfo)) {
  2292. return false;
  2293. }
  2294. return nextColInfo.getCollapsed();
  2295. }
  2296. /**
  2297. * Set the visibility state for a given column.
  2298. *
  2299. * @param columnIndex - the column to get (0-based)
  2300. * @param hidden - the visibility state of the column
  2301. */
  2302. @Override
  2303. public void setColumnHidden(int columnIndex, boolean hidden) {
  2304. columnHelper.setColHidden(columnIndex, hidden);
  2305. }
  2306. /**
  2307. * Set the width (in units of 1/256th of a character width)
  2308. *
  2309. * <p>
  2310. * The maximum column width for an individual cell is 255 characters.
  2311. * This value represents the number of characters that can be displayed
  2312. * in a cell that is formatted with the standard font (first font in the workbook).
  2313. * </p>
  2314. *
  2315. * <p>
  2316. * Character width is defined as the maximum digit width
  2317. * of the numbers {@code 0, 1, 2, ... 9} as rendered
  2318. * using the default font (first font in the workbook).
  2319. * <br>
  2320. * Unless you are using a very special font, the default character is '0' (zero),
  2321. * this is true for Arial (default font font in HSSF) and Calibri (default font in XSSF)
  2322. * </p>
  2323. *
  2324. * <p>
  2325. * Please note, that the width set by this method includes 4 pixels of margin padding (two on each side),
  2326. * plus 1 pixel padding for the gridlines (Section 3.3.1.12 of the OOXML spec).
  2327. * This results is a slightly less value of visible characters than passed to this method (approx. 1/2 of a character).
  2328. * </p>
  2329. * <p>
  2330. * To compute the actual number of visible characters,
  2331. * Excel uses the following formula (Section 3.3.1.12 of the OOXML spec):
  2332. * </p>
  2333. * {@code
  2334. * width = Truncate([{Number of Visible Characters} *
  2335. * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256
  2336. * }
  2337. * <p>Using the Calibri font as an example, the maximum digit width of 11 point font size is 7 pixels (at 96 dpi).
  2338. * If you set a column width to be eight characters wide, e.g. {@code setColumnWidth(columnIndex, 8*256)},
  2339. * then the actual value of visible characters (the value shown in Excel) is derived from the following equation:
  2340. * {@code
  2341. Truncate([numChars*7+5]/7*256)/256 = 8;
  2342. * }
  2343. *
  2344. * which gives {@code 7.29}.
  2345. *
  2346. * @param columnIndex - the column to set (0-based)
  2347. * @param width - the width in units of 1/256th of a character width
  2348. * @throws IllegalArgumentException if width &gt; 255*256 (the maximum column width in Excel is 255 characters)
  2349. */
  2350. @Override
  2351. public void setColumnWidth(int columnIndex, int width) {
  2352. if(width > 255*256) {
  2353. throw new IllegalArgumentException("The maximum column width for an individual cell is 255 characters.");
  2354. }
  2355. columnHelper.setColWidth(columnIndex, (double)width/256);
  2356. columnHelper.setCustomWidth(columnIndex, true);
  2357. }
  2358. @Override
  2359. public void setDefaultColumnStyle(int column, CellStyle style) {
  2360. columnHelper.setColDefaultStyle(column, style);
  2361. }
  2362. /**
  2363. * Specifies the number of characters of the maximum digit width of the normal style's font.
  2364. * This value does not include margin padding or extra padding for gridlines. It is only the
  2365. * number of characters.
  2366. *
  2367. * @param width the number of characters. Default value is {@code 8}.
  2368. */
  2369. @Override
  2370. public void setDefaultColumnWidth(int width) {
  2371. getSheetTypeSheetFormatPr().setBaseColWidth(width);
  2372. }
  2373. /**
  2374. * Set the default row height for the sheet (if the rows do not define their own height) in
  2375. * twips (1/20 of a point)
  2376. *
  2377. * @param height default row height in twips (1/20 of a point)
  2378. */
  2379. @Override
  2380. public void setDefaultRowHeight(short height) {
  2381. setDefaultRowHeightInPoints((float)height / Font.TWIPS_PER_POINT);
  2382. }
  2383. /**
  2384. * Sets default row height measured in point size.
  2385. *
  2386. * @param height default row height measured in point size.
  2387. */
  2388. @Override
  2389. public void setDefaultRowHeightInPoints(float height) {
  2390. CTSheetFormatPr pr = getSheetTypeSheetFormatPr();
  2391. pr.setDefaultRowHeight(height);
  2392. pr.setCustomHeight(true);
  2393. }
  2394. /**
  2395. * Sets the flag indicating whether this sheet should display formulas.
  2396. *
  2397. * @param show {@code true} if this sheet should display formulas.
  2398. */
  2399. @Override
  2400. public void setDisplayFormulas(boolean show) {
  2401. final CTSheetView dsv = getDefaultSheetView(true);
  2402. assert(dsv != null);
  2403. dsv.setShowFormulas(show);
  2404. }
  2405. /**
  2406. * Flag indicating whether the Fit to Page print option is enabled.
  2407. *
  2408. * @param b {@code true} if the Fit to Page print option is enabled.
  2409. */
  2410. @Override
  2411. public void setFitToPage(boolean b) {
  2412. getSheetTypePageSetUpPr().setFitToPage(b);
  2413. }
  2414. /**
  2415. * Center on page horizontally when printing.
  2416. *
  2417. * @param value whether to center on page horizontally when printing.
  2418. */
  2419. @Override
  2420. public void setHorizontallyCenter(boolean value) {
  2421. CTPrintOptions opts = worksheet.isSetPrintOptions() ?
  2422. worksheet.getPrintOptions() : worksheet.addNewPrintOptions();
  2423. opts.setHorizontalCentered(value);
  2424. }
  2425. /**
  2426. * Whether the output is vertically centered on the page.
  2427. *
  2428. * @param value true to vertically center, false otherwise.
  2429. */
  2430. @Override
  2431. public void setVerticallyCenter(boolean value) {
  2432. CTPrintOptions opts = worksheet.isSetPrintOptions() ?
  2433. worksheet.getPrintOptions() : worksheet.addNewPrintOptions();
  2434. opts.setVerticalCentered(value);
  2435. }
  2436. /**
  2437. * group the row It is possible for collapsed to be false and yet still have
  2438. * the rows in question hidden. This can be achieved by having a lower
  2439. * outline level collapsed, thus hiding all the child rows. Note that in
  2440. * this case, if the lowest level were expanded, the middle level would
  2441. * remain collapsed.
  2442. *
  2443. * @param rowIndex -
  2444. * the row involved, 0 based
  2445. * @param collapse -
  2446. * boolean value for collapse
  2447. */
  2448. @Override
  2449. public void setRowGroupCollapsed(int rowIndex, boolean collapse) {
  2450. if (collapse) {
  2451. collapseRow(rowIndex);
  2452. } else {
  2453. expandRow(rowIndex);
  2454. }
  2455. }
  2456. /**
  2457. * @param rowIndex the zero based row index to collapse
  2458. */
  2459. private void collapseRow(int rowIndex) {
  2460. XSSFRow row = getRow(rowIndex);
  2461. if (row != null) {
  2462. int startRow = findStartOfRowOutlineGroup(rowIndex);
  2463. // Hide all the columns until the end of the group
  2464. int lastRow = writeHidden(row, startRow, true);
  2465. if (getRow(lastRow) != null) {
  2466. getRow(lastRow).getCTRow().setCollapsed(true);
  2467. } else {
  2468. XSSFRow newRow = createRow(lastRow);
  2469. newRow.getCTRow().setCollapsed(true);
  2470. }
  2471. }
  2472. }
  2473. /**
  2474. * @param rowIndex the zero based row index to find from
  2475. */
  2476. private int findStartOfRowOutlineGroup(int rowIndex) {
  2477. // Find the start of the group.
  2478. short level = getRow(rowIndex).getCTRow().getOutlineLevel();
  2479. int currentRow = rowIndex;
  2480. while (getRow(currentRow) != null) {
  2481. if (getRow(currentRow).getCTRow().getOutlineLevel() < level) {
  2482. return currentRow + 1;
  2483. }
  2484. currentRow--;
  2485. }
  2486. return currentRow;
  2487. }
  2488. private int writeHidden(XSSFRow xRow, int rowIndex, boolean hidden) {
  2489. short level = xRow.getCTRow().getOutlineLevel();
  2490. for (Iterator<Row> it = rowIterator(); it.hasNext();) {
  2491. xRow = (XSSFRow) it.next();
  2492. // skip rows before the start of this group
  2493. if(xRow.getRowNum() < rowIndex) {
  2494. continue;
  2495. }
  2496. if (xRow.getCTRow().getOutlineLevel() >= level) {
  2497. xRow.getCTRow().setHidden(hidden);
  2498. rowIndex++;
  2499. }
  2500. }
  2501. return rowIndex;
  2502. }
  2503. /**
  2504. * @param rowNumber the zero based row index to expand
  2505. */
  2506. private void expandRow(int rowNumber) {
  2507. if (rowNumber == -1) {
  2508. return;
  2509. }
  2510. XSSFRow row = getRow(rowNumber);
  2511. // If it is already expanded do nothing.
  2512. if (!row.getCTRow().isSetHidden()) {
  2513. return;
  2514. }
  2515. // Find the start of the group.
  2516. int startIdx = findStartOfRowOutlineGroup(rowNumber);
  2517. // Find the end of the group.
  2518. int endIdx = findEndOfRowOutlineGroup(rowNumber);
  2519. // expand:
  2520. // collapsed must be unset
  2521. // hidden bit gets unset _if_ surrounding groups are expanded you can
  2522. // determine
  2523. // this by looking at the hidden bit of the enclosing group. You will
  2524. // have
  2525. // to look at the start and the end of the current group to determine
  2526. // which
  2527. // is the enclosing group
  2528. // hidden bit only is altered for this outline level. ie. don't
  2529. // un-collapse contained groups
  2530. short level = row.getCTRow().getOutlineLevel();
  2531. if (!isRowGroupHiddenByParent(rowNumber)) {
  2532. for (int i = startIdx; i < endIdx; i++) {
  2533. if (level == getRow(i).getCTRow().getOutlineLevel()) {
  2534. getRow(i).getCTRow().unsetHidden();
  2535. } else if (!isRowGroupCollapsed(i)) {
  2536. getRow(i).getCTRow().unsetHidden();
  2537. }
  2538. }
  2539. }
  2540. // Write collapse field
  2541. CTRow ctRow = getRow(endIdx).getCTRow();
  2542. // This avoids an IndexOutOfBounds if multiple nested groups are collapsed/expanded
  2543. if(ctRow.getCollapsed()) {
  2544. ctRow.unsetCollapsed();
  2545. }
  2546. }
  2547. /**
  2548. * @param row the zero based row index to find from
  2549. */
  2550. public int findEndOfRowOutlineGroup(int row) {
  2551. short level = getRow(row).getCTRow().getOutlineLevel();
  2552. int currentRow;
  2553. final int lastRowNum = getLastRowNum();
  2554. for (currentRow = row; currentRow < lastRowNum; currentRow++) {
  2555. if (getRow(currentRow) == null
  2556. || getRow(currentRow).getCTRow().getOutlineLevel() < level) {
  2557. break;
  2558. }
  2559. }
  2560. return currentRow;
  2561. }
  2562. /**
  2563. * @param row the zero based row index to find from
  2564. */
  2565. private boolean isRowGroupHiddenByParent(int row) {
  2566. // Look out outline details of end
  2567. int endLevel;
  2568. boolean endHidden;
  2569. int endOfOutlineGroupIdx = findEndOfRowOutlineGroup(row);
  2570. if (getRow(endOfOutlineGroupIdx) == null) {
  2571. endLevel = 0;
  2572. endHidden = false;
  2573. } else {
  2574. endLevel = getRow(endOfOutlineGroupIdx).getCTRow().getOutlineLevel();
  2575. endHidden = getRow(endOfOutlineGroupIdx).getCTRow().getHidden();
  2576. }
  2577. // Look out outline details of start
  2578. int startLevel;
  2579. boolean startHidden;
  2580. int startOfOutlineGroupIdx = findStartOfRowOutlineGroup(row);
  2581. if (startOfOutlineGroupIdx < 0
  2582. || getRow(startOfOutlineGroupIdx) == null) {
  2583. startLevel = 0;
  2584. startHidden = false;
  2585. } else {
  2586. startLevel = getRow(startOfOutlineGroupIdx).getCTRow()
  2587. .getOutlineLevel();
  2588. startHidden = getRow(startOfOutlineGroupIdx).getCTRow()
  2589. .getHidden();
  2590. }
  2591. if (endLevel > startLevel) {
  2592. return endHidden;
  2593. }
  2594. return startHidden;
  2595. }
  2596. /**
  2597. * @param row the zero based row index to find from
  2598. */
  2599. private boolean isRowGroupCollapsed(int row) {
  2600. int collapseRow = findEndOfRowOutlineGroup(row) + 1;
  2601. if (getRow(collapseRow) == null) {
  2602. return false;
  2603. }
  2604. return getRow(collapseRow).getCTRow().getCollapsed();
  2605. }
  2606. /**
  2607. * Window zoom magnification for current view representing percent values.
  2608. * Valid values range from 10 to 400. Horizontal &amp; Vertical scale together.
  2609. *
  2610. * For example:
  2611. * <pre>
  2612. * 10 - 10%
  2613. * 20 - 20%
  2614. * ...
  2615. * 100 - 100%
  2616. * ...
  2617. * 400 - 400%
  2618. * </pre>
  2619. *
  2620. * Current view can be Normal, Page Layout, or Page Break Preview.
  2621. *
  2622. * @param scale window zoom magnification
  2623. * @throws IllegalArgumentException if scale is invalid
  2624. */
  2625. @Override
  2626. public void setZoom(int scale) {
  2627. if (scale < 10 || scale > 400) {
  2628. throw new IllegalArgumentException("Valid scale values range from 10 to 400");
  2629. }
  2630. final CTSheetView dsv = getDefaultSheetView(true);
  2631. assert(dsv != null);
  2632. dsv.setZoomScale(scale);
  2633. }
  2634. /**
  2635. * copyRows rows from srcRows to this sheet starting at destStartRow
  2636. *
  2637. * Additionally copies merged regions that are completely defined in these
  2638. * rows (ie. merged 2 cells on a row to be shifted).
  2639. * @param srcRows the rows to copy. Formulas will be offset by the difference
  2640. * in the row number of the first row in srcRows and destStartRow (even if srcRows
  2641. * are from a different sheet).
  2642. * @param destStartRow the row in this sheet to paste the first row of srcRows
  2643. * the remainder of srcRows will be pasted below destStartRow per the cell copy policy
  2644. * @param policy is the cell copy policy, which can be used to merge the source and destination
  2645. * when the source is blank, copy styles only, paste as value, etc
  2646. */
  2647. @Beta
  2648. public void copyRows(List<? extends Row> srcRows, int destStartRow, CellCopyPolicy policy) {
  2649. if (srcRows == null || srcRows.isEmpty()) {
  2650. throw new IllegalArgumentException("No rows to copy");
  2651. }
  2652. final Row srcStartRow = srcRows.get(0);
  2653. final Row srcEndRow = srcRows.get(srcRows.size() - 1);
  2654. if (srcStartRow == null) {
  2655. throw new IllegalArgumentException("copyRows: First row cannot be null");
  2656. }
  2657. final int srcStartRowNum = srcStartRow.getRowNum();
  2658. final int srcEndRowNum = srcEndRow.getRowNum();
  2659. // check row numbers to make sure they are continuous and increasing (monotonic)
  2660. // and srcRows does not contain null rows
  2661. final int size = srcRows.size();
  2662. for (int index=1; index < size; index++) {
  2663. final Row curRow = srcRows.get(index);
  2664. if (curRow == null) {
  2665. throw new IllegalArgumentException("srcRows may not contain null rows. Found null row at index " + index + ".");
  2666. //} else if (curRow.getRowNum() != prevRow.getRowNum() + 1) {
  2667. // throw new IllegalArgumentException("srcRows must contain continuously increasing row numbers. " +
  2668. // "Got srcRows[" + (index-1) + "]=Row " + prevRow.getRowNum() + ", srcRows[" + index + "]=Row " + curRow.getRowNum() + ".");
  2669. // FIXME: assumes row objects belong to non-null sheets and sheets belong to non-null workbooks.
  2670. } else if (srcStartRow.getSheet().getWorkbook() != curRow.getSheet().getWorkbook()) {
  2671. throw new IllegalArgumentException("All rows in srcRows must belong to the same sheet in the same workbook. " +
  2672. "Expected all rows from same workbook (" + srcStartRow.getSheet().getWorkbook() + "). " +
  2673. "Got srcRows[" + index + "] from different workbook (" + curRow.getSheet().getWorkbook() + ").");
  2674. } else if (srcStartRow.getSheet() != curRow.getSheet()) {
  2675. throw new IllegalArgumentException("All rows in srcRows must belong to the same sheet. " +
  2676. "Expected all rows from " + srcStartRow.getSheet().getSheetName() + ". " +
  2677. "Got srcRows[" + index + "] from " + curRow.getSheet().getSheetName());
  2678. }
  2679. }
  2680. // FIXME: is special behavior needed if srcRows and destRows belong to the same sheets and the regions overlap?
  2681. final CellCopyPolicy options = new CellCopyPolicy(policy);
  2682. // avoid O(N^2) performance scanning through all regions for each row
  2683. // merged regions will be copied after all the rows have been copied
  2684. options.setCopyMergedRegions(false);
  2685. // FIXME: if srcRows contains gaps or null values, clear out those rows that will be overwritten
  2686. // how will this work with merging (copy just values, leave cell styles in place?)
  2687. int r = destStartRow;
  2688. for (Row srcRow : srcRows) {
  2689. int destRowNum;
  2690. if (policy.isCondenseRows()) {
  2691. destRowNum = r++;
  2692. } else {
  2693. final int shift = (srcRow.getRowNum() - srcStartRowNum);
  2694. destRowNum = destStartRow + shift;
  2695. }
  2696. //removeRow(destRowNum); //this probably clears all external formula references to destRow, causing unwanted #REF! errors
  2697. final XSSFRow destRow = createRow(destRowNum);
  2698. destRow.copyRowFrom(srcRow, options);
  2699. }
  2700. // ======================
  2701. // Only do additional copy operations here that cannot be done with Row.copyFromRow(Row, options)
  2702. // reasons: operation needs to interact with multiple rows or sheets
  2703. // Copy merged regions that are contained within the copy region
  2704. if (policy.isCopyMergedRegions()) {
  2705. // FIXME: is this something that rowShifter could be doing?
  2706. final int shift = destStartRow - srcStartRowNum;
  2707. for (CellRangeAddress srcRegion : srcStartRow.getSheet().getMergedRegions()) {
  2708. if (srcStartRowNum <= srcRegion.getFirstRow() && srcRegion.getLastRow() <= srcEndRowNum) {
  2709. // srcRegion is fully inside the copied rows
  2710. final CellRangeAddress destRegion = srcRegion.copy();
  2711. destRegion.setFirstRow(destRegion.getFirstRow() + shift);
  2712. destRegion.setLastRow(destRegion.getLastRow() + shift);
  2713. addMergedRegion(destRegion);
  2714. }
  2715. }
  2716. }
  2717. }
  2718. /**
  2719. * Copies rows between srcStartRow and srcEndRow to the same sheet, starting at destStartRow
  2720. * Convenience function for {@link #copyRows(List, int, CellCopyPolicy)}
  2721. *
  2722. * Equivalent to copyRows(getRows(srcStartRow, srcEndRow, false), destStartRow, cellCopyPolicy)
  2723. *
  2724. * @param srcStartRow the index of the first row to copy the cells from in this sheet
  2725. * @param srcEndRow the index of the last row to copy the cells from in this sheet
  2726. * @param destStartRow the index of the first row to copy the cells to in this sheet
  2727. * @param cellCopyPolicy the policy to use to determine how cells are copied
  2728. */
  2729. @Beta
  2730. public void copyRows(int srcStartRow, int srcEndRow, int destStartRow, CellCopyPolicy cellCopyPolicy) {
  2731. final List<XSSFRow> srcRows = getRows(srcStartRow, srcEndRow, false); //FIXME: should be false, no need to create rows where src is only to copy them to dest
  2732. copyRows(srcRows, destStartRow, cellCopyPolicy);
  2733. }
  2734. /**
  2735. * Shifts rows between startRow and endRow n number of rows.
  2736. * If you use a negative number, it will shift rows up.
  2737. * Code ensures that rows don't wrap around.
  2738. *
  2739. * Calls shiftRows(startRow, endRow, n, false, false);
  2740. *
  2741. * <p>
  2742. * Additionally, shifts merged regions that are completely defined in these
  2743. * rows (i.e. merged 2 cells on a row to be shifted).
  2744. * @param startRow the row to start shifting
  2745. * @param endRow the row to end shifting
  2746. * @param n the number of rows to shift
  2747. */
  2748. @Override
  2749. public void shiftRows(int startRow, int endRow, int n) {
  2750. shiftRows(startRow, endRow, n, false, false);
  2751. }
  2752. /**
  2753. * Shifts rows between startRow and endRow n number of rows.
  2754. * If you use a negative number, it will shift rows up.
  2755. * Code ensures that rows don't wrap around
  2756. *
  2757. * <p>
  2758. * Additionally, shifts merged regions that are completely defined in these
  2759. * rows (i.e. merged 2 cells on a row to be shifted). All merged regions that are
  2760. * completely overlaid by shifting will be deleted.
  2761. *
  2762. * @param startRow the row to start shifting
  2763. * @param endRow the row to end shifting
  2764. * @param n the number of rows to shift
  2765. * @param copyRowHeight whether to copy the row height during the shift
  2766. * @param resetOriginalRowHeight whether to set the original row's height to the default
  2767. */
  2768. @Override
  2769. public void shiftRows(int startRow, int endRow, final int n, boolean copyRowHeight, boolean resetOriginalRowHeight) {
  2770. List<XSSFTable> overlappingTables = new ArrayList<>();
  2771. for (XSSFTable table : getTables()) {
  2772. if ((table.getStartRowIndex() < startRow && table.getEndRowIndex() < startRow)
  2773. || (table.getStartRowIndex() > endRow && table.getEndRowIndex() > endRow)) {
  2774. // not overlapping
  2775. } else {
  2776. overlappingTables.add(table);
  2777. }
  2778. }
  2779. int sheetIndex = getWorkbook().getSheetIndex(this);
  2780. String sheetName = getWorkbook().getSheetName(sheetIndex);
  2781. FormulaShifter formulaShifter = FormulaShifter.createForRowShift(
  2782. sheetIndex, sheetName, startRow, endRow, n, SpreadsheetVersion.EXCEL2007);
  2783. removeOverwritten(startRow, endRow, n);
  2784. shiftCommentsAndRows(startRow, endRow, n);
  2785. XSSFRowShifter rowShifter = new XSSFRowShifter(this);
  2786. rowShifter.shiftMergedRegions(startRow, endRow, n);
  2787. rowShifter.updateNamedRanges(formulaShifter);
  2788. rowShifter.updateFormulas(formulaShifter);
  2789. rowShifter.updateConditionalFormatting(formulaShifter);
  2790. rowShifter.updateHyperlinks(formulaShifter);
  2791. rebuildRows();
  2792. for (XSSFTable table : overlappingTables) {
  2793. rebuildTableFormulas(table);
  2794. }
  2795. }
  2796. /**
  2797. * Shifts columns between startColumn and endColumn n number of columns.
  2798. * If you use a negative number, it will shift columns left.
  2799. * Code ensures that columns don't wrap around
  2800. *
  2801. * @param startColumn the column to start shifting
  2802. * @param endColumn the column to end shifting
  2803. * @param n length of the shifting step
  2804. */
  2805. @Override
  2806. public void shiftColumns(int startColumn, int endColumn, final int n) {
  2807. List<XSSFTable> overlappingTables = new ArrayList<>();
  2808. for (XSSFTable table : getTables()) {
  2809. if ((table.getStartColIndex() < startColumn && table.getEndColIndex() < startColumn)
  2810. || (table.getStartColIndex() > endColumn && table.getEndColIndex() > endColumn)) {
  2811. // not overlapping
  2812. } else {
  2813. overlappingTables.add(table);
  2814. }
  2815. }
  2816. XSSFVMLDrawing vml = getVMLDrawing(false);
  2817. shiftCommentsForColumns(vml, startColumn, endColumn, n);
  2818. FormulaShifter formulaShifter = FormulaShifter.createForColumnShift(this.getWorkbook().getSheetIndex(this), this.getSheetName(), startColumn, endColumn, n, SpreadsheetVersion.EXCEL2007);
  2819. XSSFColumnShifter columnShifter = new XSSFColumnShifter(this);
  2820. columnShifter.shiftColumns(startColumn, endColumn, n);
  2821. columnShifter.shiftMergedRegions(startColumn, endColumn, n);
  2822. columnShifter.updateFormulas(formulaShifter);
  2823. columnShifter.updateConditionalFormatting(formulaShifter);
  2824. columnShifter.updateHyperlinks(formulaShifter);
  2825. columnShifter.updateNamedRanges(formulaShifter);
  2826. rebuildRows();
  2827. for (XSSFTable table : overlappingTables) {
  2828. rebuildTableFormulas(table);
  2829. }
  2830. }
  2831. private void rebuildTableFormulas(XSSFTable table) {
  2832. //correct all sheet table-reference-formulas which probably got damaged after shift rows/columns
  2833. for (CTTableColumn tableCol : table.getCTTable().getTableColumns().getTableColumnList()) {
  2834. if (tableCol.getCalculatedColumnFormula() != null) {
  2835. int id = Math.toIntExact(tableCol.getId());
  2836. String formula = tableCol.getCalculatedColumnFormula().getStringValue();
  2837. int rFirst = table.getStartCellReference().getRow() + table.getHeaderRowCount();
  2838. int rLast = table.getEndCellReference().getRow() - table.getTotalsRowCount();
  2839. int c = table.getStartCellReference().getCol() + id - 1;
  2840. final boolean cellFormulaValidationFlag = getWorkbook().getCellFormulaValidation();
  2841. try {
  2842. getWorkbook().setCellFormulaValidation(false);
  2843. for (int r = rFirst; r <= rLast; r++) {
  2844. XSSFRow row = getRow(r);
  2845. if (row == null) row = createRow(r);
  2846. XSSFCell cell = row.getCell(c, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
  2847. cell.setCellFormula(formula);
  2848. }
  2849. } finally {
  2850. getWorkbook().setCellFormulaValidation(cellFormulaValidationFlag);
  2851. }
  2852. }
  2853. }
  2854. }
  2855. private void rebuildRows() {
  2856. //rebuild the CTSheetData CTRow order
  2857. SortedMap<Long, CTRow> ctRows = new TreeMap<>();
  2858. CTSheetData sheetData = getCTWorksheet().getSheetData();
  2859. for (CTRow ctRow : sheetData.getRowList()) {
  2860. Long rownumL = ctRow.getR();
  2861. ctRows.put(rownumL, ctRow);
  2862. }
  2863. List<CTRow> ctRowList = new ArrayList<>(ctRows.values());
  2864. CTRow[] ctRowArray = new CTRow[ctRowList.size()];
  2865. ctRowArray = ctRowList.toArray(ctRowArray);
  2866. sheetData.setRowArray(ctRowArray);
  2867. //rebuild the _rows map
  2868. _rows.clear();
  2869. for (CTRow ctRow : sheetData.getRowList()) {
  2870. XSSFRow row = new XSSFRow(ctRow, this);
  2871. Integer rownumI = Math.toIntExact(row.getRowNum());
  2872. _rows.put(rownumI, row);
  2873. }
  2874. }
  2875. // remove all rows which will be overwritten
  2876. private void removeOverwritten(int startRow, int endRow, final int n) {
  2877. XSSFVMLDrawing vml = getVMLDrawing(false);
  2878. HashSet<Integer> rowsToRemoveSet = new HashSet<>();
  2879. for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
  2880. XSSFRow row = (XSSFRow)it.next();
  2881. int rownum = row.getRowNum();
  2882. // check if we should remove this row as it will be overwritten by the data later
  2883. if (shouldRemoveRow(startRow, endRow, n, rownum)) {
  2884. rowsToRemoveSet.add(rownum);
  2885. for (Cell c : row) {
  2886. if (!c.isPartOfArrayFormulaGroup()) {
  2887. //the support for deleting cells that are part of array formulas is not implemented yet
  2888. c.setBlank();
  2889. }
  2890. }
  2891. // remove row from worksheet.getSheetData row array
  2892. // Performance optimization: explicit boxing is slightly faster than auto-unboxing, though may use more memory
  2893. //noinspection UnnecessaryBoxing
  2894. final Integer rownumI = Integer.valueOf(row.getRowNum()); // NOSONAR
  2895. int idx = _rows.headMap(rownumI).size();
  2896. worksheet.getSheetData().removeRow(idx);
  2897. // remove row from _rows
  2898. it.remove();
  2899. }
  2900. }
  2901. // also remove any comments associated with this row
  2902. if (sheetComments != null) {
  2903. ArrayList<CellAddress> refsToRemove = new ArrayList<>();
  2904. Iterator<CellAddress> commentAddressIterator = sheetComments.getCellAddresses();
  2905. while (commentAddressIterator.hasNext()) {
  2906. CellAddress ref = commentAddressIterator.next();
  2907. // is this comment part of the current row?
  2908. if(rowsToRemoveSet.contains(ref.getRow())) {
  2909. refsToRemove.add(ref);
  2910. }
  2911. }
  2912. for (CellAddress ref : refsToRemove) {
  2913. sheetComments.removeComment(ref);
  2914. if (vml != null) {
  2915. vml.removeCommentShape(ref.getRow(), ref.getColumn());
  2916. }
  2917. }
  2918. }
  2919. // also remove any hyperlinks associated with this row
  2920. if (hyperlinks != null) {
  2921. for (XSSFHyperlink link : new ArrayList<>(hyperlinks)) {
  2922. CellRangeAddress range = CellRangeAddress.valueOf(link.getCellRef());
  2923. if (range.getFirstRow() == range.getLastRow() && rowsToRemoveSet.contains(range.getFirstRow())) {
  2924. removeHyperlink(link);
  2925. } else if (range.getFirstRow() != range.getLastRow()) {
  2926. boolean toRemove = true;
  2927. for (int i = range.getFirstRow(); i <= range.getLastRow() && toRemove; i++) {
  2928. toRemove = rowsToRemoveSet.contains(i);
  2929. }
  2930. if (toRemove) {
  2931. removeHyperlink(link);
  2932. }
  2933. }
  2934. }
  2935. }
  2936. }
  2937. private void shiftCommentsAndRows(int startRow, int endRow, final int n) {
  2938. // then do the actual moving and also adjust comments/rowHeight
  2939. // we need to sort it in a way so the shifting does not mess up the structures,
  2940. // i.e. when shifting down, start from down and go up, when shifting up, vice-versa
  2941. SortedMap<XSSFComment, Integer> commentsToShift = new TreeMap<>((o1, o2) -> {
  2942. int row1 = o1.getRow();
  2943. int row2 = o2.getRow();
  2944. if (row1 == row2) {
  2945. // ordering is not important when row is equal, but don't return zero to still
  2946. // get multiple comments per row into the map
  2947. return o1.hashCode() - o2.hashCode();
  2948. }
  2949. // when shifting down, sort higher row-values first
  2950. if (n > 0) {
  2951. return row1 < row2 ? 1 : -1;
  2952. } else {
  2953. // sort lower-row values first when shifting up
  2954. return row1 > row2 ? 1 : -1;
  2955. }
  2956. });
  2957. for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
  2958. XSSFRow row = (XSSFRow)it.next();
  2959. int rownum = row.getRowNum();
  2960. if(sheetComments != null) {
  2961. // calculate the new rownum
  2962. int newrownum = shiftedRowNum(startRow, endRow, n, rownum);
  2963. // is there a change necessary for the current row?
  2964. if(newrownum != rownum) {
  2965. Iterator<CellAddress> commentAddressIterator = sheetComments.getCellAddresses();
  2966. while (commentAddressIterator.hasNext()) {
  2967. CellAddress cellAddress = commentAddressIterator.next();
  2968. // is this comment part of the current row?
  2969. if(cellAddress.getRow() == rownum) {
  2970. XSSFComment oldComment = sheetComments.findCellComment(cellAddress);
  2971. if (oldComment != null) {
  2972. XSSFComment xssfComment = new XSSFComment(sheetComments, oldComment.getCTComment(),
  2973. oldComment.getCTShape());
  2974. // we should not perform the shifting right here as we would then find
  2975. // already shifted comments and would shift them again...
  2976. commentsToShift.put(xssfComment, newrownum);
  2977. }
  2978. }
  2979. }
  2980. }
  2981. }
  2982. if(rownum < startRow || rownum > endRow) {
  2983. continue;
  2984. }
  2985. row.shift(n);
  2986. }
  2987. // adjust all the affected comment-structures now
  2988. // the Map is sorted and thus provides them in the order that we need here,
  2989. // i.e. from down to up if shifting down, vice-versa otherwise
  2990. for(Map.Entry<XSSFComment, Integer> entry : commentsToShift.entrySet()) {
  2991. entry.getKey().setRow(entry.getValue());
  2992. }
  2993. rebuildRows();
  2994. }
  2995. private int shiftedRowNum(int startRow, int endRow, int n, int rownum) {
  2996. // no change if before any affected row
  2997. if(rownum < startRow && (n > 0 || (startRow - rownum) > n)) {
  2998. return rownum;
  2999. }
  3000. // no change if after any affected row
  3001. if(rownum > endRow && (n < 0 || (rownum - endRow) > n)) {
  3002. return rownum;
  3003. }
  3004. // row before and things are moved up
  3005. if(rownum < startRow) {
  3006. // row is moved down by the shifting
  3007. return rownum + (endRow - startRow);
  3008. }
  3009. // row is after and things are moved down
  3010. if(rownum > endRow) {
  3011. // row is moved up by the shifting
  3012. return rownum - (endRow - startRow);
  3013. }
  3014. // row is part of the shifted block
  3015. return rownum + n;
  3016. }
  3017. private void shiftCommentsForColumns(XSSFVMLDrawing vml, int startColumnIndex, int endColumnIndex, final int n){
  3018. // then do the actual moving and also adjust comments/rowHeight
  3019. // we need to sort it in a way so the shifting does not mess up the structures,
  3020. // i.e. when shifting down, start from down and go up, when shifting up, vice-versa
  3021. SortedMap<XSSFComment, Integer> commentsToShift = new TreeMap<>((o1, o2) -> {
  3022. int column1 = o1.getColumn();
  3023. int column2 = o2.getColumn();
  3024. if (column1 == column2) {
  3025. // ordering is not important when row is equal, but don't return zero to still
  3026. // get multiple comments per row into the map
  3027. return o1.hashCode() - o2.hashCode();
  3028. }
  3029. // when shifting down, sort higher row-values first
  3030. if (n > 0) {
  3031. return column1 < column2 ? 1 : -1;
  3032. } else {
  3033. // sort lower-row values first when shifting up
  3034. return column1 > column2 ? 1 : -1;
  3035. }
  3036. });
  3037. if (sheetComments != null) {
  3038. Iterator<CellAddress> commentAddressIterator = sheetComments.getCellAddresses();
  3039. while (commentAddressIterator.hasNext()) {
  3040. CellAddress oldCommentAddress = commentAddressIterator.next();
  3041. int columnIndex = oldCommentAddress.getColumn();
  3042. int newColumnIndex = shiftedRowNum(startColumnIndex, endColumnIndex, n, columnIndex);
  3043. if(newColumnIndex != columnIndex) {
  3044. XSSFComment oldComment = sheetComments.findCellComment(oldCommentAddress);
  3045. if (oldComment != null) {
  3046. XSSFComment xssfComment = new XSSFComment(sheetComments, oldComment.getCTComment(),
  3047. oldComment.getCTShape());
  3048. commentsToShift.put(xssfComment, newColumnIndex);
  3049. }
  3050. }
  3051. }
  3052. }
  3053. // adjust all the affected comment-structures now
  3054. // the Map is sorted and thus provides them in the order that we need here,
  3055. // i.e. from down to up if shifting down, vice-versa otherwise
  3056. for(Map.Entry<XSSFComment, Integer> entry : commentsToShift.entrySet()) {
  3057. entry.getKey().setColumn(entry.getValue());
  3058. }
  3059. rebuildRows();
  3060. }
  3061. /**
  3062. * Location of the top left visible cell Location of the top left visible cell in the bottom right
  3063. * pane (when in Left-to-Right mode).
  3064. *
  3065. * @param topRow the top row to show in desktop window pane
  3066. * @param leftCol the left column to show in desktop window pane
  3067. */
  3068. @Override
  3069. public void showInPane(int topRow, int leftCol) {
  3070. final CellReference cellReference = new CellReference(topRow, leftCol);
  3071. final String cellRef = cellReference.formatAsString();
  3072. final CTPane pane = getPane(true);
  3073. assert(pane != null);
  3074. pane.setTopLeftCell(cellRef);
  3075. }
  3076. @Override
  3077. public void ungroupColumn(int fromColumn, int toColumn) {
  3078. CTCols cols = worksheet.getColsArray(0);
  3079. for (int index = fromColumn; index <= toColumn; index++) {
  3080. CTCol col = columnHelper.getColumn(index, false);
  3081. if (col != null) {
  3082. short outlineLevel = col.getOutlineLevel();
  3083. col.setOutlineLevel((short) (outlineLevel - 1));
  3084. index = Math.toIntExact(col.getMax());
  3085. if (col.getOutlineLevel() <= 0) {
  3086. int colIndex = columnHelper.getIndexOfColumn(cols, col);
  3087. worksheet.getColsArray(0).removeCol(colIndex);
  3088. }
  3089. }
  3090. }
  3091. worksheet.setColsArray(0, cols);
  3092. setSheetFormatPrOutlineLevelCol();
  3093. }
  3094. /**
  3095. * Ungroup a range of rows that were previously groupped
  3096. *
  3097. * @param fromRow start row (0-based)
  3098. * @param toRow end row (0-based)
  3099. */
  3100. @Override
  3101. public void ungroupRow(int fromRow, int toRow) {
  3102. for (int i = fromRow; i <= toRow; i++) {
  3103. XSSFRow xrow = getRow(i);
  3104. if (xrow != null) {
  3105. CTRow ctRow = xrow.getCTRow();
  3106. int outlineLevel = ctRow.getOutlineLevel();
  3107. ctRow.setOutlineLevel((short) (outlineLevel - 1));
  3108. //remove a row only if the row has no cell and if the outline level is 0
  3109. if (outlineLevel == 1 && xrow.getFirstCellNum() == -1) {
  3110. removeRow(xrow);
  3111. }
  3112. }
  3113. }
  3114. setSheetFormatPrOutlineLevelRow();
  3115. }
  3116. private void setSheetFormatPrOutlineLevelRow(){
  3117. short maxLevelRow=getMaxOutlineLevelRows();
  3118. getSheetTypeSheetFormatPr().setOutlineLevelRow(maxLevelRow);
  3119. }
  3120. private void setSheetFormatPrOutlineLevelCol(){
  3121. short maxLevelCol=getMaxOutlineLevelCols();
  3122. getSheetTypeSheetFormatPr().setOutlineLevelCol(maxLevelCol);
  3123. }
  3124. protected CTSheetViews getSheetTypeSheetViews(final boolean create) {
  3125. final CTSheetViews views = (worksheet.isSetSheetViews() || !create)
  3126. ? worksheet.getSheetViews() : worksheet.addNewSheetViews();
  3127. assert(views != null || !create);
  3128. if (views == null) {
  3129. return null;
  3130. }
  3131. if (views.sizeOfSheetViewArray() == 0 && create) {
  3132. views.addNewSheetView();
  3133. }
  3134. return views;
  3135. }
  3136. /**
  3137. * Returns a flag indicating whether this sheet is selected.
  3138. * <p>
  3139. * When only 1 sheet is selected and active, this value should be in synch with the activeTab value.
  3140. * In case of a conflict, the Start Part setting wins and sets the active sheet tab.
  3141. * </p>
  3142. * Note: multiple sheets can be selected, but only one sheet can be active at one time.
  3143. *
  3144. * @return {@code true} if this sheet is selected
  3145. */
  3146. @Override
  3147. public boolean isSelected() {
  3148. final CTSheetView dsv = getDefaultSheetView(false);
  3149. return dsv != null && dsv.getTabSelected();
  3150. }
  3151. /**
  3152. * Sets a flag indicating whether this sheet is selected.
  3153. *
  3154. * <p>
  3155. * When only 1 sheet is selected and active, this value should be in synch with the activeTab value.
  3156. * In case of a conflict, the Start Part setting wins and sets the active sheet tab.
  3157. * </p>
  3158. * Note: multiple sheets can be selected, but only one sheet can be active at one time.
  3159. *
  3160. * @param value {@code true} if this sheet is selected
  3161. */
  3162. @Override
  3163. public void setSelected(boolean value) {
  3164. final CTSheetViews views = getSheetTypeSheetViews(true);
  3165. assert(views != null);
  3166. for (CTSheetView view : views.getSheetViewArray()) {
  3167. view.setTabSelected(value);
  3168. }
  3169. }
  3170. /**
  3171. * Register a hyperlink in the collection of hyperlinks on this sheet.
  3172. * Use {@link XSSFCell#setHyperlink(Hyperlink)} if the hyperlink is just for that one cell.
  3173. * Use this method if you want to add a Hyperlink that covers a range of sells. If you use
  3174. * this method, you will need to call {@link XSSFHyperlink#setCellReference(String)} to
  3175. * explicitly cell the value, eg B2 or B2:C3 (the 4 cells with B2 at top left and C3 at bottom right)
  3176. *
  3177. * @param hyperlink the link to add
  3178. */
  3179. public void addHyperlink(XSSFHyperlink hyperlink) {
  3180. hyperlinks.add(hyperlink);
  3181. }
  3182. /**
  3183. * Remove a hyperlink in the collection of hyperlinks on this sheet.
  3184. * {@link XSSFCell#removeHyperlink()} can be used if the hyperlink is just for that one cell.
  3185. *
  3186. * @param hyperlink the link to remove
  3187. * @since POI 5.1.0
  3188. */
  3189. public void removeHyperlink(XSSFHyperlink hyperlink) {
  3190. hyperlinks.remove(hyperlink);
  3191. }
  3192. /**
  3193. * Removes a hyperlink in the collection of hyperlinks on this sheet
  3194. *
  3195. * @param row row index
  3196. * @param column column index
  3197. */
  3198. @Internal
  3199. public void removeHyperlink(int row, int column) {
  3200. // CTHyperlinks is regenerated from scratch when writing out the spreadsheet
  3201. // so don't worry about maintaining hyperlinks and CTHyperlinks in parallel.
  3202. // only maintain hyperlinks
  3203. XSSFHyperlink hyperlink = getHyperlink(row, column);
  3204. if (hyperlink != null) {
  3205. if (hyperlink.getFirstRow() == row && hyperlink.getLastRow() == row
  3206. && hyperlink.getFirstColumn() == column && hyperlink.getLastColumn() == column) {
  3207. removeHyperlink(hyperlink);
  3208. } else {
  3209. //we have a cellRef that spans multiple cells - we just want to remove the hyperlink from one cell
  3210. //we delete this hyperlink but add new hyperlinks to cover the other cells that were served
  3211. //by the old hyperlink
  3212. boolean leftCreated = false;
  3213. boolean rightCreated = false;
  3214. if (hyperlink.getFirstColumn() < column) {
  3215. XSSFHyperlink newLink = new XSSFHyperlink(hyperlink);
  3216. newLink.setFirstColumn(hyperlink.getFirstColumn());
  3217. newLink.setLastColumn(column - 1);
  3218. newLink.setFirstRow(hyperlink.getFirstRow());
  3219. newLink.setLastRow(hyperlink.getLastRow());
  3220. addHyperlink(newLink);
  3221. leftCreated = true;
  3222. }
  3223. if (hyperlink.getLastColumn() > column) {
  3224. XSSFHyperlink newLink = new XSSFHyperlink(hyperlink);
  3225. newLink.setFirstColumn(column + 1);
  3226. newLink.setLastColumn(hyperlink.getLastColumn());
  3227. newLink.setFirstRow(hyperlink.getFirstRow());
  3228. newLink.setLastRow(hyperlink.getLastRow());
  3229. addHyperlink(newLink);
  3230. rightCreated = true;
  3231. }
  3232. if (hyperlink.getFirstRow() < row) {
  3233. XSSFHyperlink newLink = new XSSFHyperlink(hyperlink);
  3234. int firstColumn = leftCreated ? row : hyperlink.getFirstColumn();
  3235. int lastColumn = rightCreated ? row : hyperlink.getLastColumn();
  3236. newLink.setFirstColumn(firstColumn);
  3237. newLink.setLastColumn(lastColumn);
  3238. newLink.setFirstRow(hyperlink.getFirstRow());
  3239. newLink.setLastRow(row - 1);
  3240. addHyperlink(newLink);
  3241. }
  3242. if (hyperlink.getLastRow() > row) {
  3243. XSSFHyperlink newLink = new XSSFHyperlink(hyperlink);
  3244. int firstColumn = leftCreated ? row : hyperlink.getFirstColumn();
  3245. int lastColumn = rightCreated ? row : hyperlink.getLastColumn();
  3246. newLink.setFirstColumn(firstColumn);
  3247. newLink.setLastColumn(lastColumn);
  3248. newLink.setFirstRow(row + 1);
  3249. newLink.setLastRow(hyperlink.getLastRow());
  3250. addHyperlink(newLink);
  3251. }
  3252. removeHyperlink(hyperlink);
  3253. }
  3254. }
  3255. }
  3256. /**
  3257. * Return location of the active cell, e.g. {@code A1}.
  3258. *
  3259. * @return the location of the active cell.
  3260. */
  3261. @Override
  3262. public CellAddress getActiveCell() {
  3263. final CTSelection sts = getSheetTypeSelection(false);
  3264. final String address = (sts != null) ? sts.getActiveCell() : null;
  3265. return (address != null) ? new CellAddress(address) : null;
  3266. }
  3267. /**
  3268. * {@inheritDoc}
  3269. */
  3270. @Override
  3271. public void setActiveCell(CellAddress address) {
  3272. final CTSelection ctsel = getSheetTypeSelection(true);
  3273. assert(ctsel != null);
  3274. String ref = address.formatAsString();
  3275. ctsel.setActiveCell(ref);
  3276. ctsel.setSqref(Collections.singletonList(ref));
  3277. }
  3278. /**
  3279. * Does this sheet have any comments on it? We need to know,
  3280. * so we can decide about writing it to disk or not
  3281. */
  3282. public boolean hasComments() {
  3283. return sheetComments != null && sheetComments.getNumberOfComments() > 0;
  3284. }
  3285. protected int getNumberOfComments() {
  3286. return sheetComments == null ? 0 : sheetComments.getNumberOfComments();
  3287. }
  3288. private CTSelection getSheetTypeSelection(final boolean create) {
  3289. final CTSheetView dsv = getDefaultSheetView(create);
  3290. assert(dsv != null || !create);
  3291. if (dsv == null) {
  3292. return null;
  3293. }
  3294. final int sz = dsv.sizeOfSelectionArray();
  3295. if (sz == 0) {
  3296. return create ? dsv.addNewSelection() : null;
  3297. }
  3298. return dsv.getSelectionArray(sz - 1);
  3299. }
  3300. /**
  3301. * Return the default sheet view. This is the last one if the sheet's views, according to sec. 3.3.1.83
  3302. * of the OOXML spec: "A single sheet view definition. When more than 1 sheet view is defined in the file,
  3303. * it means that when opening the workbook, each sheet view corresponds to a separate window within the
  3304. * spreadsheet application, where each window is showing the particular sheet. containing the same
  3305. * workbookViewId value, the last sheetView definition is loaded, and the others are discarded.
  3306. * When multiple windows are viewing the same sheet, multiple sheetView elements (with corresponding
  3307. * workbookView entries) are saved."
  3308. */
  3309. private CTSheetView getDefaultSheetView(final boolean create) {
  3310. final CTSheetViews views = getSheetTypeSheetViews(create);
  3311. assert(views != null || !create);
  3312. if (views == null) {
  3313. return null;
  3314. }
  3315. final int sz = views.sizeOfSheetViewArray();
  3316. assert(sz > 0 || !create);
  3317. return (sz == 0) ? null : views.getSheetViewArray(sz - 1);
  3318. }
  3319. /**
  3320. * Returns the sheet's comments object if there is one,
  3321. * or null if not
  3322. *
  3323. * @param create create a new comments table if it does not exist
  3324. */
  3325. protected Comments getCommentsTable(boolean create) {
  3326. if(sheetComments == null && create){
  3327. // Try to create a comments table with the same number as
  3328. // the sheet has (i.e. sheet 1 -> comments 1)
  3329. try {
  3330. sheetComments = (Comments)createRelationship(
  3331. XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), Math.toIntExact(sheet.getSheetId()));
  3332. } catch(PartAlreadyExistsException e) {
  3333. // Technically a sheet doesn't need the same number as
  3334. // its comments, and clearly someone has already pinched
  3335. // our number! Go for the next available one instead
  3336. sheetComments = (Comments)createRelationship(
  3337. XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), -1);
  3338. }
  3339. if (sheetComments != null) {
  3340. sheetComments.setSheet(this);
  3341. }
  3342. }
  3343. return sheetComments;
  3344. }
  3345. private CTPageSetUpPr getSheetTypePageSetUpPr() {
  3346. CTSheetPr sheetPr = getSheetTypeSheetPr();
  3347. return sheetPr.isSetPageSetUpPr() ? sheetPr.getPageSetUpPr() : sheetPr.addNewPageSetUpPr();
  3348. }
  3349. private static boolean shouldRemoveRow(int startRow, int endRow, int n, int rownum) {
  3350. // is this row in the target-window where the moved rows will land?
  3351. if (rownum >= (startRow + n) && rownum <= (endRow + n)) {
  3352. // only remove it if the current row is not part of the data that is copied
  3353. if (n > 0 && rownum > endRow) {
  3354. return true;
  3355. }
  3356. else {
  3357. return n < 0 && rownum < startRow;
  3358. }
  3359. }
  3360. return false;
  3361. }
  3362. private CTPane getPane(final boolean create) {
  3363. final CTSheetView dsv = getDefaultSheetView(create);
  3364. assert(dsv != null || !create);
  3365. if (dsv == null) {
  3366. return null;
  3367. }
  3368. return (dsv.isSetPane() || !create) ? dsv.getPane() : dsv.addNewPane();
  3369. }
  3370. /**
  3371. * Return a master shared formula by index
  3372. *
  3373. * @param sid shared group index
  3374. * @return a CTCellFormula bean holding shared formula or {@code null} if not found
  3375. */
  3376. @Internal
  3377. public CTCellFormula getSharedFormula(int sid){
  3378. return sharedFormulas.get(sid);
  3379. }
  3380. void onReadCell(XSSFCell cell){
  3381. //collect cells holding shared formulas
  3382. CTCell ct = cell.getCTCell();
  3383. CTCellFormula f = ct.getF();
  3384. if (f != null && f.getT() == STCellFormulaType.SHARED && f.isSetRef() && f.getStringValue() != null) {
  3385. // save a detached copy to avoid XmlValueDisconnectedException,
  3386. // this may happen when the master cell of a shared formula is changed
  3387. CTCellFormula sf = (CTCellFormula)f.copy();
  3388. CellRangeAddress sfRef = CellRangeAddress.valueOf(sf.getRef());
  3389. CellReference cellRef = new CellReference(cell);
  3390. // If the shared formula range precedes the master cell then the preceding part is discarded, e.g.
  3391. // if the cell is E60 and the shared formula range is C60:M85 then the effective range is E60:M85
  3392. // see more details in https://issues.apache.org/bugzilla/show_bug.cgi?id=51710
  3393. if(cellRef.getCol() > sfRef.getFirstColumn() || cellRef.getRow() > sfRef.getFirstRow()){
  3394. String effectiveRef = new CellRangeAddress(
  3395. Math.max(cellRef.getRow(), sfRef.getFirstRow()), Math.max(cellRef.getRow(), sfRef.getLastRow()),
  3396. Math.max(cellRef.getCol(), sfRef.getFirstColumn()), Math.max(cellRef.getCol(), sfRef.getLastColumn()))
  3397. .formatAsString();
  3398. sf.setRef(effectiveRef);
  3399. }
  3400. sharedFormulas.put(Math.toIntExact(f.getSi()), sf);
  3401. }
  3402. if (f != null && f.getT() == STCellFormulaType.ARRAY && f.getRef() != null) {
  3403. arrayFormulas.add(CellRangeAddress.valueOf(f.getRef()));
  3404. }
  3405. }
  3406. @Override
  3407. protected void commit() throws IOException {
  3408. PackagePart part = getPackagePart();
  3409. try (OutputStream out = part.getOutputStream()) {
  3410. write(out);
  3411. }
  3412. }
  3413. protected void write(OutputStream out) throws IOException {
  3414. if (worksheet == null) {
  3415. throw new POIXMLException("Cannot write invalid sheet, internal data is missing");
  3416. }
  3417. boolean setToNull = false;
  3418. if(worksheet.sizeOfColsArray() == 1) {
  3419. CTCols col = worksheet.getColsArray(0);
  3420. if(col.sizeOfColArray() == 0) {
  3421. setToNull = true;
  3422. // this is necessary so that we do not write an empty <cols/> item into the sheet-xml in the xlsx-file
  3423. // Excel complains about a corrupted file if this shows up there!
  3424. worksheet.setColsArray(null);
  3425. } else {
  3426. setColWidthAttribute(col);
  3427. }
  3428. }
  3429. // Now re-generate our CTHyperlinks, if needed
  3430. if (!hyperlinks.isEmpty()) {
  3431. if(worksheet.getHyperlinks() == null) {
  3432. worksheet.addNewHyperlinks();
  3433. }
  3434. CTHyperlink[] ctHls = new CTHyperlink[hyperlinks.size()];
  3435. for(int i=0; i<ctHls.length; i++) {
  3436. // If our sheet has hyperlinks, have them add
  3437. // any relationships that they might need
  3438. XSSFHyperlink hyperlink = hyperlinks.get(i);
  3439. hyperlink.generateRelationIfNeeded(getPackagePart());
  3440. // Now grab their underling object
  3441. ctHls[i] = hyperlink.getCTHyperlink();
  3442. }
  3443. worksheet.getHyperlinks().setHyperlinkArray(ctHls);
  3444. }
  3445. else {
  3446. if (worksheet.getHyperlinks() != null) {
  3447. final int count = worksheet.getHyperlinks().sizeOfHyperlinkArray();
  3448. for (int i=count-1; i>=0; i--) {
  3449. worksheet.getHyperlinks().removeHyperlink(i);
  3450. }
  3451. // For some reason, we have to remove the hyperlinks one by one from the CTHyperlinks array
  3452. // before unsetting the hyperlink array.
  3453. // Resetting the hyperlink array seems to break some XML nodes.
  3454. //worksheet.getHyperlinks().setHyperlinkArray(new CTHyperlink[0]);
  3455. worksheet.unsetHyperlinks();
  3456. } /*else {
  3457. // nothing to do
  3458. }*/
  3459. }
  3460. CellRangeAddress cellRangeAddress = dimensionOverride;
  3461. if (cellRangeAddress == null) {
  3462. int minCell = Integer.MAX_VALUE, maxCell = Integer.MIN_VALUE;
  3463. for(Map.Entry<Integer, XSSFRow> entry : _rows.entrySet()) {
  3464. XSSFRow row = entry.getValue();
  3465. // first perform the normal write actions for the row
  3466. row.onDocumentWrite();
  3467. // then calculate min/max cell-numbers for the worksheet-dimension
  3468. if(row.getFirstCellNum() != -1) {
  3469. minCell = Math.min(minCell, row.getFirstCellNum());
  3470. }
  3471. if(row.getLastCellNum() != -1) {
  3472. maxCell = Math.max(maxCell, row.getLastCellNum()-1);
  3473. }
  3474. }
  3475. // finally, if we had at least one cell we can populate the optional dimension-field
  3476. if(minCell != Integer.MAX_VALUE) {
  3477. cellRangeAddress = new CellRangeAddress(getFirstRowNum(), getLastRowNum(), minCell, maxCell);
  3478. }
  3479. }
  3480. if (cellRangeAddress != null) {
  3481. if (worksheet.isSetDimension()) {
  3482. worksheet.getDimension().setRef(cellRangeAddress.formatAsString());
  3483. } else {
  3484. worksheet.addNewDimension().setRef(cellRangeAddress.formatAsString());
  3485. }
  3486. }
  3487. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  3488. xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
  3489. worksheet.save(out, xmlOptions);
  3490. // Bug 52233: Ensure that we have a col-array even if write() removed it
  3491. if(setToNull) {
  3492. worksheet.addNewCols();
  3493. }
  3494. }
  3495. /**
  3496. * @return true when Autofilters are locked and the sheet is protected.
  3497. */
  3498. public boolean isAutoFilterLocked() {
  3499. return isSheetLocked() && safeGetProtectionField().getAutoFilter();
  3500. }
  3501. /**
  3502. * @return true when Deleting columns is locked and the sheet is protected.
  3503. */
  3504. public boolean isDeleteColumnsLocked() {
  3505. return isSheetLocked() && safeGetProtectionField().getDeleteColumns();
  3506. }
  3507. /**
  3508. * @return true when Deleting rows is locked and the sheet is protected.
  3509. */
  3510. public boolean isDeleteRowsLocked() {
  3511. return isSheetLocked() && safeGetProtectionField().getDeleteRows();
  3512. }
  3513. /**
  3514. * @return true when Formatting cells is locked and the sheet is protected.
  3515. */
  3516. public boolean isFormatCellsLocked() {
  3517. return isSheetLocked() && safeGetProtectionField().getFormatCells();
  3518. }
  3519. /**
  3520. * @return true when Formatting columns is locked and the sheet is protected.
  3521. */
  3522. public boolean isFormatColumnsLocked() {
  3523. return isSheetLocked() && safeGetProtectionField().getFormatColumns();
  3524. }
  3525. /**
  3526. * @return true when Formatting rows is locked and the sheet is protected.
  3527. */
  3528. public boolean isFormatRowsLocked() {
  3529. return isSheetLocked() && safeGetProtectionField().getFormatRows();
  3530. }
  3531. /**
  3532. * @return true when Inserting columns is locked and the sheet is protected.
  3533. */
  3534. public boolean isInsertColumnsLocked() {
  3535. return isSheetLocked() && safeGetProtectionField().getInsertColumns();
  3536. }
  3537. /**
  3538. * @return true when Inserting hyperlinks is locked and the sheet is protected.
  3539. */
  3540. public boolean isInsertHyperlinksLocked() {
  3541. return isSheetLocked() && safeGetProtectionField().getInsertHyperlinks();
  3542. }
  3543. /**
  3544. * @return true when Inserting rows is locked and the sheet is protected.
  3545. */
  3546. public boolean isInsertRowsLocked() {
  3547. return isSheetLocked() && safeGetProtectionField().getInsertRows();
  3548. }
  3549. /**
  3550. * @return true when Pivot tables are locked and the sheet is protected.
  3551. */
  3552. public boolean isPivotTablesLocked() {
  3553. return isSheetLocked() && safeGetProtectionField().getPivotTables();
  3554. }
  3555. /**
  3556. * @return true when Sorting is locked and the sheet is protected.
  3557. */
  3558. public boolean isSortLocked() {
  3559. return isSheetLocked() && safeGetProtectionField().getSort();
  3560. }
  3561. /**
  3562. * @return true when Objects are locked and the sheet is protected.
  3563. */
  3564. public boolean isObjectsLocked() {
  3565. return isSheetLocked() && safeGetProtectionField().getObjects();
  3566. }
  3567. /**
  3568. * @return true when Scenarios are locked and the sheet is protected.
  3569. */
  3570. public boolean isScenariosLocked() {
  3571. return isSheetLocked() && safeGetProtectionField().getScenarios();
  3572. }
  3573. /**
  3574. * @return true when Selection of locked cells is locked and the sheet is protected.
  3575. */
  3576. public boolean isSelectLockedCellsLocked() {
  3577. return isSheetLocked() && safeGetProtectionField().getSelectLockedCells();
  3578. }
  3579. /**
  3580. * @return true when Selection of unlocked cells is locked and the sheet is protected.
  3581. */
  3582. public boolean isSelectUnlockedCellsLocked() {
  3583. return isSheetLocked() && safeGetProtectionField().getSelectUnlockedCells();
  3584. }
  3585. /**
  3586. * @return true when Sheet is Protected.
  3587. */
  3588. public boolean isSheetLocked() {
  3589. return worksheet.isSetSheetProtection() && safeGetProtectionField().getSheet();
  3590. }
  3591. /**
  3592. * Enable sheet protection
  3593. */
  3594. public void enableLocking() {
  3595. safeGetProtectionField().setSheet(true);
  3596. }
  3597. /**
  3598. * Disable sheet protection
  3599. */
  3600. public void disableLocking() {
  3601. safeGetProtectionField().setSheet(false);
  3602. }
  3603. /**
  3604. * Enable or disable Autofilters locking.
  3605. * This does not modify sheet protection status.
  3606. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3607. */
  3608. public void lockAutoFilter(boolean enabled) {
  3609. safeGetProtectionField().setAutoFilter(enabled);
  3610. }
  3611. /**
  3612. * Enable or disable Deleting columns locking.
  3613. * This does not modify sheet protection status.
  3614. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3615. */
  3616. public void lockDeleteColumns(boolean enabled) {
  3617. safeGetProtectionField().setDeleteColumns(enabled);
  3618. }
  3619. /**
  3620. * Enable or disable Deleting rows locking.
  3621. * This does not modify sheet protection status.
  3622. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3623. */
  3624. public void lockDeleteRows(boolean enabled) {
  3625. safeGetProtectionField().setDeleteRows(enabled);
  3626. }
  3627. /**
  3628. * Enable or disable Formatting cells locking.
  3629. * This does not modify sheet protection status.
  3630. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3631. */
  3632. public void lockFormatCells(boolean enabled) {
  3633. safeGetProtectionField().setFormatCells(enabled);
  3634. }
  3635. /**
  3636. * Enable or disable Formatting columns locking.
  3637. * This does not modify sheet protection status.
  3638. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3639. */
  3640. public void lockFormatColumns(boolean enabled) {
  3641. safeGetProtectionField().setFormatColumns(enabled);
  3642. }
  3643. /**
  3644. * Enable or disable Formatting rows locking.
  3645. * This does not modify sheet protection status.
  3646. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3647. */
  3648. public void lockFormatRows(boolean enabled) {
  3649. safeGetProtectionField().setFormatRows(enabled);
  3650. }
  3651. /**
  3652. * Enable or disable Inserting columns locking.
  3653. * This does not modify sheet protection status.
  3654. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3655. */
  3656. public void lockInsertColumns(boolean enabled) {
  3657. safeGetProtectionField().setInsertColumns(enabled);
  3658. }
  3659. /**
  3660. * Enable or disable Inserting hyperlinks locking.
  3661. * This does not modify sheet protection status.
  3662. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3663. */
  3664. public void lockInsertHyperlinks(boolean enabled) {
  3665. safeGetProtectionField().setInsertHyperlinks(enabled);
  3666. }
  3667. /**
  3668. * Enable or disable Inserting rows locking.
  3669. * This does not modify sheet protection status.
  3670. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3671. */
  3672. public void lockInsertRows(boolean enabled) {
  3673. safeGetProtectionField().setInsertRows(enabled);
  3674. }
  3675. /**
  3676. * Enable or disable Pivot Tables locking.
  3677. * This does not modify sheet protection status.
  3678. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3679. */
  3680. public void lockPivotTables(boolean enabled) {
  3681. safeGetProtectionField().setPivotTables(enabled);
  3682. }
  3683. /**
  3684. * Enable or disable Sort locking.
  3685. * This does not modify sheet protection status.
  3686. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3687. */
  3688. public void lockSort(boolean enabled) {
  3689. safeGetProtectionField().setSort(enabled);
  3690. }
  3691. /**
  3692. * Enable or disable Objects locking.
  3693. * This does not modify sheet protection status.
  3694. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3695. */
  3696. public void lockObjects(boolean enabled) {
  3697. safeGetProtectionField().setObjects(enabled);
  3698. }
  3699. /**
  3700. * Enable or disable Scenarios locking.
  3701. * This does not modify sheet protection status.
  3702. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3703. */
  3704. public void lockScenarios(boolean enabled) {
  3705. safeGetProtectionField().setScenarios(enabled);
  3706. }
  3707. /**
  3708. * Enable or disable Selection of locked cells locking.
  3709. * This does not modify sheet protection status.
  3710. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3711. */
  3712. public void lockSelectLockedCells(boolean enabled) {
  3713. safeGetProtectionField().setSelectLockedCells(enabled);
  3714. }
  3715. /**
  3716. * Enable or disable Selection of unlocked cells locking.
  3717. * This does not modify sheet protection status.
  3718. * To enforce this un-/locking, call {@link #disableLocking()} or {@link #enableLocking()}
  3719. */
  3720. public void lockSelectUnlockedCells(boolean enabled) {
  3721. safeGetProtectionField().setSelectUnlockedCells(enabled);
  3722. }
  3723. /**
  3724. * Reads the dimensions of the sheet data
  3725. * @return dimensions of the sheet data as a Cell Range (can be null)
  3726. * @since POI 5.2.3
  3727. */
  3728. public CellRangeAddress getDimension() {
  3729. if (dimensionOverride != null) {
  3730. return dimensionOverride;
  3731. }
  3732. CTSheetDimension ctSheetDimension = worksheet.getDimension();
  3733. String ref = ctSheetDimension == null ? null : ctSheetDimension.getRef();
  3734. if (ref != null) {
  3735. return CellRangeAddress.valueOf(ref);
  3736. }
  3737. return null;
  3738. }
  3739. private CTSheetProtection safeGetProtectionField() {
  3740. if (!isSheetProtectionEnabled()) {
  3741. return worksheet.addNewSheetProtection();
  3742. }
  3743. return worksheet.getSheetProtection();
  3744. }
  3745. /* package */ boolean isSheetProtectionEnabled() {
  3746. return (worksheet.isSetSheetProtection());
  3747. }
  3748. /* package */ boolean isCellInArrayFormulaContext(XSSFCell cell) {
  3749. for (CellRangeAddress range : arrayFormulas) {
  3750. if (range.isInRange(cell.getRowIndex(), cell.getColumnIndex())) {
  3751. return true;
  3752. }
  3753. }
  3754. return false;
  3755. }
  3756. /* package */ XSSFCell getFirstCellInArrayFormula(XSSFCell cell) {
  3757. for (CellRangeAddress range : arrayFormulas) {
  3758. if (range.isInRange(cell.getRowIndex(), cell.getColumnIndex())) {
  3759. return getRow(range.getFirstRow()).getCell(range.getFirstColumn());
  3760. }
  3761. }
  3762. return null;
  3763. }
  3764. /**
  3765. * Also creates cells if they don't exist
  3766. */
  3767. private CellRange<XSSFCell> getCellRange(CellRangeAddress range) {
  3768. int firstRow = range.getFirstRow();
  3769. int firstColumn = range.getFirstColumn();
  3770. int lastRow = range.getLastRow();
  3771. int lastColumn = range.getLastColumn();
  3772. int height = lastRow - firstRow + 1;
  3773. int width = lastColumn - firstColumn + 1;
  3774. List<XSSFCell> temp = new ArrayList<>(height * width);
  3775. for (int rowIn = firstRow; rowIn <= lastRow; rowIn++) {
  3776. for (int colIn = firstColumn; colIn <= lastColumn; colIn++) {
  3777. XSSFRow row = getRow(rowIn);
  3778. if (row == null) {
  3779. row = createRow(rowIn);
  3780. }
  3781. XSSFCell cell = row.getCell(colIn);
  3782. if (cell == null) {
  3783. cell = row.createCell(colIn);
  3784. }
  3785. temp.add(cell);
  3786. }
  3787. }
  3788. return SSCellRange.create(firstRow, firstColumn, height, width, temp, XSSFCell.class);
  3789. }
  3790. @Override
  3791. public CellRange<XSSFCell> setArrayFormula(String formula, CellRangeAddress range) {
  3792. CellRange<XSSFCell> cr = getCellRange(range);
  3793. XSSFCell mainArrayFormulaCell = cr.getTopLeftCell();
  3794. mainArrayFormulaCell.setCellArrayFormula(formula, range);
  3795. arrayFormulas.add(range);
  3796. return cr;
  3797. }
  3798. @Override
  3799. public CellRange<XSSFCell> removeArrayFormula(Cell cell) {
  3800. if (cell.getSheet() != this) {
  3801. throw new IllegalArgumentException("Specified cell does not belong to this sheet.");
  3802. }
  3803. for (CellRangeAddress range : arrayFormulas) {
  3804. if (range.isInRange(cell)) {
  3805. arrayFormulas.remove(range);
  3806. CellRange<XSSFCell> cr = getCellRange(range);
  3807. for (XSSFCell c : cr) {
  3808. c.setBlank();
  3809. }
  3810. return cr;
  3811. }
  3812. }
  3813. String ref = new CellReference(cell).formatAsString();
  3814. throw new IllegalArgumentException("Cell " + ref + " is not part of an array formula.");
  3815. }
  3816. @Override
  3817. public DataValidationHelper getDataValidationHelper() {
  3818. return dataValidationHelper;
  3819. }
  3820. @Override
  3821. public List<XSSFDataValidation> getDataValidations() {
  3822. List<XSSFDataValidation> xssfValidations = new ArrayList<>();
  3823. CTDataValidations dataValidations = this.worksheet.getDataValidations();
  3824. if (dataValidations != null) {
  3825. for (CTDataValidation ctDataValidation : dataValidations.getDataValidationArray()) {
  3826. CellRangeAddressList addressList = new CellRangeAddressList();
  3827. @SuppressWarnings("unchecked")
  3828. List<String> sqref = ctDataValidation.getSqref();
  3829. for (String stRef : sqref) {
  3830. String[] regions = stRef.split(" ");
  3831. for (String region : regions) {
  3832. String[] parts = region.split(":");
  3833. CellReference begin = new CellReference(parts[0]);
  3834. CellReference end = parts.length > 1 ? new CellReference(parts[1]) : begin;
  3835. CellRangeAddress cellRangeAddress = new CellRangeAddress(begin.getRow(), end.getRow(), begin.getCol(), end.getCol());
  3836. addressList.addCellRangeAddress(cellRangeAddress);
  3837. }
  3838. }
  3839. XSSFDataValidation xssfDataValidation = new XSSFDataValidation(addressList, ctDataValidation);
  3840. xssfValidations.add(xssfDataValidation);
  3841. }
  3842. }
  3843. return xssfValidations;
  3844. }
  3845. @Override
  3846. public void addValidationData(DataValidation dataValidation) {
  3847. XSSFDataValidation xssfDataValidation = (XSSFDataValidation)dataValidation;
  3848. CTDataValidations dataValidations = worksheet.getDataValidations();
  3849. if( dataValidations==null ) {
  3850. dataValidations = worksheet.addNewDataValidations();
  3851. }
  3852. int currentCount = dataValidations.sizeOfDataValidationArray();
  3853. CTDataValidation newval = dataValidations.addNewDataValidation();
  3854. newval.set(xssfDataValidation.getCtDataValidation());
  3855. dataValidations.setCount(currentCount + 1L);
  3856. }
  3857. @Override
  3858. public XSSFAutoFilter setAutoFilter(CellRangeAddress range) {
  3859. CTAutoFilter af = worksheet.getAutoFilter();
  3860. if(af == null) {
  3861. af = worksheet.addNewAutoFilter();
  3862. }
  3863. CellRangeAddress norm = new CellRangeAddress(range.getFirstRow(), range.getLastRow(),
  3864. range.getFirstColumn(), range.getLastColumn());
  3865. String ref = norm.formatAsString();
  3866. af.setRef(ref);
  3867. XSSFWorkbook wb = getWorkbook();
  3868. int sheetIndex = getWorkbook().getSheetIndex(this);
  3869. XSSFName name = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, sheetIndex);
  3870. if (name == null) {
  3871. name = wb.createBuiltInName(XSSFName.BUILTIN_FILTER_DB, sheetIndex);
  3872. }
  3873. name.getCTName().setHidden(true);
  3874. CellReference r1 = new CellReference(getSheetName(), range.getFirstRow(), range.getFirstColumn(), true, true);
  3875. CellReference r2 = new CellReference(null, range.getLastRow(), range.getLastColumn(), true, true);
  3876. String fmla = r1.formatAsString() + ":" + r2.formatAsString();
  3877. name.setRefersToFormula(fmla);
  3878. return new XSSFAutoFilter(this);
  3879. }
  3880. /**
  3881. * Creates a new Table, and associates it with this Sheet.
  3882. * <p>
  3883. * The table is assigned a default display name (since 4.1.1) which can be overridden
  3884. * by calling {@code setDisplayName}. The default display name is guaranteed to not conflict
  3885. * with the names of any {@code XSSFName} or {@code XSSFTable} in the workbook.
  3886. *
  3887. * @param tableArea
  3888. * the area that the table should cover, should not be null
  3889. * @return the created table
  3890. * @since 4.0.0
  3891. */
  3892. public XSSFTable createTable(AreaReference tableArea) {
  3893. if (!worksheet.isSetTableParts()) {
  3894. worksheet.addNewTableParts();
  3895. }
  3896. CTTableParts tblParts = worksheet.getTableParts();
  3897. CTTablePart tbl = tblParts.addNewTablePart();
  3898. // Table numbers need to be unique in the file, not just
  3899. // unique within the sheet. Find the next one
  3900. int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1;
  3901. // the id could already be taken after insertion/deletion of different tables
  3902. boolean loop = true;
  3903. while(loop) {
  3904. loop = false;
  3905. for (PackagePart packagePart : getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType())) {
  3906. String fileName = XSSFRelation.TABLE.getFileName(tableNumber);
  3907. if(fileName.equals(packagePart.getPartName().getName())) {
  3908. // duplicate found, increase the number and start iterating again
  3909. tableNumber++;
  3910. loop = true;
  3911. }
  3912. }
  3913. }
  3914. RelationPart rp = createRelationship(XSSFRelation.TABLE, getWorkbook().getXssfFactory(), tableNumber, false);
  3915. XSSFTable table = rp.getDocumentPart();
  3916. tbl.setId(rp.getRelationship().getId());
  3917. table.getCTTable().setId(tableNumber);
  3918. tables.put(tbl.getId(), table);
  3919. if(tableArea != null && table.supportsAreaReference(tableArea)) {
  3920. table.setArea(tableArea);
  3921. }
  3922. // Set the default name of the table. This must not conflict with any defined names.
  3923. while(tableNumber<Integer.MAX_VALUE) {
  3924. final String displayName="Table"+tableNumber;
  3925. if(getWorkbook().getTable(displayName) == null &&
  3926. getWorkbook().getName(displayName) == null) {
  3927. table.setDisplayName(displayName);
  3928. table.setName(displayName);
  3929. break;
  3930. }
  3931. ++tableNumber;
  3932. }
  3933. return table;
  3934. }
  3935. /**
  3936. * Returns any tables associated with this Sheet
  3937. */
  3938. public List<XSSFTable> getTables() {
  3939. return new ArrayList<>(tables.values());
  3940. }
  3941. /**
  3942. * Remove table references and relations
  3943. * @param t table to remove
  3944. */
  3945. public void removeTable(XSSFTable t) {
  3946. String rId = getRelationId(t);
  3947. long id = t.getCTTable().getId();
  3948. Map.Entry<String, XSSFTable> toDelete = null;
  3949. for (Map.Entry<String, XSSFTable> entry : tables.entrySet()) {
  3950. if (entry.getValue().getCTTable().getId() == id) toDelete = entry;
  3951. }
  3952. if (toDelete != null) {
  3953. removeRelation(getRelationById(toDelete.getKey()), true);
  3954. tables.remove(toDelete.getKey());
  3955. toDelete.getValue().onTableDelete();
  3956. CTTableParts tblParts = worksheet.getTableParts();
  3957. int matchedPos = -1;
  3958. if (rId != null) {
  3959. for (int i = 0; i < tblParts.sizeOfTablePartArray(); i++) {
  3960. if (rId.equals(tblParts.getTablePartArray(i).getId())) {
  3961. matchedPos = i;
  3962. break;
  3963. }
  3964. }
  3965. }
  3966. if (matchedPos != -1) {
  3967. tblParts.removeTablePart(matchedPos);
  3968. }
  3969. }
  3970. }
  3971. @Override
  3972. public XSSFSheetConditionalFormatting getSheetConditionalFormatting(){
  3973. return new XSSFSheetConditionalFormatting(this);
  3974. }
  3975. /**
  3976. * Get background color of the sheet tab.
  3977. * Returns {@code null} if no sheet tab color is set.
  3978. *
  3979. * @return the background color of the sheet tab
  3980. */
  3981. public XSSFColor getTabColor() {
  3982. CTSheetPr pr = worksheet.getSheetPr();
  3983. if(pr == null) {
  3984. pr = worksheet.addNewSheetPr();
  3985. }
  3986. if (!pr.isSetTabColor()) {
  3987. return null;
  3988. }
  3989. return XSSFColor.from(pr.getTabColor(), getWorkbook().getStylesSource().getIndexedColors());
  3990. }
  3991. /**
  3992. * Set background color of the sheet tab
  3993. *
  3994. * @param color the color to set
  3995. */
  3996. public void setTabColor(XSSFColor color) {
  3997. CTSheetPr pr = worksheet.getSheetPr();
  3998. if(pr == null) {
  3999. pr = worksheet.addNewSheetPr();
  4000. }
  4001. pr.setTabColor(color.getCTColor());
  4002. }
  4003. @Override
  4004. public CellRangeAddress getRepeatingRows() {
  4005. return getRepeatingRowsOrColumns(true);
  4006. }
  4007. @Override
  4008. public CellRangeAddress getRepeatingColumns() {
  4009. return getRepeatingRowsOrColumns(false);
  4010. }
  4011. @Override
  4012. public void setRepeatingRows(CellRangeAddress rowRangeRef) {
  4013. CellRangeAddress columnRangeRef = getRepeatingColumns();
  4014. setRepeatingRowsAndColumns(rowRangeRef, columnRangeRef);
  4015. }
  4016. @Override
  4017. public void setRepeatingColumns(CellRangeAddress columnRangeRef) {
  4018. CellRangeAddress rowRangeRef = getRepeatingRows();
  4019. setRepeatingRowsAndColumns(rowRangeRef, columnRangeRef);
  4020. }
  4021. private void setRepeatingRowsAndColumns(
  4022. CellRangeAddress rowDef, CellRangeAddress colDef) {
  4023. int col1 = -1;
  4024. int col2 = -1;
  4025. int row1 = -1;
  4026. int row2 = -1;
  4027. if (rowDef != null) {
  4028. row1 = rowDef.getFirstRow();
  4029. row2 = rowDef.getLastRow();
  4030. if ((row1 == -1 && row2 != -1)
  4031. || row1 < -1 || row2 < -1 || row1 > row2) {
  4032. throw new IllegalArgumentException("Invalid row range specification");
  4033. }
  4034. }
  4035. if (colDef != null) {
  4036. col1 = colDef.getFirstColumn();
  4037. col2 = colDef.getLastColumn();
  4038. if ((col1 == -1 && col2 != -1)
  4039. || col1 < -1 || col2 < -1 || col1 > col2) {
  4040. throw new IllegalArgumentException(
  4041. "Invalid column range specification");
  4042. }
  4043. }
  4044. int sheetIndex = getWorkbook().getSheetIndex(this);
  4045. boolean removeAll = rowDef == null && colDef == null;
  4046. XSSFName name = getWorkbook().getBuiltInName(
  4047. XSSFName.BUILTIN_PRINT_TITLE, sheetIndex);
  4048. if (removeAll) {
  4049. if (name != null) {
  4050. getWorkbook().removeName(name);
  4051. }
  4052. return;
  4053. }
  4054. if (name == null) {
  4055. name = getWorkbook().createBuiltInName(
  4056. XSSFName.BUILTIN_PRINT_TITLE, sheetIndex);
  4057. }
  4058. String reference = getReferenceBuiltInRecord(
  4059. name.getSheetName(), col1, col2, row1, row2);
  4060. name.setRefersToFormula(reference);
  4061. // If the print setup isn't currently defined, then add it
  4062. // in but without printer defaults
  4063. // If it's already there, leave it as-is!
  4064. if (worksheet.isSetPageSetup() && worksheet.isSetPageMargins()) {
  4065. // Everything we need is already there
  4066. } else {
  4067. // Have initial ones put in place
  4068. getPrintSetup().setValidSettings(false);
  4069. }
  4070. }
  4071. private static String getReferenceBuiltInRecord(
  4072. String sheetName, int startC, int endC, int startR, int endR) {
  4073. // Excel example for built-in title:
  4074. // 'second sheet'!$E:$F,'second sheet'!$2:$3
  4075. CellReference colRef =
  4076. new CellReference(sheetName, 0, startC, true, true);
  4077. CellReference colRef2 =
  4078. new CellReference(sheetName, 0, endC, true, true);
  4079. CellReference rowRef =
  4080. new CellReference(sheetName, startR, 0, true, true);
  4081. CellReference rowRef2 =
  4082. new CellReference(sheetName, endR, 0, true, true);
  4083. String escapedName = SheetNameFormatter.format(sheetName);
  4084. String c = "";
  4085. String r = "";
  4086. if (startC != -1 || endC != -1) {
  4087. String col1 = colRef.getCellRefParts()[2];
  4088. String col2 = colRef2.getCellRefParts()[2];
  4089. c = escapedName + "!$" + col1 + ":$" + col2;
  4090. }
  4091. if (startR != -1 || endR != -1) {
  4092. String row1 = rowRef.getCellRefParts()[1];
  4093. String row2 = rowRef2.getCellRefParts()[1];
  4094. if (!row1.equals("0") && !row2.equals("0")) {
  4095. r = escapedName + "!$" + row1 + ":$" + row2;
  4096. }
  4097. }
  4098. StringBuilder rng = new StringBuilder();
  4099. rng.append(c);
  4100. if(rng.length() > 0 && r.length() > 0) {
  4101. rng.append(',');
  4102. }
  4103. rng.append(r);
  4104. return rng.toString();
  4105. }
  4106. private CellRangeAddress getRepeatingRowsOrColumns(boolean rows) {
  4107. int sheetIndex = getWorkbook().getSheetIndex(this);
  4108. XSSFName name = getWorkbook().getBuiltInName(
  4109. XSSFName.BUILTIN_PRINT_TITLE, sheetIndex);
  4110. if (name == null ) {
  4111. return null;
  4112. }
  4113. String refStr = name.getRefersToFormula();
  4114. if (refStr == null) {
  4115. return null;
  4116. }
  4117. String[] parts = refStr.split(",");
  4118. int maxRowIndex = SpreadsheetVersion.EXCEL2007.getLastRowIndex();
  4119. int maxColIndex = SpreadsheetVersion.EXCEL2007.getLastColumnIndex();
  4120. for (String part : parts) {
  4121. CellRangeAddress range = CellRangeAddress.valueOf(part);
  4122. if ((range.getFirstColumn() == 0
  4123. && range.getLastColumn() == maxColIndex)
  4124. || (range.getFirstColumn() == -1
  4125. && range.getLastColumn() == -1)) {
  4126. if (rows) {
  4127. return range;
  4128. }
  4129. } else if (range.getFirstRow() == 0
  4130. && range.getLastRow() == maxRowIndex
  4131. || (range.getFirstRow() == -1
  4132. && range.getLastRow() == -1)) {
  4133. if (!rows) {
  4134. return range;
  4135. }
  4136. }
  4137. }
  4138. return null;
  4139. }
  4140. /**
  4141. * Creates an empty XSSFPivotTable and sets up all its relationships
  4142. * including: pivotCacheDefinition, pivotCacheRecords
  4143. * @return returns a pivotTable
  4144. */
  4145. @Beta
  4146. private XSSFPivotTable createPivotTable() {
  4147. XSSFWorkbook wb = getWorkbook();
  4148. List<XSSFPivotTable> pivotTables = wb.getPivotTables();
  4149. int tableId = getWorkbook().getPivotTables().size()+1;
  4150. //Create relationship between pivotTable and the worksheet
  4151. XSSFPivotTable pivotTable = (XSSFPivotTable) createRelationship(XSSFRelation.PIVOT_TABLE,
  4152. getWorkbook().getXssfFactory(), tableId);
  4153. pivotTable.setParentSheet(this);
  4154. pivotTables.add(pivotTable);
  4155. XSSFWorkbook workbook = getWorkbook();
  4156. //Create relationship between the pivot cache defintion and the workbook
  4157. XSSFPivotCacheDefinition pivotCacheDefinition = (XSSFPivotCacheDefinition) workbook.
  4158. createRelationship(XSSFRelation.PIVOT_CACHE_DEFINITION, getWorkbook().getXssfFactory(), tableId);
  4159. String rId = workbook.getRelationId(pivotCacheDefinition);
  4160. //Create relationship between pivotTable and pivotCacheDefinition without creating a new instance
  4161. PackagePart pivotPackagePart = pivotTable.getPackagePart();
  4162. pivotPackagePart.addRelationship(pivotCacheDefinition.getPackagePart().getPartName(),
  4163. TargetMode.INTERNAL, XSSFRelation.PIVOT_CACHE_DEFINITION.getRelation());
  4164. pivotTable.setPivotCacheDefinition(pivotCacheDefinition);
  4165. //Create pivotCache and sets up it's relationship with the workbook
  4166. pivotTable.setPivotCache(new XSSFPivotCache(workbook.addPivotCache(rId)));
  4167. //Create relationship between pivotcacherecord and pivotcachedefinition
  4168. XSSFPivotCacheRecords pivotCacheRecords = (XSSFPivotCacheRecords) pivotCacheDefinition.
  4169. createRelationship(XSSFRelation.PIVOT_CACHE_RECORDS, getWorkbook().getXssfFactory(), tableId);
  4170. //Set relationships id for pivotCacheDefinition to pivotCacheRecords
  4171. pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().setId(pivotCacheDefinition.getRelationId(pivotCacheRecords));
  4172. wb.setPivotTables(pivotTables);
  4173. return pivotTable;
  4174. }
  4175. /**
  4176. * Create a pivot table using the AreaReference range on sourceSheet, at the given position.
  4177. * If the source reference contains a sheet name, it must match the sourceSheet
  4178. * @param source location of pivot data
  4179. * @param position A reference to the top left cell where the pivot table will start
  4180. * @param sourceSheet The sheet containing the source data, if the source reference doesn't contain a sheet name
  4181. * @throws IllegalArgumentException if source references a sheet different than sourceSheet
  4182. * @return The pivot table
  4183. */
  4184. @Beta
  4185. public XSSFPivotTable createPivotTable(final AreaReference source, CellReference position, Sheet sourceSheet) {
  4186. final String sourceSheetName = source.getFirstCell().getSheetName();
  4187. if(sourceSheetName != null && !sourceSheetName.equalsIgnoreCase(sourceSheet.getSheetName())) {
  4188. throw new IllegalArgumentException("The area is referenced in another sheet than the "
  4189. + "defined source sheet " + sourceSheet.getSheetName() + ".");
  4190. }
  4191. return createPivotTable(position, sourceSheet, wsSource -> {
  4192. final String[] firstCell = source.getFirstCell().getCellRefParts();
  4193. final String firstRow = firstCell[1];
  4194. final String firstCol = firstCell[2];
  4195. final String[] lastCell = source.getLastCell().getCellRefParts();
  4196. final String lastRow = lastCell[1];
  4197. final String lastCol = lastCell[2];
  4198. final String ref = firstCol+firstRow+':'+lastCol+lastRow; //or just source.formatAsString()
  4199. wsSource.setRef(ref);
  4200. });
  4201. }
  4202. /**
  4203. * Create a pivot table using the AreaReference or named/table range on sourceSheet, at the given position.
  4204. * If the source reference contains a sheet name, it must match the sourceSheet.
  4205. * @param position A reference to the top left cell where the pivot table will start
  4206. * @param sourceSheet The sheet containing the source data, if the source reference doesn't contain a sheet name
  4207. * @param refConfig A reference to the pivot table configurator
  4208. * @throws IllegalArgumentException if source references a sheet different than sourceSheet
  4209. * @return The pivot table
  4210. */
  4211. private XSSFPivotTable createPivotTable(CellReference position, Sheet sourceSheet, PivotTableReferenceConfigurator refConfig) {
  4212. XSSFPivotTable pivotTable = createPivotTable();
  4213. //Creates default settings for the pivot table
  4214. pivotTable.setDefaultPivotTableDefinition();
  4215. //Set sources and references
  4216. pivotTable.createSourceReferences(position, sourceSheet, refConfig);
  4217. //Create cachefield/s and empty SharedItems - must be after creating references
  4218. pivotTable.getPivotCacheDefinition().createCacheFields(sourceSheet);
  4219. pivotTable.createDefaultDataColumns();
  4220. return pivotTable;
  4221. }
  4222. /**
  4223. * Create a pivot table using the AreaReference range, at the given position.
  4224. * If the source reference contains a sheet name, that sheet is used, otherwise this sheet is assumed as the source sheet.
  4225. * @param source location of pivot data
  4226. * @param position A reference to the top left cell where the pivot table will start
  4227. * @return The pivot table
  4228. */
  4229. @Beta
  4230. public XSSFPivotTable createPivotTable(AreaReference source, CellReference position){
  4231. final String sourceSheetName = source.getFirstCell().getSheetName();
  4232. if(sourceSheetName != null && !sourceSheetName.equalsIgnoreCase(this.getSheetName())) {
  4233. final XSSFSheet sourceSheet = getWorkbook().getSheet(sourceSheetName);
  4234. return createPivotTable(source, position, sourceSheet);
  4235. }
  4236. return createPivotTable(source, position, this);
  4237. }
  4238. /**
  4239. * Create a pivot table using the Name range reference on sourceSheet, at the given position.
  4240. * If the source reference contains a sheet name, it must match the sourceSheet
  4241. * @param source location of pivot data
  4242. * @param position A reference to the top left cell where the pivot table will start
  4243. * @param sourceSheet The sheet containing the source data, if the source reference doesn't contain a sheet name
  4244. * @throws IllegalArgumentException if source references a sheet different than sourceSheet
  4245. * @return The pivot table
  4246. */
  4247. @Beta
  4248. public XSSFPivotTable createPivotTable(final Name source, CellReference position, Sheet sourceSheet) {
  4249. if(source.getSheetName() != null && !source.getSheetName().equals(sourceSheet.getSheetName())) {
  4250. throw new IllegalArgumentException("The named range references another sheet than the "
  4251. + "defined source sheet " + sourceSheet.getSheetName() + ".");
  4252. }
  4253. return createPivotTable(position, sourceSheet, wsSource -> wsSource.setName(source.getNameName()));
  4254. }
  4255. /**
  4256. * Create a pivot table using the Name range, at the given position.
  4257. * If the source reference contains a sheet name, that sheet is used, otherwise this sheet is assumed as the source sheet.
  4258. * @param source location of pivot data
  4259. * @param position A reference to the top left cell where the pivot table will start
  4260. * @return The pivot table
  4261. */
  4262. @Beta
  4263. public XSSFPivotTable createPivotTable(Name source, CellReference position) {
  4264. return createPivotTable(source, position, getWorkbook().getSheet(source.getSheetName()));
  4265. }
  4266. /**
  4267. * Create a pivot table using the Table, at the given position.
  4268. * Tables are required to have a sheet reference, so no additional logic around reference sheet is needed.
  4269. * @param source location of pivot data
  4270. * @param position A reference to the top left cell where the pivot table will start
  4271. * @return The pivot table
  4272. */
  4273. @Beta
  4274. public XSSFPivotTable createPivotTable(final Table source, CellReference position) {
  4275. return createPivotTable(position, getWorkbook().getSheet(source.getSheetName()), wsSource -> wsSource.setName(source.getName()));
  4276. }
  4277. /**
  4278. * Returns all the pivot tables for this Sheet
  4279. */
  4280. @Beta
  4281. public List<XSSFPivotTable> getPivotTables() {
  4282. List<XSSFPivotTable> tables = new ArrayList<>();
  4283. for (XSSFPivotTable table : getWorkbook().getPivotTables()) {
  4284. if (table.getParent() == this) {
  4285. tables.add(table);
  4286. }
  4287. }
  4288. return tables;
  4289. }
  4290. @Override
  4291. public int getColumnOutlineLevel(int columnIndex) {
  4292. CTCol col = columnHelper.getColumn(columnIndex, false);
  4293. if (col == null) {
  4294. return 0;
  4295. }
  4296. return col.getOutlineLevel();
  4297. }
  4298. /**
  4299. * Add ignored errors (usually to suppress them in the UI of a consuming
  4300. * application).
  4301. *
  4302. * @param cell Cell.
  4303. * @param ignoredErrorTypes Types of error to ignore there.
  4304. */
  4305. public void addIgnoredErrors(CellReference cell, IgnoredErrorType... ignoredErrorTypes) {
  4306. addIgnoredErrors(cell.formatAsString(false), ignoredErrorTypes);
  4307. }
  4308. /**
  4309. * Ignore errors across a range of cells.
  4310. *
  4311. * @param region Range of cells.
  4312. * @param ignoredErrorTypes Types of error to ignore there.
  4313. */
  4314. public void addIgnoredErrors(CellRangeAddress region, IgnoredErrorType... ignoredErrorTypes) {
  4315. region.validate(SpreadsheetVersion.EXCEL2007);
  4316. addIgnoredErrors(region.formatAsString(), ignoredErrorTypes);
  4317. }
  4318. /**
  4319. * Returns the errors currently being ignored and the ranges
  4320. * where they are ignored.
  4321. *
  4322. * @return Map of error type to the range(s) where they are ignored.
  4323. */
  4324. public Map<IgnoredErrorType, Set<CellRangeAddress>> getIgnoredErrors() {
  4325. Map<IgnoredErrorType, Set<CellRangeAddress>> result = new LinkedHashMap<>();
  4326. if (worksheet.isSetIgnoredErrors()) {
  4327. for (CTIgnoredError err : worksheet.getIgnoredErrors().getIgnoredErrorList()) {
  4328. for (IgnoredErrorType errType : XSSFIgnoredErrorHelper.getErrorTypes(err)) {
  4329. if (!result.containsKey(errType)) {
  4330. result.put(errType, new LinkedHashSet<>());
  4331. }
  4332. for (Object ref : err.getSqref()) {
  4333. result.get(errType).add(CellRangeAddress.valueOf(ref.toString()));
  4334. }
  4335. }
  4336. }
  4337. }
  4338. return result;
  4339. }
  4340. private void addIgnoredErrors(String ref, IgnoredErrorType... ignoredErrorTypes) {
  4341. CTIgnoredErrors ctIgnoredErrors = worksheet.isSetIgnoredErrors() ? worksheet.getIgnoredErrors() : worksheet.addNewIgnoredErrors();
  4342. CTIgnoredError ctIgnoredError = ctIgnoredErrors.addNewIgnoredError();
  4343. XSSFIgnoredErrorHelper.addIgnoredErrors(ctIgnoredError, ref, ignoredErrorTypes);
  4344. }
  4345. /**
  4346. * called when a sheet is being deleted/removed from a workbook, to clean up relations and other document pieces tied to the sheet
  4347. */
  4348. protected void onSheetDelete() {
  4349. for (RelationPart part : getRelationParts()) {
  4350. if (part.getDocumentPart() instanceof XSSFTable) {
  4351. // call table delete
  4352. removeTable(part.getDocumentPart());
  4353. continue;
  4354. }
  4355. removeRelation(part.getDocumentPart(), true);
  4356. }
  4357. }
  4358. /**
  4359. * when a cell with a 'master' shared formula is removed, the next cell in the range becomes the master
  4360. * @param cell The cell that is removed
  4361. * @param evalWb BaseXSSFEvaluationWorkbook in use, if one exists
  4362. */
  4363. protected void onDeleteFormula(XSSFCell cell, BaseXSSFEvaluationWorkbook evalWb){
  4364. CTCellFormula f = cell.getCTCell().getF();
  4365. if (f != null && f.getT() == STCellFormulaType.SHARED && f.isSetRef() && f.getStringValue() != null) {
  4366. CellRangeAddress ref = CellRangeAddress.valueOf(f.getRef());
  4367. if(ref.getNumberOfCells() > 1){
  4368. DONE:
  4369. for(int i = cell.getRowIndex(); i <= ref.getLastRow(); i++){
  4370. XSSFRow row = getRow(i);
  4371. if(row != null) {
  4372. for(int j = cell.getColumnIndex(); j <= ref.getLastColumn(); j++){
  4373. XSSFCell nextCell = row.getCell(j);
  4374. if(nextCell != null && nextCell != cell && nextCell.getCellType() == CellType.FORMULA) {
  4375. CTCellFormula nextF = nextCell.getCTCell().getF();
  4376. if (nextF.getT() == STCellFormulaType.SHARED && nextF.getSi() == f.getSi()) {
  4377. nextF.setStringValue(nextCell.getCellFormula(evalWb));
  4378. CellRangeAddress nextRef = new CellRangeAddress(
  4379. nextCell.getRowIndex(), ref.getLastRow(),
  4380. nextCell.getColumnIndex(), ref.getLastColumn());
  4381. nextF.setRef(nextRef.formatAsString());
  4382. sharedFormulas.put(Math.toIntExact(nextF.getSi()), nextF);
  4383. break DONE;
  4384. }
  4385. }
  4386. }
  4387. }
  4388. }
  4389. }
  4390. }
  4391. }
  4392. /**
  4393. * Determine the OleObject which links shapes with embedded resources
  4394. *
  4395. * @param shapeId the shape id
  4396. * @return the CTOleObject of the shape
  4397. */
  4398. protected CTOleObject readOleObject(long shapeId) {
  4399. if (!getCTWorksheet().isSetOleObjects()) {
  4400. return null;
  4401. }
  4402. // we use a XmlCursor here to handle oleObject with-/out AlternateContent wrappers
  4403. String xquery = "declare namespace p='"+XSSFRelation.NS_SPREADSHEETML+"' .//p:oleObject";
  4404. try (XmlCursor cur = getCTWorksheet().getOleObjects().newCursor()) {
  4405. cur.selectPath(xquery);
  4406. CTOleObject coo = null;
  4407. while (cur.toNextSelection()) {
  4408. String sId = cur.getAttributeText(new QName(null, "shapeId"));
  4409. if (sId == null || Long.parseLong(sId) != shapeId) {
  4410. continue;
  4411. }
  4412. XmlObject xObj = cur.getObject();
  4413. if (xObj instanceof CTOleObject) {
  4414. // the unusual case ...
  4415. coo = (CTOleObject)xObj;
  4416. } else {
  4417. XMLStreamReader reader = cur.newXMLStreamReader();
  4418. try {
  4419. CTOleObjects coos = CTOleObjects.Factory.parse(reader);
  4420. if (coos.sizeOfOleObjectArray() == 0) {
  4421. continue;
  4422. }
  4423. coo = coos.getOleObjectArray(0);
  4424. } catch (XmlException e) {
  4425. LOG.atInfo().withThrowable(e).log("can't parse CTOleObjects");
  4426. } finally {
  4427. try {
  4428. reader.close();
  4429. } catch (XMLStreamException e) {
  4430. LOG.atInfo().withThrowable(e).log("can't close reader");
  4431. }
  4432. }
  4433. }
  4434. // there are choice and fallback OleObject ... we prefer the one having the objectPr element,
  4435. // which is in the choice element
  4436. if (cur.toChild(XSSFRelation.NS_SPREADSHEETML, "objectPr")) {
  4437. break;
  4438. }
  4439. }
  4440. return coo;
  4441. }
  4442. }
  4443. public XSSFHeaderFooterProperties getHeaderFooterProperties() {
  4444. return new XSSFHeaderFooterProperties(getSheetTypeHeaderFooter());
  4445. }
  4446. /**
  4447. * Currently, this is for internal use. Overrides the default dimensions of the sheet.
  4448. * @param dimension {@link CellRangeAddress}, <code>null</code> removes the existing override
  4449. * @since POI 5.2.3
  4450. */
  4451. @Beta
  4452. public void setDimensionOverride(CellRangeAddress dimension) {
  4453. this.dimensionOverride = dimension;
  4454. }
  4455. static void cloneTables(XSSFSheet sheet) {
  4456. for (XSSFTable table : sheet.getTables()) {
  4457. // clone table; XSSFTable.setArea fails and throws exception for too small tables
  4458. XSSFTable clonedTable = null;
  4459. if (table.supportsAreaReference(table.getArea())) {
  4460. clonedTable = sheet.createTable(table.getArea());
  4461. }
  4462. if (clonedTable != null) {
  4463. clonedTable.updateHeaders();
  4464. // clone style
  4465. clonedTable.setStyleName(table.getStyleName());
  4466. XSSFTableStyleInfo style = (XSSFTableStyleInfo)table.getStyle();
  4467. XSSFTableStyleInfo clonedStyle = (XSSFTableStyleInfo)clonedTable.getStyle();
  4468. if (style != null && clonedStyle != null) {
  4469. clonedStyle.setShowColumnStripes(style.isShowColumnStripes());
  4470. clonedStyle.setShowRowStripes(style.isShowRowStripes());
  4471. clonedStyle.setFirstColumn(style.isShowFirstColumn());
  4472. clonedStyle.setLastColumn(style.isShowLastColumn());
  4473. }
  4474. //clone autofilter
  4475. clonedTable.getCTTable().setAutoFilter(table.getCTTable().getAutoFilter());
  4476. //clone totalsrow
  4477. int totalsRowCount = table.getTotalsRowCount();
  4478. if (totalsRowCount == 1) { // never seen more than one totals row
  4479. XSSFRow totalsRow = sheet.getRow(clonedTable.getEndCellReference().getRow());
  4480. if (clonedTable.getCTTable().getTableColumns() != null
  4481. && !clonedTable.getCTTable().getTableColumns().getTableColumnList().isEmpty()) {
  4482. clonedTable.getCTTable().setTotalsRowCount(totalsRowCount);
  4483. for (int i = 0; i < clonedTable.getCTTable().getTableColumns().getTableColumnList().size(); i++) {
  4484. CTTableColumn tableCol = table.getCTTable().getTableColumns().getTableColumnList().get(i);
  4485. CTTableColumn clonedTableCol = clonedTable.getCTTable().getTableColumns().getTableColumnList().get(i);
  4486. clonedTableCol.setTotalsRowFunction(tableCol.getTotalsRowFunction());
  4487. int intTotalsRowFunction = clonedTableCol.getTotalsRowFunction().intValue();
  4488. sheet.getWorkbook().setCellFormulaValidation(false);
  4489. if (intTotalsRowFunction == 10) { //custom
  4490. CTTableFormula totalsRowFormula = tableCol.getTotalsRowFormula();
  4491. clonedTableCol.setTotalsRowFormula(totalsRowFormula);
  4492. totalsRow.getCell(clonedTable.getStartCellReference().getCol()+i).setCellFormula(totalsRowFormula.getStringValue());
  4493. } else if (intTotalsRowFunction == 1) { //none
  4494. //totalsRow.getCell(clonedTable.getStartCellReference().getCol()+i).setBlank();
  4495. } else {
  4496. String subtotalFormulaStart = getSubtotalFormulaStartFromTotalsRowFunction(intTotalsRowFunction);
  4497. if (subtotalFormulaStart != null)
  4498. totalsRow.getCell(clonedTable.getStartCellReference().getCol()+i).setCellFormula(subtotalFormulaStart + "," + clonedTable.getName() +"[" + clonedTableCol.getName()+ "])");
  4499. }
  4500. }
  4501. }
  4502. }
  4503. // clone calculated column formulas
  4504. if (clonedTable.getCTTable().getTableColumns() != null
  4505. && !clonedTable.getCTTable().getTableColumns().getTableColumnList().isEmpty()) {
  4506. clonedTable.getCTTable().setTotalsRowCount(totalsRowCount);
  4507. for (int i = 0; i < clonedTable.getCTTable().getTableColumns().getTableColumnList().size(); i++) {
  4508. CTTableColumn tableCol = table.getCTTable().getTableColumns().getTableColumnList().get(i);
  4509. CTTableColumn clonedTableCol = clonedTable.getCTTable().getTableColumns().getTableColumnList().get(i);
  4510. if (tableCol.getCalculatedColumnFormula() != null) {
  4511. clonedTableCol.setCalculatedColumnFormula(tableCol.getCalculatedColumnFormula());
  4512. CTTableFormula calculatedColumnFormula = clonedTableCol.getCalculatedColumnFormula();
  4513. String formula = tableCol.getCalculatedColumnFormula().getStringValue();
  4514. String clonedFormula = formula.replace(table.getName(), clonedTable.getName());
  4515. calculatedColumnFormula.setStringValue(clonedFormula);
  4516. int rFirst = clonedTable.getStartCellReference().getRow() + clonedTable.getHeaderRowCount();
  4517. int rLast = clonedTable.getEndCellReference().getRow() - clonedTable.getTotalsRowCount();
  4518. int c = clonedTable.getStartCellReference().getCol() + i;
  4519. sheet.getWorkbook().setCellFormulaValidation(false);
  4520. for (int r = rFirst; r <= rLast; r++) {
  4521. XSSFRow row = sheet.getRow(r);
  4522. if (row == null) row = sheet.createRow(r);
  4523. XSSFCell cell = row.getCell(c, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
  4524. cell.setCellFormula(clonedFormula);
  4525. }
  4526. }
  4527. }
  4528. }
  4529. }
  4530. // remove old table
  4531. sheet.removeTable(table);
  4532. }
  4533. }
  4534. private static String getSubtotalFormulaStartFromTotalsRowFunction(int intTotalsRowFunction) {
  4535. final int INT_NONE = 1;
  4536. final int INT_SUM = 2;
  4537. final int INT_MIN = 3;
  4538. final int INT_MAX = 4;
  4539. final int INT_AVERAGE = 5;
  4540. final int INT_COUNT = 6;
  4541. final int INT_COUNT_NUMS = 7;
  4542. final int INT_STD_DEV = 8;
  4543. final int INT_VAR = 9;
  4544. final int INT_CUSTOM = 10;
  4545. String subtotalFormulaStart = null;
  4546. switch (intTotalsRowFunction) {
  4547. case INT_NONE:
  4548. subtotalFormulaStart = null;
  4549. break;
  4550. case INT_SUM:
  4551. subtotalFormulaStart = "SUBTOTAL(109";
  4552. break;
  4553. case INT_MIN:
  4554. subtotalFormulaStart = "SUBTOTAL(105";
  4555. break;
  4556. case INT_MAX:
  4557. subtotalFormulaStart = "SUBTOTAL(104";
  4558. break;
  4559. case INT_AVERAGE:
  4560. subtotalFormulaStart = "SUBTOTAL(101";
  4561. break;
  4562. case INT_COUNT:
  4563. subtotalFormulaStart = "SUBTOTAL(103";
  4564. break;
  4565. case INT_COUNT_NUMS:
  4566. subtotalFormulaStart = "SUBTOTAL(102";
  4567. break;
  4568. case INT_STD_DEV:
  4569. subtotalFormulaStart = "SUBTOTAL(107";
  4570. break;
  4571. case INT_VAR:
  4572. subtotalFormulaStart = "SUBTOTAL(110";
  4573. break;
  4574. case INT_CUSTOM:
  4575. subtotalFormulaStart = null;
  4576. break;
  4577. default:
  4578. subtotalFormulaStart = null;
  4579. }
  4580. return subtotalFormulaStart;
  4581. }
  4582. }