spreadjs_zh.js 150 KB

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