spreadjs_zh.js 128 KB

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