spreadjs_zh.js 141 KB

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