spreadjs_zh.js 149 KB

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