spreadjs_zh.js 164 KB

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