spreadjs_zh.js 116 KB

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