spreadjs_zh.js 119 KB

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