spreadjs_zh.js 156 KB

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