spreadjs_zh.js 179 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848
  1. /**
  2. * Spreadjs 通用方法集
  3. *
  4. * @author Mai
  5. * @date 2018/02/06
  6. * @version
  7. */
  8. // 定义画点线的方法
  9. const proto = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype;
  10. proto.dottedLine = function (x1, y1, x2, y2, interval = 4) {
  11. const isHorizontal = x1 == x2 ? false : true;
  12. const dotLen = 1;
  13. let len = isHorizontal ? x2 - x1 : y2 - y1;
  14. this.moveTo(x1, y1);
  15. let progress = 0;
  16. while (len > progress) {
  17. if (progress > len) { progress = len; }
  18. if (isHorizontal) {
  19. this.moveTo(x1 + progress, y1);
  20. this.lineTo(x1 + progress + dotLen, y1);
  21. } else {
  22. this.moveTo(x1, y1 + progress);
  23. this.lineTo(x1, y1 + progress + dotLen);
  24. }
  25. progress += interval;
  26. }
  27. };
  28. const textUtils = {
  29. getStringListDisplayWidth: function(sheet, font, strList) {
  30. const xs = sheet.getParent().xs;
  31. const ctx = xs.childNodes[0].getContext("2d");
  32. ctx.font = font;
  33. const widths = strList.map(x => { return ctx.measureText(x).width; });
  34. return Math.max(...widths);
  35. },
  36. getTextDisplayWidth: function(sheet, font, str) {
  37. const xs = sheet.getParent().xs;
  38. const ctx = xs.childNodes[0].getContext("2d");
  39. ctx.font = font;
  40. return ctx.measureText(str).width;
  41. },
  42. getTextAutoHeight: function(sheet, font, str, displayWidth) {
  43. const xs = sheet.getParent().xs;
  44. const ctx = xs.childNodes[0].getContext("2d");
  45. ctx.font = font;
  46. const textMertrices = ctx.measureText(str);
  47. const fontHeight = textMertrices.fontBoundingBoxAscent + textMertrices.fontBoundingBoxDescent;
  48. const textRow = Math.ceil(textMertrices.width / displayWidth);
  49. return textRow * fontHeight + 2 + (textRow - 1) * 1;
  50. }
  51. };
  52. // 简写Spread常量
  53. const spreadNS = GC.Spread.Sheets;
  54. // 授权码
  55. // V10版本
  56. // spreadNS.LicenseKey = "559432293813965#A0y3iTOzEDOzkjMyMDN9UTNiojIklkI1pjIEJCLi4TPB9mM5AFNTd4cvZ7SaJUVy3CWKtWYXx4VVhjMpp7dYNGdx2ia9sEVlZGOTh7NRlTUwkWR9wEV4gmbjBDZ4ElR8N7cGdHVvEWVBtCOwIGW0ZmeYVWVr3mI0IyUiwCMzETN8kzNzYTM0IicfJye&Qf35VfiEzRwEkI0IyQiwiIwEjL6ByUKBCZhVmcwNlI0IiTis7W0ICZyBlIsIyNyMzM5ADI5ADNwcTMwIjI0ICdyNkIsIibj9SbvNmL4N7bjRnch56ciojIz5GRiwiI8+Y9sWY9QmZ0Jyp96uL9v6L0wap9biY9qiq95q197Wr9g+89iojIh94Wiqi";
  57. // V11版本
  58. spreadNS.LicenseKey = "yhqa.smartcost.com.cn|yhqm.smartcost.com.cn|yhuat.smartcost.com.cn|yhum.smartcost.com.cn|yhyun.smartcost.com.cn|yhm.smartcost.com.cn|ym.smartcost.com.cn|qm.smartcost.com.cn|um.smartcost.com.cn|civil.smartcost.com.cn|yun.smartcost.com.cn|cloud.smartcost.com.cn|qa.smartcost.com.cn|uat.smartcost.com.cn|zbqa.smartcost.com.cn|zbuat.smartcost.com.cn|jlqa.smartcost.com.cn|jluat.smartcost.com.cn|zb.smartcost.com.cn|jl.smartcost.com.cn|jladmin.smartcost.com.cn|jlqaadmin.smartcost.com.cn|jluatadmin.smartcost.com.cn,834986144349993#A0B5njRnch56cu4Wds86Yu46bj9Cdz36Y4JXYtNnLtFHLuNmLt36YuQ7cvNGdyFWbz9Sb9xibj9SbvNmL4N7bjRnch56cu4Ga9xibj9SbvNmL4N7bjRnch56cu8Wd9hWes86Yu46bj9Cdz36Y4JXYtNnLtVHa9xibj9SbvNmL4N7bjRnch56cuQXY5hWes86Yu46bj9Cdz36Y4JXYtNnLtFHa9xibj9SbvNmL4N7bjRnch56cuEWcolnI0IyctRkIsICuPWOrFWOkZmeicaut1S+r3iOsWa+mIWuqoaet0e+t5aOoPeuI0ISYONkIsIyM9kTO4MDN4EjN8kDNzgjI0ICZJJCL3V6csFmZ0IiczRmI1pjIs9WQisnOiQkIsISP3c6LJh4Sm3iWrx6QulkNzhVSMB7ZCBFdGdVMyw4bztiRalHMrg4SzclTGV5LqFUTQ9ETpdTbVp7KwgFcw8mRUhDWOV5QMlWcKBTMRhVQ7IHWEFHcoh5SiojITJCLyMTOwMDNxMzN0IicfJye&Qf35VfiMjY6cmI0IyQiwiIxEjL6ByUKBCZhVmcwNlI0IiTis7W0ICZyBlIsICOykzM6ADIwMzMwAjMwIjI0ICdyNkIsIibj9SbvNmL4N7bjRnch56cu8WatRWY4FWdspGLuNmLt36YuQ7cvNGdyFWbz9ibp5GZhFWcspGLuNmLt36YuQ7cvNGdyFWbz9ibp5GZhxmas86Yu46bj9Cdz36Y4JXYtNnLspGLuNmLt36YuQ7cvNGdyFWbz9iY0xibj9SbvNmL4N7bjRnch56cuQXY5xmas86Yu46bj9Cdz36Y4JXYtNnLhFHbqxibj9SbvNmL4N7bjRnch56cuQXY5Jmes86Yu46bj9Cdz36Y4JXYtNnLhFnY0xibj9SbvNmL4N7bjRnch56cuQXY5xibj9SbvNmL4N7bjRnch56cuEWcs86Yu46bj9Cdz36Y4JXYtNnLkV7bsNGLuNmLt36YuQ7cvNGdyFWbz9ib5lHLuNmLt36YuQ7cvNGdyFWbz9CbpZXajxibj9Sbvlmn0";
  59. // SpreadJs常用方法
  60. const SpreadJsObj = {
  61. initSpreadSettingEvents: function (setting, events) {
  62. const getEvent = function (eventName) {
  63. const names = eventName.split('.');
  64. let event = events;
  65. for (let name of names) {
  66. if (event[name]) {
  67. event = event[name];
  68. } else {
  69. return null;
  70. }
  71. }
  72. if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
  73. return null;
  74. } else {
  75. return event;
  76. }
  77. };
  78. for (const col of setting.cols) {
  79. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object String]") {
  80. col.readOnly = getEvent(col.readOnly);
  81. }
  82. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object String]") {
  83. col.getValue = getEvent(col.getValue);
  84. }
  85. if (col.foreColor && Object.prototype.toString.apply(col.foreColor) === "[object String]") {
  86. col.foreColor = getEvent(col.foreColor);
  87. }
  88. if (col.backColor2 && Object.prototype.toString.apply(col.backColor2) === "[object String]") {
  89. col.backColor2 = getEvent(col.backColor2);
  90. }
  91. }
  92. },
  93. DataType: {
  94. Data: 'data',
  95. Tree: 'tree',
  96. },
  97. /**
  98. * 创建Spread(默认1张表,3行数据)
  99. * @param obj 用于创建spreadjs的Dom元素
  100. * @returns {GC.Spread.Sheets.Workbook}
  101. */
  102. createNewSpread: function (obj, hidden) {
  103. const spread = new spreadNS.Workbook(obj, {sheetCount: 1});
  104. if (hidden) return spread;
  105. spread.options.tabStripVisible = false;
  106. spread.options.scrollbarMaxAlign = true;
  107. spread.options.cutCopyIndicatorVisible = false;
  108. spread.options.allowCopyPasteExcelStyle = false;
  109. spread.options.allowUserDragDrop = false;
  110. spread.options.allowUserDragFill = false;
  111. spread.options.showDragFillSmartTag = false;
  112. spread.options.allowUserEditFormula = false;
  113. spread.options.allowExtendPasteRange = true;
  114. spread.options.allowContextMenu = false;
  115. spread.options.copyPasteHeaderOptions = spreadNS.CopyPasteHeaderOptions.noHeaders;
  116. spread.getActiveSheet().options.clipBoardOptions = spreadNS.ClipboardPasteOptions.values;//设置粘贴时只粘贴值
  117. spread.getActiveSheet().setRowCount(3);
  118. return spread;
  119. },
  120. addCutEvents: function (spread, fun) {
  121. spread.cutFun = fun;
  122. const cut = spreadNS.Commands.copy.execute;
  123. spreadNS.Commands.cut.execute = function (context, options, isUndo) {
  124. const sheet = context.getActiveSheet();
  125. const sels = sheet.getSelections();
  126. const sel = sels ? sels[0] : null;
  127. if (sel) {
  128. return context.cutFun(sheet, sel, function (isUndo) {
  129. cut(context, options, isUndo);
  130. });
  131. }
  132. }
  133. },
  134. /**
  135. * 保护sheet(需设置保护后, 单元格的locked等属性方可生效)
  136. * @param {GC.Spread.Sheets.Worksheet} sheet
  137. */
  138. protectedSheet: function (sheet) {
  139. const option = {
  140. allowSelectLockedCells: true,
  141. allowSelectUnlockedCells: true,
  142. allowResizeRows: true,
  143. allowResizeColumns: true
  144. };
  145. const defaultStyle = sheet.getDefaultStyle();
  146. defaultStyle.locked = false;
  147. sheet.setDefaultStyle(defaultStyle);
  148. sheet.options.protectionOptions = option;
  149. sheet.options.isProtected = true;
  150. sheet.options.allowCellOverflow = false;
  151. },
  152. /**
  153. * sheet批量操作优化(sheet操作大批量数据时, 屏蔽数据刷新, 可优化大量时间)
  154. * @param {GC.Spread.Sheets.Worksheet} sheet
  155. * @param {function} operation
  156. */
  157. beginMassOperation: function (sheet) {
  158. sheet.suspendPaint();
  159. sheet.suspendEvent();
  160. },
  161. endMassOperation: function (sheet) {
  162. sheet.resumeEvent();
  163. sheet.resumePaint();
  164. },
  165. massOperationSheet: function (sheet, operation) {
  166. this.beginMassOperation(sheet);
  167. operation();
  168. this.endMassOperation(sheet);
  169. },
  170. /**
  171. * 获取Obj左顶点位置(部分功能需通过spreadjs左顶点位置计算)
  172. * @param obj
  173. * @returns {{x: number, y: number}}
  174. */
  175. getObjPos: function (obj) {
  176. if (!obj) return null;
  177. let target = obj;
  178. let pos = {x: obj.offsetLeft, y: obj.offsetTop};
  179. target = obj.offsetParent;
  180. while (target) {
  181. pos.x += target.offsetLeft;
  182. pos.y += target.offsetTop;
  183. target = target.offsetParent;
  184. }
  185. return pos;
  186. },
  187. /**
  188. * 以下四个方法来自Spread示例, 参见官网或文档
  189. */
  190. getHitTest: function (obj, e, sheet) {
  191. var offset = obj.offset(),
  192. x = e.pageX - offset.left,
  193. y = e.pageY - offset.top;
  194. return sheet.hitTest(x, y);
  195. },
  196. getTargetSelection: function (sheet, target) {
  197. if (target.hitTestType === spreadNS.SheetArea.colHeader) {
  198. return sheet.getRange(-1, target.col, sheet.getRowCount(), 1);
  199. } else if (target.hitTestType === spreadNS.SheetArea.rowHeader) {
  200. return sheet.getRange(target.row, -1, 1, sheet.getColumnCount());
  201. } else if (target.hitTestType === spreadNS.SheetArea.viewport) {
  202. return sheet.getRange(target.row, target.col, 1, 1);
  203. } else if (target.hitTestType === spreadNS.SheetArea.corner) {
  204. return sheet.getRange(-1, -1, sheet.getRowCount(), sheet.getColumnCount());
  205. }
  206. },
  207. getCellInSelections: function (selections, row, col) {
  208. const count = selections.length;
  209. let range;
  210. for (var i = 0; i < count; i++) {
  211. range = selections[i];
  212. if (range.contains(row, col)) {
  213. return range;
  214. }
  215. }
  216. return null;
  217. },
  218. checkTargetInSelection: function (selections, range) {
  219. var count = selections.length, sel;
  220. for (var i = 0; i < count; i++) {
  221. sel = selections[i];
  222. if (sel.containsRange(range)) {
  223. return true;
  224. }
  225. }
  226. return false;
  227. },
  228. /**
  229. * 获取 spread 在鼠标右键时, spread的选中区域
  230. * viewport, 选中鼠标点击单元格X, X不在原选中区域内, 则选中X
  231. * colHeader, 选中整列
  232. * rowHeader, 选中整行
  233. * corner, 全选
  234. * 该方法返回不符合需求时,可通过getHitTest/getTargetSelection/getCellInSelections/checkTargetInSelection来确定鼠标右键点击时,spread当前Sheet应选中的单元格
  235. * @param obj: 创建Spread的Dom元素(jquery-contextmenu.build方法的第一个变量可读取)
  236. * @param e: jquery-contextmenu.build方法的第二个变量
  237. * @param {GC.Spread.Sheets.Workbook} spread
  238. * @returns {*}
  239. */
  240. safeRightClickSelection: function (obj, e, spread) {
  241. const sheet = spread.getActiveSheet();
  242. if (sheet.isEditing()) sheet.endEdit(true);
  243. const selections = sheet.getSelections(), target = this.getHitTest(obj, e, sheet), range = this.getTargetSelection(sheet, target);
  244. if (!this.checkTargetInSelection(selections, range)) {
  245. sheet.setSelection(range.row, range.col, range.rowCount, range.colCount);
  246. SpreadJsObj.reloadRowsBackColor(sheet, [selections[0].row, range.row]);
  247. }
  248. return target;
  249. },
  250. /**
  251. * 禁用右键菜单
  252. * @param selector
  253. * @param spread
  254. */
  255. forbiddenSpreadContextMenu: function (selector, spread) {
  256. $.contextMenu({
  257. selector: selector,
  258. build: function ($trigger, e) {
  259. const target = SpreadJsObj.safeRightClickSelection($trigger, e, spread);
  260. return target.hitTestType === spreadNS.SheetArea.viewport || target.hitTestType === spreadNS.SheetArea.rowHeader;
  261. }
  262. });
  263. },
  264. selChangedTipsSum: function(sheet) {
  265. sheet.bind(spreadNS.Events.SelectionChanged, function(e, info) {
  266. let sum = 0, count = 0, average = 0;
  267. const sel = info.newSelections[0];
  268. if (sel) {
  269. for (let iRow = sel.row; iRow < sel.row + sel.rowCount; iRow++) {
  270. if (!info.sheet.getRowVisible(iRow)) continue;
  271. for (let iCol = sel.col; iCol < sel.col + sel.colCount; iCol++) {
  272. if (!info.sheet.getColumnVisible(iCol)) continue;
  273. const text = info.sheet.getText(iRow, iCol);
  274. if (text) {
  275. count++;
  276. const value = _.toNumber(text);
  277. sum = ZhCalc.add(sum, _.isNaN(value) ? 0 : value);
  278. }
  279. }
  280. }
  281. }
  282. if (count > 1) {
  283. const maxHintWidth = 200, indent = 10;
  284. average = ZhCalc.div(sum, count);
  285. const text = [`合计:${sum}`, `计数:${count}`, `平均值:${average}`];
  286. let sumTip = $('#spread-sum-tip')[0];
  287. if (!sumTip) {
  288. sumTip = document.createElement("div");
  289. $(sumTip).css("position", "absolute")
  290. .css("border", "1px #C0C0C0 solid")
  291. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  292. .css("font", "9pt Arial")
  293. .css("background", "white")
  294. .css("padding", 5)
  295. .css("z-index", 999)
  296. .css("max-width", maxHintWidth)
  297. .css("word-wrap", "break-word")
  298. .attr("id", 'spread-sum-tip');
  299. document.body.insertBefore(sumTip, null);
  300. }
  301. const hitinfo = info.sheet.getCellRect(sel.row, sel.col + sel.colCount - 1);
  302. const pos = SpreadJsObj.getObjPos(info.sheet.getParent().qo);
  303. const showTop = Math.min(pos.y + hitinfo.y + indent, window.innerHeight - 72); // 52(w)+10(p)+10(i)
  304. let showLeft = pos.x + hitinfo.x + hitinfo.width + indent;
  305. if (showLeft > window.innerWidth - maxHintWidth) {
  306. const textWidth = textUtils.getStringListDisplayWidth(info.sheet, "9pt Arial", text);
  307. showLeft = Math.min(showLeft, window.innerWidth - textWidth - 10 - indent);
  308. }
  309. $(sumTip).html(text.join('<br/>')).css("top", showTop).css("left", showLeft);
  310. setTimeout(()=>{ if (sumTip) $(sumTip).show("fast");}, 100);
  311. // setTimeout(()=>{ if (sumTip) $(sumTip).hide();}, 5000);
  312. } else {
  313. const sumTip = $('#spread-sum-tip');
  314. if (sumTip) sumTip.hide();
  315. }
  316. });
  317. },
  318. selChangedRefreshBackColor: function (sheet) {
  319. sheet.bind(spreadNS.Events.SelectionChanged, function (e, info) {
  320. const rows = [];
  321. if (info.oldSelections && info.oldSelections[0]) rows.push(info.oldSelections[0].row);
  322. if (info.newSelections && info.newSelections[0]) rows.push(info.newSelections[0].row);
  323. if (rows.length > 0) SpreadJsObj.reloadRowsBackColor(info.sheet, rows);
  324. });
  325. },
  326. /**
  327. * 获取写入sheet的数据序列
  328. * data:sheet.zh_data, tree: sheet.zh_tree.nodes
  329. * @param sheet
  330. * @returns {*}
  331. */
  332. getSortData: function (sheet) {
  333. if (sheet.zh_dataType) {
  334. if (sheet.zh_dataType === this.DataType.Data) {
  335. return sheet.zh_data;
  336. } else if (sheet.zh_dataType === this.DataType.Tree) {
  337. return sheet.zh_tree.nodes;
  338. } else {
  339. return null;
  340. }
  341. } else {
  342. return null;
  343. }
  344. },
  345. /**
  346. * sheet中 使用delete键,触发EndEdited事件
  347. * @param {GC.Spreads.Sheets.Workbook} spread
  348. * @param {function} fun
  349. */
  350. addDeleteBind: function (spread, fun) {
  351. spread.commandManager().register('deleteEvent', function () {
  352. fun(spread.getActiveSheet());
  353. });
  354. spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  355. spread.commandManager().setShortcutKey('deleteEvent', GC.Spread.Commands.Key.del, false, false, false, false);
  356. },
  357. _initSheetDeafult: function (sheet) {
  358. if (sheet.zh_setting.headerFont) {
  359. const vStyle = new spreadNS.Style();
  360. vStyle.font = sheet.zh_setting.headerFont;
  361. vStyle.locked = false;
  362. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.colHeader);
  363. }
  364. if (sheet.zh_setting.font) {
  365. const vStyle = new spreadNS.Style();
  366. vStyle.font = sheet.zh_setting.font;
  367. vStyle.hAlign = 1;
  368. vStyle.locked = false;
  369. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.rowHeader);
  370. }
  371. if (sheet.zh_setting.font) {
  372. const vStyle = new spreadNS.Style();
  373. vStyle.font = sheet.zh_setting.font;
  374. vStyle.locked = false;
  375. sheet.setDefaultStyle(vStyle, spreadNS.SheetArea.viewport);
  376. }
  377. },
  378. /**
  379. * 根据sheet.zh_setting初始化sheet表头
  380. * @param {GC.Spread.Sheets.Worksheet} sheet
  381. */
  382. _initSheetHeader: function (sheet, force) {
  383. const setting = sheet.zh_setting;
  384. if (!setting || !setting.cols) { return; }
  385. if (force) {
  386. sheet.setColumnCount(0);
  387. sheet.setRowCount(0, spreadNS.SheetArea.colHeader)
  388. }
  389. sheet.setColumnCount(setting.cols.length);
  390. sheet.setRowCount(setting.headRows, spreadNS.SheetArea.colHeader);
  391. for (let iRow = 0; iRow < setting.headRowHeight.length; iRow ++) {
  392. sheet.setRowHeight(iRow, setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  393. }
  394. for (let iCol = 0; iCol < setting.cols.length; iCol++) {
  395. const col = setting.cols[iCol];
  396. const title = col.title.split('|');
  397. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  398. for (let i = 0; i < title.length; i++) {
  399. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  400. cell.text(title[i]).wordWrap(true);
  401. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  402. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  403. }
  404. if (col.headerCellType) {
  405. this._defineColHeader(sheet, cell, col);
  406. }
  407. }
  408. sheet.setColumnWidth(iCol, col.width);
  409. if (col.visible !== undefined && col.visible !== null) {
  410. sheet.setColumnVisible(iCol, col.visible);
  411. }
  412. }
  413. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  414. sheet.showRowOutline(false);
  415. if (setting.defaultRowHeight) {
  416. sheet.defaults.rowHeight = setting.defaultRowHeight;
  417. }
  418. if (setting.frozenLineColor) sheet.options.frozenlineColor = setting.frozenLineColor;
  419. if (setting.frozenColCount) sheet.frozenColumnCount(setting.frozenColCount);
  420. },
  421. reinitSheetHeader: function (sheet) {
  422. this.beginMassOperation(sheet);
  423. if (sheet.zh_setting.headColWidth) {
  424. for (const [i, w] of sheet.zh_setting.headColWidth.entries()) {
  425. sheet.setColumnWidth(i, w, spreadNS.SheetArea.rowHeader);
  426. }
  427. }
  428. this._initSheetHeader(sheet);
  429. this.endMassOperation(sheet);
  430. },
  431. _rememberColWidth: function (sheet) {
  432. sheet.bind(spreadNS.Events.ColumnWidthChanged, function (e, info) {
  433. if (!info.sheet.zh_setting) return;
  434. const setting = info.sheet.zh_setting;
  435. if (!setting.localCache || !setting.localCache.key) return;
  436. if (!setting.localCache.colWidthCache) return;
  437. const cache = setting.localCache.colWidthCache;
  438. for (const col of info.colList) {
  439. const colSetting = setting.cols[col];
  440. if (colSetting && colSetting.field)
  441. cache[colSetting.field] = info.sheet.getColumnWidth(col);
  442. }
  443. setLocalCache(setting.localCache.key + '-colWidth', JSON.stringify(cache));
  444. });
  445. },
  446. _loadCacheSetting: function (sheet, setting) {
  447. if (!setting.localCache || !setting.localCache.key) return;
  448. if (setting.localCache.colWidth) {
  449. const cache = getLocalCache(setting.localCache.key + '-colWidth');
  450. setting.localCache.colWidthCache = cache ? JSON.parse(cache) : {};
  451. for (const prop in setting.localCache.colWidthCache) {
  452. const col = _.find(setting.cols, {field: prop});
  453. if (col) col.width = setting.localCache.colWidthCache[prop];
  454. }
  455. }
  456. },
  457. /**
  458. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  459. * @param {GC.Spread.Sheets.Worksheet} sheet
  460. * @param setting
  461. */
  462. initSheet: function (sheet, setting) {
  463. const self = this;
  464. this.beginMassOperation(sheet);
  465. this._loadCacheSetting(sheet, setting);
  466. this._rememberColWidth(sheet);
  467. setting.pos = sheet.getParent().pos;
  468. if (!setting.tree) setting.tree = {};
  469. sheet.zh_setting = setting;
  470. if (sheet.zh_setting.headColWidth) {
  471. for (const [i, w] of sheet.zh_setting.headColWidth.entries()) {
  472. sheet.setColumnWidth(i, w, spreadNS.SheetArea.rowHeader);
  473. }
  474. }
  475. sheet.extendCellType = {};
  476. sheet.extendRowHeader = {};
  477. sheet.extendColHeader = {};
  478. this._initSheetDeafult(sheet);
  479. this._initSheetHeader(sheet, true);
  480. sheet.setRowCount(sheet.zh_setting.emptyRows);
  481. sheet.zh_setting.rowHeader && sheet.zh_setting.rowHeader.forEach(function (col, j) {
  482. self._defineRowHeader(sheet, j, col);
  483. });
  484. sheet.borderLine = new spreadNS.LineBorder('#cccccc', spreadNS.LineStyle.thin);
  485. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly).setBorder(sheet.borderLine, {all: true});
  486. if (setting.selectedBackColor) {
  487. SpreadJsObj.selChangedRefreshBackColor(sheet);
  488. }
  489. if (setting.tipsSum) SpreadJsObj.selChangedTipsSum(sheet);
  490. this.endMassOperation(sheet);
  491. if (!setting.invalidCopyFilterHiddenRow) this.copyFilterHiddenRow(sheet);
  492. },
  493. reLoadSheetHeader: function (sheet, force = false) {
  494. if (sheet.zh_setting) {
  495. this.beginMassOperation(sheet);
  496. this._loadCacheSetting(sheet, sheet.zh_setting);
  497. this._initSheetHeader(sheet, force);
  498. this.endMassOperation(sheet);
  499. }
  500. },
  501. _loadCellData: function (sheet, data, iRow, iCol) {
  502. if (!data || !sheet.zh_setting) return;
  503. const col = sheet.zh_setting.cols[iCol];
  504. const cell = sheet.getCell(iRow, iCol);
  505. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  506. cell.value(col.getValue(data, col));
  507. } else if (col.field !== '' && data[col.field]) {
  508. cell.value(data[col.field]);
  509. }
  510. if (col.textIndent) cell.textIndent(col.textIndent);
  511. let font = sheet.getDefaultStyle().font;
  512. if (col.font) {
  513. font = col.font;
  514. }
  515. if (sheet.zh_setting.tree.getFont && Object.prototype.toString.apply(sheet.zh_setting.tree.getFont) === "[object Function]") {
  516. font = sheet.zh_setting.tree.getFont(sheet, data, iRow, col, font);
  517. }
  518. cell.font(font);
  519. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  520. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  521. } else {
  522. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  523. }
  524. if(col.type === 'Number') {
  525. if (col.formatter) {
  526. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  527. } else {
  528. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  529. }
  530. } else if (col.formatter) {
  531. cell.formatter(col.formatter);
  532. }
  533. cell.backColor(SpreadJsObj._getBackColor(sheet, data, iRow, col, cell.backColor()));
  534. cell.foreColor(SpreadJsObj._getForeColor(sheet, data, iRow, col, cell.foreColor()));
  535. cell.setBorder(sheet.borderLine, {all: true});
  536. },
  537. _getForeColor: function (sheet, data, row, col) {
  538. let foreColor = sheet.getDefaultStyle().foreColor;
  539. if (col.foreColor) {
  540. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  541. foreColor = col.foreColor(data, foreColor, col);
  542. } else {
  543. foreColor = col.foreColor || foreColor;
  544. }
  545. } else {
  546. if (sheet.zh_setting.tree.getForeColor && Object.prototype.toString.apply(sheet.zh_setting.tree.getForeColor) === "[object Function]") {
  547. foreColor = sheet.zh_setting.tree.getForeColor(sheet, data, row, col, foreColor);
  548. }
  549. if (sheet.zh_setting.getForeColor && Object.prototype.toString.apply(sheet.zh_setting.getForeColor) === "[object Function]") {
  550. foreColor = sheet.zh_setting.getForeColor(sheet, data, row, col, foreColor);
  551. }
  552. }
  553. return foreColor;
  554. },
  555. _getBackColor: function (sheet, data, row, col) {
  556. let backColor = sheet.getDefaultStyle().backColor;
  557. let sels = sheet.getSelections();
  558. if (sheet.zh_setting.selectedBackColor && sels && sels[0].row === row ) {
  559. return sheet.zh_setting.selectedBackColor;
  560. } else {
  561. if (col.backColor2) {
  562. if (Object.prototype.toString.apply(col.backColor2) === "[object Function]") {
  563. backColor = col.backColor2(data, backColor);
  564. } else {
  565. backColor = col.backColor2 || backColor;
  566. }
  567. }
  568. if (sheet.zh_setting.tree.getColor && Object.prototype.toString.apply(sheet.zh_setting.tree.getColor) === "[object Function]") {
  569. backColor = sheet.zh_setting.tree.getColor(sheet, data, row, col, backColor);
  570. }
  571. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  572. backColor = sheet.zh_setting.getColor(sheet, data, row, col, backColor);
  573. }
  574. return backColor;
  575. }
  576. },
  577. _loadRowStyle: function (sheet, data, row) {
  578. sheet.zh_setting.cols.forEach(function (col, j) {
  579. const cell = sheet.getCell(row, j);
  580. if (col.font) {
  581. cell.font(col.font);
  582. }
  583. const readOnly1 = (sheet.zh_setting.readOnly && Object.prototype.toString.apply(sheet.zh_setting.readOnly) === "[object Function]")
  584. ? sheet.zh_setting.readOnly(data) : (sheet.zh_setting.readOnly || false);
  585. const readOnly2 = (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]")
  586. ? col.readOnly(data) : col.readOnly || false;
  587. cell.locked(readOnly1 || readOnly2).vAlign(1).hAlign(col.hAlign);
  588. if(col.type === 'Number') {
  589. if (col.formatter) {
  590. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  591. } else {
  592. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  593. }
  594. } else if (col.formatter) {
  595. cell.formatter(col.formatter);
  596. }
  597. cell.setBorder(sheet.borderLine, {all: true});
  598. });
  599. },
  600. _autoFitRowWithTree: function(sheet, data, row, treeCol) {
  601. const treeCell = sheet.getCell(row, treeCol);
  602. const setting = sheet.zh_tree.setting;
  603. const xOffset = 2 + 5;
  604. const indent = 20;
  605. const levelIndent = -5;
  606. const move = (data[setting.level]) * indent + (data[setting.level]) * levelIndent + xOffset;
  607. const validWidth = sheet.zh_setting.cols[treeCol].width - move;
  608. const getTextHeight = function(sheet, str, font, width) {
  609. const xs = sheet.getParent().xs;
  610. const ctx = xs.childNodes[0].getContext("2d");
  611. ctx.font = font;
  612. const textMertrices = ctx.measureText(str);
  613. const fontHeight = textMertrices.fontBoundingBoxAscent + textMertrices.fontBoundingBoxDescent;
  614. const textRow = Math.ceil(textMertrices.width / width);
  615. return textRow * fontHeight + 2 + (textRow - 1) * 1;
  616. };
  617. const treeCellHeight = getTextHeight(sheet, treeCell.text(), treeCell.font(), validWidth);
  618. sheet.setRowHeight(row, Math.max(treeCellHeight, sheet.getRowHeight(row)));
  619. },
  620. // 仅可在已经载入数据后使用
  621. _calcRowHeight: function (sheet, data, row) {
  622. let autoFit = false, treeWordWrapCol = -1;
  623. sheet.zh_setting.cols.forEach(function (col, j) {
  624. if (col.cellType === 'tree' && col.wordWrap) treeWordWrapCol = j;
  625. const cell = sheet.getCell(row, j);
  626. if (col.visible === undefined) col.visible = 1;
  627. if (col.wordWrap && col.visible) {
  628. cell.wordWrap(true);
  629. autoFit = true;
  630. }
  631. });
  632. if (autoFit) {
  633. sheet.autoFitRow(row);
  634. if (treeWordWrapCol >= 0) this._autoFitRowWithTree(sheet, data, row, treeWordWrapCol);
  635. } else {
  636. sheet.setRowHeight(row, 21);
  637. }
  638. },
  639. _loadRowData: function (sheet, data, row) {
  640. // 单元格重新写入数据
  641. if (!data) { return }
  642. if (sheet.zh_dataType === SpreadJsObj.DataType.Tree && !data.visible && data.waitingLoading === undefined) {
  643. data.waitingLoading = true;
  644. return;
  645. }
  646. let autoFit = false, treeWordWrapCol = -1;
  647. sheet.zh_setting.cols.forEach(function (col, j) {
  648. if (col.cellType === 'tree' && col.wordWrap) treeWordWrapCol = j;
  649. const cell = sheet.getCell(row, j);
  650. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  651. cell.value(col.getValue(data, col));
  652. } else if (col.field !== '' && data[col.field]) {
  653. cell.value(data[col.field]);
  654. }
  655. let font = sheet.getDefaultStyle().font;
  656. if (col.font) {
  657. font = col.font;
  658. }
  659. if (sheet.zh_setting.tree.getFont && Object.prototype.toString.apply(sheet.zh_setting.tree.getFont) === "[object Function]") {
  660. font = sheet.zh_setting.tree.getFont(sheet, data, row, col, font);
  661. }
  662. cell.font(font);
  663. if (col.textIndent) cell.textIndent(col.textIndent);
  664. const readOnly1 = (sheet.zh_setting.readOnly && Object.prototype.toString.apply(sheet.zh_setting.readOnly) === "[object Function]")
  665. ? sheet.zh_setting.readOnly(data) : (sheet.zh_setting.readOnly || false);
  666. const readOnly2 = (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]")
  667. ? col.readOnly(data) : col.readOnly || false;
  668. cell.locked(readOnly1 || readOnly2).vAlign(1).hAlign(col.hAlign);
  669. if(col.type === 'Number') {
  670. if (col.formatter) {
  671. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  672. } else {
  673. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  674. }
  675. } else if (col.formatter) {
  676. cell.formatter(col.formatter);
  677. }
  678. cell.backColor(SpreadJsObj._getBackColor(sheet, data, row, col, cell.backColor()));
  679. cell.foreColor(SpreadJsObj._getForeColor(sheet, data, row, col, cell.foreColor()));
  680. cell.setBorder(sheet.borderLine, {all: true});
  681. if (col.visible === undefined) col.visible = 1;
  682. if (col.wordWrap && col.visible) {
  683. cell.wordWrap(true);
  684. autoFit = true;
  685. }
  686. data.waitingLoading = false;
  687. });
  688. if (autoFit) {
  689. sheet.autoFitRow(row);
  690. if (treeWordWrapCol >= 0) this._autoFitRowWithTree(sheet, data, row, treeWordWrapCol);
  691. }
  692. },
  693. _addActivePaintEvents: function (sheet, cellType) {
  694. if (!sheet.ActiveType) {
  695. sheet.ActiveType = [];
  696. }
  697. sheet.ActiveType.push(cellType);
  698. if (!sheet.AcitveRefresh) {
  699. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  700. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  701. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  702. info.sheet.leaveCell = {row: info.row, col: info.col};
  703. } else {
  704. delete info.sheet.leaveCell;
  705. }
  706. });
  707. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  708. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  709. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  710. info.sheet.repaint(info.sheet.getCellRect(info.row, info.col));
  711. }
  712. if (info.sheet.leaveCell) {
  713. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  714. }
  715. });
  716. sheet.AcitveRefresh = true;
  717. }
  718. },
  719. _defineRowHeader: function (sheet, col, colSetting) {
  720. if (colSetting.rowHeaderType === 'tag') {
  721. if (!sheet.extendRowHeader.tag) {
  722. sheet.extendRowHeader.tag = this.RowHeader.getTagRowHeader(colSetting.setting);
  723. }
  724. sheet.getRange(-1, col, -1, 1, spreadNS.SheetArea.rowHeader).cellType(sheet.extendRowHeader.tag);
  725. }
  726. if (colSetting.rowHeaderType === 'circle') {
  727. if (!sheet.extendRowHeader.multiTag) {
  728. sheet.extendRowHeader.multiTag = this.RowHeader.getCircleTagRowHeader(colSetting.setting);
  729. }
  730. sheet.getRange(-1, col, -1, 1, spreadNS.SheetArea.rowHeader).cellType(sheet.extendRowHeader.multiTag);
  731. }
  732. },
  733. _defineColHeader: function(sheet, cell, colSetting) {
  734. if (colSetting.headerCellType === 'tip') {
  735. if (!sheet.extendColHeader.tip) {
  736. sheet.extendColHeader.tip = this.ColHeader.getTipHeader(colSetting);
  737. }
  738. cell.cellType(sheet.extendColHeader.tip);
  739. }
  740. if (colSetting.headerCellType === 'button') {
  741. if (!sheet.extendColHeader.button) {
  742. sheet.extendColHeader.button = this.ColHeader.getButtonHeader(colSetting);
  743. }
  744. cell.cellType(sheet.extendColHeader.button);
  745. }
  746. },
  747. _defineColCellType: function (sheet, col, colSetting) {
  748. sheet.AcitveComboRefresh = false;
  749. if(colSetting.cellType === 'ellipsis') {
  750. if (!sheet.extendCellType.ellipsis) {
  751. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  752. }
  753. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  754. }
  755. if(colSetting.cellType === 'ellipsisAutoTip') {
  756. if (!sheet.extendCellType.ellipsisAutoTip) {
  757. const scrollHeightClass = colSetting.scrollHeightClass ? colSetting.scrollHeightClass : false;
  758. sheet.extendCellType.ellipsisAutoTip = this.CellType.getEllipsisTextAutoTipCellType(scrollHeightClass);
  759. }
  760. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsisAutoTip);
  761. }
  762. if(colSetting.cellType === 'html') {
  763. if (!sheet.extendCellType.html) {
  764. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  765. }
  766. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  767. }
  768. if (colSetting.cellType === 'image') {
  769. if (!sheet.extendCellType.image) {
  770. sheet.extendCellType.image = this.CellType.getImageCellType();
  771. }
  772. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  773. }
  774. if (colSetting.cellType === 'imageBtn') {
  775. if (!sheet.extendCellType.imageBtn) {
  776. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  777. }
  778. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  779. }
  780. if (colSetting.cellType === 'activeImageBtn') {
  781. if (!sheet.extendCellType.activeImageBtn) {
  782. sheet.extendCellType.activeImageBtn = this.CellType.getActiveImageButtonCellType();
  783. }
  784. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.activeImageBtn);
  785. }
  786. if (colSetting.cellType === 'tree') {
  787. if (!sheet.extendCellType.tree) {
  788. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  789. }
  790. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  791. }
  792. if (colSetting.cellType === 'tip') {
  793. if (!sheet.extendCellType.tip) {
  794. sheet.extendCellType.tip = this.CellType.getTipCellType();
  795. }
  796. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  797. }
  798. if (colSetting.cellType === 'autoTip') {
  799. if (!sheet.extendCellType.autoTip) {
  800. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  801. }
  802. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  803. }
  804. if (colSetting.cellType === 'delayTip') {
  805. if (!sheet.extendCellType.tip) {
  806. sheet.extendCellType.tip = this.CellType.getDelayTipCellType();
  807. }
  808. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  809. }
  810. if (colSetting.cellType === 'checkbox') {
  811. if (!sheet.extendCellType.checkbox) {
  812. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  813. }
  814. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  815. sheet.getRange(-1, col, -1, 1).hAlign(colSetting ? colSetting.hAlign : spreadNS.HorizontalAlign.center);// 空白行不居中问题,checkbox要默认居中
  816. }
  817. if (colSetting.cellType === 'signalCheckbox') {
  818. if (!sheet.extendCellType.signalCheckbox) {
  819. sheet.extendCellType.signalCheckbox = this.CellType.getSignalCheckboxCellType();
  820. }
  821. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.signalCheckbox);
  822. sheet.getRange(-1, col, -1, 1).hAlign(colSetting ? colSetting.hAlign : spreadNS.HorizontalAlign.center);// 空白行不居中问题,checkbox要默认居中
  823. }
  824. if (colSetting.cellType === 'unit') {
  825. if (!sheet.extendCellType.unit) {
  826. const comboEdit = colSetting.comboEdit ? colSetting.comboEdit : false;
  827. sheet.extendCellType.unit = colSetting.comboItems ? this.CellType.getUnitCellType(comboEdit, colSetting.comboItems) : this.CellType.getUnitCellType(comboEdit);
  828. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.unit);
  829. }
  830. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  831. }
  832. if (colSetting.cellType === 'customizeCombo') {
  833. const cellKey = colSetting.cellTypeKey ? 'customizeCombo-' + colSetting.cellTypeKey : 'customizeCombo';
  834. if (!sheet.extendCellType[cellKey]) {
  835. sheet.extendCellType[cellKey] = this.CellType.getCustomizeComboCellType(colSetting.comboItems, colSetting.maxDrop);
  836. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType[cellKey]);
  837. }
  838. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  839. }
  840. if (colSetting.cellType === 'stageCombo') {
  841. if (!sheet.extendCellType.stageCombo) {
  842. sheet.extendCellType.stageCombo = this.CellType.getStageComboCellType();
  843. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.stageCombo);
  844. }
  845. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.stageCombo);
  846. }
  847. if (colSetting.cellType === 'specCombo') {
  848. const cellKey = colSetting.cellTypeKey ? 'specCombo-' + colSetting.cellTypeKey : 'specCombo';
  849. if (!sheet.extendCellType[cellKey]) {
  850. sheet.extendCellType[cellKey] = this.CellType.getSpecComboCellType(colSetting.comboItems);
  851. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType[cellKey]);
  852. }
  853. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  854. }
  855. if (colSetting.cellType === 'datepicker') {
  856. if (!sheet.extendCellType.datepicker) {
  857. sheet.extendCellType.datepicker = this.CellType.getDatePickerCellType();
  858. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.datepicker);
  859. }
  860. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.datepicker);
  861. }
  862. if (colSetting.cellType === 'mouseTouch') {
  863. if (!sheet.extendCellType.mouseTouch) {
  864. sheet.extendCellType.mouseTouch = this.CellType.getMouseTouchCellType();
  865. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.mouseTouch);
  866. }
  867. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.mouseTouch);
  868. }
  869. if (colSetting.cellType === 'stackedBar') {
  870. if (!sheet.extendCellType.stackedBar) {
  871. sheet.extendCellType.stackedBar = this.CellType.getStackedBarCellType();
  872. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.stackedBar);
  873. }
  874. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.stackedBar);
  875. }
  876. if (colSetting.formatter) {
  877. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  878. if(colSetting.type === 'Number') {
  879. if (col.formatter) {
  880. sheet.getRange(-1, col, -1, 1).formatter(SpreadJsObj.Formatter.getNumberFormatter(colSetting.formatter));
  881. } else {
  882. sheet.getRange(-1, col, -1, 1).formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  883. }
  884. } else if (col.formatter) {
  885. cell.formatter(col.formatter);
  886. }
  887. }
  888. },
  889. /**
  890. * 整个sheet重新加载数据
  891. * @param {GC.Spread.Sheets.Worksheet} sheet
  892. */
  893. reLoadSheetData: function (sheet, force = false) {
  894. const self = this;
  895. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  896. this.beginMassOperation(sheet);
  897. try {
  898. force && sheet.setRowCount(0, spreadNS.SheetArea.viewport);
  899. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  900. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  901. // 设置总行数
  902. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  903. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  904. if (sortData) {
  905. // 单元格写入数据
  906. sortData.forEach(function (data, i) {
  907. self._loadRowData(sheet, data, i);
  908. sheet.setRowVisible(i, data.visible);
  909. });
  910. }
  911. // 控制空白行
  912. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  913. emptyRows.locked(sheet.zh_dataType === 'tree' || sheet.zh_setting.readOnly);
  914. if (sheet.zh_setting.forceLoadEmpty) {
  915. for (let iRow = emptyRows.row; iRow < emptyRows.row + emptyRows.rowCount; iRow++) {
  916. self._loadRowData(sheet, {}, iRow);
  917. }
  918. }
  919. // 设置列单元格格式
  920. sheet.zh_setting.cols.forEach(function (col, j) {
  921. self._defineColCellType(sheet, j, col);
  922. });
  923. this.endMassOperation(sheet);
  924. } catch (err) {
  925. console.log(err);
  926. this.endMassOperation(sheet);
  927. }
  928. },
  929. /**
  930. * 重新加载部分数据行
  931. * @param {GC.Spread.Sheets.Worksheet} sheet
  932. * @param {Number} row
  933. * @param {Number} count
  934. */
  935. reLoadRowData: function (sheet, row, count = 1) {
  936. if (row < 0) { return; }
  937. const self = this;
  938. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  939. this.beginMassOperation(sheet);
  940. try {
  941. // 清空原单元格数据
  942. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  943. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  944. // 单元格重新写入数据
  945. for (let i = row; i < row + count; i++) {
  946. const data = sortData[i];
  947. if (!data) { continue; }
  948. this._loadRowData(sheet, data, i);
  949. sheet.setRowVisible(i, data.visible);
  950. }
  951. this.endMassOperation(sheet);
  952. } catch (err) {
  953. this.endMassOperation(sheet);
  954. }
  955. },
  956. /**
  957. * 重新加载部分行数据
  958. * @param {GC.Spread.Sheets.Worksheet} sheet
  959. * @param {Array} rows
  960. */
  961. reLoadRowsData: function (sheet, rows) {
  962. const self = this;
  963. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  964. this.beginMassOperation(sheet);
  965. try {
  966. for (const row of rows) {
  967. if (row < 0) { continue; }
  968. // 清空原单元格数据
  969. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  970. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  971. const data = sortData[row];
  972. // 单元格重新写入数据
  973. this._loadRowData(sheet, data, row);
  974. sheet.setRowVisible(row, data.visible);
  975. }
  976. this.endMassOperation(sheet);
  977. } catch (err) {
  978. this.endMassOperation(sheet);
  979. }
  980. },
  981. reloadRowForeColor: function (sheet, row, count) {
  982. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  983. this.beginMassOperation(sheet);
  984. try {
  985. for (let i = row; i < row + count; i++) {
  986. if (i < 0) { continue; }
  987. const data = sortData[i];
  988. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  989. sheet.getCell(i, iCol).foreColor(SpreadJsObj._getForeColor(sheet, data, i, col));
  990. }
  991. };
  992. this.endMassOperation(sheet);
  993. } catch (err) {
  994. this.endMassOperation(sheet);
  995. }
  996. },
  997. reloadRowsForeColor: function (sheet, rows) {
  998. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  999. this.beginMassOperation(sheet);
  1000. try {
  1001. for (const row of rows) {
  1002. if (row < 0) { continue; }
  1003. const data = sortData[row];
  1004. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  1005. sheet.getCell(row, iCol).foreColor(SpreadJsObj._getForeColor(sheet, data, row, col));
  1006. }
  1007. };
  1008. this.endMassOperation(sheet);
  1009. } catch (err) {
  1010. this.endMassOperation(sheet);
  1011. }
  1012. },
  1013. reloadRowBackColor: function (sheet, row, count = 1) {
  1014. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  1015. this.beginMassOperation(sheet);
  1016. try {
  1017. for (let i = row; i < row + count; i++) {
  1018. if (i < 0) { continue; }
  1019. const data = sortData[i];
  1020. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  1021. sheet.getCell(i, iCol).backColor(SpreadJsObj._getBackColor(sheet, data, i, col));
  1022. }
  1023. }
  1024. this.endMassOperation(sheet);
  1025. } catch (err) {
  1026. this.endMassOperation(sheet);
  1027. }
  1028. },
  1029. reloadRowsBackColor: function (sheet, rows) {
  1030. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  1031. this.beginMassOperation(sheet);
  1032. try {
  1033. for (const row of rows) {
  1034. if (row < 0) { continue; }
  1035. const data = sortData[row];
  1036. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  1037. sheet.getCell(row, iCol).backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  1038. }
  1039. };
  1040. this.endMassOperation(sheet);
  1041. } catch (err) {
  1042. this.endMassOperation(sheet);
  1043. }
  1044. },
  1045. reloadRowsReadonly: function (sheet, rows) {
  1046. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  1047. this.beginMassOperation(sheet);
  1048. try {
  1049. for (const row of rows) {
  1050. if (row < 0) { continue; }
  1051. this._loadRowStyle(sheet, sortData[row], row);
  1052. };
  1053. } catch (err) {
  1054. }
  1055. this.endMassOperation(sheet);
  1056. },
  1057. reloadColData: function (sheet, col, count = 1) {
  1058. const cols = [];
  1059. for (let i = 0; i < count; i++) {
  1060. cols.push(col+i);
  1061. }
  1062. this.reLoadColsData(sheet, cols);
  1063. },
  1064. /**
  1065. * 重新加载部分列数据
  1066. * @param {GC.Spread.Sheets.Worksheet} sheet
  1067. * @param {Array} cols
  1068. */
  1069. reLoadColsData: function (sheet, cols) {
  1070. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  1071. if (!sortData) return;
  1072. this.beginMassOperation(sheet);
  1073. try {
  1074. for (const col of cols) {
  1075. // 清空原单元格数据
  1076. sheet.clear(-1, col, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  1077. for (const [iRow, data] of sortData.entries()) {
  1078. this._loadCellData(sheet, data, iRow, col);
  1079. }
  1080. }
  1081. this.endMassOperation(sheet);
  1082. } catch (err) {
  1083. console.log(err);
  1084. this.endMassOperation(sheet);
  1085. }
  1086. },
  1087. reLoadNodesData: function (sheet, nodes) {
  1088. this.beginMassOperation(sheet);
  1089. nodes = nodes instanceof Array ? nodes : [nodes];
  1090. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  1091. for (const node of nodes) {
  1092. this._loadRowData(sheet, node, sortData.indexOf(node));
  1093. }
  1094. this.endMassOperation(sheet);
  1095. },
  1096. /**
  1097. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  1098. * @param {GC.Spread.Sheets.Worksheet} sheet
  1099. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  1100. * @param {Array|PathTree} data - 对dataType对应
  1101. */
  1102. loadSheetData: function (sheet, dataType, data, force = false){
  1103. sheet.zh_dataType = dataType;
  1104. if (dataType === 'tree') {
  1105. sheet.zh_tree = data;
  1106. } else {
  1107. sheet.zh_data = data;
  1108. }
  1109. this.protectedSheet(sheet);
  1110. this.reLoadSheetData(sheet, force);
  1111. },
  1112. loadSimpleSheetData: function(sheet, data, font) {
  1113. const sortData = data;
  1114. this.beginMassOperation(sheet);
  1115. try {
  1116. sheet.setRowCount(0, spreadNS.SheetArea.viewport);
  1117. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  1118. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  1119. // 设置总行数
  1120. const totalRow = sortData.length;
  1121. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  1122. const totalCol = sortData.map(x => { return x.length; }).reduce((x, y) => { return math.max(x, y)});
  1123. if (sortData && sortData.length > 0) sheet.setColumnCount(totalCol, spreadNS.SheetArea.viewport);
  1124. if (sortData) {
  1125. // 单元格写入数据
  1126. sortData.forEach(function (d, i) {
  1127. // 单元格重新写入数据
  1128. if (!d) return;
  1129. d.forEach(function (value, j) {
  1130. const cell = sheet.getCell(i, j);
  1131. cell.value(value).vAlign(1).hAlign(1);
  1132. font && cell.font(font);
  1133. cell.setBorder(sheet.borderLine, {all: true});
  1134. });
  1135. });
  1136. sortData[0].forEach(function(value, j) {
  1137. sheet.autoFitColumn(j);
  1138. })
  1139. }
  1140. this.endMassOperation(sheet);
  1141. } catch (err) {
  1142. console.log(err);
  1143. this.endMassOperation(sheet);
  1144. }
  1145. },
  1146. /**
  1147. * 获取复制数据HTML格式(过滤不可见单元格)
  1148. * @param {GC.Spread.Sheets.Worksheet} sheet
  1149. * @returns {string}
  1150. */
  1151. getFilterCopyHTML: function (sheet) {
  1152. const sel = sheet.getSelections()[0];
  1153. const html = [];
  1154. html.push('<table>');
  1155. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  1156. // 跳过隐藏行
  1157. if (!sheet.getCell(i, -1).visible()) { continue; }
  1158. const rowHtml = [];
  1159. rowHtml.push('<tr>');
  1160. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  1161. const data = sheet.getText(i, j);
  1162. rowHtml.push('<td>' + data + '</td>');
  1163. }
  1164. rowHtml.push('</tr>');
  1165. html.push(rowHtml.join(''));
  1166. }
  1167. html.push('</table>');
  1168. return html.join('');
  1169. },
  1170. /**
  1171. * 获取复制数据Text格式(过滤不可见单元格)
  1172. * @param {GC.Spread.Sheets.Worksheet} sheet
  1173. * @returns {string}
  1174. */
  1175. getFilterCopyText: function (sheet) {
  1176. const copyData = [];
  1177. const sel = sheet.getSelections()[0];
  1178. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  1179. // 跳过隐藏行
  1180. if (!sheet.getCell(i, -1).visible()) { continue; }
  1181. const rowText = [];
  1182. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  1183. const data = sheet.getText(i, j);
  1184. rowText.push(data);
  1185. }
  1186. copyData.push(rowText.join('\t'));
  1187. }
  1188. return copyData.join('\r\n');
  1189. },
  1190. analysisPasteText: function (text, transpose = false) {
  1191. const result = [];
  1192. if (text === '') return result;
  1193. const rows = text.split('\r\n');
  1194. if (rows[rows.length - 1] === '') rows.splice(rows.length - 1, 1);
  1195. for (const r of rows) {
  1196. const cols = r.split('\t');
  1197. result.push(cols);
  1198. }
  1199. return transpose ? SpreadJsObj.transposePasteData(result) : result;
  1200. },
  1201. analysisPasteHtml: function (html) {
  1202. const result = [];
  1203. if (!html || html === '') return result;
  1204. },
  1205. transposePasteData: function (data) {
  1206. const result = [];
  1207. const rowCount = data.length, colCount = data.reduce((r, x) => { return Math.max(r, x.length); }, 0);
  1208. for (let c = 0; c < colCount; c++) {
  1209. const newRow = [];
  1210. for (let r = 0; r < rowCount; r++) {
  1211. newRow.push(data[r][c]);
  1212. }
  1213. result.push(newRow)
  1214. }
  1215. return result;
  1216. },
  1217. copyFilterHiddenRow: function (sheet) {
  1218. sheet.bind(spreadNS.Events.ClipboardChanged, function (e, info) {
  1219. SpreadJsObj.Clipboard.setSysClipboard(SpreadJsObj.getFilterCopyText(info.sheet));
  1220. });
  1221. },
  1222. /**
  1223. * 树表结构,定位至指定的节点
  1224. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  1225. * @param {Number} id - 定位节点的id
  1226. */
  1227. locateTreeNode: function (sheet, id, autoExpand = false) {
  1228. const tree = sheet.zh_tree;
  1229. if (!tree) { return }
  1230. const node = tree.getItems(id);
  1231. if (!node) { return }
  1232. if (autoExpand && !node.visible) {
  1233. const parents = tree.autoExpandNode(node);
  1234. if (parents && parents.length > 0) {
  1235. SpreadJsObj.reLoadNodesData(sheet, parents);
  1236. SpreadJsObj.refreshTreeRowVisible(sheet);
  1237. }
  1238. }
  1239. const index = tree.nodes.indexOf(node);
  1240. const sels = sheet.getSelections();
  1241. sheet.setSelection(index, sels[0].col, 1, 1);
  1242. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  1243. sheet.getParent().focus();
  1244. sheet.showRow(index, spreadNS.VerticalPosition.center);
  1245. },
  1246. locateData: function (sheet, data) {
  1247. if (!sheet.zh_data) { return }
  1248. const index = sheet.zh_data.indexOf(data);
  1249. if (index < 0) return;
  1250. const sels = sheet.getSelections();
  1251. sheet.setSelection(index, sels[0].col, 1, 1);
  1252. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  1253. sheet.getParent().focus();
  1254. sheet.showRow(index, spreadNS.VerticalPosition.center);
  1255. },
  1256. locateRow: function (sheet, row, focus = true) {
  1257. const sels = sheet.getSelections();
  1258. sheet.setSelection(row, sels[0].col, 1, 1);
  1259. SpreadJsObj.reloadRowsBackColor(sheet, [row, sels[0].row]);
  1260. if (focus) sheet.getParent().focus();
  1261. sheet.showRow(row, spreadNS.VerticalPosition.center);
  1262. },
  1263. locateDataBy: function(sheet, fun) {
  1264. if (!sheet.zh_data) { return }
  1265. const index = sheet.zh_data.findIndex(d => { return fun(d); });
  1266. if (index < 0) return;
  1267. this.locateRow(sheet, index);
  1268. },
  1269. saveTopAndSelect: function (sheet, cacheKey) {
  1270. const sel = sheet.getSelections()[0];
  1271. const top = sheet.getViewportTopRow(1);
  1272. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  1273. },
  1274. loadTopAndSelect: function (sheet, cacheKey) {
  1275. let ts = getLocalCache(cacheKey);
  1276. if (ts !== '') {
  1277. ts = JSON.parse(ts);
  1278. if (ts === undefined || ts === null) return;
  1279. if (ts.sel) {
  1280. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  1281. SpreadJsObj.reloadRowsBackColor(sheet, [0, ts.sel.row]);
  1282. }
  1283. if (ts.top) {
  1284. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  1285. }
  1286. }
  1287. },
  1288. resetTopAndSelect: function (sheet) {
  1289. sheet.setSelection(0, 0, 1, 1);
  1290. sheet.showRow(0, spreadNS.VerticalPosition.top);
  1291. },
  1292. /**
  1293. * 获取当前选行的数据对象
  1294. * @param {GC.Spread.Sheets.Worksheet} sheet
  1295. * @returns {Object}
  1296. */
  1297. getSelectObject: function (sheet) {
  1298. if (!sheet) {
  1299. return null;
  1300. } else if (sheet.zh_dataType) {
  1301. const sel = sheet.getSelections()[0];
  1302. if (!sel) {
  1303. return null;
  1304. }
  1305. if (sheet.zh_dataType === this.DataType.Tree) {
  1306. return sheet.zh_tree.nodes[sel.row];
  1307. } else if (sheet.zh_dataType === this.DataType.Data) {
  1308. return sheet.zh_data[sel.row];
  1309. } else {
  1310. return null;
  1311. }
  1312. }
  1313. },
  1314. getRowObject: function (sheet, row) {
  1315. if (!sheet) {
  1316. return null;
  1317. } else if (sheet.zh_dataType) {
  1318. if (sheet.zh_dataType === this.DataType.Tree) {
  1319. return sheet.zh_tree.nodes[row];
  1320. } else if (sheet.zh_dataType === this.DataType.Data) {
  1321. return sheet.zh_data[row];
  1322. } else {
  1323. return null;
  1324. }
  1325. }
  1326. },
  1327. /**
  1328. * 刷新列显示
  1329. * @param sheet
  1330. */
  1331. refreshColumnVisible: function (sheet, refreshRowHeight = false) {
  1332. if (!sheet.zh_setting) return;
  1333. this.beginMassOperation(sheet);
  1334. try {
  1335. sheet.zh_setting.cols.forEach(function (col, index) {
  1336. if (col.visible !== undefined && col.visible !== null) {
  1337. sheet.setColumnVisible(index, col.visible);
  1338. }
  1339. });
  1340. if (refreshRowHeight) {
  1341. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  1342. // 重算行高
  1343. for (let i = 0, iLen = sortData.length; i < iLen; i++) {
  1344. const data = sortData[i];
  1345. if (!data) { continue; }
  1346. this._calcRowHeight(sheet, data, i);
  1347. }
  1348. }
  1349. this.endMassOperation(sheet);
  1350. } catch (err) {
  1351. console.log(err);
  1352. this.endMassOperation(sheet);
  1353. }
  1354. },
  1355. /**
  1356. * 刷新行显示
  1357. * @param sheet
  1358. */
  1359. refreshTreeRowVisible: function (sheet) {
  1360. this.beginMassOperation(sheet);
  1361. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  1362. for (const iRow in sortData) {
  1363. const node = sortData[iRow];
  1364. if (node.visible !== undefined && node.visible !== null) {
  1365. if (node.visible && node.waitingLoading) {
  1366. SpreadJsObj._loadRowData(sheet, node, parseInt(iRow));
  1367. }
  1368. sheet.setRowVisible(iRow, node.visible);
  1369. } else {
  1370. if (node.waitingLoading) {
  1371. this._loadRowData(sheet, node, parseInt(iRow));
  1372. }
  1373. sheet.setRowVisible(iRow, true);
  1374. }
  1375. }
  1376. this.endMassOperation(sheet);
  1377. },
  1378. refreshColumnAlign: function (sheet) {
  1379. if (sheet.zh_setting) {
  1380. for (const iCol in sheet.zh_setting.cols) {
  1381. const col = sheet.zh_setting.cols[iCol];
  1382. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  1383. }
  1384. }
  1385. },
  1386. refreshColumnCombo: function(sheet, col) {
  1387. let cellKey;
  1388. if (col.cellType === 'customizeCombo') {
  1389. cellKey = col.cellTypeKey ? 'customizeCombo-' + col.cellTypeKey : 'customizeCombo';
  1390. }
  1391. if (col.cellType === 'specCombo') {
  1392. cellKey = col.cellTypeKey ? 'specCombo-' + col.cellTypeKey : 'specCombo';
  1393. }
  1394. if (sheet.extendCellType[cellKey]) {
  1395. sheet.extendCellType[cellKey].items(col.comboItems);
  1396. }
  1397. },
  1398. refreshSheetReadOnly: function (sheet) {
  1399. this.beginMassOperation(sheet);
  1400. if (sheet.zh_setting) {
  1401. sheet.zh_setting.cols.forEach(function (col, i) {
  1402. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1403. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1404. }
  1405. });
  1406. }
  1407. this.endMassOperation(sheet);
  1408. },
  1409. /**
  1410. * 刷新列是否只读
  1411. * @param sheet
  1412. * @param field
  1413. * @param readonly
  1414. */
  1415. resetFieldReadOnly: function (sheet, field, readonly) {
  1416. const fields = field instanceof Array ? field : [field];
  1417. if (sheet.zh_setting) {
  1418. sheet.zh_setting.cols.forEach(function (col, i) {
  1419. if (fields.indexOf(col.field) !== -1) {
  1420. col.readOnly = readonly;
  1421. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1422. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1423. }
  1424. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1425. }
  1426. });
  1427. }
  1428. },
  1429. CellType: {
  1430. /**
  1431. * 获取 带悬浮提示的CellType
  1432. * @returns {TipCellType}
  1433. */
  1434. getTipCellType: function (scrollHeightClass = false) {
  1435. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1436. const TipCellType = function () {};
  1437. // 继承 SpreadJs定义的 普通的TextCellType
  1438. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1439. const proto = TipCellType.prototype;
  1440. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1441. const xs = hitinfo.sheet.getParent().xs;
  1442. const ctx = xs.childNodes[0].getContext("2d");
  1443. if (font && font !== '') {
  1444. ctx.font = font;
  1445. } else {
  1446. ctx.font = hitinfo.cellStyle.font;
  1447. }
  1448. return ctx.measureText(str).width;
  1449. };
  1450. proto.showTip = function (hitinfo, text) {
  1451. return text && text !== '';
  1452. };
  1453. /**
  1454. * 获取点击信息
  1455. * @param {Number} x
  1456. * @param {Number} y
  1457. * @param {Object} cellStyle
  1458. * @param {Object} cellRect
  1459. * @param {Object} context
  1460. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1461. */
  1462. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1463. return {
  1464. x: x,
  1465. y: y,
  1466. row: context.row,
  1467. col: context.col,
  1468. cellStyle: cellStyle,
  1469. cellRect: cellRect,
  1470. sheet: context.sheet,
  1471. sheetArea: context.sheetArea,
  1472. ctx: context.sheet.getParent().xs,
  1473. };
  1474. };
  1475. /**
  1476. * 鼠标进入单元格事件 - 显示悬浮提示
  1477. * @param {Object} hitinfo - 见getHitInfo返回值
  1478. */
  1479. proto.processMouseEnter = function (hitinfo) {
  1480. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1481. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1482. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1483. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1484. text = col.getTip(sortData[hitinfo.row]);
  1485. }
  1486. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1487. if (pos && this.showTip(hitinfo, text)) {
  1488. if (!this._toolTipElement) {
  1489. let div = $('#autoTip')[0];
  1490. if (!div) {
  1491. div = document.createElement("div");
  1492. $(div).css("position", "absolute")
  1493. .css("border", "1px #C0C0C0 solid")
  1494. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1495. .css("font", "9pt Arial")
  1496. .css("background", "white")
  1497. .css("padding", 5)
  1498. .css("z-index", 9999)
  1499. .css("max-width", maxHintWidth)
  1500. .css("word-wrap", "break-word")
  1501. .attr("id", 'autoTip');
  1502. document.body.insertBefore(div, null);
  1503. }
  1504. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1505. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1506. const scrollTop = scrollHeightClass ? $(scrollHeightClass).scrollTop() : 0;
  1507. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1508. $(div).html(text).css("top", pos.y + hitinfo.y - scrollTop + indent).css("left", pos.x + hitinfo.x + indent);
  1509. } else if (textWidth > maxHintWidth) {
  1510. $(div).html(text).css("top", pos.y + hitinfo.y - scrollTop + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1511. } else {
  1512. $(div).html(text).css("top", pos.y + hitinfo.y - scrollTop + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1513. }
  1514. this._toolTipElement = div;
  1515. $(div).show("fast");
  1516. }
  1517. }
  1518. };
  1519. /**
  1520. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1521. * @param {Object} hitinfo - 见getHitInfo返回值
  1522. */
  1523. proto.processMouseLeave = function (hitinfo) {
  1524. if (this._toolTipElement) {
  1525. $(this._toolTipElement).hide();
  1526. this._toolTipElement = null;
  1527. }
  1528. };
  1529. return new TipCellType();
  1530. },
  1531. getAutoTipCellType: function (scrollHeightClass) {
  1532. const AutoTipCellType = function () {};
  1533. // 继承 TipCellType
  1534. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType(scrollHeightClass);
  1535. const proto = AutoTipCellType.prototype;
  1536. proto.showTip = function (hitinfo, text) {
  1537. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1538. };
  1539. return new AutoTipCellType();
  1540. },
  1541. getDelayTipCellType: function () {
  1542. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1543. const TipCellType = function () {};
  1544. // 继承 SpreadJs定义的 普通的TextCellType
  1545. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1546. const proto = TipCellType.prototype;
  1547. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1548. const xs = hitinfo.sheet.getParent().xs;
  1549. const ctx = xs.childNodes[0].getContext("2d");
  1550. if (font && font !== '') {
  1551. ctx.font = font;
  1552. } else {
  1553. ctx.font = hitinfo.cellStyle.font;
  1554. }
  1555. return ctx.measureText(str).width;
  1556. };
  1557. proto.showTip = function (hitinfo, text) {
  1558. return text && text !== '';
  1559. };
  1560. /**
  1561. * 获取点击信息
  1562. * @param {Number} x
  1563. * @param {Number} y
  1564. * @param {Object} cellStyle
  1565. * @param {Object} cellRect
  1566. * @param {Object} context
  1567. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1568. */
  1569. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1570. return {
  1571. x: x,
  1572. y: y,
  1573. row: context.row,
  1574. col: context.col,
  1575. cellStyle: cellStyle,
  1576. cellRect: cellRect,
  1577. sheet: context.sheet,
  1578. sheetArea: context.sheetArea,
  1579. ctx: context.sheet.getParent().xs,
  1580. };
  1581. };
  1582. /**
  1583. * 鼠标进入单元格事件 - 显示悬浮提示
  1584. * @param {Object} hitinfo - 见getHitInfo返回值
  1585. */
  1586. proto.processMouseEnter = function (hitinfo) {
  1587. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1588. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1589. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1590. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1591. text = col.getTip(sortData[hitinfo.row]);
  1592. }
  1593. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1594. if (pos && this.showTip(hitinfo, text)) {
  1595. if (!this._toolTipElement) {
  1596. let div = $('#autoTip')[0];
  1597. if (!div) {
  1598. div = document.createElement("div");
  1599. $(div).css("position", "absolute")
  1600. .css("border", "1px #C0C0C0 solid")
  1601. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1602. .css("font", "9pt Arial")
  1603. .css("background", "white")
  1604. .css("padding", 5)
  1605. .css("z-index", 999)
  1606. .css("max-width", maxHintWidth)
  1607. .css("word-wrap", "break-word")
  1608. .attr("id", 'autoTip');
  1609. document.body.insertBefore(div, null);
  1610. }
  1611. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1612. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1613. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1614. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1615. } else if (textWidth > maxHintWidth) {
  1616. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1617. } else {
  1618. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1619. }
  1620. this._toolTipElement = div;
  1621. const self = this;
  1622. setTimeout(()=>{ if (self._toolTipElement) $(self._toolTipElement).show("fast");}, 300);
  1623. }
  1624. }
  1625. };
  1626. /**
  1627. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1628. * @param {Object} hitinfo - 见getHitInfo返回值
  1629. */
  1630. proto.processMouseLeave = function (hitinfo) {
  1631. if (this._toolTipElement) {
  1632. $(this._toolTipElement).hide();
  1633. this._toolTipElement = null;
  1634. }
  1635. };
  1636. return new TipCellType();
  1637. },
  1638. /**
  1639. * 获取树结构CellType
  1640. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  1641. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  1642. * @returns {TreeNodeCellType}
  1643. */
  1644. getTreeNodeCellType: function () {
  1645. const xOffset = 2 + 5;
  1646. const indent = 20;
  1647. const levelIndent = -5;
  1648. const halfBoxLength = 5;
  1649. const halfExpandLength = 3;
  1650. const lineColor = '#515151';//'#b8b8b8';
  1651. const expandBoxColor = '#434343';//'#808080';
  1652. const dotLine = true;
  1653. const hintMaxHintWidth = 200, hintIndent = 15, hintBorderIndent = 10;
  1654. /**
  1655. * 画一条点线段
  1656. * @param canvas - 画布
  1657. * @param x1 - 线段起点 x
  1658. * @param y1 - 线段起点 y
  1659. * @param x2 - 线段终点 x
  1660. * @param y2 - 线段终点 y
  1661. * @param color - 线段颜色
  1662. */
  1663. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  1664. canvas.save();
  1665. // 设置偏移量
  1666. canvas.translate(0.5, 0.5);
  1667. canvas.beginPath();
  1668. canvas.strokeStyle = color;
  1669. canvas.dottedLine(x1, y1, x2, y2);
  1670. canvas.stroke();
  1671. canvas.restore();
  1672. };
  1673. /**
  1674. * 画一条线段
  1675. * @param canvas - 画布
  1676. * @param x1 - 线段起点 x
  1677. * @param y1 - 线段起点 y
  1678. * @param x2 - 线段终点 x
  1679. * @param y2 - 线段终点 y
  1680. * @param color - 线段颜色
  1681. */
  1682. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  1683. canvas.save();
  1684. // 设置偏移量
  1685. canvas.translate(0.5, 0.5);
  1686. canvas.beginPath();
  1687. canvas.moveTo(x1, y1);
  1688. canvas.lineTo(x2, y2);
  1689. canvas.strokeStyle = color;
  1690. canvas.stroke();
  1691. canvas.restore();
  1692. };
  1693. /**
  1694. * 画一个方框
  1695. * @param {Object} canvas - 画布
  1696. * @param {Object} rect - 方框区域
  1697. * @param {String} lineColor - 画线颜色
  1698. * @param {String} fillColor - 填充颜色
  1699. */
  1700. const drawBox = function (canvas, rect, lineColor, fillColor) {
  1701. canvas.save();
  1702. // 设置偏移量
  1703. canvas.translate(0.5, 0.5);
  1704. canvas.strokeStyle = lineColor;
  1705. canvas.beginPath();
  1706. canvas.moveTo(rect.left, rect.top);
  1707. canvas.lineTo(rect.left, rect.bottom);
  1708. canvas.lineTo(rect.right, rect.bottom);
  1709. canvas.lineTo(rect.right, rect.top);
  1710. canvas.lineTo(rect.left, rect.top);
  1711. canvas.stroke();
  1712. canvas.fillStyle = fillColor ? fillColor : 'white';
  1713. canvas.fill();
  1714. canvas.restore();
  1715. };
  1716. /**
  1717. * 画树结构-展开收起按钮
  1718. * @param {Object} canvas - 画布
  1719. * @param {Number} x - 单元格左顶点坐标 x
  1720. * @param {Number} y - 单元格左顶点坐标 y
  1721. * @param {Number} w - 单元格宽度
  1722. * @param {Number} h - 单元格高度
  1723. * @param {Number} centerX - 按钮中央坐标
  1724. * @param {Number} centerY - 按钮中央坐标
  1725. * @param {Boolean} expanded - 当前节点展开收起状态
  1726. */
  1727. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded, style) {
  1728. let rect = {
  1729. top: centerY - halfBoxLength,
  1730. bottom: centerY + halfBoxLength,
  1731. left: centerX - halfBoxLength,
  1732. right: centerX + halfBoxLength
  1733. };
  1734. let h1, h2, offset = 1;
  1735. if (rect.left < x + w) {
  1736. // 方框超出单元格宽度时,超出部分不画。
  1737. rect.right = Math.min(rect.right, x + w);
  1738. drawBox(canvas, rect, expandBoxColor, style.backColor);
  1739. // 画中心十字
  1740. // 画十字横线
  1741. h1 = centerX - halfExpandLength;
  1742. h2 = Math.min(centerX + halfExpandLength, x + w);
  1743. if (h2 > h1) {
  1744. drawLine(canvas, h1, centerY, h2, centerY, expandBoxColor);
  1745. }
  1746. // 画十字竖线
  1747. if (!expanded && (centerX < x + w)) {
  1748. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, expandBoxColor);
  1749. }
  1750. }
  1751. };
  1752. /**
  1753. * 绘制文件夹图标(使用arcTo兼容旧浏览器)
  1754. * @param {Object} canvas - 画布
  1755. * @param {Number} x - 左上角x坐标
  1756. * @param {Number} y - 左上角y坐标
  1757. * @param {Number} s - 图标尺寸
  1758. */
  1759. const drawFolderIcon = function (canvas, x, y, s) {
  1760. canvas.save();
  1761. // 文件夹整体(含顶部标签页)
  1762. canvas.fillStyle = '#FFC107';
  1763. canvas.strokeStyle = '#F57F17';
  1764. canvas.lineWidth = 0.8;
  1765. canvas.beginPath();
  1766. canvas.moveTo(x, y + 3);
  1767. canvas.lineTo(x, y);
  1768. canvas.lineTo(x + s * 0.4, y);
  1769. canvas.lineTo(x + s * 0.5, y + 3);
  1770. canvas.lineTo(x + s, y + 3);
  1771. canvas.arcTo(x + s, y + s, x, y + s, 1.5);
  1772. canvas.arcTo(x, y + s, x, y, 1.5);
  1773. canvas.closePath();
  1774. canvas.fill();
  1775. canvas.stroke();
  1776. // 前面板(浅色,营造层次感)
  1777. canvas.fillStyle = '#FFD54F';
  1778. canvas.beginPath();
  1779. canvas.moveTo(x, y + 5);
  1780. canvas.arcTo(x + s, y + 5, x + s, y + s, 1.5);
  1781. canvas.arcTo(x + s, y + s, x, y + s, 1.5);
  1782. canvas.closePath();
  1783. canvas.fill();
  1784. canvas.restore();
  1785. };
  1786. /**
  1787. * 绘制文件图标(带折角和文字线条)
  1788. * @param {Object} canvas - 画布
  1789. * @param {Number} x - 左上角x坐标
  1790. * @param {Number} y - 左上角y坐标
  1791. * @param {Number} s - 图标尺寸
  1792. */
  1793. const drawFileIcon = function (canvas, x, y, s) {
  1794. const fold = s * 0.3;
  1795. canvas.save();
  1796. // 文件主体(带折角轮廓)
  1797. canvas.fillStyle = '#E3F2FD';
  1798. canvas.strokeStyle = '#42A5F5';
  1799. canvas.lineWidth = 0.8;
  1800. canvas.beginPath();
  1801. canvas.moveTo(x, y);
  1802. canvas.lineTo(x + s - fold, y);
  1803. canvas.lineTo(x + s, y + fold);
  1804. canvas.lineTo(x + s, y + s);
  1805. canvas.arcTo(x, y + s, x, y, 1.5);
  1806. canvas.closePath();
  1807. canvas.fill();
  1808. canvas.stroke();
  1809. // 折角三角形
  1810. canvas.fillStyle = '#90CAF9';
  1811. canvas.beginPath();
  1812. canvas.moveTo(x + s - fold, y);
  1813. canvas.lineTo(x + s - fold, y + fold);
  1814. canvas.lineTo(x + s, y + fold);
  1815. canvas.closePath();
  1816. canvas.fill();
  1817. canvas.stroke();
  1818. // 文字线条
  1819. canvas.strokeStyle = '#90CAF9';
  1820. canvas.lineWidth = 0.6;
  1821. canvas.beginPath();
  1822. canvas.moveTo(x + 2, y + fold + 3);
  1823. canvas.lineTo(x + s - 3, y + fold + 3);
  1824. canvas.moveTo(x + 2, y + fold + 5.5);
  1825. canvas.lineTo(x + s - 5, y + fold + 5.5);
  1826. canvas.stroke();
  1827. canvas.restore();
  1828. };
  1829. let TreeNodeCellType = function (){};
  1830. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  1831. const proto = TreeNodeCellType.prototype;
  1832. /**
  1833. * 绘制方法
  1834. * @param {Object} canvas - 画布
  1835. * @param value - cell.value
  1836. * @param {Number} x - 单元格左顶点坐标 x
  1837. * @param {Number} y - 单元格左顶点坐标 y
  1838. * @param {Number} w - 单元格宽度
  1839. * @param {Number} h - 单元格高度
  1840. * @param {Object} style - cell.style
  1841. * @param {Object} options
  1842. */
  1843. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1844. // 清理 画布--单元格范围 旧数据
  1845. if (style.backColor) {
  1846. canvas.save();
  1847. canvas.fillStyle = style.backColor;
  1848. canvas.fillRect(x, y, w, h);
  1849. canvas.restore();
  1850. } else {
  1851. // 不知道为什么在 概算投资-造价对比 下,这一句代码无法生效。
  1852. // canvas.clearRect(x, y, w, h);
  1853. canvas.save();
  1854. canvas.fillStyle = 'white';
  1855. canvas.fillRect(x, y, w, h);
  1856. canvas.restore();
  1857. }
  1858. const tree = options.sheet.zh_tree;
  1859. const setting = tree.setting;
  1860. // 使用TreeCellType前,需定义sheet.tree
  1861. if (tree) {
  1862. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  1863. if (node) {
  1864. const showTreeLine = true;
  1865. const centerX = Math.floor(x) + (node[setting.level] - 1) * indent + (node[setting.level]) * levelIndent + indent / 2 + xOffset;
  1866. const centerY = Math.floor((y + (y + h)) / 2);
  1867. // Draw Sibling Line
  1868. if (showTreeLine) {
  1869. // Draw Horizontal Line
  1870. if (centerX < x + w) {
  1871. const x1 = centerX + indent / 2;
  1872. if (dotLine) {
  1873. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1874. // drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, style.foreColor);
  1875. } else {
  1876. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1877. // drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, style.foreColor);
  1878. }
  1879. }
  1880. // Draw Vertical Line
  1881. if (centerX < x + w) {
  1882. const y1 = tree.isLastViewSibling(node) ? centerY : y + h;
  1883. const parent = tree.getParent(node);
  1884. const y2 = y1 - centerY;
  1885. if (node[setting.order] === 1 && !parent) {
  1886. if (dotLine) {
  1887. drawDotLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1888. // drawDotLine(canvas, centerX, centerY, centerX, y1, style.foreColor);
  1889. } else {
  1890. drawLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1891. // drawLine(canvas, centerX, centerY, centerX, y1, style.foreColor);
  1892. }
  1893. } else {
  1894. if (dotLine) {
  1895. drawDotLine(canvas, centerX, y, centerX, y1, lineColor);
  1896. // drawDotLine(canvas, centerX, y, centerX, y1, style.foreColor);
  1897. } else {
  1898. drawLine(canvas, centerX, y, centerX, y1, lineColor);
  1899. // drawLine(canvas, centerX, y, centerX, y1, style.foreColor);
  1900. }
  1901. }
  1902. }
  1903. }
  1904. // Draw Expand Box
  1905. if (!node[setting.isLeaf]) {
  1906. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded, style);
  1907. }
  1908. // Draw Parent Line
  1909. if (showTreeLine) {
  1910. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  1911. while (parent) {
  1912. if (!tree.isLastViewSibling(parent)) {
  1913. if (parentCenterX < x + w) {
  1914. if (dotLine) {
  1915. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1916. // drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, style.foreColor);
  1917. } else {
  1918. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1919. // drawLine(canvas, parentCenterX, y, parentCenterX, y + h, style.foreColor);
  1920. }
  1921. }
  1922. }
  1923. parent = tree.getParent(parent);
  1924. parentCenterX -= (indent + levelIndent);
  1925. }
  1926. };
  1927. // 重定位x
  1928. const move = (node[setting.level]) * indent + (node[setting.level]) * levelIndent + xOffset;
  1929. x = x + move;
  1930. w = w - move;
  1931. // 绘制节点图标(文件夹/文件等)
  1932. const col = options.sheet.zh_setting.cols[options.col];
  1933. if (col && col.getIcon) {
  1934. const iconType = col.getIcon(node);
  1935. if (iconType) {
  1936. const customImage = typeof iconType === 'object' ? iconType.image : null;
  1937. const customSize = typeof iconType === 'object' ? Number(iconType.size) : 0;
  1938. const fallbackType = typeof iconType === 'object' ? iconType.fallback : iconType;
  1939. const iconSize = customSize > 0 ? customSize : 14;
  1940. const iconY = y + Math.floor((h - iconSize) / 2);
  1941. const imageReady = customImage && customImage.complete &&
  1942. (customImage.naturalWidth || customImage.width);
  1943. if (imageReady) {
  1944. canvas.drawImage(customImage, x, iconY, iconSize, iconSize);
  1945. } else if (fallbackType === 'folder') {
  1946. drawFolderIcon(canvas, x, iconY, iconSize);
  1947. } else if (fallbackType === 'file') {
  1948. drawFileIcon(canvas, x, iconY, iconSize);
  1949. }
  1950. x += iconSize + 4;
  1951. w -= iconSize + 4;
  1952. }
  1953. }
  1954. }
  1955. }
  1956. // Drawing Text
  1957. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1958. };
  1959. /**
  1960. * 获取点击信息
  1961. * @param {Number} x
  1962. * @param {Number} y
  1963. * @param {Object} cellStyle
  1964. * @param {Object} cellRect
  1965. * @param {Object} context
  1966. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1967. */
  1968. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1969. return {
  1970. x: x,
  1971. y: y,
  1972. row: context.row,
  1973. col: context.col,
  1974. cellStyle: cellStyle,
  1975. cellRect: cellRect,
  1976. sheet: context.sheet,
  1977. sheetArea: context.sheetArea,
  1978. ctx: context.sheet.getParent().xs,
  1979. };
  1980. };
  1981. /**
  1982. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1983. * @param {Object} hitinfo - 见getHitInfo
  1984. */
  1985. proto.processMouseDown = function (hitinfo) {
  1986. const offset = -1;
  1987. const tree = hitinfo.sheet.zh_tree;
  1988. if (!tree) { return; }
  1989. const setting = tree.setting;
  1990. const node = tree.nodes[hitinfo.row];
  1991. if (!node) { return; }
  1992. let centerX = hitinfo.cellRect.x + offset + (node[setting.level] - 1) * indent + (node[setting.level]) * levelIndent + indent / 2 + xOffset;
  1993. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1994. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1995. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1996. const children = tree.getChildren(node);
  1997. if (!node.expanded && !node[setting.isLeaf] && children.length === 0 && tree.loadChildren) {
  1998. tree.loadChildren(node, function () {
  1999. node.expanded = true;
  2000. const children = tree.getChildren(node);
  2001. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  2002. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  2003. });
  2004. } else {
  2005. tree.setExpanded(node, !node.expanded);
  2006. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  2007. const posterity = tree.getPosterity(node);
  2008. for (const child of posterity) {
  2009. const cIndex = tree.getNodeIndex(child);
  2010. if (child.visible && child.waitingLoading) {
  2011. SpreadJsObj._loadRowData(hitinfo.sheet, child, cIndex);
  2012. }
  2013. //if (child.visible !== hitinfo.sheet.getRowVisible(cIndex, hitinfo.sheetArea)) {
  2014. hitinfo.sheet.setRowVisible(cIndex, child.visible, hitinfo.sheetArea);
  2015. //}
  2016. }
  2017. });
  2018. hitinfo.sheet.repaint();
  2019. }
  2020. }
  2021. };
  2022. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  2023. const xs = hitinfo.sheet.getParent().xs;
  2024. const ctx = xs.childNodes[0].getContext("2d");
  2025. if (font && font !== '') {
  2026. ctx.font = font;
  2027. } else {
  2028. ctx.font = hitinfo.cellStyle.font;
  2029. }
  2030. return ctx.measureText(str).width;
  2031. };
  2032. proto.showTip = function (hitinfo, text) {
  2033. if (!text) return false;
  2034. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2035. if (!col.showTips) return false;
  2036. const tree = hitinfo.sheet.zh_tree;
  2037. const node = hitinfo.row < tree.nodes.length ? tree.nodes[hitinfo.row] : null;
  2038. if (!node) return false;
  2039. const move = (node[tree.setting.level]) * indent + (node[tree.setting.level]) * levelIndent + xOffset;
  2040. let iconWidth = 0;
  2041. if (col.getIcon) {
  2042. const iconType = col.getIcon(node);
  2043. if (iconType) {
  2044. const customSize = typeof iconType === 'object' ? Number(iconType.size) : 0;
  2045. iconWidth = (customSize > 0 ? customSize : 14) + 4;
  2046. }
  2047. }
  2048. const validWidth = hitinfo.cellRect.width - move - iconWidth;
  2049. return this.getTextDisplayWidth(hitinfo, text) > validWidth;
  2050. };
  2051. /**
  2052. * 鼠标进入单元格事件 - 显示悬浮提示
  2053. * @param {Object} hitinfo - 见getHitInfo返回值
  2054. */
  2055. proto.processMouseEnter = function (hitinfo) {
  2056. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  2057. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2058. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  2059. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2060. text = col.getTip(sortData[hitinfo.row]);
  2061. }
  2062. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  2063. if (pos && this.showTip(hitinfo, text)) {
  2064. if (!this._toolTipElement) {
  2065. let div = $('#autoTip')[0];
  2066. if (!div) {
  2067. div = document.createElement("div");
  2068. $(div).css("position", "absolute")
  2069. .css("border", "1px #C0C0C0 solid")
  2070. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  2071. .css("font", "9pt Arial")
  2072. .css("background", "white")
  2073. .css("padding", 5)
  2074. .css("z-index", 1999)
  2075. .css("max-width", hintMaxHintWidth)
  2076. .css("word-wrap", "break-word")
  2077. .attr("id", 'autoTip');
  2078. document.body.insertBefore(div, null);
  2079. }
  2080. const validWidth = $(window).width() - (pos.x + hitinfo.x + hintIndent) - hintBorderIndent;
  2081. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  2082. if (validWidth >= hintMaxHintWidth || textWidth <= validWidth) {
  2083. $(div).html(text).css("top", pos.y + hitinfo.y + hintIndent).css("left", pos.x + hitinfo.x + hintIndent);
  2084. } else if (textWidth > hintMaxHintWidth) {
  2085. $(div).html(text).css("top", pos.y + hitinfo.y + hintIndent).css("left", pos.x + hitinfo.x - hintIndent - hintMaxHintWidth);
  2086. } else {
  2087. $(div).html(text).css("top", pos.y + hitinfo.y + hintIndent).css("left", pos.x + hitinfo.x - hintIndent - textWidth);
  2088. }
  2089. this._toolTipElement = div;
  2090. const self = this;
  2091. setTimeout(()=>{ if (self._toolTipElement) $(self._toolTipElement).show("fast");}, 300);
  2092. }
  2093. }
  2094. };
  2095. /**
  2096. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2097. * @param {Object} hitinfo - 见getHitInfo返回值
  2098. */
  2099. proto.processMouseLeave = function (hitinfo) {
  2100. if (this._toolTipElement) {
  2101. $(this._toolTipElement).hide();
  2102. this._toolTipElement = null;
  2103. }
  2104. };
  2105. return new TreeNodeCellType();
  2106. },
  2107. /**
  2108. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  2109. *
  2110. * img:
  2111. * 1. 整列固定,则传入img的select
  2112. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  2113. *
  2114. * 2. 各单元格自定义,则
  2115. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  2116. * function getAttachmentImage (data) {
  2117. * $('#attachment-img').url = data.attachmentImageUrl;
  2118. * return $('#attachment-img')[0];
  2119. * }
  2120. *
  2121. * @returns {ImageCellType}
  2122. */
  2123. getImageCellType: function () {
  2124. const ImageCellType = function (){};
  2125. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  2126. const proto = ImageCellType.prototype;
  2127. proto.getImage = function (sheet, iRow, iCol) {
  2128. const col = sheet.zh_setting.cols[iCol];
  2129. let imgSource = col.img;
  2130. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  2131. const sortData = SpreadJsObj.getSortData(sheet);
  2132. const data = sortData ? sortData[iRow] : null;
  2133. return data ? imgSource(data) : null;
  2134. } else {
  2135. return $(imgSource)[0] ? $(imgSource)[0] : null;
  2136. }
  2137. };
  2138. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2139. const col = options.sheet.zh_setting.cols[options.col];
  2140. const img = this.getImage(options.sheet, options.row, options.col);
  2141. const indent = col.indent ? col.indent : 10;
  2142. if (img) {
  2143. if (style.backColor) {
  2144. canvas.save();
  2145. canvas.fillStyle = style.backColor;
  2146. canvas.fillRect(x, y, indent + img.width, h);
  2147. canvas.restore();
  2148. }
  2149. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  2150. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  2151. style.hAlign = spreadNS.HorizontalAlign.left;
  2152. }
  2153. x = x + indent + img.width;
  2154. w = w - indent - img.width;
  2155. }
  2156. // Drawing Text
  2157. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2158. };
  2159. /**
  2160. * 获取点击信息
  2161. * @param {Number} x
  2162. * @param {Number} y
  2163. * @param {Object} cellStyle
  2164. * @param {Object} cellRect
  2165. * @param {Object} context
  2166. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2167. */
  2168. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2169. return {
  2170. x: x,
  2171. y: y,
  2172. row: context.row,
  2173. col: context.col,
  2174. cellStyle: cellStyle,
  2175. cellRect: cellRect,
  2176. sheet: context.sheet,
  2177. sheetArea: context.sheetArea
  2178. };
  2179. };
  2180. /**
  2181. * 鼠标点击
  2182. * @param {Object} hitinfo - 见getHitInfo
  2183. */
  2184. proto.processMouseDown = function (hitinfo) {
  2185. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2186. if (img) {
  2187. const halfX = img.width / 2, halfY = img.height / 2;
  2188. const centerX = hitinfo.cellRect.x + indent + halfX;
  2189. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2190. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  2191. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2192. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2193. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2194. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2195. const data = sortData ? sortData[hitinfo.row] : null;
  2196. imageClick(data, hitinfo);
  2197. }
  2198. }
  2199. }
  2200. };
  2201. return new ImageCellType();
  2202. },
  2203. /**
  2204. *
  2205. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  2206. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  2207. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  2208. *
  2209. * @returns {ImageCellType}
  2210. */
  2211. getImageButtonCellType: function () {
  2212. let hover = 1, active = 2;
  2213. const ImageCellType = function (){};
  2214. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  2215. const proto = ImageCellType.prototype;
  2216. proto.getImage = function (sheet, iRow, iCol) {
  2217. const col = sheet.zh_setting.cols[iCol];
  2218. let imgSource = col.normalImg;
  2219. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  2220. if (tag === active) {
  2221. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  2222. } else if (tag === hover) {
  2223. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  2224. }
  2225. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  2226. const sortData = SpreadJsObj.getSortData(sheet);
  2227. const data = sortData ? sortData[iRow] : null;
  2228. return data ? imgSource(data) : null;
  2229. } else {
  2230. return $(imgSource)[0] ? $(imgSource)[0] : null;
  2231. }
  2232. };
  2233. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2234. const col = options.sheet.zh_setting.cols[options.col];
  2235. const sortData = SpreadJsObj.getSortData(options.sheet);
  2236. const data = sortData ? sortData[options.row] : null;
  2237. let showImage = true;
  2238. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2239. showImage = col.showImage(data);
  2240. }
  2241. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  2242. const indent = col.indent ? col.indent : 10;
  2243. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  2244. if (img) {
  2245. if (style.backColor) {
  2246. canvas.save();
  2247. canvas.fillStyle = style.backColor;
  2248. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  2249. canvas.restore();
  2250. }
  2251. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  2252. w = w - indent - img.width;
  2253. }
  2254. // Drawing Text
  2255. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2256. } else if (style.hAlign === spreadNS.HorizontalAlign.center) {
  2257. if (img) {
  2258. if (style.backColor) {
  2259. canvas.save();
  2260. canvas.fillStyle = style.backColor;
  2261. canvas.fillRect((x + x + w - indent - img.width) / 2, y, (indent + img.width + img.width) / 2, h);
  2262. canvas.restore();
  2263. }
  2264. canvas.drawImage(img, (x + x + w - indent - img.width) / 2, y + (h - img.height) / 2);
  2265. w = w - indent - img.width;
  2266. }
  2267. // Drawing Text
  2268. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2269. } else {
  2270. if (img) {
  2271. if (style.backColor) {
  2272. canvas.save();
  2273. canvas.fillStyle = style.backColor;
  2274. canvas.fillRect(x + indent, y, indent + img.width, h);
  2275. canvas.restore();
  2276. }
  2277. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  2278. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  2279. style.hAlign = spreadNS.HorizontalAlign.left;
  2280. }
  2281. x = x + indent + img.width;
  2282. w = w - indent - img.width;
  2283. }
  2284. // Drawing Text
  2285. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2286. }
  2287. };
  2288. /**
  2289. * 获取点击信息
  2290. * @param {Number} x
  2291. * @param {Number} y
  2292. * @param {Object} cellStyle
  2293. * @param {Object} cellRect
  2294. * @param {Object} context
  2295. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2296. */
  2297. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2298. return {
  2299. x: x,
  2300. y: y,
  2301. row: context.row,
  2302. col: context.col,
  2303. cellStyle: cellStyle,
  2304. cellRect: cellRect,
  2305. sheet: context.sheet,
  2306. sheetArea: context.sheetArea
  2307. };
  2308. };
  2309. proto.processMouseEnter = function (hitinfo) {
  2310. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2311. // Drawing Image
  2312. if (col.hoverImg) {
  2313. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2314. cell.tag(hover);
  2315. hitinfo.sheet.repaint(hitinfo.cellRect);
  2316. }
  2317. };
  2318. proto.processMouseLeave = function (hitinfo) {
  2319. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2320. // Drawing Image
  2321. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2322. cell.tag(null);
  2323. hitinfo.sheet.repaint(hitinfo.cellRect);
  2324. };
  2325. // proto.processMouseDown = function (hitinfo) {
  2326. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2327. // if (col.activeImg) {
  2328. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2329. // cell.tag(active);
  2330. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2331. // }
  2332. // };
  2333. // proto.processMouseUp = function (hitinfo) {
  2334. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2335. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2336. // const data = sortData ? sortData[hitinfo.row] : null;
  2337. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2338. // if (!col.showImage(data)) {
  2339. // return;
  2340. // }
  2341. // }
  2342. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2343. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2344. // imageClick(data);
  2345. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2346. // cell.tag(null);
  2347. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2348. // }
  2349. // };
  2350. /*
  2351. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  2352. */
  2353. proto.processMouseDown = function (hitinfo) {
  2354. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2355. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2356. const halfX = img.width / 2, halfY = img.height / 2;
  2357. const indent = col.indent ? col.indent : 10;
  2358. let centerX;
  2359. if (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right) {
  2360. centerX = hitinfo.cellRect.x + hitinfo.cellRect.width - indent - halfX;
  2361. } else if (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.center) {
  2362. centerX = hitinfo.cellRect.x + (hitinfo.cellRect.width - indent) / 2 + halfX;
  2363. } else {
  2364. centerX = hitinfo.cellRect.x + indent + halfX
  2365. }
  2366. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2367. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2368. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2369. cell.tag(active);
  2370. hitinfo.sheet.repaint(hitinfo.cellRect);
  2371. }
  2372. };
  2373. proto.processMouseUp = function (hitinfo) {
  2374. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2375. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2376. const halfX = img.width / 2, halfY = img.height / 2;
  2377. const indent = col.indent ? col.indent : 10;
  2378. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  2379. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2380. : hitinfo.cellRect.x + indent + halfX;
  2381. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2382. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2383. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2384. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2385. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2386. const data = sortData ? sortData[hitinfo.row] : null;
  2387. imageClick(data, hitinfo);
  2388. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2389. cell.tag(null);
  2390. hitinfo.sheet.repaint(hitinfo.cellRect);
  2391. }
  2392. }
  2393. };
  2394. proto.processMouseMove = function (hitinfo) {
  2395. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2396. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2397. const halfX = img.width / 2, halfY = img.height / 2;
  2398. const indent = col.indent ? col.indent : 10;
  2399. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  2400. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2401. : hitinfo.cellRect.x + indent + halfX;
  2402. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2403. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2404. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2405. if (cell.tag() !== hover) {
  2406. cell.tag(hover);
  2407. hitinfo.sheet.repaint(hitinfo.cellRect);
  2408. }
  2409. } else {
  2410. if (cell.tag() === hover) {
  2411. cell.tag(null);
  2412. hitinfo.sheet.repaint(hitinfo.cellRect);
  2413. }
  2414. }
  2415. };
  2416. return new ImageCellType();
  2417. },
  2418. getActiveImageButtonCellType: function () {
  2419. let show = 1, hover = 2, active = 2;
  2420. const ImageCellType = function (){};
  2421. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  2422. const proto = ImageCellType.prototype;
  2423. proto.getImage = function (sheet, iRow, iCol) {
  2424. const col = sheet.zh_setting.cols[iCol];
  2425. let imgSource = null;
  2426. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  2427. if (tag === show) {
  2428. imgSource = col.normalImg;
  2429. } else if (tag === active) {
  2430. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  2431. } else if (tag === hover) {
  2432. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  2433. }
  2434. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  2435. const sortData = SpreadJsObj.getSortData(sheet);
  2436. const data = sortData ? sortData[iRow] : null;
  2437. return data ? imgSource(data) : null;
  2438. } else {
  2439. return $(imgSource)[0] ? $(imgSource)[0] : null;
  2440. }
  2441. };
  2442. proto.getActiveImage = function(sheet, iRow, iCol) {
  2443. const col = sheet.zh_setting.cols[iCol];
  2444. const sortData = SpreadJsObj.getSortData(sheet);
  2445. const data = sortData ? sortData[iRow] : null;
  2446. let showImage = true;
  2447. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2448. showImage = col.showImage(data);
  2449. }
  2450. const img = showImage ? this.getImage(sheet, iRow, iCol) : null;
  2451. return [col, img];
  2452. };
  2453. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2454. const [col, img] = this.getActiveImage(options.sheet, options.row, options.col);
  2455. const indent = col.indent ? col.indent : 10;
  2456. if (style.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right) {
  2457. if (img) {
  2458. if (style.backColor) {
  2459. canvas.save();
  2460. canvas.fillStyle = style.backColor;
  2461. canvas.fillRect(x + w - indent - img.width, y, img.width + indent, h);
  2462. canvas.restore();
  2463. }
  2464. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  2465. w = w - indent - img.width;
  2466. }
  2467. // Drawing Text
  2468. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2469. } else {
  2470. if (img) {
  2471. if (style.backColor) {
  2472. canvas.save();
  2473. canvas.fillStyle = style.backColor;
  2474. canvas.fillRect(x, y, indent + img.width, h);
  2475. canvas.restore();
  2476. }
  2477. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  2478. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  2479. style.hAlign = spreadNS.HorizontalAlign.left;
  2480. }
  2481. x = x + indent + img.width;
  2482. w = w - indent - img.width;
  2483. }
  2484. // Drawing Text
  2485. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2486. }
  2487. };
  2488. /**
  2489. * 获取点击信息
  2490. * @param {Number} x
  2491. * @param {Number} y
  2492. * @param {Object} cellStyle
  2493. * @param {Object} cellRect
  2494. * @param {Object} context
  2495. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2496. */
  2497. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2498. return {
  2499. x: x,
  2500. y: y,
  2501. row: context.row,
  2502. col: context.col,
  2503. cellStyle: cellStyle,
  2504. cellRect: cellRect,
  2505. sheet: context.sheet,
  2506. sheetArea: context.sheetArea
  2507. };
  2508. };
  2509. proto.processMouseEnter = function (hitinfo) {
  2510. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2511. // Drawing Image
  2512. if (col.normalImg) {
  2513. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2514. cell.tag(show);
  2515. hitinfo.sheet.repaint(hitinfo.cellRect);
  2516. }
  2517. };
  2518. proto.processMouseLeave = function (hitinfo) {
  2519. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2520. cell.tag(null);
  2521. hitinfo.sheet.repaint(hitinfo.cellRect);
  2522. };
  2523. // proto.processMouseDown = function (hitinfo) {
  2524. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2525. // if (col.activeImg) {
  2526. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2527. // cell.tag(active);
  2528. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2529. // }
  2530. // };
  2531. // proto.processMouseUp = function (hitinfo) {
  2532. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2533. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2534. // const data = sortData ? sortData[hitinfo.row] : null;
  2535. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2536. // if (!col.showImage(data)) {
  2537. // return;
  2538. // }
  2539. // }
  2540. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2541. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2542. // imageClick(data);
  2543. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2544. // cell.tag(null);
  2545. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2546. // }
  2547. // };
  2548. /*
  2549. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  2550. */
  2551. proto.processMouseDown = function (hitinfo) {
  2552. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2553. if (!img) return;
  2554. const halfX = img.width / 2, halfY = img.height / 2;
  2555. const indent = col.indent ? col.indent : 10;
  2556. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  2557. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2558. : hitinfo.cellRect.x + indent + halfX;
  2559. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2560. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2561. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2562. cell.tag(active);
  2563. hitinfo.sheet.repaint(hitinfo.cellRect);
  2564. }
  2565. };
  2566. proto.processMouseUp = function (hitinfo) {
  2567. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2568. if (!img) return;
  2569. const halfX = img.width / 2, halfY = img.height / 2;
  2570. const indent = col.indent ? col.indent : 10;
  2571. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  2572. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2573. : hitinfo.cellRect.x + indent + halfX;
  2574. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2575. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2576. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2577. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2578. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2579. const data = sortData ? sortData[hitinfo.row] : null;
  2580. imageClick(data, hitinfo);
  2581. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2582. cell.tag(hover);
  2583. hitinfo.sheet.repaint(hitinfo.cellRect);
  2584. }
  2585. }
  2586. };
  2587. proto.processMouseMove = function (hitinfo) {
  2588. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2589. if (!img) return;
  2590. const halfX = img.width / 2, halfY = img.height / 2;
  2591. const indent = col.indent ? col.indent : 10;
  2592. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  2593. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2594. : hitinfo.cellRect.x + indent + halfX;
  2595. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2596. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2597. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2598. if (cell.tag() !== hover) {
  2599. cell.tag(hover);
  2600. hitinfo.sheet.repaint(hitinfo.cellRect);
  2601. }
  2602. } else {
  2603. if (cell.tag() === hover) {
  2604. cell.tag(show);
  2605. hitinfo.sheet.repaint(hitinfo.cellRect);
  2606. }
  2607. }
  2608. };
  2609. return new ImageCellType();
  2610. },
  2611. /**
  2612. * 获取 嵌入Html的cellType
  2613. * @returns {HTMLCellType}
  2614. */
  2615. getHtmlCellType: function () {
  2616. const HTMLCellType = function (){};
  2617. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  2618. const proto = HTMLCellType.prototype;
  2619. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2620. let DOMURL = window.URL || window.webkitURL || window;
  2621. let cell = context.sheet.getCell(context.row, context.col);
  2622. let img = cell.tag();
  2623. if (img) {
  2624. try {
  2625. ctx.save();
  2626. ctx.rect(x, y, w, h);
  2627. ctx.clip();
  2628. ctx.drawImage(img, x + 2, y + 2);
  2629. ctx.restore();
  2630. cell.tag(null);
  2631. return;
  2632. }
  2633. catch (err) {
  2634. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context]);
  2635. cell.tag(null);
  2636. return;
  2637. }
  2638. }
  2639. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  2640. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  2641. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  2642. let doc = document.implementation.createHTMLDocument("");
  2643. doc.write(data);
  2644. // Get well-formed markup
  2645. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  2646. img = new Image();
  2647. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  2648. //var url = DOMURL.createObjectURL(svg);
  2649. //img.src = url;
  2650. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  2651. cell.tag(img);
  2652. img.onload = function () {
  2653. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  2654. }
  2655. };
  2656. return new HTMLCellType();
  2657. },
  2658. /**
  2659. * 获取 字符超长缩略的cellType
  2660. * @returns {EllipsisTextCellType}
  2661. */
  2662. getEllipsisTextCellType: function () {
  2663. const EllipsisTextCellType = function (){};
  2664. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  2665. const proto = EllipsisTextCellType.prototype;
  2666. proto.getEllipsisText = function(c, str, maxWidth) {
  2667. var width = c.measureText(str).width;
  2668. var ellipsis = '…';
  2669. var ellipsisWidth = c.measureText(ellipsis).width;
  2670. if (width <= maxWidth || width <= ellipsisWidth) {
  2671. return str;
  2672. } else {
  2673. var len = str.length;
  2674. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  2675. str = str.substring(0, len);
  2676. width = c.measureText(str).width;
  2677. }
  2678. return str + ellipsis;
  2679. }
  2680. };
  2681. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2682. ctx.font = style.font;
  2683. value = this.getEllipsisText(ctx, value, w - 2);
  2684. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  2685. };
  2686. return new EllipsisTextCellType();
  2687. },
  2688. getEllipsisTextAutoTipCellType: function (scrollHeightClass) {
  2689. const CellType = function () {};
  2690. // 继承 TipCellType
  2691. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType(scrollHeightClass);
  2692. const proto = CellType.prototype;
  2693. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  2694. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  2695. proto.paint = ellipsisTextCellType.paint;
  2696. return new CellType();
  2697. },
  2698. /**
  2699. * 获取 动态显示ComboBox的cellType
  2700. * @returns {ActiveComboCellType}
  2701. */
  2702. getActiveComboCellType: function () {
  2703. const ActiveComboCellType = function () {};
  2704. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2705. const proto = ActiveComboCellType.prototype;
  2706. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  2707. const sheet = options.sheet;
  2708. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2709. && !sheet.getCell(options.row, options.col).locked()) {
  2710. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  2711. } else {
  2712. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2713. }
  2714. };
  2715. proto.processMouseEnter = function (hitinfo) {
  2716. hitinfo.sheet.repaint(hitinfo.cellRect);
  2717. };
  2718. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  2719. const sheet = options.sheet;
  2720. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2721. && !sheet.getCell(options.row, options.col).locked()) {
  2722. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  2723. } else {
  2724. return {
  2725. x: x,
  2726. y: y,
  2727. row: options.row,
  2728. col: options.col,
  2729. cellStyle: cellStyle,
  2730. cellRect: cellRect,
  2731. sheetArea: options.sheetArea
  2732. };
  2733. }
  2734. };
  2735. return new ActiveComboCellType();
  2736. },
  2737. /**
  2738. * 获取 单位的CellType
  2739. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  2740. */
  2741. getUnitCellType: function (comboEdit, items = ['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  2742. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '延米', '每一试桩',
  2743. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '辆', '艘班', '亩', '片',
  2744. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2'], maxDrop) {
  2745. let combo = this.getActiveComboCellType();
  2746. combo.editable(comboEdit);
  2747. combo.itemHeight(10).items(items);
  2748. if (maxDrop) combo.maxDropDownItems(maxDrop);
  2749. return combo;
  2750. },
  2751. /**
  2752. * 获取 自定义的CellType
  2753. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  2754. */
  2755. getCustomizeComboCellType: function (items, maxDrop) {
  2756. let combo = this.getActiveComboCellType();
  2757. if (typeof items[0] === 'string') {
  2758. combo.itemHeight(10).items(items);
  2759. } else {
  2760. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  2761. }
  2762. if (maxDrop) combo.maxDropDownItems(maxDrop);
  2763. return combo;
  2764. },
  2765. getSpecComboCellType: function (items, maxDrop) {
  2766. const ComboCellType = function () {};
  2767. ComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2768. const proto = ComboCellType.prototype;
  2769. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  2770. const sheet = options.sheet;
  2771. const setting = options.sheet.zh_setting;
  2772. const col = setting.cols[options.col];
  2773. const data = SpreadJsObj.getSelectObject(options.sheet);
  2774. if (!data) {
  2775. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2776. return;
  2777. }
  2778. const comboEdit = (col.comboEdit && Object.prototype.toString.apply(col.comboEdit) === "[object Function]")
  2779. ? col.comboEdit(options.sheet, data)
  2780. : (col.comboEdit !== undefined ? col.comboEdit : true);
  2781. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2782. && !sheet.getCell(options.row, options.col).locked() && comboEdit) {
  2783. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  2784. } else {
  2785. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2786. }
  2787. };
  2788. proto.processMouseEnter = function (hitinfo) {
  2789. hitinfo.sheet.repaint(hitinfo.cellRect);
  2790. };
  2791. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  2792. const sheet = options.sheet;
  2793. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2794. && !sheet.getCell(options.row, options.col).locked()) {
  2795. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  2796. } else {
  2797. return {
  2798. x: x,
  2799. y: y,
  2800. row: options.row,
  2801. col: options.col,
  2802. cellStyle: cellStyle,
  2803. cellRect: cellRect,
  2804. sheetArea: options.sheetArea
  2805. };
  2806. }
  2807. };
  2808. const combo = new ComboCellType();
  2809. combo.itemHeight(10).items(items).editorValueType(spreadNS.CellTypes.EditorValueType.value);
  2810. if (maxDrop) combo.maxDropDownItems(maxDrop);
  2811. return combo;
  2812. },
  2813. getStageComboCellType: function () {
  2814. const ComboCellType = function () {};
  2815. ComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2816. const proto = ComboCellType.prototype;
  2817. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2818. const setting = context.sheet.zh_setting;
  2819. const col = setting.cols[context.col];
  2820. const data = SpreadJsObj.getSelectObject(context.sheet);
  2821. this.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(col.getItems(data));
  2822. if (col.maxDrop) combo.maxDropDownItems(col.maxDrop);
  2823. };
  2824. return new ComboCellType();
  2825. },
  2826. /**
  2827. * 获取 日期选择 CellType
  2828. * @returns {DatePickerCellType}
  2829. */
  2830. getDatePickerCellType: function () {
  2831. let datepicker;
  2832. const DatePickerCellType = function () {};
  2833. DatePickerCellType.prototype = new spreadNS.CellTypes.Text();
  2834. const proto = DatePickerCellType.prototype;
  2835. proto.createEditorElement = function (context) {
  2836. return document.createElement("input");
  2837. };
  2838. proto.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  2839. const self = this;
  2840. if (editorContext) {
  2841. const $editor = $(editorContext);
  2842. spreadNS.CellTypes.Text.prototype.activateEditor.apply(this, arguments);
  2843. $editor.css("position", "absolute");
  2844. datepicker = $editor.datepicker({
  2845. language: 'zh',
  2846. dateFormat: 'yyyy-MM-DD',
  2847. autoClose: true,
  2848. }).data('datepicker');
  2849. datepicker.show();
  2850. }
  2851. };
  2852. proto.deactivateEditor = function (editorContext, context) {
  2853. datepicker.hide();
  2854. datepicker.destroy();
  2855. spreadNS.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments);
  2856. };
  2857. proto.setEditorValue = function (editor, value, context) {
  2858. if (datepicker) {
  2859. datepicker.selectDate(value);
  2860. }
  2861. };
  2862. proto.getEditorValue = function (editor, context) {
  2863. return datepicker ? datepicker.focused : null;
  2864. };
  2865. proto.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  2866. if (editorContext) {
  2867. const $editor = $(editorContext);
  2868. $editor.css("width", cellRect.width + 1);
  2869. $editor.css("height", cellRect.height + 3);
  2870. }
  2871. };
  2872. return new DatePickerCellType();
  2873. },
  2874. /**
  2875. * 获取 鼠标移入变手势 CellType
  2876. * @returns {DatePickerCellType}
  2877. */
  2878. getMouseTouchCellType: function () {
  2879. const mouseTouchCellType = function () {};
  2880. mouseTouchCellType.prototype = new spreadNS.CellTypes.Base();
  2881. mouseTouchCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2882. var xm = cellRect.x + cellRect.width / 2,
  2883. ym = cellRect.y + cellRect.height / 2,
  2884. size = 10;
  2885. var info = { x: x, y: y, row: context.row, col: context.col, cellRect: cellRect, sheetArea: context.sheetArea };
  2886. if (xm - size <= x && x <= xm + size && ym - size <= y && y <= ym + size) {
  2887. info.isReservedLocation = true;
  2888. }
  2889. return info;
  2890. };
  2891. mouseTouchCellType.prototype.processMouseMove = function (hitInfo) {
  2892. var sheet = hitInfo.sheet;
  2893. var div = sheet.getParent().getHost();
  2894. var canvasId = div.id + "vp_vp";
  2895. var canvas = $("#"+canvasId)[0];
  2896. if (sheet && hitInfo.isReservedLocation) {
  2897. canvas.style.cursor='pointer';
  2898. return true;
  2899. }else{
  2900. canvas.style.cursor='default';
  2901. }
  2902. return false;
  2903. };
  2904. return new mouseTouchCellType();
  2905. },
  2906. getStackedBarCellType: function () {
  2907. const stackedBarCellType = function(){};
  2908. stackedBarCellType.prototype = new spreadNS.CellTypes.Text();
  2909. const indent = 3, defaultR = 0.05, minHeight = 2;
  2910. const tipsMaxHintWidth = 200, tipsIndent = 15, tipsBorderIndent = 10;
  2911. /**
  2912. * 画一个长条
  2913. * @param {Object} canvas - 画布
  2914. * @param {Object} rect - 方框区域
  2915. * @param {String} lineColor - 画线颜色
  2916. * @param {String} fillColor - 填充颜色
  2917. */
  2918. // roundRect方法在Chrome99版本以后才支持
  2919. const drawBarBefore99 = function (canvas, x, y, w, h, r, color) {
  2920. const rArr = r instanceof Array ? r : [r, r, r, r];
  2921. canvas.save();
  2922. // 设置偏移量
  2923. canvas.translate(0.5, 0.5);
  2924. canvas.strokeStyle = color;
  2925. canvas.beginPath();
  2926. canvas.moveTo(x+rArr[0], y);
  2927. canvas.arcTo(x+w, y, x+w, y+h, Math.min(rArr[1], w));
  2928. canvas.arcTo(x+w, y+h, x, y+h, Math.min(rArr[2], w));
  2929. canvas.arcTo(x, y+h, x, y, Math.min(rArr[3], w));
  2930. canvas.arcTo(x, y, x+w, y, Math.min(rArr[0], w));
  2931. canvas.stroke();
  2932. canvas.fillStyle = color;
  2933. canvas.fill();
  2934. canvas.restore();
  2935. };
  2936. const drawBar = function (canvas, x, y, w, h, r, color) {
  2937. canvas.save();
  2938. // 设置偏移量
  2939. canvas.translate(0.5, 0.5);
  2940. canvas.strokeStyle = color;
  2941. canvas.beginPath();
  2942. canvas.roundRect(x, y, w, h, r);
  2943. canvas.stroke();
  2944. canvas.fillStyle = color;
  2945. canvas.fill();
  2946. canvas.restore();
  2947. };
  2948. const drawSB = function (canvas, x, y, w, h, r, color) {
  2949. canvas.save();
  2950. // 设置偏移量
  2951. canvas.translate(0.5, 0.5);
  2952. canvas.strokeStyle = color;
  2953. canvas.beginPath();
  2954. canvas.moveTo(x, y);
  2955. canvas.lineTo(x+w, y);
  2956. if (w > r) {
  2957. canvas.arcTo(x+w, y+h, x, y+h, r);
  2958. canvas.lineTo(x, y+h);
  2959. } else {
  2960. canvas.arcTo(x+w, y+h-r+w, x, y+h-r+w, Math.min(r, w));
  2961. canvas.lineTo(x, y+h-r+w);
  2962. }
  2963. //canvas.arcTo(x+w, y+h, x, y+h, Math.min(r, w));
  2964. //canvas.lineTo(x, y+h);
  2965. canvas.lineTo(x, y);
  2966. canvas.stroke();
  2967. canvas.fillStyle = color;
  2968. canvas.fill();
  2969. canvas.restore();
  2970. };
  2971. const drawText = function (canvas, x, y, w, h, text) {
  2972. // if (w < 33) return; // 33为100%的长度
  2973. const textWidth = canvas.measureText(text).width;
  2974. if (textWidth > w - 4) return;
  2975. const tx = x + w - textWidth - 2;
  2976. const ty = y + h - 2;
  2977. canvas.textAlign = 'left';
  2978. canvas.fillText(text, tx, ty);
  2979. };
  2980. const proto = stackedBarCellType.prototype;
  2981. /**
  2982. * 绘制方法
  2983. * @param {Object} canvas - 画布
  2984. * @param value - cell.value
  2985. * @param {Number} x - 单元格左顶点坐标 x
  2986. * @param {Number} y - 单元格左顶点坐标 y
  2987. * @param {Number} w - 单元格宽度
  2988. * @param {Number} h - 单元格高度
  2989. * @param {Object} style - cell.style
  2990. * @param {Object} options
  2991. */
  2992. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2993. // 清理 画布--单元格范围 旧数据
  2994. canvas.save();
  2995. canvas.fillStyle = style.backColor || 'white';
  2996. canvas.fillRect(x, y, w, h);
  2997. canvas.restore();
  2998. const col = options.sheet.zh_setting.cols[options.col];
  2999. const barData = value || [];
  3000. const left = x + indent;
  3001. const startTop = y + indent;
  3002. const validHeight = h - indent - indent - (col.stackedBarCover || barData.length === 1 ? 1 : barData.length / 2 + 1);
  3003. const height = col.stackedBarCover || barData.length === 0 ? validHeight : Math.max(validHeight/barData.length, minHeight);
  3004. const validWidth = w - indent - indent - 1;
  3005. for (const [i, bd] of barData.entries()) {
  3006. let width = ZhCalc.mul(validWidth, bd.percent, 2);
  3007. if (width < defaultR) continue;
  3008. let percent = ZhCalc.mul(bd.percent, 100, 0);
  3009. const hintText = bd.hint || (percent ? percent + '%' : '');
  3010. const hintNum = bd.hintNum !== undefined ? bd.hintNum : (col.hintNum || false);
  3011. const top = col.stackedBarCover ? startTop : startTop + height * i + i - 1;
  3012. //drawBarBefore99(canvas, left, top, width, height, ZhCalc.mul(height, defaultR, 2), bd.color);
  3013. //drawBarBefore99(canvas, left, top, width, height, [0, 0, ZhCalc.div(height, 2, 2), 0], bd.color);
  3014. if (col.stackedBarCover) {
  3015. drawSB(canvas, left, top, width, height, ZhCalc.div(height, 2, 2), bd.color);
  3016. if (hintNum && hintText) drawText(canvas, left, top, width, height, hintText);
  3017. } else {
  3018. drawBarBefore99(canvas, left, top, width, height, ZhCalc.mul(height, defaultR, 2), bd.color);
  3019. if (hintNum && hintText) drawText(canvas, left, top, width, height, hintText);
  3020. }
  3021. }
  3022. };
  3023. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  3024. const xs = hitinfo.sheet.getParent().xs;
  3025. const ctx = xs.childNodes[0].getContext("2d");
  3026. if (font && font !== '') {
  3027. ctx.font = font;
  3028. } else {
  3029. ctx.font = hitinfo.cellStyle.font;
  3030. }
  3031. return ctx.measureText(str).width;
  3032. };
  3033. proto.showTip = function (hitinfo, text) {
  3034. return text && text !== '';
  3035. };
  3036. /**
  3037. * 获取点击信息
  3038. * @param {Number} x
  3039. * @param {Number} y
  3040. * @param {Object} cellStyle
  3041. * @param {Object} cellRect
  3042. * @param {Object} context
  3043. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  3044. */
  3045. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  3046. return {
  3047. x: x,
  3048. y: y,
  3049. row: context.row,
  3050. col: context.col,
  3051. cellStyle: cellStyle,
  3052. cellRect: cellRect,
  3053. sheet: context.sheet,
  3054. sheetArea: context.sheetArea,
  3055. ctx: context.sheet.getParent().xs,
  3056. };
  3057. };
  3058. /**
  3059. * 鼠标进入单元格事件 - 显示悬浮提示
  3060. * @param {Object} hitinfo - 见getHitInfo返回值
  3061. */
  3062. proto.processMouseEnter = function (hitinfo) {
  3063. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  3064. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3065. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  3066. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  3067. text = col.getTip(sortData[hitinfo.row]);
  3068. }
  3069. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  3070. if (pos && this.showTip(hitinfo, text)) {
  3071. if (!this._toolTipElement) {
  3072. let div = $('#autoTip')[0];
  3073. if (!div) {
  3074. div = document.createElement("div");
  3075. $(div).css("position", "absolute")
  3076. .css("border", "1px #C0C0C0 solid")
  3077. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  3078. .css("font", "9pt Arial")
  3079. .css("background", "white")
  3080. .css("padding", 5)
  3081. .css("z-index", 999)
  3082. .css("max-width", tipsMaxHintWidth)
  3083. .css("word-wrap", "break-word")
  3084. .attr("id", 'autoTip');
  3085. document.body.insertBefore(div, null);
  3086. }
  3087. const validWidth = $(window).width() - (pos.x + hitinfo.x + tipsIndent) - tipsBorderIndent;
  3088. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  3089. if (validWidth >= tipsMaxHintWidth || textWidth <= validWidth) {
  3090. $(div).html(text).css("top", pos.y + hitinfo.y + tipsIndent).css("left", pos.x + hitinfo.x + tipsIndent);
  3091. } else if (textWidth > tipsMaxHintWidth) {
  3092. $(div).html(text).css("top", pos.y + hitinfo.y + tipsIndent).css("left", pos.x + hitinfo.x - tipsIndent - tipsMaxHintWidth);
  3093. } else {
  3094. $(div).html(text).css("top", pos.y + hitinfo.y + tipsIndent).css("left", pos.x + hitinfo.x - tipsIndent - textWidth);
  3095. }
  3096. this._toolTipElement = div;
  3097. $(div).show("fast");
  3098. }
  3099. }
  3100. };
  3101. /**
  3102. * 鼠标移出单元格事件 - 隐藏悬浮提示
  3103. * @param {Object} hitinfo - 见getHitInfo返回值
  3104. */
  3105. proto.processMouseLeave = function (hitinfo) {
  3106. if (this._toolTipElement) {
  3107. $(this._toolTipElement).hide();
  3108. this._toolTipElement = null;
  3109. }
  3110. };
  3111. return new stackedBarCellType();
  3112. },
  3113. getSignalCheckboxCellType: function () {
  3114. const SignalCheckboxCellType = function () {};
  3115. SignalCheckboxCellType.prototype = new spreadNS.CellTypes.CheckBox();
  3116. const proto = SignalCheckboxCellType.prototype;
  3117. proto.showCheckBox = function (sheet, iRow, iCol) {
  3118. const col = sheet.zh_setting.cols[iCol];
  3119. if (col.show && Object.prototype.toString.apply(col.show) === "[object Function]") {
  3120. const sortData = SpreadJsObj.getSortData(sheet);
  3121. const data = sortData ? sortData[iRow] : null;
  3122. return data ? col.show(data, col) : false;
  3123. } else {
  3124. return col.show !== undefined && col.show !== null ? col.show : true ;
  3125. }
  3126. };
  3127. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  3128. if (this.showCheckBox(options.sheet, options.row, options.col)) {
  3129. spreadNS.CellTypes.CheckBox.prototype.paintValue.apply(this, arguments);
  3130. } else {
  3131. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, [ctx, '', x, y, w, h, style, options]);
  3132. }
  3133. };
  3134. // proto.paint = function (canvas, value, x, y, w, h, style, options) {
  3135. // if (this.showCheckBox(options.sheet, options.row, options.col)) {
  3136. // spreadNS.CellTypes.CheckBox.prototype.paintContent.apply(this, [canvas, value, x, y, w, h, style, options]);
  3137. // } else {
  3138. // spreadNS.CellTypes.Base.prototype.paintContent.apply(this, [canvas, value, x, y, w, h, style, options]);
  3139. // }
  3140. // };
  3141. return new SignalCheckboxCellType();
  3142. },
  3143. },
  3144. RowHeader: {
  3145. getTagRowHeader: function (setting) {
  3146. const indent = setting.indent || 18, maxHintWidth = 200, borderIndent = 10;
  3147. const height = setting.tagSize ? 11 * setting.tagSize : 11, width = setting.tagSize ? 10 * setting.tagSize : 10;
  3148. const tagFont = setting.tagFont || '9px 微软雅黑';
  3149. const drawTag = function (canvas, x, y, fillColor) {
  3150. canvas.save();
  3151. // 设置偏移量
  3152. canvas.translate(0.5, 0.5);
  3153. canvas.beginPath();
  3154. canvas.moveTo(x, y);
  3155. canvas.lineTo(x, y+height/11*5);
  3156. canvas.lineTo(x+width/10*5.5, y+height);
  3157. canvas.lineTo(x+width, y+height-width/10*4.5);
  3158. canvas.lineTo(x+width/11*5, y);
  3159. canvas.lineTo(x, y);
  3160. canvas.stroke();
  3161. canvas.fillStyle = fillColor instanceof Array ? fillColor[0] : fillColor;
  3162. canvas.fill();
  3163. canvas.beginPath();
  3164. canvas.arc(x+2, y+2, 1, 0, Math.PI*2, true);
  3165. canvas.closePath();
  3166. canvas.fillStyle = 'white';
  3167. canvas.fill();
  3168. if (fillColor instanceof Array && fillColor.length > 1) {
  3169. canvas.fillStyle = '#444444';
  3170. canvas.font = tagFont;
  3171. canvas.fillText(fillColor.length, x+width/11*13, y+height/11*14);
  3172. }
  3173. canvas.restore();
  3174. };
  3175. let TagCellType = function (){};
  3176. TagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  3177. const proto = TagCellType.prototype;
  3178. proto.getTagColor = setting.getColor;
  3179. proto.getTagHtml = setting.getTagHtml;
  3180. proto.basePaint = proto.paint;
  3181. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  3182. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  3183. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - indent, h, style, options]);
  3184. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  3185. const color = this.getTagColor(options.row, node);
  3186. const centerX = x + w - indent + height/2, centerY = y + h/2;
  3187. color && drawTag(canvas, centerX - height/2, centerY - width/2, color);
  3188. };
  3189. /**
  3190. * 获取点击信息
  3191. * @param {Number} x
  3192. * @param {Number} y
  3193. * @param {Object} cellStyle
  3194. * @param {Object} cellRect
  3195. * @param {Object} context
  3196. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  3197. */
  3198. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  3199. return {
  3200. x: x,
  3201. y: y,
  3202. row: context.row,
  3203. col: context.col,
  3204. cellStyle: cellStyle,
  3205. cellRect: cellRect,
  3206. sheet: context.sheet,
  3207. sheetArea: context.sheetArea,
  3208. ctx: context.sheet.getParent().xs,
  3209. };
  3210. };
  3211. /**
  3212. * 鼠标进入单元格事件 - 显示悬浮提示
  3213. * @param {Object} hitinfo - 见getHitInfo返回值
  3214. */
  3215. proto.processMouseEnter = function (hitinfo) {
  3216. const node = SpreadJsObj.getRowObject(hitinfo.sheet, hitinfo.row);
  3217. let html = this.getTagHtml(hitinfo.row, node);
  3218. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  3219. if (pos && html) {
  3220. if (!this._tagTipElement) {
  3221. let div = $('#autoTip')[0];
  3222. if (!div) {
  3223. div = document.createElement("div");
  3224. $(div).css("position", "absolute")
  3225. .css("border", "1px #C0C0C0 solid")
  3226. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  3227. .css("font", "9pt Arial")
  3228. .css("background", "white")
  3229. .css("padding", 5)
  3230. .css("width", maxHintWidth)
  3231. .css("z-index", 999)
  3232. .attr("id", 'autoTagTip')
  3233. .css("top", pos.y + hitinfo.y + indent)
  3234. .css("left", pos.x + hitinfo.x + indent)
  3235. .html(html);
  3236. document.body.insertBefore(div, null);
  3237. }
  3238. this._tagTipElement = div;
  3239. $(div).show("fast");
  3240. }
  3241. }
  3242. };
  3243. /**
  3244. * 鼠标移出单元格事件 - 隐藏悬浮提示
  3245. * @param {Object} hitinfo - 见getHitInfo返回值
  3246. */
  3247. proto.processMouseLeave = function (hitinfo) {
  3248. if (this._tagTipElement) {
  3249. $(this._tagTipElement).hide().remove();
  3250. this._tagTipElement = null;
  3251. }
  3252. };
  3253. return new TagCellType();
  3254. },
  3255. getCircleTagRowHeader: function (setting) {
  3256. const drawCircle = function (canvas, x, y, r, fillColor) {
  3257. canvas.save();
  3258. canvas.beginPath();
  3259. canvas.arc(x, y, r, 0, Math.PI*2, true);
  3260. canvas.closePath();
  3261. canvas.fillStyle = fillColor;
  3262. canvas.fill();
  3263. canvas.restore();
  3264. };
  3265. const drawCircle2 = function (canvas, x, y, r, lineColor, fillColor) {
  3266. canvas.save();
  3267. canvas.beginPath();
  3268. canvas.arc(x, y, r, 0, Math.PI*2, true);
  3269. canvas.closePath();
  3270. canvas.fillStyle = lineColor;
  3271. canvas.fill();
  3272. canvas.beginPath();
  3273. canvas.arc(x, y, r-2, 0, Math.PI*2, true);
  3274. canvas.closePath();
  3275. canvas.fillStyle = fillColor;
  3276. canvas.fill();
  3277. canvas.restore();
  3278. };
  3279. let CircleTagCellType = function (){};
  3280. CircleTagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  3281. const proto = CircleTagCellType.prototype;
  3282. proto.indent = setting.indent || 16;
  3283. proto.size = setting.size || 6;
  3284. proto.getTagColor = setting.getColor;
  3285. proto.basePaint = proto.paint;
  3286. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  3287. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  3288. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - this.indent, h, style, options]);
  3289. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  3290. let backColor = style.backColor;
  3291. if (!backColor) {
  3292. const tag = options.sheet.getTag(options.row, options.col, options.sheetArea);
  3293. if (tag && tag === 'hover') backColor = '#dddfe1';
  3294. if (!backColor) {
  3295. let sel = options.sheet.getSelections();
  3296. sel = sel ? sel[0] : null;
  3297. backColor = (sel && options.row >= sel.row && options.row < sel.row + sel.rowCount ? '#dddfe1' : '#e9ecef');
  3298. }
  3299. }
  3300. let color = this.getTagColor(options.row, node);
  3301. color = color instanceof Array ? color : [color];
  3302. for (let i = color.length - 1; i >= 0; i--) {
  3303. drawCircle2(canvas, x + w - this.indent + 5 + i*5 , y + h/2, this.size, backColor, color[i]);
  3304. }
  3305. };
  3306. /**
  3307. * 获取点击信息
  3308. * @param {Number} x
  3309. * @param {Number} y
  3310. * @param {Object} cellStyle
  3311. * @param {Object} cellRect
  3312. * @param {Object} context
  3313. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  3314. */
  3315. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  3316. return {
  3317. x: x,
  3318. y: y,
  3319. row: context.row,
  3320. col: context.col,
  3321. cellStyle: cellStyle,
  3322. cellRect: cellRect,
  3323. sheet: context.sheet,
  3324. sheetArea: context.sheetArea,
  3325. ctx: context.sheet.getParent().xs,
  3326. };
  3327. };
  3328. /**
  3329. * 鼠标进入单元格事件 - 显示悬浮提示
  3330. * @param {Object} hitinfo - 见getHitInfo返回值
  3331. */
  3332. proto.processMouseEnter = function (hitinfo) {
  3333. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, 'hover', hitinfo.sheetArea);
  3334. };
  3335. /**
  3336. * 鼠标移出单元格事件 - 隐藏悬浮提示
  3337. * @param {Object} hitinfo - 见getHitInfo返回值
  3338. */
  3339. proto.processMouseLeave = function (hitinfo) {
  3340. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, undefined, hitinfo.sheetArea);
  3341. };
  3342. return new CircleTagCellType();
  3343. },
  3344. },
  3345. ColHeader: {
  3346. getTipHeader: function () {
  3347. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  3348. const TipCellType = function () {};
  3349. // 继承 SpreadJs定义的 普通的TextCellType
  3350. TipCellType.prototype = new spreadNS.CellTypes.ColumnHeader();
  3351. const proto = TipCellType.prototype;
  3352. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  3353. const xs = hitinfo.sheet.getParent().xs;
  3354. const ctx = xs.childNodes[0].getContext("2d");
  3355. if (font && font !== '') {
  3356. ctx.font = font;
  3357. } else {
  3358. ctx.font = hitinfo.cellStyle.font;
  3359. }
  3360. return ctx.measureText(str).width;
  3361. };
  3362. proto.showTip = function (hitinfo, text) {
  3363. return text && text !== '';
  3364. };
  3365. /**
  3366. * 获取点击信息
  3367. * @param {Number} x
  3368. * @param {Number} y
  3369. * @param {Object} cellStyle
  3370. * @param {Object} cellRect
  3371. * @param {Object} context
  3372. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  3373. */
  3374. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  3375. return {
  3376. x: x,
  3377. y: y,
  3378. row: context.row,
  3379. col: context.col,
  3380. cellStyle: cellStyle,
  3381. cellRect: cellRect,
  3382. sheet: context.sheet,
  3383. sheetArea: context.sheetArea,
  3384. ctx: context.sheet.getParent().xs,
  3385. };
  3386. };
  3387. /**
  3388. * 鼠标进入单元格事件 - 显示悬浮提示
  3389. * @param {Object} hitinfo - 见getHitInfo返回值
  3390. */
  3391. proto.processMouseEnter = function (hitinfo) {
  3392. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  3393. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3394. if (col.getHeaderTip && Object.prototype.toString.apply(col.getHeaderTip) === "[object Function]") {
  3395. text = col.getHeaderTip();
  3396. }
  3397. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  3398. if (pos && this.showTip(hitinfo, text)) {
  3399. if (!this._toolTipElement) {
  3400. let div = $('#autoTip')[0];
  3401. if (!div) {
  3402. div = document.createElement("div");
  3403. $(div).css("position", "absolute")
  3404. .css("border", "1px #C0C0C0 solid")
  3405. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  3406. .css("font", "9pt Arial")
  3407. .css("background", "white")
  3408. .css("padding", 5)
  3409. .css("z-index", 999)
  3410. .css("max-width", maxHintWidth)
  3411. .css("word-wrap", "break-word")
  3412. .attr("id", 'autoTip');
  3413. document.body.insertBefore(div, null);
  3414. }
  3415. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  3416. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  3417. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  3418. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  3419. } else if (textWidth > maxHintWidth) {
  3420. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  3421. } else {
  3422. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  3423. }
  3424. this._toolTipElement = div;
  3425. $(div).show("fast");
  3426. }
  3427. }
  3428. };
  3429. /**
  3430. * 鼠标移出单元格事件 - 隐藏悬浮提示
  3431. * @param {Object} hitinfo - 见getHitInfo返回值
  3432. */
  3433. proto.processMouseLeave = function (hitinfo) {
  3434. if (this._toolTipElement) {
  3435. $(this._toolTipElement).hide();
  3436. this._toolTipElement = null;
  3437. }
  3438. };
  3439. return new TipCellType();
  3440. },
  3441. getButtonHeader: function() {
  3442. const hover = 1, active = 2;
  3443. const ImageButtonCellType = function (){};
  3444. ImageButtonCellType.prototype = new spreadNS.CellTypes.ColumnHeader();
  3445. const getIndent = function(col) {
  3446. return col.headerIndent ? col.headerIndent : 3;
  3447. };
  3448. const proto = ImageButtonCellType.prototype;
  3449. proto.getImage = function (sheet, iRow, iCol) {
  3450. const col = sheet.zh_setting.cols[iCol];
  3451. let imgSource = col.headerNormalImg;
  3452. const cell = sheet.getCell(iRow, iCol, spreadNS.SheetArea.colHeader), tag = cell.tag();
  3453. if (tag === active) {
  3454. imgSource = col.headerActiveImg ? col.headerActiveImg : (col.headerHoverImg ? col.headerHoverImg : col.headerNormalImg);
  3455. } else if (tag === hover) {
  3456. imgSource = col.headerHoverImg ? col.headerHoverImg : col.headerNormalImg;
  3457. }
  3458. return $(imgSource)[0] ? $(imgSource)[0] : null;
  3459. };
  3460. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  3461. // Drawing Text
  3462. const col = options.sheet.zh_setting.cols[options.col];
  3463. if (col.headerButtonPos === 'below') {
  3464. spreadNS.CellTypes.ColumnHeader.prototype.paint.apply(this, [canvas, value+'\n', x, y, w, h, style, options]);
  3465. } else {
  3466. spreadNS.CellTypes.ColumnHeader.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  3467. }
  3468. const img = this.getImage(options.sheet, options.row, options.col);
  3469. const indent = getIndent(col);
  3470. if (img) {
  3471. if (col.headerButtonPos === 'below') {
  3472. canvas.drawImage(img, x + w / 2 - img.width / 2, y + h / 2 + indent);
  3473. } else {
  3474. canvas.drawImage(img, x + w - indent - img.width - indent, y + (h - img.height) / 2);
  3475. }
  3476. }
  3477. };
  3478. /**
  3479. * 获取点击信息
  3480. * @param {Number} x
  3481. * @param {Number} y
  3482. * @param {Object} cellStyle
  3483. * @param {Object} cellRect
  3484. * @param {Object} context
  3485. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  3486. */
  3487. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  3488. return {
  3489. x: x,
  3490. y: y,
  3491. row: context.row,
  3492. col: context.col,
  3493. cellStyle: cellStyle,
  3494. cellRect: cellRect,
  3495. sheet: context.sheet,
  3496. sheetArea: context.sheetArea
  3497. };
  3498. };
  3499. proto.processMouseEnter = function (hitinfo) {
  3500. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3501. // Drawing Image
  3502. if (col.hoverImg) {
  3503. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3504. cell.tag(hover);
  3505. hitinfo.sheet.repaint(hitinfo.cellRect);
  3506. }
  3507. };
  3508. proto.processMouseLeave = function (hitinfo) {
  3509. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3510. // Drawing Image
  3511. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3512. cell.tag(null);
  3513. hitinfo.sheet.repaint(hitinfo.cellRect);
  3514. };
  3515. proto.processMouseDown = function (hitinfo) {
  3516. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3517. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  3518. const halfX = img.width / 2, halfY = img.height / 2;
  3519. const indent = getIndent(col);
  3520. const centerX = col.headerButtonPos === 'below'
  3521. ? hitinfo.cellRect.x + hitinfo.cellRect.width / 2
  3522. : hitinfo.cellRect.x + hitinfo.cellRect.width - indent - halfX - indent;
  3523. const centerY = col.headerButtonPos === 'below'
  3524. ? hitinfo.cellRect.y + hitinfo.cellRect.height / 2 + halfY + indent
  3525. : hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  3526. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  3527. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3528. cell.tag(active);
  3529. hitinfo.sheet.repaint(hitinfo.cellRect);
  3530. }
  3531. };
  3532. proto.processMouseUp = function (hitinfo) {
  3533. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3534. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  3535. const halfX = img.width / 2, halfY = img.height / 2;
  3536. const indent = getIndent(col);
  3537. const centerX = col.headerButtonPos === 'below'
  3538. ? hitinfo.cellRect.x + hitinfo.cellRect.width / 2
  3539. : hitinfo.cellRect.x + hitinfo.cellRect.width - indent - halfX - indent;
  3540. const centerY = col.headerButtonPos === 'below'
  3541. ? hitinfo.cellRect.y + hitinfo.cellRect.height / 2 + halfY + indent
  3542. : hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  3543. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  3544. const imageClick = col.headerButtonClick;
  3545. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  3546. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  3547. const data = sortData ? sortData[hitinfo.row] : null;
  3548. imageClick(data, hitinfo);
  3549. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3550. cell.tag(null);
  3551. hitinfo.sheet.repaint(hitinfo.cellRect);
  3552. }
  3553. }
  3554. };
  3555. proto.processMouseMove = function (hitinfo) {
  3556. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  3557. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  3558. const halfX = img.width / 2, halfY = img.height / 2;
  3559. const indent = getIndent(col);
  3560. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  3561. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  3562. : hitinfo.cellRect.x + indent + halfX;
  3563. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  3564. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col, spreadNS.SheetArea.colHeader);
  3565. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  3566. if (cell.tag() !== hover) {
  3567. cell.tag(hover);
  3568. hitinfo.sheet.repaint(hitinfo.cellRect);
  3569. }
  3570. } else {
  3571. if (cell.tag() === hover) {
  3572. cell.tag(null);
  3573. hitinfo.sheet.repaint(hitinfo.cellRect);
  3574. }
  3575. }
  3576. };
  3577. return new ImageButtonCellType();
  3578. }
  3579. },
  3580. Formatter: {
  3581. baseNumberFormatter: function () {
  3582. const formatter = function () {};
  3583. formatter.prototype = new GC.Spread.Formatter.FormatterBase();
  3584. const proto = formatter.prototype;
  3585. /**
  3586. * 格式化数字显示方式
  3587. * 用法
  3588. * formatNumber(12345.999,'#,##0.00');
  3589. * formatNumber(12345.999,'#,##0.##');
  3590. * formatNumber(123,'000000');
  3591. * @param num
  3592. * @param pattern
  3593. */
  3594. proto.formatNum = function (num, pattern) {
  3595. var minusStr = num < 0 ? '-' : '';
  3596. var strarr = num ? Math.abs(num).toString().split('.'):['0'];
  3597. var fmtarr = pattern?pattern.split('.'):[''];
  3598. var retstr='';
  3599. // 整数部分
  3600. var str = strarr[0];
  3601. var fmt = fmtarr[0];
  3602. var i = str.length-1;
  3603. var comma = false;
  3604. for(var f=fmt.length-1;f>=0;f--){
  3605. switch(fmt.substr(f,1)){
  3606. case '#':
  3607. if(i>=0 ) retstr = str.substr(i--,1) + retstr;
  3608. break;
  3609. case '0':
  3610. if(i>=0) retstr = str.substr(i--,1) + retstr;
  3611. else retstr = '0' + retstr;
  3612. break;
  3613. case ',':
  3614. comma = true;
  3615. retstr=','+retstr;
  3616. break;
  3617. }
  3618. }
  3619. if(i>=0){
  3620. if(comma){
  3621. var l = str.length;
  3622. for(;i>=0;i--){
  3623. retstr = str.substr(i,1) + retstr;
  3624. if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;
  3625. }
  3626. }
  3627. else retstr = str.substr(0,i+1) + retstr;
  3628. }
  3629. retstr = retstr+'.';
  3630. // 处理小数部分
  3631. str=strarr.length>1?strarr[1]:'';
  3632. fmt=fmtarr.length>1?fmtarr[1]:'';
  3633. i=0;
  3634. for(var f=0;f<fmt.length;f++){
  3635. switch(fmt.substr(f,1)){
  3636. case '#':
  3637. if(i<str.length) retstr+=str.substr(i++,1);
  3638. break;
  3639. case '0':
  3640. if(i<str.length) retstr+= str.substr(i++,1);
  3641. else retstr+='0';
  3642. break;
  3643. }
  3644. }
  3645. return minusStr + retstr.replace(/^,+/,'').replace(/\.$/,'');
  3646. };
  3647. proto.format = function (obj, formattedData) {
  3648. if (this.pattern) {
  3649. return this.formatNum(obj, this.pattern);
  3650. } else {
  3651. return obj;
  3652. }
  3653. };
  3654. return new formatter();
  3655. },
  3656. _numFormatter: [],
  3657. getNumberFormatter(pattern) {
  3658. let formatter = this._numFormatter.find(function (f) {
  3659. return f.pattern === pattern;
  3660. });
  3661. if (!formatter) {
  3662. formatter = this.baseNumberFormatter();
  3663. formatter.pattern = pattern;
  3664. this._numFormatter.push(formatter);
  3665. }
  3666. return formatter;
  3667. },
  3668. },
  3669. Clipboard: (function () {
  3670. let cText = '', cHtml;
  3671. const setCopyData = function (text, html) {
  3672. cText = text;
  3673. cHtml = html;
  3674. };
  3675. const getPasteData = function () {
  3676. return {text: cText, html: cHtml};
  3677. };
  3678. const clearData = function () {
  3679. cText = '';
  3680. cHtml = undefined;
  3681. };
  3682. const setSheetFilterCopyData = function (sheet) {
  3683. cText = SpreadJsObj.getFilterCopyText(sheet);
  3684. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  3685. };
  3686. const getAnalysisPasteText = function () {
  3687. return SpreadJsObj.analysisPasteText(cText);
  3688. };
  3689. const getAnalysisPasteHtml = function () {
  3690. return SpreadJsObj.analysisPasteHtml(cHtml);
  3691. };
  3692. const setSysClipboard = function(text) {
  3693. let textArea = document.createElement("textarea");
  3694. textArea.style.position = 'fixed';
  3695. textArea.style.top = 0;
  3696. textArea.style.left = 0;
  3697. textArea.style.width = '2em';
  3698. textArea.style.height = '2em';
  3699. textArea.style.padding = 0;
  3700. textArea.style.border = 'none';
  3701. textArea.style.outline = 'none';
  3702. textArea.style.boxShadow = 'none';
  3703. textArea.style.background = 'transparent';
  3704. textArea.value = text;
  3705. document.body.appendChild(textArea);
  3706. textArea.select();
  3707. try {
  3708. document.execCommand('copy');
  3709. } catch (err) {
  3710. }
  3711. document.body.removeChild(textArea);
  3712. };
  3713. return {
  3714. setCopyData, getPasteData, clearData,
  3715. setSheetFilterCopyData,
  3716. getAnalysisPasteText, getAnalysisPasteHtml,
  3717. setSysClipboard
  3718. }
  3719. })(),
  3720. };
  3721. const GridSpreadUtils = {
  3722. getDefaultSelectInfo: function (sheet, isMulti = true) {
  3723. const gridData = sheet.zh_data;
  3724. if (!gridData) return[gridData, null, -1, 0];
  3725. const sel = sheet.getSelections()[0];
  3726. const node = gridData[sel.row];
  3727. if (!node) return [gridData, node, sel.row, 0];
  3728. if (!isMulti || sel.rowCount === 1) return [gridData, node, sel.row, 1];
  3729. let count = 1;
  3730. for (let r = 1; r < sel.rowCount; r++) {
  3731. const rNode = gridData[sel.row + r];
  3732. if (!rNode) break;
  3733. count += 1;
  3734. }
  3735. return [gridData, node, sel.row, count];
  3736. },
  3737. };