spreadjs_zh.js 150 KB

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