spreadjs_zh.js 118 KB

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