spreadjs_zh.js 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221
  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. saveTopAndSelect: function (sheet, cacheKey) {
  1085. const sel = sheet.getSelections()[0];
  1086. const top = sheet.getViewportTopRow(1);
  1087. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  1088. },
  1089. loadTopAndSelect: function (sheet, cacheKey) {
  1090. let ts = getLocalCache(cacheKey);
  1091. if (ts !== '') {
  1092. ts = JSON.parse(ts);
  1093. if (ts === undefined || ts === null) return;
  1094. if (ts.sel) {
  1095. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  1096. SpreadJsObj.reloadRowsBackColor(sheet, [0, ts.sel.row]);
  1097. }
  1098. if (ts.top) {
  1099. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  1100. }
  1101. }
  1102. },
  1103. resetTopAndSelect: function (sheet) {
  1104. sheet.setSelection(0, 0, 1, 1);
  1105. sheet.showRow(0, spreadNS.VerticalPosition.top);
  1106. },
  1107. /**
  1108. * 获取当前选行的数据对象
  1109. * @param {GC.Spread.Sheets.Worksheet} sheet
  1110. * @returns {Object}
  1111. */
  1112. getSelectObject: function (sheet) {
  1113. if (!sheet) {
  1114. return null;
  1115. } else if (sheet.zh_dataType) {
  1116. const sel = sheet.getSelections()[0];
  1117. if (!sel) {
  1118. return null;
  1119. }
  1120. if (sheet.zh_dataType === this.DataType.Tree) {
  1121. return sheet.zh_tree.nodes[sel.row];
  1122. } else if (sheet.zh_dataType === this.DataType.Data) {
  1123. return sheet.zh_data[sel.row];
  1124. } else {
  1125. return null;
  1126. }
  1127. }
  1128. },
  1129. getRowObject: function (sheet, row) {
  1130. if (!sheet) {
  1131. return null;
  1132. } else if (sheet.zh_dataType) {
  1133. if (sheet.zh_dataType === this.DataType.Tree) {
  1134. return sheet.zh_tree.nodes[row];
  1135. } else if (sheet.zh_dataType === this.DataType.Data) {
  1136. return sheet.zh_data[row];
  1137. } else {
  1138. return null;
  1139. }
  1140. }
  1141. },
  1142. /**
  1143. * 刷新列显示
  1144. * @param sheet
  1145. */
  1146. refreshColumnVisible: function (sheet) {
  1147. if(sheet.zh_setting) {
  1148. sheet.zh_setting.cols.forEach(function (col, index) {
  1149. if (col.visible !== undefined && col.visible !== null) {
  1150. sheet.setColumnVisible(index, col.visible);
  1151. }
  1152. });
  1153. }
  1154. },
  1155. /**
  1156. * 刷新行显示
  1157. * @param sheet
  1158. */
  1159. refreshTreeRowVisible: function (sheet) {
  1160. this.beginMassOperation(sheet);
  1161. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  1162. for (const iRow in sortData) {
  1163. const node = sortData[iRow];
  1164. if (node.visible !== undefined && node.visible !== null) {
  1165. if (node.visible && node.waitingLoading) {
  1166. SpreadJsObj._loadRowData(sheet, node, parseInt(iRow));
  1167. }
  1168. sheet.setRowVisible(iRow, node.visible);
  1169. } else {
  1170. if (node.waitingLoading) {
  1171. this._loadRowData(sheet, node, parseInt(iRow));
  1172. }
  1173. sheet.setRowVisible(iRow, true);
  1174. }
  1175. }
  1176. this.endMassOperation(sheet);
  1177. },
  1178. refreshColumnAlign: function (sheet) {
  1179. if (sheet.zh_setting) {
  1180. for (const iCol in sheet.zh_setting.cols) {
  1181. const col = sheet.zh_setting.cols[iCol];
  1182. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  1183. }
  1184. }
  1185. },
  1186. refreshSheetReadOnly: function (sheet) {
  1187. this.beginMassOperation(sheet);
  1188. if (sheet.zh_setting) {
  1189. sheet.zh_setting.cols.forEach(function (col, i) {
  1190. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1191. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1192. }
  1193. });
  1194. }
  1195. this.endMassOperation(sheet);
  1196. },
  1197. /**
  1198. * 刷新列是否只读
  1199. * @param sheet
  1200. * @param field
  1201. * @param readonly
  1202. */
  1203. resetFieldReadOnly: function (sheet, field, readonly) {
  1204. const fields = field instanceof Array ? field : [field];
  1205. if (sheet.zh_setting) {
  1206. sheet.zh_setting.cols.forEach(function (col, i) {
  1207. if (fields.indexOf(col.field) !== -1) {
  1208. col.readOnly = readonly;
  1209. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1210. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1211. }
  1212. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1213. }
  1214. });
  1215. }
  1216. },
  1217. CellType: {
  1218. /**
  1219. * 获取树结构CellType
  1220. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  1221. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  1222. * @returns {TreeNodeCellType}
  1223. */
  1224. getTreeNodeCellType: function () {
  1225. const xOffset = 2 + 5;
  1226. const indent = 20;
  1227. const levelIndent = -5;
  1228. const halfBoxLength = 5;
  1229. const halfExpandLength = 3;
  1230. const lineColor = '#515151';//'#b8b8b8';
  1231. const expandBoxColor = '#434343';//'#808080';
  1232. const dotLine = true;
  1233. /**
  1234. * 画一条点线段
  1235. * @param canvas - 画布
  1236. * @param x1 - 线段起点 x
  1237. * @param y1 - 线段起点 y
  1238. * @param x2 - 线段终点 x
  1239. * @param y2 - 线段终点 y
  1240. * @param color - 线段颜色
  1241. */
  1242. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  1243. canvas.save();
  1244. // 设置偏移量
  1245. canvas.translate(0.5, 0.5);
  1246. canvas.beginPath();
  1247. canvas.strokeStyle = color;
  1248. canvas.dottedLine(x1, y1, x2, y2);
  1249. canvas.stroke();
  1250. canvas.restore();
  1251. };
  1252. /**
  1253. * 画一条线段
  1254. * @param canvas - 画布
  1255. * @param x1 - 线段起点 x
  1256. * @param y1 - 线段起点 y
  1257. * @param x2 - 线段终点 x
  1258. * @param y2 - 线段终点 y
  1259. * @param color - 线段颜色
  1260. */
  1261. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  1262. canvas.save();
  1263. // 设置偏移量
  1264. canvas.translate(0.5, 0.5);
  1265. canvas.beginPath();
  1266. canvas.moveTo(x1, y1);
  1267. canvas.lineTo(x2, y2);
  1268. canvas.strokeStyle = color;
  1269. canvas.stroke();
  1270. canvas.restore();
  1271. };
  1272. /**
  1273. * 画一个方框
  1274. * @param {Object} canvas - 画布
  1275. * @param {Object} rect - 方框区域
  1276. * @param {String} lineColor - 画线颜色
  1277. * @param {String} fillColor - 填充颜色
  1278. */
  1279. const drawBox = function (canvas, rect, lineColor, fillColor) {
  1280. canvas.save();
  1281. // 设置偏移量
  1282. canvas.translate(0.5, 0.5);
  1283. canvas.strokeStyle = lineColor;
  1284. canvas.beginPath();
  1285. canvas.moveTo(rect.left, rect.top);
  1286. canvas.lineTo(rect.left, rect.bottom);
  1287. canvas.lineTo(rect.right, rect.bottom);
  1288. canvas.lineTo(rect.right, rect.top);
  1289. canvas.lineTo(rect.left, rect.top);
  1290. canvas.stroke();
  1291. canvas.fillStyle = fillColor ? fillColor : 'white';
  1292. canvas.fill();
  1293. canvas.restore();
  1294. };
  1295. /**
  1296. * 画树结构-展开收起按钮
  1297. * @param {Object} canvas - 画布
  1298. * @param {Number} x - 单元格左顶点坐标 x
  1299. * @param {Number} y - 单元格左顶点坐标 y
  1300. * @param {Number} w - 单元格宽度
  1301. * @param {Number} h - 单元格高度
  1302. * @param {Number} centerX - 按钮中央坐标
  1303. * @param {Number} centerY - 按钮中央坐标
  1304. * @param {Boolean} expanded - 当前节点展开收起状态
  1305. */
  1306. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded, style) {
  1307. let rect = {
  1308. top: centerY - halfBoxLength,
  1309. bottom: centerY + halfBoxLength,
  1310. left: centerX - halfBoxLength,
  1311. right: centerX + halfBoxLength
  1312. };
  1313. let h1, h2, offset = 1;
  1314. if (rect.left < x + w) {
  1315. // 方框超出单元格宽度时,超出部分不画。
  1316. rect.right = Math.min(rect.right, x + w);
  1317. drawBox(canvas, rect, expandBoxColor, style.backColor);
  1318. // 画中心十字
  1319. // 画十字横线
  1320. h1 = centerX - halfExpandLength;
  1321. h2 = Math.min(centerX + halfExpandLength, x + w);
  1322. if (h2 > h1) {
  1323. drawLine(canvas, h1, centerY, h2, centerY, expandBoxColor);
  1324. }
  1325. // 画十字竖线
  1326. if (!expanded && (centerX < x + w)) {
  1327. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, expandBoxColor);
  1328. }
  1329. }
  1330. };
  1331. let TreeNodeCellType = function (){};
  1332. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  1333. const proto = TreeNodeCellType.prototype;
  1334. /**
  1335. * 绘制方法
  1336. * @param {Object} canvas - 画布
  1337. * @param value - cell.value
  1338. * @param {Number} x - 单元格左顶点坐标 x
  1339. * @param {Number} y - 单元格左顶点坐标 y
  1340. * @param {Number} w - 单元格宽度
  1341. * @param {Number} h - 单元格高度
  1342. * @param {Object} style - cell.style
  1343. * @param {Object} options
  1344. */
  1345. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1346. // 清理 画布--单元格范围 旧数据
  1347. if (style.backColor) {
  1348. canvas.save();
  1349. canvas.fillStyle = style.backColor;
  1350. canvas.fillRect(x, y, w, h);
  1351. canvas.restore();
  1352. } else {
  1353. // 不知道为什么在 概算投资-造价对比 下,这一句代码无法生效。
  1354. // canvas.clearRect(x, y, w, h);
  1355. canvas.save();
  1356. canvas.fillStyle = 'white';
  1357. canvas.fillRect(x, y, w, h);
  1358. canvas.restore();
  1359. }
  1360. const tree = options.sheet.zh_tree;
  1361. const setting = tree.setting;
  1362. // 使用TreeCellType前,需定义sheet.tree
  1363. if (tree) {
  1364. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  1365. if (node) {
  1366. const showTreeLine = true;
  1367. const centerX = Math.floor(x) + (node[setting.level] - 1) * indent + (node[setting.level]) * levelIndent + indent / 2 + xOffset;
  1368. const centerY = Math.floor((y + (y + h)) / 2);
  1369. // Draw Sibling Line
  1370. if (showTreeLine) {
  1371. // Draw Horizontal Line
  1372. if (centerX < x + w) {
  1373. const x1 = centerX + indent / 2;
  1374. if (dotLine) {
  1375. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1376. // drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, style.foreColor);
  1377. } else {
  1378. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1379. // drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, style.foreColor);
  1380. }
  1381. }
  1382. // Draw Vertical Line
  1383. if (centerX < x + w) {
  1384. const y1 = tree.isLastViewSibling(node) ? centerY : y + h;
  1385. const parent = tree.getParent(node);
  1386. const y2 = y1 - centerY;
  1387. if (node[setting.order] === 1 && !parent) {
  1388. if (dotLine) {
  1389. drawDotLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1390. // drawDotLine(canvas, centerX, centerY, centerX, y1, style.foreColor);
  1391. } else {
  1392. drawLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1393. // drawLine(canvas, centerX, centerY, centerX, y1, style.foreColor);
  1394. }
  1395. } else {
  1396. if (dotLine) {
  1397. drawDotLine(canvas, centerX, y, centerX, y1, lineColor);
  1398. // drawDotLine(canvas, centerX, y, centerX, y1, style.foreColor);
  1399. } else {
  1400. drawLine(canvas, centerX, y, centerX, y1, lineColor);
  1401. // drawLine(canvas, centerX, y, centerX, y1, style.foreColor);
  1402. }
  1403. }
  1404. }
  1405. }
  1406. // Draw Expand Box
  1407. if (!node[setting.isLeaf]) {
  1408. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded, style);
  1409. }
  1410. // Draw Parent Line
  1411. if (showTreeLine) {
  1412. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  1413. while (parent) {
  1414. if (!tree.isLastViewSibling(parent)) {
  1415. if (parentCenterX < x + w) {
  1416. if (dotLine) {
  1417. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1418. // drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, style.foreColor);
  1419. } else {
  1420. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1421. // drawLine(canvas, parentCenterX, y, parentCenterX, y + h, style.foreColor);
  1422. }
  1423. }
  1424. }
  1425. parent = tree.getParent(parent);
  1426. parentCenterX -= (indent + levelIndent);
  1427. }
  1428. };
  1429. // 重定位x
  1430. const move = (node[setting.level]) * indent + (node[setting.level]) * levelIndent + xOffset;
  1431. x = x + move;
  1432. w = w - move;
  1433. }
  1434. }
  1435. // Drawing Text
  1436. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1437. };
  1438. /**
  1439. * 获取点击信息
  1440. * @param {Number} x
  1441. * @param {Number} y
  1442. * @param {Object} cellStyle
  1443. * @param {Object} cellRect
  1444. * @param {Object} context
  1445. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1446. */
  1447. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1448. return {
  1449. x: x,
  1450. y: y,
  1451. row: context.row,
  1452. col: context.col,
  1453. cellStyle: cellStyle,
  1454. cellRect: cellRect,
  1455. sheet: context.sheet,
  1456. sheetArea: context.sheetArea
  1457. };
  1458. };
  1459. /**
  1460. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1461. * @param {Object} hitinfo - 见getHitInfo
  1462. */
  1463. proto.processMouseDown = function (hitinfo) {
  1464. const offset = -1;
  1465. const tree = hitinfo.sheet.zh_tree;
  1466. if (!tree) { return; }
  1467. const setting = tree.setting;
  1468. const node = tree.nodes[hitinfo.row];
  1469. if (!node) { return; }
  1470. let centerX = hitinfo.cellRect.x + offset + (node[setting.level] - 1) * indent + (node[setting.level]) * levelIndent + indent / 2 + xOffset;
  1471. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1472. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1473. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1474. const children = tree.getChildren(node);
  1475. if (!node.expanded && !node[setting.isLeaf] && children.length === 0 && tree.loadChildren) {
  1476. tree.loadChildren(node, function () {
  1477. node.expanded = true;
  1478. const children = tree.getChildren(node);
  1479. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1480. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1481. });
  1482. } else {
  1483. tree.setExpanded(node, !node.expanded);
  1484. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1485. const posterity = tree.getPosterity(node);
  1486. for (const child of posterity) {
  1487. const cIndex = tree.getNodeIndex(child);
  1488. if (child.visible && child.waitingLoading) {
  1489. SpreadJsObj._loadRowData(hitinfo.sheet, child, cIndex);
  1490. }
  1491. //if (child.visible !== hitinfo.sheet.getRowVisible(cIndex, hitinfo.sheetArea)) {
  1492. hitinfo.sheet.setRowVisible(cIndex, child.visible, hitinfo.sheetArea);
  1493. //}
  1494. }
  1495. });
  1496. hitinfo.sheet.repaint();
  1497. }
  1498. }
  1499. };
  1500. return new TreeNodeCellType();
  1501. },
  1502. /**
  1503. * 获取 带悬浮提示的CellType
  1504. * @returns {TipCellType}
  1505. */
  1506. getTipCellType: function (scrollHeightClass = false) {
  1507. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1508. const TipCellType = function () {};
  1509. // 继承 SpreadJs定义的 普通的TextCellType
  1510. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1511. const proto = TipCellType.prototype;
  1512. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1513. const xs = hitinfo.sheet.getParent().xs;
  1514. const ctx = xs.childNodes[0].getContext("2d");
  1515. if (font && font !== '') {
  1516. ctx.font = font;
  1517. } else {
  1518. ctx.font = hitinfo.cellStyle.font;
  1519. }
  1520. return ctx.measureText(str).width;
  1521. };
  1522. proto.showTip = function (hitinfo, text) {
  1523. return text && text !== '';
  1524. };
  1525. /**
  1526. * 获取点击信息
  1527. * @param {Number} x
  1528. * @param {Number} y
  1529. * @param {Object} cellStyle
  1530. * @param {Object} cellRect
  1531. * @param {Object} context
  1532. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1533. */
  1534. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1535. return {
  1536. x: x,
  1537. y: y,
  1538. row: context.row,
  1539. col: context.col,
  1540. cellStyle: cellStyle,
  1541. cellRect: cellRect,
  1542. sheet: context.sheet,
  1543. sheetArea: context.sheetArea,
  1544. ctx: context.sheet.getParent().xs,
  1545. };
  1546. };
  1547. /**
  1548. * 鼠标进入单元格事件 - 显示悬浮提示
  1549. * @param {Object} hitinfo - 见getHitInfo返回值
  1550. */
  1551. proto.processMouseEnter = function (hitinfo) {
  1552. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1553. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1554. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1555. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1556. text = col.getTip(sortData[hitinfo.row]);
  1557. }
  1558. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1559. if (pos && this.showTip(hitinfo, text)) {
  1560. if (!this._toolTipElement) {
  1561. let div = $('#autoTip')[0];
  1562. if (!div) {
  1563. div = document.createElement("div");
  1564. $(div).css("position", "absolute")
  1565. .css("border", "1px #C0C0C0 solid")
  1566. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1567. .css("font", "9pt Arial")
  1568. .css("background", "white")
  1569. .css("padding", 5)
  1570. .css("z-index", 999)
  1571. .css("max-width", maxHintWidth)
  1572. .css("word-wrap", "break-word")
  1573. .attr("id", 'autoTip');
  1574. document.body.insertBefore(div, null);
  1575. }
  1576. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1577. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1578. const scrollTop = scrollHeightClass ? $(scrollHeightClass).scrollTop() : 0;
  1579. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1580. $(div).html(text).css("top", pos.y + hitinfo.y - scrollTop + indent).css("left", pos.x + hitinfo.x + indent);
  1581. } else if (textWidth > maxHintWidth) {
  1582. $(div).html(text).css("top", pos.y + hitinfo.y - scrollTop + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1583. } else {
  1584. $(div).html(text).css("top", pos.y + hitinfo.y - scrollTop + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1585. }
  1586. this._toolTipElement = div;
  1587. $(div).show("fast");
  1588. }
  1589. }
  1590. };
  1591. /**
  1592. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1593. * @param {Object} hitinfo - 见getHitInfo返回值
  1594. */
  1595. proto.processMouseLeave = function (hitinfo) {
  1596. if (this._toolTipElement) {
  1597. $(this._toolTipElement).hide();
  1598. this._toolTipElement = null;
  1599. }
  1600. };
  1601. return new TipCellType();
  1602. },
  1603. getAutoTipCellType: function (scrollHeightClass) {
  1604. const AutoTipCellType = function () {};
  1605. // 继承 TipCellType
  1606. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType(scrollHeightClass);
  1607. const proto = AutoTipCellType.prototype;
  1608. proto.showTip = function (hitinfo, text) {
  1609. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1610. };
  1611. return new AutoTipCellType();
  1612. },
  1613. getDelayTipCellType: function () {
  1614. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1615. const TipCellType = function () {};
  1616. // 继承 SpreadJs定义的 普通的TextCellType
  1617. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1618. const proto = TipCellType.prototype;
  1619. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1620. const xs = hitinfo.sheet.getParent().xs;
  1621. const ctx = xs.childNodes[0].getContext("2d");
  1622. if (font && font !== '') {
  1623. ctx.font = font;
  1624. } else {
  1625. ctx.font = hitinfo.cellStyle.font;
  1626. }
  1627. return ctx.measureText(str).width;
  1628. };
  1629. proto.showTip = function (hitinfo, text) {
  1630. return text && text !== '';
  1631. };
  1632. /**
  1633. * 获取点击信息
  1634. * @param {Number} x
  1635. * @param {Number} y
  1636. * @param {Object} cellStyle
  1637. * @param {Object} cellRect
  1638. * @param {Object} context
  1639. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1640. */
  1641. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1642. return {
  1643. x: x,
  1644. y: y,
  1645. row: context.row,
  1646. col: context.col,
  1647. cellStyle: cellStyle,
  1648. cellRect: cellRect,
  1649. sheet: context.sheet,
  1650. sheetArea: context.sheetArea,
  1651. ctx: context.sheet.getParent().xs,
  1652. };
  1653. };
  1654. /**
  1655. * 鼠标进入单元格事件 - 显示悬浮提示
  1656. * @param {Object} hitinfo - 见getHitInfo返回值
  1657. */
  1658. proto.processMouseEnter = function (hitinfo) {
  1659. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1660. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1661. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1662. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1663. text = col.getTip(sortData[hitinfo.row]);
  1664. }
  1665. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1666. if (pos && this.showTip(hitinfo, text)) {
  1667. if (!this._toolTipElement) {
  1668. let div = $('#autoTip')[0];
  1669. if (!div) {
  1670. div = document.createElement("div");
  1671. $(div).css("position", "absolute")
  1672. .css("border", "1px #C0C0C0 solid")
  1673. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1674. .css("font", "9pt Arial")
  1675. .css("background", "white")
  1676. .css("padding", 5)
  1677. .css("z-index", 999)
  1678. .css("max-width", maxHintWidth)
  1679. .css("word-wrap", "break-word")
  1680. .attr("id", 'autoTip');
  1681. document.body.insertBefore(div, null);
  1682. }
  1683. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1684. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1685. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1686. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1687. } else if (textWidth > maxHintWidth) {
  1688. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1689. } else {
  1690. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1691. }
  1692. this._toolTipElement = div;
  1693. const self = this;
  1694. setTimeout(()=>{ if (self._toolTipElement) $(self._toolTipElement).show("fast");}, 300);
  1695. }
  1696. }
  1697. };
  1698. /**
  1699. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1700. * @param {Object} hitinfo - 见getHitInfo返回值
  1701. */
  1702. proto.processMouseLeave = function (hitinfo) {
  1703. if (this._toolTipElement) {
  1704. $(this._toolTipElement).hide();
  1705. this._toolTipElement = null;
  1706. }
  1707. };
  1708. return new TipCellType();
  1709. },
  1710. /**
  1711. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1712. *
  1713. * img:
  1714. * 1. 整列固定,则传入img的select
  1715. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1716. *
  1717. * 2. 各单元格自定义,则
  1718. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1719. * function getAttachmentImage (data) {
  1720. * $('#attachment-img').url = data.attachmentImageUrl;
  1721. * return $('#attachment-img')[0];
  1722. * }
  1723. *
  1724. * @returns {ImageCellType}
  1725. */
  1726. getImageCellType: function () {
  1727. const ImageCellType = function (){};
  1728. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1729. const proto = ImageCellType.prototype;
  1730. proto.getImage = function (sheet, iRow, iCol) {
  1731. const col = sheet.zh_setting.cols[iCol];
  1732. let imgSource = col.img;
  1733. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1734. const sortData = SpreadJsObj.getSortData(sheet);
  1735. const data = sortData ? sortData[iRow] : null;
  1736. return data ? imgSource(data) : null;
  1737. } else {
  1738. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1739. }
  1740. };
  1741. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1742. const col = options.sheet.zh_setting.cols[options.col];
  1743. const img = this.getImage(options.sheet, options.row, options.col);
  1744. const indent = col.indent ? col.indent : 10;
  1745. if (img) {
  1746. if (style.backColor) {
  1747. canvas.save();
  1748. canvas.fillStyle = style.backColor;
  1749. canvas.fillRect(x, y, indent + img.width, h);
  1750. canvas.restore();
  1751. }
  1752. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1753. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1754. style.hAlign = spreadNS.HorizontalAlign.left;
  1755. }
  1756. x = x + indent + img.width;
  1757. w = w - indent - img.width;
  1758. }
  1759. // Drawing Text
  1760. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1761. };
  1762. /**
  1763. * 获取点击信息
  1764. * @param {Number} x
  1765. * @param {Number} y
  1766. * @param {Object} cellStyle
  1767. * @param {Object} cellRect
  1768. * @param {Object} context
  1769. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1770. */
  1771. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1772. return {
  1773. x: x,
  1774. y: y,
  1775. row: context.row,
  1776. col: context.col,
  1777. cellStyle: cellStyle,
  1778. cellRect: cellRect,
  1779. sheet: context.sheet,
  1780. sheetArea: context.sheetArea
  1781. };
  1782. };
  1783. /**
  1784. * 鼠标点击
  1785. * @param {Object} hitinfo - 见getHitInfo
  1786. */
  1787. proto.processMouseDown = function (hitinfo) {
  1788. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1789. if (img) {
  1790. const halfX = img.width / 2, halfY = img.height / 2;
  1791. const centerX = hitinfo.cellRect.x + indent + halfX;
  1792. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1793. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1794. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1795. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1796. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1797. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1798. const data = sortData ? sortData[hitinfo.row] : null;
  1799. imageClick(data, hitinfo);
  1800. }
  1801. }
  1802. }
  1803. };
  1804. return new ImageCellType();
  1805. },
  1806. /**
  1807. *
  1808. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1809. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1810. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1811. *
  1812. * @returns {ImageCellType}
  1813. */
  1814. getImageButtonCellType: function () {
  1815. let hover = 1, active = 2;
  1816. const ImageCellType = function (){};
  1817. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1818. const proto = ImageCellType.prototype;
  1819. proto.getImage = function (sheet, iRow, iCol) {
  1820. const col = sheet.zh_setting.cols[iCol];
  1821. let imgSource = col.normalImg;
  1822. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1823. if (tag === active) {
  1824. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1825. } else if (tag === hover) {
  1826. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1827. }
  1828. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1829. const sortData = SpreadJsObj.getSortData(sheet);
  1830. const data = sortData ? sortData[iRow] : null;
  1831. return data ? imgSource(data) : null;
  1832. } else {
  1833. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1834. }
  1835. };
  1836. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1837. const col = options.sheet.zh_setting.cols[options.col];
  1838. const sortData = SpreadJsObj.getSortData(options.sheet);
  1839. const data = sortData ? sortData[options.row] : null;
  1840. let showImage = true;
  1841. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1842. showImage = col.showImage(data);
  1843. }
  1844. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1845. const indent = col.indent ? col.indent : 10;
  1846. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1847. if (img) {
  1848. if (style.backColor) {
  1849. canvas.save();
  1850. canvas.fillStyle = style.backColor;
  1851. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1852. canvas.restore();
  1853. }
  1854. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1855. w = w - indent - img.width;
  1856. }
  1857. // Drawing Text
  1858. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1859. } else if (style.hAlign === spreadNS.HorizontalAlign.center) {
  1860. if (img) {
  1861. if (style.backColor) {
  1862. canvas.save();
  1863. canvas.fillStyle = style.backColor;
  1864. canvas.fillRect((x + x + w - indent - img.width) / 2, y, (indent + img.width + img.width) / 2, h);
  1865. canvas.restore();
  1866. }
  1867. canvas.drawImage(img, (x + 10 + x + w - indent - img.width) / 2, y + (h - img.height) / 2);
  1868. w = w - indent - img.width;
  1869. }
  1870. // Drawing Text
  1871. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1872. } else {
  1873. if (img) {
  1874. if (style.backColor) {
  1875. canvas.save();
  1876. canvas.fillStyle = style.backColor;
  1877. canvas.fillRect(x, y, indent + img.width, h);
  1878. canvas.restore();
  1879. }
  1880. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1881. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1882. style.hAlign = spreadNS.HorizontalAlign.left;
  1883. }
  1884. x = x + indent + img.width;
  1885. w = w - indent - img.width;
  1886. }
  1887. // Drawing Text
  1888. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1889. }
  1890. };
  1891. /**
  1892. * 获取点击信息
  1893. * @param {Number} x
  1894. * @param {Number} y
  1895. * @param {Object} cellStyle
  1896. * @param {Object} cellRect
  1897. * @param {Object} context
  1898. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1899. */
  1900. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1901. return {
  1902. x: x,
  1903. y: y,
  1904. row: context.row,
  1905. col: context.col,
  1906. cellStyle: cellStyle,
  1907. cellRect: cellRect,
  1908. sheet: context.sheet,
  1909. sheetArea: context.sheetArea
  1910. };
  1911. };
  1912. proto.processMouseEnter = function (hitinfo) {
  1913. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1914. // Drawing Image
  1915. if (col.hoverImg) {
  1916. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1917. cell.tag(hover);
  1918. hitinfo.sheet.repaint(hitinfo.cellRect);
  1919. }
  1920. };
  1921. proto.processMouseLeave = function (hitinfo) {
  1922. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1923. // Drawing Image
  1924. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1925. cell.tag(null);
  1926. hitinfo.sheet.repaint(hitinfo.cellRect);
  1927. };
  1928. // proto.processMouseDown = function (hitinfo) {
  1929. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1930. // if (col.activeImg) {
  1931. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1932. // cell.tag(active);
  1933. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1934. // }
  1935. // };
  1936. // proto.processMouseUp = function (hitinfo) {
  1937. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1938. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1939. // const data = sortData ? sortData[hitinfo.row] : null;
  1940. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1941. // if (!col.showImage(data)) {
  1942. // return;
  1943. // }
  1944. // }
  1945. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1946. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1947. // imageClick(data);
  1948. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1949. // cell.tag(null);
  1950. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1951. // }
  1952. // };
  1953. /*
  1954. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1955. */
  1956. proto.processMouseDown = function (hitinfo) {
  1957. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1958. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1959. const halfX = img.width / 2, halfY = img.height / 2;
  1960. const indent = col.indent ? col.indent : 10;
  1961. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1962. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1963. : hitinfo.cellRect.x + indent + halfX;
  1964. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1965. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1966. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1967. cell.tag(active);
  1968. hitinfo.sheet.repaint(hitinfo.cellRect);
  1969. }
  1970. };
  1971. proto.processMouseUp = function (hitinfo) {
  1972. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1973. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1974. const halfX = img.width / 2, halfY = img.height / 2;
  1975. const indent = col.indent ? col.indent : 10;
  1976. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1977. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1978. : hitinfo.cellRect.x + indent + halfX;
  1979. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1980. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1981. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1982. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1983. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1984. const data = sortData ? sortData[hitinfo.row] : null;
  1985. imageClick(data, hitinfo);
  1986. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1987. cell.tag(null);
  1988. hitinfo.sheet.repaint(hitinfo.cellRect);
  1989. }
  1990. }
  1991. };
  1992. proto.processMouseMove = function (hitinfo) {
  1993. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1994. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1995. const halfX = img.width / 2, halfY = img.height / 2;
  1996. const indent = col.indent ? col.indent : 10;
  1997. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1998. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1999. : hitinfo.cellRect.x + indent + halfX;
  2000. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2001. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2002. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2003. if (cell.tag() !== hover) {
  2004. cell.tag(hover);
  2005. hitinfo.sheet.repaint(hitinfo.cellRect);
  2006. }
  2007. } else {
  2008. if (cell.tag() === hover) {
  2009. cell.tag(null);
  2010. hitinfo.sheet.repaint(hitinfo.cellRect);
  2011. }
  2012. }
  2013. };
  2014. return new ImageCellType();
  2015. },
  2016. getActiveImageButtonCellType: function () {
  2017. let show = 1, hover = 2, active = 2;
  2018. const ImageCellType = function (){};
  2019. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  2020. const proto = ImageCellType.prototype;
  2021. proto.getImage = function (sheet, iRow, iCol) {
  2022. const col = sheet.zh_setting.cols[iCol];
  2023. let imgSource = null;
  2024. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  2025. if (tag === show) {
  2026. imgSource = col.normalImg;
  2027. } else if (tag === active) {
  2028. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  2029. } else if (tag === hover) {
  2030. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  2031. }
  2032. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  2033. const sortData = SpreadJsObj.getSortData(sheet);
  2034. const data = sortData ? sortData[iRow] : null;
  2035. return data ? imgSource(data) : null;
  2036. } else {
  2037. return $(imgSource)[0] ? $(imgSource)[0] : null;
  2038. }
  2039. };
  2040. proto.getActiveImage = function(sheet, iRow, iCol) {
  2041. const col = sheet.zh_setting.cols[iCol];
  2042. const sortData = SpreadJsObj.getSortData(sheet);
  2043. const data = sortData ? sortData[iRow] : null;
  2044. let showImage = true;
  2045. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2046. showImage = col.showImage(data);
  2047. }
  2048. const img = showImage ? this.getImage(sheet, iRow, iCol) : null;
  2049. return [col, img];
  2050. };
  2051. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2052. const [col, img] = this.getActiveImage(options.sheet, options.row, options.col);
  2053. const indent = col.indent ? col.indent : 10;
  2054. if (style.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right) {
  2055. if (img) {
  2056. if (style.backColor) {
  2057. canvas.save();
  2058. canvas.fillStyle = style.backColor;
  2059. canvas.fillRect(x + w - indent - img.width, y, img.width + indent, h);
  2060. canvas.restore();
  2061. }
  2062. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  2063. w = w - indent - img.width;
  2064. }
  2065. // Drawing Text
  2066. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2067. } else {
  2068. if (img) {
  2069. if (style.backColor) {
  2070. canvas.save();
  2071. canvas.fillStyle = style.backColor;
  2072. canvas.fillRect(x, y, indent + img.width, h);
  2073. canvas.restore();
  2074. }
  2075. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  2076. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  2077. style.hAlign = spreadNS.HorizontalAlign.left;
  2078. }
  2079. x = x + indent + img.width;
  2080. w = w - indent - img.width;
  2081. }
  2082. // Drawing Text
  2083. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  2084. }
  2085. };
  2086. /**
  2087. * 获取点击信息
  2088. * @param {Number} x
  2089. * @param {Number} y
  2090. * @param {Object} cellStyle
  2091. * @param {Object} cellRect
  2092. * @param {Object} context
  2093. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2094. */
  2095. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2096. return {
  2097. x: x,
  2098. y: y,
  2099. row: context.row,
  2100. col: context.col,
  2101. cellStyle: cellStyle,
  2102. cellRect: cellRect,
  2103. sheet: context.sheet,
  2104. sheetArea: context.sheetArea
  2105. };
  2106. };
  2107. proto.processMouseEnter = function (hitinfo) {
  2108. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2109. // Drawing Image
  2110. if (col.normalImg) {
  2111. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2112. cell.tag(show);
  2113. hitinfo.sheet.repaint(hitinfo.cellRect);
  2114. }
  2115. };
  2116. proto.processMouseLeave = function (hitinfo) {
  2117. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2118. cell.tag(null);
  2119. hitinfo.sheet.repaint(hitinfo.cellRect);
  2120. };
  2121. // proto.processMouseDown = function (hitinfo) {
  2122. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2123. // if (col.activeImg) {
  2124. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2125. // cell.tag(active);
  2126. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2127. // }
  2128. // };
  2129. // proto.processMouseUp = function (hitinfo) {
  2130. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2131. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2132. // const data = sortData ? sortData[hitinfo.row] : null;
  2133. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  2134. // if (!col.showImage(data)) {
  2135. // return;
  2136. // }
  2137. // }
  2138. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2139. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2140. // imageClick(data);
  2141. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2142. // cell.tag(null);
  2143. // hitinfo.sheet.repaint(hitinfo.cellRect);
  2144. // }
  2145. // };
  2146. /*
  2147. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  2148. */
  2149. proto.processMouseDown = function (hitinfo) {
  2150. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2151. if (!img) return;
  2152. const halfX = img.width / 2, halfY = img.height / 2;
  2153. const indent = col.indent ? col.indent : 10;
  2154. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  2155. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2156. : hitinfo.cellRect.x + indent + halfX;
  2157. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2158. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2159. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2160. cell.tag(active);
  2161. hitinfo.sheet.repaint(hitinfo.cellRect);
  2162. }
  2163. };
  2164. proto.processMouseUp = function (hitinfo) {
  2165. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2166. if (!img) return;
  2167. const halfX = img.width / 2, halfY = img.height / 2;
  2168. const indent = col.indent ? col.indent : 10;
  2169. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  2170. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2171. : hitinfo.cellRect.x + indent + halfX;
  2172. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2173. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2174. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  2175. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  2176. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2177. const data = sortData ? sortData[hitinfo.row] : null;
  2178. imageClick(data, hitinfo);
  2179. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2180. cell.tag(hover);
  2181. hitinfo.sheet.repaint(hitinfo.cellRect);
  2182. }
  2183. }
  2184. };
  2185. proto.processMouseMove = function (hitinfo) {
  2186. const [col, img] = this.getActiveImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  2187. if (!img) return;
  2188. const halfX = img.width / 2, halfY = img.height / 2;
  2189. const indent = col.indent ? col.indent : 10;
  2190. const centerX = (hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right || col.imgAlign === spreadNS.HorizontalAlign.right)
  2191. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  2192. : hitinfo.cellRect.x + indent + halfX;
  2193. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  2194. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  2195. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  2196. if (cell.tag() !== hover) {
  2197. cell.tag(hover);
  2198. hitinfo.sheet.repaint(hitinfo.cellRect);
  2199. }
  2200. } else {
  2201. if (cell.tag() === hover) {
  2202. cell.tag(show);
  2203. hitinfo.sheet.repaint(hitinfo.cellRect);
  2204. }
  2205. }
  2206. };
  2207. return new ImageCellType();
  2208. },
  2209. /**
  2210. * 获取 嵌入Html的cellType
  2211. * @returns {HTMLCellType}
  2212. */
  2213. getHtmlCellType: function () {
  2214. const HTMLCellType = function (){};
  2215. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  2216. const proto = HTMLCellType.prototype;
  2217. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2218. let DOMURL = window.URL || window.webkitURL || window;
  2219. let cell = context.sheet.getCell(context.row, context.col);
  2220. let img = cell.tag();
  2221. if (img) {
  2222. try {
  2223. ctx.save();
  2224. ctx.rect(x, y, w, h);
  2225. ctx.clip();
  2226. ctx.drawImage(img, x + 2, y + 2);
  2227. ctx.restore();
  2228. cell.tag(null);
  2229. return;
  2230. }
  2231. catch (err) {
  2232. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context]);
  2233. cell.tag(null);
  2234. return;
  2235. }
  2236. }
  2237. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  2238. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  2239. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  2240. let doc = document.implementation.createHTMLDocument("");
  2241. doc.write(data);
  2242. // Get well-formed markup
  2243. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  2244. img = new Image();
  2245. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  2246. //var url = DOMURL.createObjectURL(svg);
  2247. //img.src = url;
  2248. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  2249. cell.tag(img);
  2250. img.onload = function () {
  2251. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  2252. }
  2253. };
  2254. return new HTMLCellType();
  2255. },
  2256. /**
  2257. * 获取 字符超长缩略的cellType
  2258. * @returns {EllipsisTextCellType}
  2259. */
  2260. getEllipsisTextCellType: function () {
  2261. const EllipsisTextCellType = function (){};
  2262. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  2263. const proto = EllipsisTextCellType.prototype;
  2264. proto.getEllipsisText = function(c, str, maxWidth) {
  2265. var width = c.measureText(str).width;
  2266. var ellipsis = '…';
  2267. var ellipsisWidth = c.measureText(ellipsis).width;
  2268. if (width <= maxWidth || width <= ellipsisWidth) {
  2269. return str;
  2270. } else {
  2271. var len = str.length;
  2272. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  2273. str = str.substring(0, len);
  2274. width = c.measureText(str).width;
  2275. }
  2276. return str + ellipsis;
  2277. }
  2278. };
  2279. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2280. ctx.font = style.font;
  2281. value = this.getEllipsisText(ctx, value, w - 2);
  2282. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  2283. };
  2284. return new EllipsisTextCellType();
  2285. },
  2286. getEllipsisTextAutoTipCellType: function (scrollHeightClass) {
  2287. const CellType = function () {};
  2288. // 继承 TipCellType
  2289. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType(scrollHeightClass);
  2290. const proto = CellType.prototype;
  2291. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  2292. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  2293. proto.paint = ellipsisTextCellType.paint;
  2294. return new CellType();
  2295. },
  2296. /**
  2297. * 获取 动态显示ComboBox的cellType
  2298. * @returns {ActiveComboCellType}
  2299. */
  2300. getActiveComboCellType: function () {
  2301. const ActiveComboCellType = function () {};
  2302. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2303. const proto = ActiveComboCellType.prototype;
  2304. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  2305. const sheet = options.sheet;
  2306. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2307. && !sheet.getCell(options.row, options.col).locked()) {
  2308. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  2309. } else {
  2310. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2311. }
  2312. };
  2313. proto.processMouseEnter = function (hitinfo) {
  2314. hitinfo.sheet.repaint(hitinfo.cellRect);
  2315. };
  2316. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  2317. const sheet = options.sheet;
  2318. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2319. && !sheet.getCell(options.row, options.col).locked()) {
  2320. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  2321. } else {
  2322. return {
  2323. x: x,
  2324. y: y,
  2325. row: options.row,
  2326. col: options.col,
  2327. cellStyle: cellStyle,
  2328. cellRect: cellRect,
  2329. sheetArea: options.sheetArea
  2330. };
  2331. }
  2332. };
  2333. return new ActiveComboCellType();
  2334. },
  2335. /**
  2336. * 获取 单位的CellType
  2337. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  2338. */
  2339. getUnitCellType: function (comboEdit, items = ['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  2340. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '延米', '每一试桩',
  2341. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '辆', '艘班', '亩', '片',
  2342. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']) {
  2343. let combo = this.getActiveComboCellType();
  2344. combo.editable(comboEdit);
  2345. combo.itemHeight(10).items(items);
  2346. return combo;
  2347. },
  2348. /**
  2349. * 获取 自定义的CellType
  2350. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  2351. */
  2352. getCustomizeComboCellType: function (items) {
  2353. let combo = this.getActiveComboCellType();
  2354. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  2355. return combo;
  2356. },
  2357. getSpecComboCellType: function (items) {
  2358. const ComboCellType = function () {};
  2359. ComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2360. const proto = ComboCellType.prototype;
  2361. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  2362. const sheet = options.sheet;
  2363. const setting = options.sheet.zh_setting;
  2364. const col = setting.cols[options.col];
  2365. const data = SpreadJsObj.getSelectObject(options.sheet);
  2366. if (!data) {
  2367. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2368. return;
  2369. }
  2370. const comboEdit = (col.comboEdit && Object.prototype.toString.apply(col.comboEdit) === "[object Function]")
  2371. ? col.comboEdit(options.sheet, data)
  2372. : (col.comboEdit !== undefined ? col.comboEdit : true);
  2373. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2374. && !sheet.getCell(options.row, options.col).locked() && comboEdit) {
  2375. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  2376. } else {
  2377. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  2378. }
  2379. };
  2380. proto.processMouseEnter = function (hitinfo) {
  2381. hitinfo.sheet.repaint(hitinfo.cellRect);
  2382. };
  2383. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  2384. const sheet = options.sheet;
  2385. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  2386. && !sheet.getCell(options.row, options.col).locked()) {
  2387. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  2388. } else {
  2389. return {
  2390. x: x,
  2391. y: y,
  2392. row: options.row,
  2393. col: options.col,
  2394. cellStyle: cellStyle,
  2395. cellRect: cellRect,
  2396. sheetArea: options.sheetArea
  2397. };
  2398. }
  2399. };
  2400. const combo = new ComboCellType();
  2401. combo.itemHeight(10).items(items).editorValueType(spreadNS.CellTypes.EditorValueType.value);
  2402. return combo;
  2403. },
  2404. getStageComboCellType: function () {
  2405. const ComboCellType = function () {};
  2406. ComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  2407. const proto = ComboCellType.prototype;
  2408. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  2409. const setting = context.sheet.zh_setting;
  2410. const col = setting.cols[context.col];
  2411. const data = SpreadJsObj.getSelectObject(context.sheet);
  2412. this.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(col.getItems(data));
  2413. };
  2414. return new ComboCellType();
  2415. },
  2416. /**
  2417. * 获取 日期选择 CellType
  2418. * @returns {DatePickerCellType}
  2419. */
  2420. getDatePickerCellType: function () {
  2421. let datepicker;
  2422. const DatePickerCellType = function () {};
  2423. DatePickerCellType.prototype = new spreadNS.CellTypes.Text();
  2424. const proto = DatePickerCellType.prototype;
  2425. proto.createEditorElement = function (context) {
  2426. return document.createElement("input");
  2427. };
  2428. proto.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  2429. const self = this;
  2430. if (editorContext) {
  2431. const $editor = $(editorContext);
  2432. //spreadNS.CellTypes.Text.prototype.activateEditor.apply(this, arguments);
  2433. $editor.css("position", "absolute");
  2434. datepicker = $editor.datepicker({
  2435. language: 'zh',
  2436. dateFormat: 'yyyy-MM-DD'
  2437. }).data('datepicker');
  2438. datepicker.show();
  2439. }
  2440. };
  2441. proto.deactivateEditor = function (editorContext, context) {
  2442. datepicker.hide();
  2443. datepicker.destroy();
  2444. spreadNS.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments);
  2445. };
  2446. proto.setEditorValue = function (editor, value, context) {
  2447. if (datepicker) {
  2448. datepicker.selectDate(value);
  2449. }
  2450. };
  2451. proto.getEditorValue = function (editor, context) {
  2452. return datepicker ? datepicker.focused : null;
  2453. };
  2454. proto.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  2455. if (editorContext) {
  2456. const $editor = $(editorContext);
  2457. $editor.css("width", cellRect.width + 1);
  2458. $editor.css("height", cellRect.height + 3);
  2459. }
  2460. };
  2461. return new DatePickerCellType();
  2462. },
  2463. /**
  2464. * 获取 鼠标移入变手势 CellType
  2465. * @returns {DatePickerCellType}
  2466. */
  2467. getMouseTouchCellType: function () {
  2468. const mouseTouchCellType = function () {};
  2469. mouseTouchCellType.prototype = new spreadNS.CellTypes.Base();
  2470. mouseTouchCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2471. var xm = cellRect.x + cellRect.width / 2,
  2472. ym = cellRect.y + cellRect.height / 2,
  2473. size = 10;
  2474. var info = { x: x, y: y, row: context.row, col: context.col, cellRect: cellRect, sheetArea: context.sheetArea };
  2475. if (xm - size <= x && x <= xm + size && ym - size <= y && y <= ym + size) {
  2476. info.isReservedLocation = true;
  2477. }
  2478. return info;
  2479. };
  2480. mouseTouchCellType.prototype.processMouseMove = function (hitInfo) {
  2481. var sheet = hitInfo.sheet;
  2482. var div = sheet.getParent().getHost();
  2483. var canvasId = div.id + "vp_vp";
  2484. var canvas = $("#"+canvasId)[0];
  2485. if (sheet && hitInfo.isReservedLocation) {
  2486. canvas.style.cursor='pointer';
  2487. return true;
  2488. }else{
  2489. canvas.style.cursor='default';
  2490. }
  2491. return false;
  2492. };
  2493. return new mouseTouchCellType();
  2494. },
  2495. getStackedBarCellType: function () {
  2496. const stackedBarCellType = function(){};
  2497. stackedBarCellType.prototype = new spreadNS.CellTypes.Text();
  2498. const indent = 3, defaultR = 0.05, minHeight = 2;
  2499. const tipsMaxHintWidth = 200, tipsIndent = 15, tipsBorderIndent = 10;
  2500. /**
  2501. * 画一个长条
  2502. * @param {Object} canvas - 画布
  2503. * @param {Object} rect - 方框区域
  2504. * @param {String} lineColor - 画线颜色
  2505. * @param {String} fillColor - 填充颜色
  2506. */
  2507. // roundRect方法在Chrome99版本以后才支持
  2508. const drawBarBefore99 = function (canvas, x, y, w, h, r, color) {
  2509. const rArr = r instanceof Array ? r : [r, r, r, r];
  2510. canvas.save();
  2511. // 设置偏移量
  2512. canvas.translate(0.5, 0.5);
  2513. canvas.strokeStyle = color;
  2514. canvas.beginPath();
  2515. canvas.moveTo(x+rArr[0], y);
  2516. canvas.arcTo(x+w, y, x+w, y+h, Math.min(rArr[1], w));
  2517. canvas.arcTo(x+w, y+h, x, y+h, Math.min(rArr[2], w));
  2518. canvas.arcTo(x, y+h, x, y, Math.min(rArr[3], w));
  2519. canvas.arcTo(x, y, x+w, y, Math.min(rArr[0], w));
  2520. canvas.stroke();
  2521. canvas.fillStyle = color;
  2522. canvas.fill();
  2523. canvas.restore();
  2524. };
  2525. const drawBar = function (canvas, x, y, w, h, r, color) {
  2526. canvas.save();
  2527. // 设置偏移量
  2528. canvas.translate(0.5, 0.5);
  2529. canvas.strokeStyle = color;
  2530. canvas.beginPath();
  2531. canvas.roundRect(x, y, w, h, r);
  2532. canvas.stroke();
  2533. canvas.fillStyle = color;
  2534. canvas.fill();
  2535. canvas.restore();
  2536. };
  2537. const drawSB = function (canvas, x, y, w, h, r, color) {
  2538. canvas.save();
  2539. // 设置偏移量
  2540. canvas.translate(0.5, 0.5);
  2541. canvas.strokeStyle = color;
  2542. canvas.beginPath();
  2543. canvas.moveTo(x, y);
  2544. canvas.lineTo(x+w, y);
  2545. if (w > r) {
  2546. canvas.arcTo(x+w, y+h, x, y+h, r);
  2547. canvas.lineTo(x, y+h);
  2548. } else {
  2549. canvas.arcTo(x+w, y+h-r+w, x, y+h-r+w, Math.min(r, w));
  2550. canvas.lineTo(x, y+h-r+w);
  2551. }
  2552. //canvas.arcTo(x+w, y+h, x, y+h, Math.min(r, w));
  2553. //canvas.lineTo(x, y+h);
  2554. canvas.lineTo(x, y);
  2555. canvas.stroke();
  2556. canvas.fillStyle = color;
  2557. canvas.fill();
  2558. canvas.restore();
  2559. };
  2560. const proto = stackedBarCellType.prototype;
  2561. /**
  2562. * 绘制方法
  2563. * @param {Object} canvas - 画布
  2564. * @param value - cell.value
  2565. * @param {Number} x - 单元格左顶点坐标 x
  2566. * @param {Number} y - 单元格左顶点坐标 y
  2567. * @param {Number} w - 单元格宽度
  2568. * @param {Number} h - 单元格高度
  2569. * @param {Object} style - cell.style
  2570. * @param {Object} options
  2571. */
  2572. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2573. // 清理 画布--单元格范围 旧数据
  2574. canvas.save();
  2575. canvas.fillStyle = style.backColor || 'white';
  2576. canvas.fillRect(x, y, w, h);
  2577. canvas.restore();
  2578. const col = options.sheet.zh_setting.cols[options.col];
  2579. const barData = value;
  2580. const left = x + indent;
  2581. const startTop = y + indent;
  2582. const validHeight = h - indent - indent - (col.stackedBarCover || barData.length === 1 ? 1 : barData.length / 2 + 1);
  2583. const height = col.stackedBarCover || barData.length === 0 ? validHeight : Math.max(validHeight/barData.length, minHeight);
  2584. const validWidth = w - indent - indent - 1;
  2585. for (const [i, bd] of barData.entries()) {
  2586. let width = ZhCalc.mul(validWidth, bd.percent, 2);
  2587. if (width < defaultR) continue;
  2588. const top = col.stackedBarCover ? startTop : startTop + height * i + i - 1;
  2589. //drawBarBefore99(canvas, left, top, width, height, ZhCalc.mul(height, defaultR, 2), bd.color);
  2590. //drawBarBefore99(canvas, left, top, width, height, [0, 0, ZhCalc.div(height, 2, 2), 0], bd.color);
  2591. if (col.stackedBarCover) {
  2592. drawSB(canvas, left, top, width, height, ZhCalc.div(height, 2, 2), bd.color);
  2593. } else {
  2594. drawBarBefore99(canvas, left, top, width, height, ZhCalc.mul(height, defaultR, 2), bd.color);
  2595. }
  2596. }
  2597. };
  2598. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  2599. const xs = hitinfo.sheet.getParent().xs;
  2600. const ctx = xs.childNodes[0].getContext("2d");
  2601. if (font && font !== '') {
  2602. ctx.font = font;
  2603. } else {
  2604. ctx.font = hitinfo.cellStyle.font;
  2605. }
  2606. return ctx.measureText(str).width;
  2607. };
  2608. proto.showTip = function (hitinfo, text) {
  2609. return text && text !== '';
  2610. };
  2611. /**
  2612. * 获取点击信息
  2613. * @param {Number} x
  2614. * @param {Number} y
  2615. * @param {Object} cellStyle
  2616. * @param {Object} cellRect
  2617. * @param {Object} context
  2618. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2619. */
  2620. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2621. return {
  2622. x: x,
  2623. y: y,
  2624. row: context.row,
  2625. col: context.col,
  2626. cellStyle: cellStyle,
  2627. cellRect: cellRect,
  2628. sheet: context.sheet,
  2629. sheetArea: context.sheetArea,
  2630. ctx: context.sheet.getParent().xs,
  2631. };
  2632. };
  2633. /**
  2634. * 鼠标进入单元格事件 - 显示悬浮提示
  2635. * @param {Object} hitinfo - 见getHitInfo返回值
  2636. */
  2637. proto.processMouseEnter = function (hitinfo) {
  2638. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  2639. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2640. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  2641. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  2642. text = col.getTip(sortData[hitinfo.row]);
  2643. }
  2644. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  2645. if (pos && this.showTip(hitinfo, text)) {
  2646. if (!this._toolTipElement) {
  2647. let div = $('#autoTip')[0];
  2648. if (!div) {
  2649. div = document.createElement("div");
  2650. $(div).css("position", "absolute")
  2651. .css("border", "1px #C0C0C0 solid")
  2652. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  2653. .css("font", "9pt Arial")
  2654. .css("background", "white")
  2655. .css("padding", 5)
  2656. .css("z-index", 999)
  2657. .css("max-width", tipsMaxHintWidth)
  2658. .css("word-wrap", "break-word")
  2659. .attr("id", 'autoTip');
  2660. document.body.insertBefore(div, null);
  2661. }
  2662. const validWidth = $(window).width() - (pos.x + hitinfo.x + tipsIndent) - tipsBorderIndent;
  2663. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  2664. if (validWidth >= tipsMaxHintWidth || textWidth <= validWidth) {
  2665. $(div).html(text).css("top", pos.y + hitinfo.y + tipsIndent).css("left", pos.x + hitinfo.x + tipsIndent);
  2666. } else if (textWidth > tipsMaxHintWidth) {
  2667. $(div).html(text).css("top", pos.y + hitinfo.y + tipsIndent).css("left", pos.x + hitinfo.x - tipsIndent - tipsMaxHintWidth);
  2668. } else {
  2669. $(div).html(text).css("top", pos.y + hitinfo.y + tipsIndent).css("left", pos.x + hitinfo.x - tipsIndent - textWidth);
  2670. }
  2671. this._toolTipElement = div;
  2672. $(div).show("fast");
  2673. }
  2674. }
  2675. };
  2676. /**
  2677. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2678. * @param {Object} hitinfo - 见getHitInfo返回值
  2679. */
  2680. proto.processMouseLeave = function (hitinfo) {
  2681. if (this._toolTipElement) {
  2682. $(this._toolTipElement).hide();
  2683. this._toolTipElement = null;
  2684. }
  2685. };
  2686. return new stackedBarCellType();
  2687. },
  2688. },
  2689. RowHeader: {
  2690. getTagRowHeader: function (setting) {
  2691. const indent = setting.indent || 18, maxHintWidth = 200, borderIndent = 10;
  2692. const height = setting.tagSize ? 11 * setting.tagSize : 11, width = setting.tagSize ? 10 * setting.tagSize : 10;
  2693. const tagFont = setting.tagFont || '9px 微软雅黑';
  2694. const drawTag = function (canvas, x, y, fillColor) {
  2695. canvas.save();
  2696. // 设置偏移量
  2697. canvas.translate(0.5, 0.5);
  2698. canvas.beginPath();
  2699. canvas.moveTo(x, y);
  2700. canvas.lineTo(x, y+height/11*5);
  2701. canvas.lineTo(x+width/10*5.5, y+height);
  2702. canvas.lineTo(x+width, y+height-width/10*4.5);
  2703. canvas.lineTo(x+width/11*5, y);
  2704. canvas.lineTo(x, y);
  2705. canvas.stroke();
  2706. canvas.fillStyle = fillColor instanceof Array ? fillColor[0] : fillColor;
  2707. canvas.fill();
  2708. canvas.beginPath();
  2709. canvas.arc(x+2, y+2, 1, 0, Math.PI*2, true);
  2710. canvas.closePath();
  2711. canvas.fillStyle = 'white';
  2712. canvas.fill();
  2713. if (fillColor instanceof Array && fillColor.length > 1) {
  2714. canvas.fillStyle = '#444444';
  2715. canvas.font = tagFont;
  2716. canvas.fillText(fillColor.length, x+width/11*13, y+height/11*14);
  2717. }
  2718. canvas.restore();
  2719. };
  2720. let TagCellType = function (){};
  2721. TagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  2722. const proto = TagCellType.prototype;
  2723. proto.getTagColor = setting.getColor;
  2724. proto.getTagHtml = setting.getTagHtml;
  2725. proto.basePaint = proto.paint;
  2726. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2727. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  2728. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - indent, h, style, options]);
  2729. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  2730. const color = this.getTagColor(options.row, node);
  2731. const centerX = x + w - indent + height/2, centerY = y + h/2;
  2732. color && drawTag(canvas, centerX - height/2, centerY - width/2, color);
  2733. };
  2734. /**
  2735. * 获取点击信息
  2736. * @param {Number} x
  2737. * @param {Number} y
  2738. * @param {Object} cellStyle
  2739. * @param {Object} cellRect
  2740. * @param {Object} context
  2741. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2742. */
  2743. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2744. return {
  2745. x: x,
  2746. y: y,
  2747. row: context.row,
  2748. col: context.col,
  2749. cellStyle: cellStyle,
  2750. cellRect: cellRect,
  2751. sheet: context.sheet,
  2752. sheetArea: context.sheetArea,
  2753. ctx: context.sheet.getParent().xs,
  2754. };
  2755. };
  2756. /**
  2757. * 鼠标进入单元格事件 - 显示悬浮提示
  2758. * @param {Object} hitinfo - 见getHitInfo返回值
  2759. */
  2760. proto.processMouseEnter = function (hitinfo) {
  2761. const node = SpreadJsObj.getRowObject(hitinfo.sheet, hitinfo.row);
  2762. let html = this.getTagHtml(hitinfo.row, node);
  2763. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  2764. if (pos && html) {
  2765. if (!this._tagTipElement) {
  2766. let div = $('#autoTip')[0];
  2767. if (!div) {
  2768. div = document.createElement("div");
  2769. $(div).css("position", "absolute")
  2770. .css("border", "1px #C0C0C0 solid")
  2771. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  2772. .css("font", "9pt Arial")
  2773. .css("background", "white")
  2774. .css("padding", 5)
  2775. .css("width", maxHintWidth)
  2776. .css("z-index", 999)
  2777. .attr("id", 'autoTagTip')
  2778. .css("top", pos.y + hitinfo.y + indent)
  2779. .css("left", pos.x + hitinfo.x + indent)
  2780. .html(html);
  2781. document.body.insertBefore(div, null);
  2782. }
  2783. this._tagTipElement = div;
  2784. $(div).show("fast");
  2785. }
  2786. }
  2787. };
  2788. /**
  2789. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2790. * @param {Object} hitinfo - 见getHitInfo返回值
  2791. */
  2792. proto.processMouseLeave = function (hitinfo) {
  2793. if (this._tagTipElement) {
  2794. $(this._tagTipElement).hide().remove();
  2795. this._tagTipElement = null;
  2796. }
  2797. };
  2798. return new TagCellType();
  2799. },
  2800. getCircleTagRowHeader: function (setting) {
  2801. const drawCircle = function (canvas, x, y, r, fillColor) {
  2802. canvas.save();
  2803. canvas.beginPath();
  2804. canvas.arc(x, y, r, 0, Math.PI*2, true);
  2805. canvas.closePath();
  2806. canvas.fillStyle = fillColor;
  2807. canvas.fill();
  2808. canvas.restore();
  2809. };
  2810. const drawCircle2 = function (canvas, x, y, r, lineColor, fillColor) {
  2811. canvas.save();
  2812. canvas.beginPath();
  2813. canvas.arc(x, y, r, 0, Math.PI*2, true);
  2814. canvas.closePath();
  2815. canvas.fillStyle = lineColor;
  2816. canvas.fill();
  2817. canvas.beginPath();
  2818. canvas.arc(x, y, r-2, 0, Math.PI*2, true);
  2819. canvas.closePath();
  2820. canvas.fillStyle = fillColor;
  2821. canvas.fill();
  2822. canvas.restore();
  2823. };
  2824. let CircleTagCellType = function (){};
  2825. CircleTagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  2826. const proto = CircleTagCellType.prototype;
  2827. proto.indent = setting.indent || 16;
  2828. proto.size = setting.size || 6;
  2829. proto.getTagColor = setting.getColor;
  2830. proto.basePaint = proto.paint;
  2831. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2832. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  2833. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - this.indent, h, style, options]);
  2834. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  2835. let backColor = style.backColor;
  2836. if (!backColor) {
  2837. const tag = options.sheet.getTag(options.row, options.col, options.sheetArea);
  2838. if (tag && tag === 'hover') backColor = '#dddfe1';
  2839. if (!backColor) {
  2840. let sel = options.sheet.getSelections();
  2841. sel = sel ? sel[0] : null;
  2842. backColor = (sel && options.row >= sel.row && options.row < sel.row + sel.rowCount ? '#dddfe1' : '#e9ecef');
  2843. }
  2844. }
  2845. let color = this.getTagColor(options.row, node);
  2846. color = color instanceof Array ? color : [color];
  2847. for (let i = color.length - 1; i >= 0; i--) {
  2848. drawCircle2(canvas, x + w - this.indent + 5 + i*5 , y + h/2, this.size, backColor, color[i]);
  2849. }
  2850. };
  2851. /**
  2852. * 获取点击信息
  2853. * @param {Number} x
  2854. * @param {Number} y
  2855. * @param {Object} cellStyle
  2856. * @param {Object} cellRect
  2857. * @param {Object} context
  2858. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2859. */
  2860. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2861. return {
  2862. x: x,
  2863. y: y,
  2864. row: context.row,
  2865. col: context.col,
  2866. cellStyle: cellStyle,
  2867. cellRect: cellRect,
  2868. sheet: context.sheet,
  2869. sheetArea: context.sheetArea,
  2870. ctx: context.sheet.getParent().xs,
  2871. };
  2872. };
  2873. /**
  2874. * 鼠标进入单元格事件 - 显示悬浮提示
  2875. * @param {Object} hitinfo - 见getHitInfo返回值
  2876. */
  2877. proto.processMouseEnter = function (hitinfo) {
  2878. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, 'hover', hitinfo.sheetArea);
  2879. };
  2880. /**
  2881. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2882. * @param {Object} hitinfo - 见getHitInfo返回值
  2883. */
  2884. proto.processMouseLeave = function (hitinfo) {
  2885. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, undefined, hitinfo.sheetArea);
  2886. };
  2887. return new CircleTagCellType();
  2888. },
  2889. },
  2890. ColHeader: {
  2891. getTipHeader: function () {
  2892. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  2893. const TipCellType = function () {};
  2894. // 继承 SpreadJs定义的 普通的TextCellType
  2895. TipCellType.prototype = new spreadNS.CellTypes.ColumnHeader();
  2896. const proto = TipCellType.prototype;
  2897. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  2898. const xs = hitinfo.sheet.getParent().xs;
  2899. const ctx = xs.childNodes[0].getContext("2d");
  2900. if (font && font !== '') {
  2901. ctx.font = font;
  2902. } else {
  2903. ctx.font = hitinfo.cellStyle.font;
  2904. }
  2905. return ctx.measureText(str).width;
  2906. };
  2907. proto.showTip = function (hitinfo, text) {
  2908. return text && text !== '';
  2909. };
  2910. /**
  2911. * 获取点击信息
  2912. * @param {Number} x
  2913. * @param {Number} y
  2914. * @param {Object} cellStyle
  2915. * @param {Object} cellRect
  2916. * @param {Object} context
  2917. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2918. */
  2919. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2920. return {
  2921. x: x,
  2922. y: y,
  2923. row: context.row,
  2924. col: context.col,
  2925. cellStyle: cellStyle,
  2926. cellRect: cellRect,
  2927. sheet: context.sheet,
  2928. sheetArea: context.sheetArea,
  2929. ctx: context.sheet.getParent().xs,
  2930. };
  2931. };
  2932. /**
  2933. * 鼠标进入单元格事件 - 显示悬浮提示
  2934. * @param {Object} hitinfo - 见getHitInfo返回值
  2935. */
  2936. proto.processMouseEnter = function (hitinfo) {
  2937. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  2938. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  2939. if (col.getHeaderTip && Object.prototype.toString.apply(col.getHeaderTip) === "[object Function]") {
  2940. text = col.getHeaderTip();
  2941. }
  2942. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  2943. if (pos && this.showTip(hitinfo, text)) {
  2944. if (!this._toolTipElement) {
  2945. let div = $('#autoTip')[0];
  2946. if (!div) {
  2947. div = document.createElement("div");
  2948. $(div).css("position", "absolute")
  2949. .css("border", "1px #C0C0C0 solid")
  2950. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  2951. .css("font", "9pt Arial")
  2952. .css("background", "white")
  2953. .css("padding", 5)
  2954. .css("z-index", 999)
  2955. .css("max-width", maxHintWidth)
  2956. .css("word-wrap", "break-word")
  2957. .attr("id", 'autoTip');
  2958. document.body.insertBefore(div, null);
  2959. }
  2960. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  2961. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  2962. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  2963. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  2964. } else if (textWidth > maxHintWidth) {
  2965. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  2966. } else {
  2967. $(div).html(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  2968. }
  2969. this._toolTipElement = div;
  2970. $(div).show("fast");
  2971. }
  2972. }
  2973. };
  2974. /**
  2975. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2976. * @param {Object} hitinfo - 见getHitInfo返回值
  2977. */
  2978. proto.processMouseLeave = function (hitinfo) {
  2979. if (this._toolTipElement) {
  2980. $(this._toolTipElement).hide();
  2981. this._toolTipElement = null;
  2982. }
  2983. };
  2984. return new TipCellType();
  2985. },
  2986. },
  2987. Formatter: {
  2988. baseNumberFormatter: function () {
  2989. const formatter = function () {};
  2990. formatter.prototype = new GC.Spread.Formatter.FormatterBase();
  2991. const proto = formatter.prototype;
  2992. /**
  2993. * 格式化数字显示方式
  2994. * 用法
  2995. * formatNumber(12345.999,'#,##0.00');
  2996. * formatNumber(12345.999,'#,##0.##');
  2997. * formatNumber(123,'000000');
  2998. * @param num
  2999. * @param pattern
  3000. */
  3001. proto.formatNum = function (num, pattern) {
  3002. var minusStr = num < 0 ? '-' : '';
  3003. var strarr = num ? Math.abs(num).toString().split('.'):['0'];
  3004. var fmtarr = pattern?pattern.split('.'):[''];
  3005. var retstr='';
  3006. // 整数部分
  3007. var str = strarr[0];
  3008. var fmt = fmtarr[0];
  3009. var i = str.length-1;
  3010. var comma = false;
  3011. for(var f=fmt.length-1;f>=0;f--){
  3012. switch(fmt.substr(f,1)){
  3013. case '#':
  3014. if(i>=0 ) retstr = str.substr(i--,1) + retstr;
  3015. break;
  3016. case '0':
  3017. if(i>=0) retstr = str.substr(i--,1) + retstr;
  3018. else retstr = '0' + retstr;
  3019. break;
  3020. case ',':
  3021. comma = true;
  3022. retstr=','+retstr;
  3023. break;
  3024. }
  3025. }
  3026. if(i>=0){
  3027. if(comma){
  3028. var l = str.length;
  3029. for(;i>=0;i--){
  3030. retstr = str.substr(i,1) + retstr;
  3031. if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;
  3032. }
  3033. }
  3034. else retstr = str.substr(0,i+1) + retstr;
  3035. }
  3036. retstr = retstr+'.';
  3037. // 处理小数部分
  3038. str=strarr.length>1?strarr[1]:'';
  3039. fmt=fmtarr.length>1?fmtarr[1]:'';
  3040. i=0;
  3041. for(var f=0;f<fmt.length;f++){
  3042. switch(fmt.substr(f,1)){
  3043. case '#':
  3044. if(i<str.length) retstr+=str.substr(i++,1);
  3045. break;
  3046. case '0':
  3047. if(i<str.length) retstr+= str.substr(i++,1);
  3048. else retstr+='0';
  3049. break;
  3050. }
  3051. }
  3052. return minusStr + retstr.replace(/^,+/,'').replace(/\.$/,'');
  3053. };
  3054. proto.format = function (obj, formattedData) {
  3055. if (this.pattern) {
  3056. return this.formatNum(obj, this.pattern);
  3057. } else {
  3058. return obj;
  3059. }
  3060. };
  3061. return new formatter();
  3062. },
  3063. _numFormatter: [],
  3064. getNumberFormatter(pattern) {
  3065. let formatter = this._numFormatter.find(function (f) {
  3066. return f.pattern === pattern;
  3067. });
  3068. if (!formatter) {
  3069. formatter = this.baseNumberFormatter();
  3070. formatter.pattern = pattern;
  3071. this._numFormatter.push(formatter);
  3072. }
  3073. return formatter;
  3074. },
  3075. },
  3076. Clipboard: (function () {
  3077. let cText = '', cHtml;
  3078. const setCopyData = function (text, html) {
  3079. cText = text;
  3080. cHtml = html;
  3081. };
  3082. const getPasteData = function () {
  3083. return {text: cText, html: cHtml};
  3084. };
  3085. const clearData = function () {
  3086. cText = '';
  3087. cHtml = undefined;
  3088. };
  3089. const setSheetFilterCopyData = function (sheet) {
  3090. cText = SpreadJsObj.getFilterCopyText(sheet);
  3091. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  3092. };
  3093. const getAnalysisPasteText = function () {
  3094. return SpreadJsObj.analysisPasteText(cText);
  3095. };
  3096. const getAnalysisPasteHtml = function () {
  3097. return SpreadJsObj.analysisPasteHtml(cHtml);
  3098. };
  3099. const setSysClipboard = function(text) {
  3100. let textArea = document.createElement("textarea");
  3101. textArea.style.position = 'fixed';
  3102. textArea.style.top = 0;
  3103. textArea.style.left = 0;
  3104. textArea.style.width = '2em';
  3105. textArea.style.height = '2em';
  3106. textArea.style.padding = 0;
  3107. textArea.style.border = 'none';
  3108. textArea.style.outline = 'none';
  3109. textArea.style.boxShadow = 'none';
  3110. textArea.style.background = 'transparent';
  3111. textArea.value = text;
  3112. document.body.appendChild(textArea);
  3113. textArea.select();
  3114. try {
  3115. document.execCommand('copy');
  3116. } catch (err) {
  3117. }
  3118. document.body.removeChild(textArea);
  3119. };
  3120. return {
  3121. setCopyData, getPasteData, clearData,
  3122. setSheetFilterCopyData,
  3123. getAnalysisPasteText, getAnalysisPasteHtml,
  3124. setSysClipboard
  3125. }
  3126. })(),
  3127. };