spreadjs_zh.js 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949
  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(0);
  302. sheet.setColumnCount(setting.cols.length);
  303. sheet.setRowCount(setting.headRows, spreadNS.SheetArea.colHeader);
  304. for (let iRow = 0; iRow < setting.headRowHeight.length; iRow ++) {
  305. sheet.setRowHeight(iRow, setting.headRowHeight[iRow], spreadNS.SheetArea.colHeader);
  306. }
  307. for (let iCol = 0; iCol < setting.cols.length; iCol++) {
  308. const col = setting.cols[iCol];
  309. const title = col.title.split('|');
  310. const colSpan = col.colSpan ? col.colSpan.split('|'): ['1'], rowSpan = col.rowSpan ? col.rowSpan.split('|'): ['1'];
  311. for (let i = 0; i < title.length; i++) {
  312. const cell = sheet.getCell(i, iCol, spreadNS.SheetArea.colHeader);
  313. cell.text(title[i]).wordWrap(true);
  314. if ((colSpan[i] !== '' && colSpan[i] !== '1') || (rowSpan[i] !== '' && rowSpan[i] !== '1')) {
  315. sheet.addSpan(i, iCol, parseInt(rowSpan[i]), parseInt(colSpan[i]), spreadNS.SheetArea.colHeader);
  316. }
  317. }
  318. sheet.setColumnWidth(iCol, col.width);
  319. if (col.visible !== undefined && col.visible !== null) {
  320. sheet.setColumnVisible(iCol, col.visible);
  321. }
  322. }
  323. sheet.rowOutlines.direction(spreadNS.Outlines.OutlineDirection.backward);
  324. sheet.showRowOutline(false);
  325. if (setting.defaultRowHeight) {
  326. sheet.defaults.rowHeight = setting.defaultRowHeight;
  327. }
  328. if (setting.frozenLineColor) sheet.options.frozenlineColor = setting.frozenLineColor;
  329. if (setting.frozenColCount) sheet.frozenColumnCount(setting.frozenColCount);
  330. },
  331. reinitSheetHeader: function (sheet) {
  332. this.beginMassOperation(sheet);
  333. if (sheet.zh_setting.headColWidth) {
  334. for (const [i, w] of sheet.zh_setting.headColWidth.entries()) {
  335. sheet.setColumnWidth(i, w, spreadNS.SheetArea.rowHeader);
  336. }
  337. }
  338. this._initSheetHeader(sheet);
  339. this.endMassOperation(sheet);
  340. },
  341. _rememberColWidth: function (sheet) {
  342. sheet.bind(spreadNS.Events.ColumnWidthChanged, function (e, info) {
  343. if (!info.sheet.zh_setting) return;
  344. const setting = info.sheet.zh_setting;
  345. if (!setting.localCache || !setting.localCache.key) return;
  346. if (!setting.localCache.colWidthCache) return;
  347. const cache = setting.localCache.colWidthCache;
  348. for (const col of info.colList) {
  349. const colSetting = setting.cols[col];
  350. if (colSetting && colSetting.field)
  351. cache[colSetting.field] = info.sheet.getColumnWidth(col);
  352. }
  353. setLocalCache(setting.localCache.key + '-colWidth', JSON.stringify(cache));
  354. });
  355. },
  356. _loadCacheSetting: function (sheet, setting) {
  357. if (!setting.localCache || !setting.localCache.key) return;
  358. if (setting.localCache.colWidth) {
  359. const cache = getLocalCache(setting.localCache.key + '-colWidth');
  360. setting.localCache.colWidthCache = cache ? JSON.parse(cache) : {};
  361. for (const prop in setting.localCache.colWidthCache) {
  362. const col = _.find(setting.cols, {field: prop});
  363. if (col) col.width = setting.localCache.colWidthCache[prop];
  364. }
  365. }
  366. },
  367. /**
  368. * 初始化sheet, 设置sheet.zh_setting, 并初始化表头
  369. * @param {GC.Spread.Sheets.Worksheet} sheet
  370. * @param setting
  371. */
  372. initSheet: function (sheet, setting) {
  373. const self = this;
  374. this.beginMassOperation(sheet);
  375. this._loadCacheSetting(sheet, setting);
  376. this._rememberColWidth(sheet);
  377. setting.pos = sheet.getParent().pos;
  378. if (!setting.tree) setting.tree = {};
  379. sheet.zh_setting = setting;
  380. if (sheet.zh_setting.headColWidth) {
  381. for (const [i, w] of sheet.zh_setting.headColWidth.entries()) {
  382. sheet.setColumnWidth(i, w, spreadNS.SheetArea.rowHeader);
  383. }
  384. }
  385. this._initSheetDeafult(sheet);
  386. this._initSheetHeader(sheet);
  387. sheet.setRowCount(sheet.zh_setting.emptyRows);
  388. sheet.extendCellType = {};
  389. sheet.extendRowHeader = {};
  390. sheet.zh_setting.rowHeader && sheet.zh_setting.rowHeader.forEach(function (col, j) {
  391. self._defineRowHeader(sheet, j, col);
  392. });
  393. sheet.borderLine = new spreadNS.LineBorder('#cccccc', spreadNS.LineStyle.thin);
  394. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly).setBorder(sheet.borderLine, {all: true});
  395. if (setting.selectedBackColor) {
  396. SpreadJsObj.selChangedRefreshBackColor(sheet);
  397. }
  398. this.endMassOperation(sheet);
  399. if (!setting.invalidCopyFilterHiddenRow) this.copyFilterHiddenRow(sheet);
  400. },
  401. reLoadSheetHeader: function (sheet) {
  402. if (sheet.zh_setting) {
  403. this.beginMassOperation(sheet);
  404. this._loadCacheSetting(sheet, sheet.zh_setting);
  405. this._initSheetHeader(sheet);
  406. this.endMassOperation(sheet);
  407. }
  408. },
  409. _loadCellData: function (sheet, data, iRow, iCol) {
  410. if (!data || !sheet.zh_setting) return;
  411. const col = sheet.zh_setting.cols[iCol];
  412. const cell = sheet.getCell(iRow, iCol);
  413. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  414. cell.value(col.getValue(data));
  415. } else if (col.field !== '' && data[col.field]) {
  416. cell.value(data[col.field]);
  417. }
  418. let font = sheet.getDefaultStyle().font;
  419. if (col.font) {
  420. font = col.font;
  421. }
  422. if (sheet.zh_setting.tree.getFont && Object.prototype.toString.apply(sheet.zh_setting.tree.getFont) === "[object Function]") {
  423. font = sheet.zh_setting.tree.getFont(sheet, data, iRow, col, font);
  424. }
  425. cell.font(font);
  426. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  427. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  428. } else {
  429. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  430. }
  431. if(col.type === 'Number') {
  432. if (col.formatter) {
  433. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  434. } else {
  435. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  436. }
  437. } else if (col.formatter) {
  438. cell.formatter(col.formatter);
  439. }
  440. cell.backColor(SpreadJsObj._getBackColor(sheet, data, iRow, col));
  441. cell.foreColor(SpreadJsObj._getForeColor(sheet, data, iRow, col));
  442. cell.setBorder(sheet.borderLine, {all: true});
  443. },
  444. _getForeColor: function (sheet, data, row, col) {
  445. let foreColor = sheet.getDefaultStyle().foreColor;
  446. if (sheet.zh_setting.tree.getForeColor && Object.prototype.toString.apply(sheet.zh_setting.tree.getForeColor) === "[object Function]") {
  447. foreColor = sheet.zh_setting.tree.getColor(sheet, data, row, col, foreColor);
  448. }
  449. if (sheet.zh_setting.getForeColor && Object.prototype.toString.apply(sheet.zh_setting.getForeColor) === "[object Function]") {
  450. foreColor = sheet.zh_setting.getForeColor(sheet, data, row, col, foreColor);
  451. }
  452. return foreColor;
  453. },
  454. _getBackColor: function (sheet, data, row, col) {
  455. let backColor = sheet.getDefaultStyle().backColor;
  456. let sels = sheet.getSelections();
  457. if (sheet.zh_setting.selectedBackColor && sels && sels[0].row === row ) {
  458. return sheet.zh_setting.selectedBackColor;
  459. } else {
  460. if (sheet.zh_setting.tree.getColor && Object.prototype.toString.apply(sheet.zh_setting.tree.getColor) === "[object Function]") {
  461. backColor = sheet.zh_setting.tree.getColor(sheet, data, row, col, backColor);
  462. }
  463. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  464. backColor = sheet.zh_setting.getColor(sheet, data, row, col, backColor);
  465. }
  466. return backColor;
  467. }
  468. },
  469. _loadRowStyle: function (sheet, data, row) {
  470. sheet.zh_setting.cols.forEach(function (col, j) {
  471. const cell = sheet.getCell(row, j);
  472. if (col.font) {
  473. cell.font(col.font);
  474. }
  475. const readOnly1 = (sheet.zh_setting.readOnly && Object.prototype.toString.apply(sheet.zh_setting.readOnly) === "[object Function]")
  476. ? sheet.zh_setting.readOnly(data) : (sheet.zh_setting.readOnly || false);
  477. const readOnly2 = (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]")
  478. ? col.readOnly(data) : col.readOnly || false;
  479. cell.locked(readOnly1 || readOnly2).vAlign(1).hAlign(col.hAlign);
  480. if(col.type === 'Number') {
  481. if (col.formatter) {
  482. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  483. } else {
  484. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  485. }
  486. } else if (col.formatter) {
  487. cell.formatter(col.formatter);
  488. }
  489. cell.setBorder(sheet.borderLine, {all: true});
  490. });
  491. },
  492. _loadRowData: function (sheet, data, row) {
  493. // 单元格重新写入数据
  494. if (!data) { return }
  495. if (sheet.zh_dataType === SpreadJsObj.DataType.Tree && !data.visible && data.waitingLoading === undefined) {
  496. data.waitingLoading = true;
  497. return;
  498. }
  499. sheet.zh_setting.cols.forEach(function (col, j) {
  500. const cell = sheet.getCell(row, j);
  501. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  502. cell.value(col.getValue(data));
  503. } else if (col.field !== '' && data[col.field]) {
  504. cell.value(data[col.field]);
  505. }
  506. let font = sheet.getDefaultStyle().font;
  507. if (col.font) {
  508. font = col.font;
  509. }
  510. if (sheet.zh_setting.tree.getFont && Object.prototype.toString.apply(sheet.zh_setting.tree.getFont) === "[object Function]") {
  511. font = sheet.zh_setting.tree.getFont(sheet, data, row, col, font);
  512. }
  513. cell.font(font);
  514. if (col.foreColor) {
  515. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  516. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  517. } else {
  518. cell.foreColor(col.foreColor);
  519. }
  520. }
  521. const readOnly1 = (sheet.zh_setting.readOnly && Object.prototype.toString.apply(sheet.zh_setting.readOnly) === "[object Function]")
  522. ? sheet.zh_setting.readOnly(data) : (sheet.zh_setting.readOnly || false);
  523. const readOnly2 = (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]")
  524. ? col.readOnly(data) : col.readOnly || false;
  525. cell.locked(readOnly1 || readOnly2).vAlign(1).hAlign(col.hAlign);
  526. if(col.type === 'Number') {
  527. if (col.formatter) {
  528. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter(col.formatter))
  529. } else {
  530. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  531. }
  532. } else if (col.formatter) {
  533. cell.formatter(col.formatter);
  534. }
  535. cell.backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  536. cell.foreColor(SpreadJsObj._getForeColor(sheet, data, row, col));
  537. cell.setBorder(sheet.borderLine, {all: true});
  538. data.waitingLoading = false;
  539. });
  540. },
  541. _addActivePaintEvents: function (sheet, cellType) {
  542. if (!sheet.ActiveType) {
  543. sheet.ActiveType = [];
  544. }
  545. sheet.ActiveType.push(cellType);
  546. if (!sheet.AcitveRefresh) {
  547. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  548. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  549. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  550. info.sheet.leaveCell = {row: info.row, col: info.col};
  551. } else {
  552. delete info.sheet.leaveCell;
  553. }
  554. });
  555. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  556. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  557. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  558. info.sheet.repaint(info.sheet.getCellRect(info.row, info.col));
  559. }
  560. if (info.sheet.leaveCell) {
  561. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  562. }
  563. });
  564. sheet.AcitveRefresh = true;
  565. }
  566. },
  567. _defineRowHeader: function (sheet, col, colSetting) {
  568. if (colSetting.rowHeaderType === 'tag') {
  569. if (!sheet.extendRowHeader.tag) {
  570. sheet.extendRowHeader.tag = this.RowHeader.getTagRowHeader(colSetting.setting);
  571. }
  572. sheet.getRange(-1, col, -1, 1, spreadNS.SheetArea.rowHeader).cellType(sheet.extendRowHeader.tag);
  573. }
  574. if (colSetting.rowHeaderType === 'circle') {
  575. if (!sheet.extendRowHeader.multiTag) {
  576. sheet.extendRowHeader.multiTag = this.RowHeader.getCircleTagRowHeader(colSetting.setting);
  577. }
  578. sheet.getRange(-1, col, -1, 1, spreadNS.SheetArea.rowHeader).cellType(sheet.extendRowHeader.multiTag);
  579. }
  580. },
  581. _defineColCellType: function (sheet, col, colSetting) {
  582. sheet.AcitveComboRefresh = false;
  583. if(colSetting.cellType === 'ellipsis') {
  584. if (!sheet.extendCellType.ellipsis) {
  585. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  586. }
  587. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  588. }
  589. if(colSetting.cellType === 'ellipsisAutoTip') {
  590. if (!sheet.extendCellType.ellipsisAutoTip) {
  591. sheet.extendCellType.ellipsisAutoTip = this.CellType.getEllipsisTextAutoTipCellType();
  592. }
  593. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsisAutoTip);
  594. }
  595. if(colSetting.cellType === 'html') {
  596. if (!sheet.extendCellType.html) {
  597. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  598. }
  599. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  600. }
  601. if (colSetting.cellType === 'image') {
  602. if (!sheet.extendCellType.image) {
  603. sheet.extendCellType.image = this.CellType.getImageCellType();
  604. }
  605. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  606. }
  607. if (colSetting.cellType === 'imageBtn') {
  608. if (!sheet.extendCellType.imageBtn) {
  609. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  610. }
  611. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  612. }
  613. if (colSetting.cellType === 'activeImageBtn') {
  614. if (!sheet.extendCellType.activeImageBtn) {
  615. sheet.extendCellType.activeImageBtn = this.CellType.getActiveImageButtonCellType();
  616. }
  617. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.activeImageBtn);
  618. }
  619. if (colSetting.cellType === 'tree') {
  620. if (!sheet.extendCellType.tree) {
  621. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  622. }
  623. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  624. }
  625. if (colSetting.cellType === 'tip') {
  626. if (!sheet.extendCellType.tip) {
  627. sheet.extendCellType.tip = this.CellType.getTipCellType();
  628. }
  629. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  630. }
  631. if (colSetting.cellType === 'autoTip') {
  632. if (!sheet.extendCellType.autoTip) {
  633. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  634. }
  635. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  636. }
  637. if (colSetting.cellType === 'delayTip') {
  638. if (!sheet.extendCellType.tip) {
  639. sheet.extendCellType.tip = this.CellType.getDelayTipCellType();
  640. }
  641. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  642. }
  643. if (colSetting.cellType === 'checkbox') {
  644. if (!sheet.extendCellType.checkbox) {
  645. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  646. }
  647. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  648. sheet.getRange(-1, col, -1, 1).hAlign(col ? col.hAlign : spreadNS.HorizontalAlign.center);// 空白行不居中问题,checkbox要默认居中
  649. }
  650. if (colSetting.cellType === 'unit') {
  651. if (!sheet.extendCellType.unit) {
  652. const comboEdit = colSetting.comboEdit ? colSetting.comboEdit : false;
  653. sheet.extendCellType.unit = colSetting.comboItems ? this.CellType.getUnitCellType(comboEdit, colSetting.comboItems) : this.CellType.getUnitCellType(comboEdit);
  654. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.unit);
  655. }
  656. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  657. }
  658. if (colSetting.cellType === 'customizeCombo') {
  659. const cellKey = colSetting.cellTypeKey ? 'customizeCombo-' + colSetting.cellTypeKey : 'customizeCombo';
  660. if (!sheet.extendCellType[cellKey]) {
  661. sheet.extendCellType[cellKey] = this.CellType.getCustomizeComboCellType(colSetting.comboItems);
  662. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType[cellKey]);
  663. }
  664. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  665. }
  666. if (colSetting.cellType === 'stageCombo') {
  667. if (!sheet.extendCellType.stageCombo) {
  668. sheet.extendCellType.stageCombo = this.CellType.getStageComboCellType();
  669. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.stageCombo);
  670. }
  671. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.stageCombo);
  672. }
  673. if (colSetting.cellType === 'specCombo') {
  674. if (!sheet.extendCellType.specCombo) {
  675. sheet.extendCellType.specCombo = this.CellType.getSpecComboCellType(colSetting.comboItems);
  676. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.specCombo);
  677. }
  678. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.specCombo);
  679. }
  680. if (colSetting.cellType === 'datepicker') {
  681. if (!sheet.extendCellType.datepicker) {
  682. sheet.extendCellType.datepicker = this.CellType.getDatePickerCellType();
  683. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.datepicker);
  684. }
  685. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.datepicker);
  686. }
  687. if (colSetting.cellType === 'mouseTouch') {
  688. if (!sheet.extendCellType.mouseTouch) {
  689. sheet.extendCellType.mouseTouch = this.CellType.getMouseTouchCellType();
  690. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.mouseTouch);
  691. }
  692. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.mouseTouch);
  693. }
  694. if (colSetting.cellType === 'stackedBar') {
  695. if (!sheet.extendCellType.stackedBar) {
  696. sheet.extendCellType.stackedBar = this.CellType.getStackedBarCellType();
  697. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.stackedBar);
  698. }
  699. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.stackedBar);
  700. }
  701. if (colSetting.formatter) {
  702. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  703. if(colSetting.type === 'Number') {
  704. if (col.formatter) {
  705. sheet.getRange(-1, col, -1, 1).formatter(SpreadJsObj.Formatter.getNumberFormatter(colSetting.formatter));
  706. } else {
  707. sheet.getRange(-1, col, -1, 1).formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  708. }
  709. } else if (col.formatter) {
  710. cell.formatter(col.formatter);
  711. }
  712. }
  713. },
  714. /**
  715. * 整个sheet重新加载数据
  716. * @param {GC.Spread.Sheets.Worksheet} sheet
  717. */
  718. reLoadSheetData: function (sheet, force = false) {
  719. const self = this;
  720. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  721. this.beginMassOperation(sheet);
  722. try {
  723. force && sheet.setRowCount(0, spreadNS.SheetArea.viewport);
  724. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  725. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  726. // 设置总行数
  727. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  728. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  729. // 控制空白行
  730. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  731. emptyRows.locked(sheet.zh_dataType === 'tree' || sheet.zh_setting.readOnly);
  732. if (sortData) {
  733. // 单元格写入数据
  734. sortData.forEach(function (data, i) {
  735. self._loadRowData(sheet, data, i);
  736. sheet.setRowVisible(i, data.visible);
  737. });
  738. // for (let iRow = sortData.length - 1; iRow < totalRow; iRow++) {
  739. // self._loadRowStyle(sheet, iRow);
  740. // }
  741. }
  742. // 设置列单元格格式
  743. sheet.zh_setting.cols.forEach(function (col, j) {
  744. //if (!col.cellType) { return; }
  745. self._defineColCellType(sheet, j, col);
  746. });
  747. this.endMassOperation(sheet);
  748. } catch (err) {
  749. console.log(err);
  750. this.endMassOperation(sheet);
  751. }
  752. },
  753. /**
  754. * 重新加载部分数据行
  755. * @param {GC.Spread.Sheets.Worksheet} sheet
  756. * @param {Number} row
  757. * @param {Number} count
  758. */
  759. reLoadRowData: function (sheet, row, count = 1) {
  760. if (row < 0) { return; }
  761. const self = this;
  762. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  763. this.beginMassOperation(sheet);
  764. try {
  765. // 清空原单元格数据
  766. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  767. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  768. // 单元格重新写入数据
  769. for (let i = row; i < row + count; i++) {
  770. const data = sortData[i];
  771. if (!data) { continue; }
  772. this._loadRowData(sheet, data, i);
  773. sheet.setRowVisible(i, data.visible);
  774. }
  775. this.endMassOperation(sheet);
  776. } catch (err) {
  777. this.endMassOperation(sheet);
  778. }
  779. },
  780. /**
  781. * 重新加载部分行数据
  782. * @param {GC.Spread.Sheets.Worksheet} sheet
  783. * @param {Array} rows
  784. */
  785. reLoadRowsData: function (sheet, rows) {
  786. const self = this;
  787. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  788. this.beginMassOperation(sheet);
  789. try {
  790. for (const row of rows) {
  791. if (row < 0) { continue; }
  792. // 清空原单元格数据
  793. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  794. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  795. const data = sortData[row];
  796. // 单元格重新写入数据
  797. this._loadRowData(sheet, data, row);
  798. sheet.setRowVisible(row, data.visible);
  799. };
  800. this.endMassOperation(sheet);
  801. } catch (err) {
  802. this.endMassOperation(sheet);
  803. }
  804. },
  805. reloadRowForeColor: function (sheet, row, count) {
  806. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  807. this.beginMassOperation(sheet);
  808. try {
  809. for (let i = row; i < row + count; i++) {
  810. if (i < 0) { continue; }
  811. const data = sortData[i];
  812. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  813. sheet.getCell(i, iCol).foreColor(SpreadJsObj._getForeColor(sheet, data, i, col));
  814. }
  815. };
  816. this.endMassOperation(sheet);
  817. } catch (err) {
  818. this.endMassOperation(sheet);
  819. }
  820. },
  821. reloadRowsForeColor: function (sheet, rows) {
  822. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  823. this.beginMassOperation(sheet);
  824. try {
  825. for (const row of rows) {
  826. if (row < 0) { continue; }
  827. const data = sortData[row];
  828. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  829. sheet.getCell(row, iCol).foreColor(SpreadJsObj._getForeColor(sheet, data, row, col));
  830. }
  831. };
  832. this.endMassOperation(sheet);
  833. } catch (err) {
  834. this.endMassOperation(sheet);
  835. }
  836. },
  837. reloadRowBackColor: function (sheet, row, count = 1) {
  838. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  839. this.beginMassOperation(sheet);
  840. try {
  841. for (let i = row; i < row + count; i++) {
  842. if (i < 0) { continue; }
  843. const data = sortData[i];
  844. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  845. sheet.getCell(i, iCol).backColor(SpreadJsObj._getBackColor(sheet, data, i, col));
  846. }
  847. };
  848. this.endMassOperation(sheet);
  849. } catch (err) {
  850. this.endMassOperation(sheet);
  851. }
  852. },
  853. reloadRowsBackColor: function (sheet, rows) {
  854. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  855. this.beginMassOperation(sheet);
  856. try {
  857. for (const row of rows) {
  858. if (row < 0) { continue; }
  859. const data = sortData[row];
  860. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  861. sheet.getCell(row, iCol).backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  862. }
  863. };
  864. this.endMassOperation(sheet);
  865. } catch (err) {
  866. this.endMassOperation(sheet);
  867. }
  868. },
  869. reloadRowsReadonly: function (sheet, rows) {
  870. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  871. this.beginMassOperation(sheet);
  872. try {
  873. for (const row of rows) {
  874. if (row < 0) { continue; }
  875. this._loadRowStyle(sheet, sortData[row], row);
  876. };
  877. } catch (err) {
  878. }
  879. this.endMassOperation(sheet);
  880. },
  881. reloadColData: function (sheet, col, count = 1) {
  882. const cols = [];
  883. for (let i = 0; i < count; i++) {
  884. cols.push(col+i);
  885. }
  886. this.reLoadColsData(sheet, cols);
  887. },
  888. /**
  889. * 重新加载部分列数据
  890. * @param {GC.Spread.Sheets.Worksheet} sheet
  891. * @param {Array} cols
  892. */
  893. reLoadColsData: function (sheet, cols) {
  894. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  895. if (!sortData) return;
  896. this.beginMassOperation(sheet);
  897. try {
  898. for (const col of cols) {
  899. // 清空原单元格数据
  900. sheet.clear(-1, col, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  901. for (const [iRow, data] of sortData.entries()) {
  902. this._loadCellData(sheet, data, iRow, col);
  903. }
  904. }
  905. this.endMassOperation(sheet);
  906. } catch (err) {
  907. console.log(err);
  908. this.endMassOperation(sheet);
  909. }
  910. },
  911. reLoadNodesData: function (sheet, nodes) {
  912. this.beginMassOperation(sheet);
  913. nodes = nodes instanceof Array ? nodes : [nodes];
  914. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  915. for (const node of nodes) {
  916. this._loadRowData(sheet, node, sortData.indexOf(node));
  917. }
  918. this.endMassOperation(sheet);
  919. },
  920. repaintNodesRowHeader: function (sheet, nodes) {
  921. nodes = nodes instanceof Array ? nodes : [nodes];
  922. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  923. const rowIndex = [];
  924. for (const node of nodes) {
  925. rowIndex.push(sortData.indexOf(node));
  926. }
  927. this.repaintRowsRowHeader(sheet, rowIndex);
  928. },
  929. repaintRowsRowHeader: function (sheet, rows) {
  930. for (const r of rows) {
  931. const cellRect = sheet.getCellRect(r, 0);
  932. cellRect.width = cellRect.x;
  933. cellRect.x = 0;
  934. sheet.repaint(cellRect);
  935. }
  936. },
  937. /**
  938. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  939. * @param {GC.Spread.Sheets.Worksheet} sheet
  940. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  941. * @param {Array|PathTree} data - 对dataType对应
  942. */
  943. loadSheetData: function (sheet, dataType, data, force = false){
  944. sheet.zh_dataType = dataType;
  945. if (dataType === 'tree') {
  946. sheet.zh_tree = data;
  947. } else {
  948. sheet.zh_data = data;
  949. }
  950. this.protectedSheet(sheet);
  951. this.reLoadSheetData(sheet, force);
  952. },
  953. /**
  954. * 获取复制数据HTML格式(过滤不可见单元格)
  955. * @param {GC.Spread.Sheets.Worksheet} sheet
  956. * @returns {string}
  957. */
  958. getFilterCopyHTML: function (sheet) {
  959. const sel = sheet.getSelections()[0];
  960. const html = [];
  961. html.push('<table>');
  962. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  963. // 跳过隐藏行
  964. if (!sheet.getCell(i, -1).visible()) { continue; }
  965. const rowHtml = [];
  966. rowHtml.push('<tr>');
  967. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  968. const data = sheet.getText(i, j);
  969. rowHtml.push('<td>' + data + '</td>');
  970. }
  971. rowHtml.push('</tr>');
  972. html.push(rowHtml.join(''));
  973. }
  974. html.push('</table>');
  975. return html.join('');
  976. },
  977. /**
  978. * 获取复制数据Text格式(过滤不可见单元格)
  979. * @param {GC.Spread.Sheets.Worksheet} sheet
  980. * @returns {string}
  981. */
  982. getFilterCopyText: function (sheet) {
  983. const copyData = [];
  984. const sel = sheet.getSelections()[0];
  985. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  986. // 跳过隐藏行
  987. if (!sheet.getCell(i, -1).visible()) { continue; }
  988. const rowText = [];
  989. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  990. const data = sheet.getText(i, j);
  991. rowText.push(data);
  992. }
  993. copyData.push(rowText.join('\t'));
  994. }
  995. return copyData.join('\r\n');
  996. },
  997. analysisPasteText: function (text, transpose = false) {
  998. const result = [];
  999. if (text === '') return result;
  1000. const rows = text.split('\r\n');
  1001. if (rows[rows.length - 1] === '') rows.splice(rows.length - 1, 1);
  1002. for (const r of rows) {
  1003. const cols = r.split('\t');
  1004. result.push(cols);
  1005. }
  1006. return transpose ? SpreadJsObj.transposePasteData(result) : result;
  1007. },
  1008. analysisPasteHtml: function (html) {
  1009. const result = [];
  1010. if (!html || html === '') return result;
  1011. },
  1012. transposePasteData: function (data) {
  1013. const result = [];
  1014. const rowCount = data.length, colCount = data.reduce((r, x) => { return Math.max(r, x.length); }, 0);
  1015. for (let c = 0; c < colCount; c++) {
  1016. const newRow = [];
  1017. for (let r = 0; r < rowCount; r++) {
  1018. newRow.push(data[r][c]);
  1019. }
  1020. result.push(newRow)
  1021. }
  1022. return result;
  1023. },
  1024. copyFilterHiddenRow: function (sheet) {
  1025. sheet.bind(spreadNS.Events.ClipboardChanged, function (e, info) {
  1026. SpreadJsObj.Clipboard.setSysClipboard(SpreadJsObj.getFilterCopyText(info.sheet));
  1027. });
  1028. },
  1029. /**
  1030. * 树表结构,定位至指定的节点
  1031. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  1032. * @param {Number} id - 定位节点的id
  1033. */
  1034. locateTreeNode: function (sheet, id, autoExpand = false) {
  1035. const tree = sheet.zh_tree;
  1036. if (!tree) { return }
  1037. const node = tree.getItems(id);
  1038. if (!node) { return }
  1039. if (autoExpand && !node.visible) {
  1040. const parents = tree.autoExpandNode(node);
  1041. if (parents && parents.length > 0) {
  1042. SpreadJsObj.reLoadNodesData(sheet, parents);
  1043. SpreadJsObj.refreshTreeRowVisible(sheet);
  1044. }
  1045. }
  1046. const index = tree.nodes.indexOf(node);
  1047. const sels = sheet.getSelections();
  1048. sheet.setSelection(index, sels[0].col, 1, 1);
  1049. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  1050. sheet.getParent().focus();
  1051. sheet.showRow(index, spreadNS.VerticalPosition.center);
  1052. },
  1053. locateData: function (sheet, data) {
  1054. if (!sheet.zh_data) { return }
  1055. const index = sheet.zh_data.indexOf(data);
  1056. if (index < 0) return;
  1057. const sels = sheet.getSelections();
  1058. sheet.setSelection(index, sels[0].col, 1, 1);
  1059. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  1060. sheet.getParent().focus();
  1061. sheet.showRow(index, spreadNS.VerticalPosition.center);
  1062. },
  1063. locateRow: function (sheet, row) {
  1064. const sels = sheet.getSelections();
  1065. sheet.setSelection(row, sels[0].col, 1, 1);
  1066. SpreadJsObj.reloadRowsBackColor(sheet, [row, sels[0].row]);
  1067. sheet.getParent().focus();
  1068. sheet.showRow(row, spreadNS.VerticalPosition.center);
  1069. },
  1070. saveTopAndSelect: function (sheet, cacheKey) {
  1071. const sel = sheet.getSelections()[0];
  1072. const top = sheet.getViewportTopRow(1);
  1073. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  1074. },
  1075. loadTopAndSelect: function (sheet, cacheKey) {
  1076. let ts = getLocalCache(cacheKey);
  1077. if (ts !== '') {
  1078. ts = JSON.parse(ts);
  1079. if (ts === undefined || ts === null) return;
  1080. if (ts.sel) {
  1081. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  1082. SpreadJsObj.reloadRowsBackColor(sheet, [0, ts.sel.row]);
  1083. }
  1084. if (ts.top) {
  1085. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  1086. }
  1087. }
  1088. },
  1089. resetTopAndSelect: function (sheet) {
  1090. sheet.setSelection(0, 0, 1, 1);
  1091. sheet.showRow(0, spreadNS.VerticalPosition.top);
  1092. },
  1093. /**
  1094. * 获取当前选行的数据对象
  1095. * @param {GC.Spread.Sheets.Worksheet} sheet
  1096. * @returns {Object}
  1097. */
  1098. getSelectObject: function (sheet) {
  1099. if (!sheet) {
  1100. return null;
  1101. } else if (sheet.zh_dataType) {
  1102. const sel = sheet.getSelections()[0];
  1103. if (!sel) {
  1104. return null;
  1105. }
  1106. if (sheet.zh_dataType === this.DataType.Tree) {
  1107. return sheet.zh_tree.nodes[sel.row];
  1108. } else if (sheet.zh_dataType === this.DataType.Data) {
  1109. return sheet.zh_data[sel.row];
  1110. } else {
  1111. return null;
  1112. }
  1113. }
  1114. },
  1115. getRowObject: function (sheet, row) {
  1116. if (!sheet) {
  1117. return null;
  1118. } else if (sheet.zh_dataType) {
  1119. if (sheet.zh_dataType === this.DataType.Tree) {
  1120. return sheet.zh_tree.nodes[row];
  1121. } else if (sheet.zh_dataType === this.DataType.Data) {
  1122. return sheet.zh_data[row];
  1123. } else {
  1124. return null;
  1125. }
  1126. }
  1127. },
  1128. /**
  1129. * 刷新列显示
  1130. * @param sheet
  1131. */
  1132. refreshColumnVisible: function (sheet) {
  1133. if(sheet.zh_setting) {
  1134. sheet.zh_setting.cols.forEach(function (col, index) {
  1135. if (col.visible !== undefined && col.visible !== null) {
  1136. sheet.setColumnVisible(index, col.visible);
  1137. }
  1138. });
  1139. }
  1140. },
  1141. /**
  1142. * 刷新行显示
  1143. * @param sheet
  1144. */
  1145. refreshTreeRowVisible: function (sheet) {
  1146. this.beginMassOperation(sheet);
  1147. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  1148. for (const iRow in sortData) {
  1149. const node = sortData[iRow];
  1150. if (node.visible !== undefined && node.visible !== null) {
  1151. if (node.visible && node.waitingLoading) {
  1152. SpreadJsObj._loadRowData(sheet, node, parseInt(iRow));
  1153. }
  1154. sheet.setRowVisible(iRow, node.visible);
  1155. } else {
  1156. if (node.waitingLoading) {
  1157. this._loadRowData(sheet, node, parseInt(iRow));
  1158. }
  1159. sheet.setRowVisible(iRow, true);
  1160. }
  1161. }
  1162. this.endMassOperation(sheet);
  1163. },
  1164. refreshColumnAlign: function (sheet) {
  1165. if (sheet.zh_setting) {
  1166. for (const iCol in sheet.zh_setting.cols) {
  1167. const col = sheet.zh_setting.cols[iCol];
  1168. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  1169. }
  1170. }
  1171. },
  1172. refreshSheetReadOnly: function (sheet) {
  1173. this.beginMassOperation(sheet);
  1174. if (sheet.zh_setting) {
  1175. sheet.zh_setting.cols.forEach(function (col, i) {
  1176. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1177. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1178. }
  1179. });
  1180. }
  1181. this.endMassOperation(sheet);
  1182. },
  1183. /**
  1184. * 刷新列是否只读
  1185. * @param sheet
  1186. * @param field
  1187. * @param readonly
  1188. */
  1189. resetFieldReadOnly: function (sheet, field, readonly) {
  1190. const fields = field instanceof Array ? field : [field];
  1191. if (sheet.zh_setting) {
  1192. sheet.zh_setting.cols.forEach(function (col, i) {
  1193. if (fields.indexOf(col.field) !== -1) {
  1194. col.readOnly = readonly;
  1195. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  1196. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1197. }
  1198. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  1199. }
  1200. });
  1201. }
  1202. },
  1203. CellType: {
  1204. /**
  1205. * 获取树结构CellType
  1206. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  1207. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  1208. * @returns {TreeNodeCellType}
  1209. */
  1210. getTreeNodeCellType: function () {
  1211. const xOffset = 2 + 5;
  1212. const indent = 20;
  1213. const levelIndent = -5;
  1214. const halfBoxLength = 5;
  1215. const halfExpandLength = 3;
  1216. const lineColor = '#515151';//'#b8b8b8';
  1217. const expandBoxColor = '#434343';//'#808080';
  1218. const dotLine = true;
  1219. /**
  1220. * 画一条点线段
  1221. * @param canvas - 画布
  1222. * @param x1 - 线段起点 x
  1223. * @param y1 - 线段起点 y
  1224. * @param x2 - 线段终点 x
  1225. * @param y2 - 线段终点 y
  1226. * @param color - 线段颜色
  1227. */
  1228. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  1229. canvas.save();
  1230. // 设置偏移量
  1231. canvas.translate(0.5, 0.5);
  1232. canvas.beginPath();
  1233. canvas.strokeStyle = color;
  1234. canvas.dottedLine(x1, y1, x2, y2);
  1235. canvas.stroke();
  1236. canvas.restore();
  1237. };
  1238. /**
  1239. * 画一条线段
  1240. * @param canvas - 画布
  1241. * @param x1 - 线段起点 x
  1242. * @param y1 - 线段起点 y
  1243. * @param x2 - 线段终点 x
  1244. * @param y2 - 线段终点 y
  1245. * @param color - 线段颜色
  1246. */
  1247. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  1248. canvas.save();
  1249. // 设置偏移量
  1250. canvas.translate(0.5, 0.5);
  1251. canvas.beginPath();
  1252. canvas.moveTo(x1, y1);
  1253. canvas.lineTo(x2, y2);
  1254. canvas.strokeStyle = color;
  1255. canvas.stroke();
  1256. canvas.restore();
  1257. };
  1258. /**
  1259. * 画一个方框
  1260. * @param {Object} canvas - 画布
  1261. * @param {Object} rect - 方框区域
  1262. * @param {String} lineColor - 画线颜色
  1263. * @param {String} fillColor - 填充颜色
  1264. */
  1265. const drawBox = function (canvas, rect, lineColor, fillColor) {
  1266. canvas.save();
  1267. // 设置偏移量
  1268. canvas.translate(0.5, 0.5);
  1269. canvas.strokeStyle = lineColor;
  1270. canvas.beginPath();
  1271. canvas.moveTo(rect.left, rect.top);
  1272. canvas.lineTo(rect.left, rect.bottom);
  1273. canvas.lineTo(rect.right, rect.bottom);
  1274. canvas.lineTo(rect.right, rect.top);
  1275. canvas.lineTo(rect.left, rect.top);
  1276. canvas.stroke();
  1277. canvas.fillStyle = fillColor ? fillColor : 'white';
  1278. canvas.fill();
  1279. canvas.restore();
  1280. };
  1281. /**
  1282. * 画树结构-展开收起按钮
  1283. * @param {Object} canvas - 画布
  1284. * @param {Number} x - 单元格左顶点坐标 x
  1285. * @param {Number} y - 单元格左顶点坐标 y
  1286. * @param {Number} w - 单元格宽度
  1287. * @param {Number} h - 单元格高度
  1288. * @param {Number} centerX - 按钮中央坐标
  1289. * @param {Number} centerY - 按钮中央坐标
  1290. * @param {Boolean} expanded - 当前节点展开收起状态
  1291. */
  1292. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded, style) {
  1293. let rect = {
  1294. top: centerY - halfBoxLength,
  1295. bottom: centerY + halfBoxLength,
  1296. left: centerX - halfBoxLength,
  1297. right: centerX + halfBoxLength
  1298. };
  1299. let h1, h2, offset = 1;
  1300. if (rect.left < x + w) {
  1301. // 方框超出单元格宽度时,超出部分不画。
  1302. rect.right = Math.min(rect.right, x + w);
  1303. drawBox(canvas, rect, expandBoxColor, style.backColor);
  1304. // 画中心十字
  1305. // 画十字横线
  1306. h1 = centerX - halfExpandLength;
  1307. h2 = Math.min(centerX + halfExpandLength, x + w);
  1308. if (h2 > h1) {
  1309. drawLine(canvas, h1, centerY, h2, centerY, expandBoxColor);
  1310. }
  1311. // 画十字竖线
  1312. if (!expanded && (centerX < x + w)) {
  1313. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, expandBoxColor);
  1314. }
  1315. }
  1316. };
  1317. let TreeNodeCellType = function (){};
  1318. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  1319. const proto = TreeNodeCellType.prototype;
  1320. /**
  1321. * 绘制方法
  1322. * @param {Object} canvas - 画布
  1323. * @param value - cell.value
  1324. * @param {Number} x - 单元格左顶点坐标 x
  1325. * @param {Number} y - 单元格左顶点坐标 y
  1326. * @param {Number} w - 单元格宽度
  1327. * @param {Number} h - 单元格高度
  1328. * @param {Object} style - cell.style
  1329. * @param {Object} options
  1330. */
  1331. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1332. // 清理 画布--单元格范围 旧数据
  1333. if (style.backColor) {
  1334. canvas.save();
  1335. canvas.fillStyle = style.backColor;
  1336. canvas.fillRect(x, y, w, h);
  1337. canvas.restore();
  1338. } else {
  1339. // 不知道为什么在 概算投资-造价对比 下,这一句代码无法生效。
  1340. // canvas.clearRect(x, y, w, h);
  1341. canvas.save();
  1342. canvas.fillStyle = 'white';
  1343. canvas.fillRect(x, y, w, h);
  1344. canvas.restore();
  1345. }
  1346. const tree = options.sheet.zh_tree;
  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.level - 1) * indent + (node.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.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.is_leaf) {
  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.level) * indent + (node.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 = 3, minHeight = 2;
  2465. /**
  2466. * 画一个长条
  2467. * @param {Object} canvas - 画布
  2468. * @param {Object} rect - 方框区域
  2469. * @param {String} lineColor - 画线颜色
  2470. * @param {String} fillColor - 填充颜色
  2471. */
  2472. const drawBar = function (canvas, x, y, w, h, r, color) {
  2473. canvas.save();
  2474. // 设置偏移量
  2475. canvas.translate(0.5, 0.5);
  2476. canvas.strokeStyle = color;
  2477. canvas.beginPath();
  2478. canvas.roundRect(x, y, w, h, r);
  2479. canvas.stroke();
  2480. canvas.fillStyle = color;
  2481. canvas.fill();
  2482. canvas.restore();
  2483. };
  2484. const proto = stackedBarCellType.prototype;
  2485. /**
  2486. * 绘制方法
  2487. * @param {Object} canvas - 画布
  2488. * @param value - cell.value
  2489. * @param {Number} x - 单元格左顶点坐标 x
  2490. * @param {Number} y - 单元格左顶点坐标 y
  2491. * @param {Number} w - 单元格宽度
  2492. * @param {Number} h - 单元格高度
  2493. * @param {Object} style - cell.style
  2494. * @param {Object} options
  2495. */
  2496. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2497. // 清理 画布--单元格范围 旧数据
  2498. canvas.save();
  2499. canvas.fillStyle = style.backColor || 'white';
  2500. canvas.fillRect(x, y, w, h);
  2501. canvas.restore();
  2502. const col = options.sheet.zh_setting.cols[options.col];
  2503. const barData = value;
  2504. const left = x + indent;
  2505. const startTop = y + indent;
  2506. const validHeight = h - indent - indent - (col.stackedBarCover || barData.length === 1 ? 1 : barData.length / 2 + 1);
  2507. const height = col.stackedBarCover || barData.length === 0 ? validHeight : Math.max(validHeight/barData.length, minHeight);
  2508. const validWidth = w - indent - indent - 1;
  2509. for (const [i, bd] of barData.entries()) {
  2510. let width = ZhCalc.mul(validWidth, bd.percent, 2);
  2511. if (width < defaultR) continue;
  2512. const top = col.stackedBarCover ? startTop : startTop + height * i + i - 1;
  2513. drawBar(canvas, left, top, width, height, defaultR, bd.color);
  2514. }
  2515. };
  2516. return new stackedBarCellType();
  2517. },
  2518. },
  2519. RowHeader: {
  2520. getTagRowHeader: function (setting) {
  2521. const indent = setting.indent || 18, maxHintWidth = 200, borderIndent = 10;
  2522. const height = setting.tagSize ? 11 * setting.tagSize : 11, width = setting.tagSize ? 10 * setting.tagSize : 10;
  2523. const tagFont = setting.tagFont || '9px 微软雅黑';
  2524. const drawTag = function (canvas, x, y, fillColor) {
  2525. canvas.save();
  2526. // 设置偏移量
  2527. canvas.translate(0.5, 0.5);
  2528. canvas.beginPath();
  2529. canvas.moveTo(x, y);
  2530. canvas.lineTo(x, y+height/11*5);
  2531. canvas.lineTo(x+width/10*5.5, y+height);
  2532. canvas.lineTo(x+width, y+height-width/10*4.5);
  2533. canvas.lineTo(x+width/11*5, y);
  2534. canvas.lineTo(x, y);
  2535. canvas.stroke();
  2536. canvas.fillStyle = fillColor instanceof Array ? fillColor[0] : fillColor;
  2537. canvas.fill();
  2538. canvas.beginPath();
  2539. canvas.arc(x+2, y+2, 1, 0, Math.PI*2, true);
  2540. canvas.closePath();
  2541. canvas.fillStyle = 'white';
  2542. canvas.fill();
  2543. if (fillColor instanceof Array && fillColor.length > 1) {
  2544. canvas.fillStyle = '#444444';
  2545. canvas.font = tagFont;
  2546. canvas.fillText(fillColor.length, x+width/11*13, y+height/11*14);
  2547. }
  2548. canvas.restore();
  2549. };
  2550. let TagCellType = function (){};
  2551. TagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  2552. const proto = TagCellType.prototype;
  2553. proto.getTagColor = setting.getColor;
  2554. proto.getTagHtml = setting.getTagHtml;
  2555. proto.basePaint = proto.paint;
  2556. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2557. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  2558. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - indent, h, style, options]);
  2559. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  2560. const color = this.getTagColor(options.row, node);
  2561. const centerX = x + w - indent + height/2, centerY = y + h/2;
  2562. color && drawTag(canvas, centerX - height/2, centerY - width/2, color);
  2563. };
  2564. /**
  2565. * 获取点击信息
  2566. * @param {Number} x
  2567. * @param {Number} y
  2568. * @param {Object} cellStyle
  2569. * @param {Object} cellRect
  2570. * @param {Object} context
  2571. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2572. */
  2573. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2574. return {
  2575. x: x,
  2576. y: y,
  2577. row: context.row,
  2578. col: context.col,
  2579. cellStyle: cellStyle,
  2580. cellRect: cellRect,
  2581. sheet: context.sheet,
  2582. sheetArea: context.sheetArea,
  2583. ctx: context.sheet.getParent().xs,
  2584. };
  2585. };
  2586. /**
  2587. * 鼠标进入单元格事件 - 显示悬浮提示
  2588. * @param {Object} hitinfo - 见getHitInfo返回值
  2589. */
  2590. proto.processMouseEnter = function (hitinfo) {
  2591. const node = SpreadJsObj.getRowObject(hitinfo.sheet, hitinfo.row);
  2592. let html = this.getTagHtml(hitinfo.row, node);
  2593. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  2594. if (pos && html) {
  2595. if (!this._tagTipElement) {
  2596. let div = $('#autoTip')[0];
  2597. if (!div) {
  2598. div = document.createElement("div");
  2599. $(div).css("position", "absolute")
  2600. .css("border", "1px #C0C0C0 solid")
  2601. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  2602. .css("font", "9pt Arial")
  2603. .css("background", "white")
  2604. .css("padding", 5)
  2605. .css("width", maxHintWidth)
  2606. .css("z-index", 999)
  2607. .attr("id", 'autoTagTip')
  2608. .css("top", pos.y + hitinfo.y + indent)
  2609. .css("left", pos.x + hitinfo.x + indent)
  2610. .html(html);
  2611. document.body.insertBefore(div, null);
  2612. }
  2613. this._tagTipElement = div;
  2614. $(div).show("fast");
  2615. }
  2616. }
  2617. };
  2618. /**
  2619. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2620. * @param {Object} hitinfo - 见getHitInfo返回值
  2621. */
  2622. proto.processMouseLeave = function (hitinfo) {
  2623. if (this._tagTipElement) {
  2624. $(this._tagTipElement).hide().remove();
  2625. this._tagTipElement = null;
  2626. }
  2627. };
  2628. return new TagCellType();
  2629. },
  2630. getCircleTagRowHeader: function (setting) {
  2631. const drawCircle = function (canvas, x, y, r, fillColor) {
  2632. canvas.save();
  2633. canvas.beginPath();
  2634. canvas.arc(x, y, r, 0, Math.PI*2, true);
  2635. canvas.closePath();
  2636. canvas.fillStyle = fillColor;
  2637. canvas.fill();
  2638. canvas.restore();
  2639. };
  2640. const drawCircle2 = function (canvas, x, y, r, lineColor, fillColor) {
  2641. canvas.save();
  2642. canvas.beginPath();
  2643. canvas.arc(x, y, r, 0, Math.PI*2, true);
  2644. canvas.closePath();
  2645. canvas.fillStyle = lineColor;
  2646. canvas.fill();
  2647. canvas.beginPath();
  2648. canvas.arc(x, y, r-2, 0, Math.PI*2, true);
  2649. canvas.closePath();
  2650. canvas.fillStyle = fillColor;
  2651. canvas.fill();
  2652. canvas.restore();
  2653. };
  2654. let CircleTagCellType = function (){};
  2655. CircleTagCellType.prototype = new spreadNS.CellTypes.RowHeader();
  2656. const proto = CircleTagCellType.prototype;
  2657. proto.indent = setting.indent || 16;
  2658. proto.size = setting.size || 6;
  2659. proto.getTagColor = setting.getColor;
  2660. proto.basePaint = proto.paint;
  2661. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  2662. spreadNS.CellTypes.RowHeader.prototype.paint.apply(this, [canvas, '', x, y, w , h, style, options]);
  2663. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w - this.indent, h, style, options]);
  2664. const node = SpreadJsObj.getRowObject(options.sheet, options.row);
  2665. let backColor = style.backColor;
  2666. if (!backColor) {
  2667. const tag = options.sheet.getTag(options.row, options.col, options.sheetArea);
  2668. if (tag && tag === 'hover') backColor = '#dddfe1';
  2669. if (!backColor) {
  2670. let sel = options.sheet.getSelections();
  2671. sel = sel ? sel[0] : null;
  2672. backColor = (sel && options.row >= sel.row && options.row < sel.row + sel.rowCount ? '#dddfe1' : '#e9ecef');
  2673. }
  2674. }
  2675. let color = this.getTagColor(options.row, node);
  2676. color = color instanceof Array ? color : [color];
  2677. for (let i = color.length - 1; i >= 0; i--) {
  2678. drawCircle2(canvas, x + w - this.indent + 5 + i*5 , y + h/2, this.size, backColor, color[i]);
  2679. }
  2680. };
  2681. /**
  2682. * 获取点击信息
  2683. * @param {Number} x
  2684. * @param {Number} y
  2685. * @param {Object} cellStyle
  2686. * @param {Object} cellRect
  2687. * @param {Object} context
  2688. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  2689. */
  2690. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  2691. return {
  2692. x: x,
  2693. y: y,
  2694. row: context.row,
  2695. col: context.col,
  2696. cellStyle: cellStyle,
  2697. cellRect: cellRect,
  2698. sheet: context.sheet,
  2699. sheetArea: context.sheetArea,
  2700. ctx: context.sheet.getParent().xs,
  2701. };
  2702. };
  2703. /**
  2704. * 鼠标进入单元格事件 - 显示悬浮提示
  2705. * @param {Object} hitinfo - 见getHitInfo返回值
  2706. */
  2707. proto.processMouseEnter = function (hitinfo) {
  2708. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, 'hover', hitinfo.sheetArea);
  2709. };
  2710. /**
  2711. * 鼠标移出单元格事件 - 隐藏悬浮提示
  2712. * @param {Object} hitinfo - 见getHitInfo返回值
  2713. */
  2714. proto.processMouseLeave = function (hitinfo) {
  2715. hitinfo.sheet.setTag(hitinfo.row, hitinfo.col, undefined, hitinfo.sheetArea);
  2716. };
  2717. return new CircleTagCellType();
  2718. },
  2719. },
  2720. Formatter: {
  2721. baseNumberFormatter: function () {
  2722. const formatter = function () {};
  2723. formatter.prototype = new GC.Spread.Formatter.FormatterBase();
  2724. const proto = formatter.prototype;
  2725. /**
  2726. * 格式化数字显示方式
  2727. * 用法
  2728. * formatNumber(12345.999,'#,##0.00');
  2729. * formatNumber(12345.999,'#,##0.##');
  2730. * formatNumber(123,'000000');
  2731. * @param num
  2732. * @param pattern
  2733. */
  2734. proto.formatNum = function (num, pattern) {
  2735. var strarr = num?num.toString().split('.'):['0'];
  2736. var fmtarr = pattern?pattern.split('.'):[''];
  2737. var retstr='';
  2738. // 整数部分
  2739. var str = strarr[0];
  2740. var fmt = fmtarr[0];
  2741. var i = str.length-1;
  2742. var comma = false;
  2743. for(var f=fmt.length-1;f>=0;f--){
  2744. switch(fmt.substr(f,1)){
  2745. case '#':
  2746. if(i>=0 ) retstr = str.substr(i--,1) + retstr;
  2747. break;
  2748. case '0':
  2749. if(i>=0) retstr = str.substr(i--,1) + retstr;
  2750. else retstr = '0' + retstr;
  2751. break;
  2752. case ',':
  2753. comma = true;
  2754. retstr=','+retstr;
  2755. break;
  2756. }
  2757. }
  2758. if(i>=0){
  2759. if(comma){
  2760. var l = str.length;
  2761. for(;i>=0;i--){
  2762. retstr = str.substr(i,1) + retstr;
  2763. if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;
  2764. }
  2765. }
  2766. else retstr = str.substr(0,i+1) + retstr;
  2767. }
  2768. retstr = retstr+'.';
  2769. // 处理小数部分
  2770. str=strarr.length>1?strarr[1]:'';
  2771. fmt=fmtarr.length>1?fmtarr[1]:'';
  2772. i=0;
  2773. for(var f=0;f<fmt.length;f++){
  2774. switch(fmt.substr(f,1)){
  2775. case '#':
  2776. if(i<str.length) retstr+=str.substr(i++,1);
  2777. break;
  2778. case '0':
  2779. if(i<str.length) retstr+= str.substr(i++,1);
  2780. else retstr+='0';
  2781. break;
  2782. }
  2783. }
  2784. return retstr.replace(/^,+/,'').replace(/\.$/,'');
  2785. };
  2786. proto.format = function (obj, formattedData) {
  2787. if (this.pattern) {
  2788. return this.formatNum(obj, this.pattern);
  2789. } else {
  2790. return obj;
  2791. }
  2792. };
  2793. return new formatter();
  2794. },
  2795. _numFormatter: [],
  2796. getNumberFormatter(pattern) {
  2797. let formatter = this._numFormatter.find(function (f) {
  2798. return f.pattern === pattern;
  2799. });
  2800. if (!formatter) {
  2801. formatter = this.baseNumberFormatter();
  2802. formatter.pattern = pattern;
  2803. this._numFormatter.push(formatter);
  2804. }
  2805. return formatter;
  2806. },
  2807. },
  2808. Clipboard: (function () {
  2809. let cText = '', cHtml;
  2810. const setCopyData = function (text, html) {
  2811. cText = text;
  2812. cHtml = html;
  2813. };
  2814. const getPasteData = function () {
  2815. return {text: cText, html: cHtml};
  2816. };
  2817. const clearData = function () {
  2818. cText = '';
  2819. cHtml = undefined;
  2820. };
  2821. const setSheetFilterCopyData = function (sheet) {
  2822. cText = SpreadJsObj.getFilterCopyText(sheet);
  2823. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  2824. };
  2825. const getAnalysisPasteText = function () {
  2826. return SpreadJsObj.analysisPasteText(cText);
  2827. };
  2828. const getAnalysisPasteHtml = function () {
  2829. return SpreadJsObj.analysisPasteHtml(cHtml);
  2830. };
  2831. const setSysClipboard = function(text) {
  2832. let textArea = document.createElement("textarea");
  2833. textArea.style.position = 'fixed';
  2834. textArea.style.top = 0;
  2835. textArea.style.left = 0;
  2836. textArea.style.width = '2em';
  2837. textArea.style.height = '2em';
  2838. textArea.style.padding = 0;
  2839. textArea.style.border = 'none';
  2840. textArea.style.outline = 'none';
  2841. textArea.style.boxShadow = 'none';
  2842. textArea.style.background = 'transparent';
  2843. textArea.value = text;
  2844. document.body.appendChild(textArea);
  2845. textArea.select();
  2846. try {
  2847. document.execCommand('copy');
  2848. } catch (err) {
  2849. }
  2850. document.body.removeChild(textArea);
  2851. };
  2852. return {
  2853. setCopyData, getPasteData, clearData,
  2854. setSheetFilterCopyData,
  2855. getAnalysisPasteText, getAnalysisPasteHtml,
  2856. setSysClipboard
  2857. }
  2858. })(),
  2859. };