spreadjs_zh.js 148 KB

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