spreadjs_zh.js 153 KB

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