spreadjs_zh.js 149 KB

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