spreadjs_zh.js 141 KB

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