spreadjs_zh.js 169 KB

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