spreadjs_zh.js 147 KB

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