spreadjs_zh.js 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  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. this.beginMassOperation(sheet);
  369. this._loadCacheSetting(sheet, setting);
  370. setting.pos = sheet.getParent().pos;
  371. if (!setting.tree) setting.tree = {};
  372. sheet.zh_setting = setting;
  373. if (sheet.zh_setting.headColWidth) {
  374. for (const [i, w] of sheet.zh_setting.headColWidth.entries()) {
  375. sheet.setColumnWidth(i, w, spreadNS.SheetArea.rowHeader);
  376. }
  377. }
  378. this._initSheetDeafult(sheet);
  379. this._initSheetHeader(sheet);
  380. sheet.setRowCount(sheet.zh_setting.emptyRows);
  381. sheet.extendCellType = {};
  382. sheet.borderLine = new spreadNS.LineBorder('#cccccc', spreadNS.LineStyle.thin);
  383. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).locked(setting.readOnly).setBorder(sheet.borderLine, {all: true});
  384. if (setting.selectedBackColor) {
  385. SpreadJsObj.selChangedRefreshBackColor(sheet);
  386. }
  387. this.endMassOperation(sheet);
  388. },
  389. reLoadSheetHeader: function (sheet) {
  390. if (sheet.zh_setting) {
  391. this.beginMassOperation(sheet);
  392. this._initSheetHeader(sheet);
  393. this.endMassOperation(sheet);
  394. }
  395. },
  396. _getBackColor: function (sheet, data, row, col) {
  397. let backColor = sheet.getDefaultStyle().backColor;
  398. let sels = sheet.getSelections();
  399. if (sheet.zh_setting.selectedBackColor && sels && sels[0].row === row ) {
  400. return sheet.zh_setting.selectedBackColor;
  401. } else {
  402. if (sheet.zh_setting.tree.getColor && Object.prototype.toString.apply(sheet.zh_setting.tree.getColor) === "[object Function]") {
  403. backColor = sheet.zh_setting.tree.getColor(sheet, data, row, col, backColor);
  404. }
  405. if (sheet.zh_setting.getColor && Object.prototype.toString.apply(sheet.zh_setting.getColor) === "[object Function]") {
  406. backColor = sheet.zh_setting.getColor(sheet, data, row, col, backColor);
  407. }
  408. return backColor;
  409. }
  410. },
  411. _loadRowStyle: function (sheet, row) {
  412. sheet.zh_setting.cols.forEach(function (col, j) {
  413. const cell = sheet.getCell(row, j);
  414. if (col.font) {
  415. cell.font(col.font);
  416. }
  417. if (col.foreColor && Object.prototype.toString.apply(col.foreColor) !== "[object Function]") {
  418. cell.foreColor(col.foreColor);
  419. }
  420. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) !== "[object Function]") {
  421. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false);
  422. }
  423. cell.vAlign(1).hAlign(col.hAlign);
  424. if (col.formatter) {
  425. cell.formatter(col.formatter);
  426. } else if (col.type === 'Number') {
  427. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  428. }
  429. cell.setBorder(sheet.borderLine, {all: true});
  430. });
  431. },
  432. _loadRowData: function (sheet, data, row) {
  433. // 单元格重新写入数据
  434. if (!data) { return }
  435. sheet.zh_setting.cols.forEach(function (col, j) {
  436. const cell = sheet.getCell(row, j);
  437. if (col.getValue && Object.prototype.toString.apply(col.getValue) === "[object Function]") {
  438. cell.value(col.getValue(data));
  439. } else if (col.field !== '' && data[col.field]) {
  440. cell.value(data[col.field]);
  441. }
  442. let font = sheet.getDefaultStyle().font;
  443. if (col.font) {
  444. font = col.font;
  445. }
  446. if (sheet.zh_setting.tree.getFont && Object.prototype.toString.apply(sheet.zh_setting.tree.getFont) === "[object Function]") {
  447. font = sheet.zh_setting.tree.getFont(sheet, data, row, col, font);
  448. }
  449. cell.font(font);
  450. if (col.foreColor) {
  451. if (Object.prototype.toString.apply(col.foreColor) === "[object Function]") {
  452. cell.foreColor(col.foreColor(data, sheet.getDefaultStyle().foreColor));
  453. } else {
  454. cell.foreColor(col.foreColor);
  455. }
  456. }
  457. if (col.readOnly && Object.prototype.toString.apply(col.readOnly) === "[object Function]") {
  458. cell.locked(col.readOnly(data) || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  459. } else {
  460. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  461. }
  462. if (col.formatter) {
  463. cell.formatter(col.formatter);
  464. } else if (col.type === 'Number') {
  465. cell.formatter(SpreadJsObj.Formatter.getNumberFormatter('0.######'));
  466. }
  467. cell.backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  468. cell.setBorder(sheet.borderLine, {all: true});
  469. });
  470. },
  471. _addActivePaintEvents: function (sheet, cellType) {
  472. if (!sheet.ActiveType) {
  473. sheet.ActiveType = [];
  474. }
  475. sheet.ActiveType.push(cellType);
  476. if (!sheet.AcitveRefresh) {
  477. sheet.bind(spreadNS.Events.LeaveCell, function (e, info) {
  478. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  479. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  480. info.sheet.leaveCell = {row: info.row, col: info.col};
  481. } else {
  482. delete info.sheet.leaveCell;
  483. }
  484. });
  485. sheet.bind(spreadNS.Events.EnterCell, function (e, info) {
  486. const cellType = info.sheet.getCell(info.row, info.col).cellType();
  487. if (sheet.ActiveType.indexOf(cellType) >= 0) {
  488. info.sheet.repaint(info.sheet.getCellRect(info.row, info.col));
  489. }
  490. if (info.sheet.leaveCell) {
  491. info.sheet.repaint(info.sheet.getCellRect(info.sheet.leaveCell.row, info.sheet.leaveCell.col));
  492. }
  493. });
  494. sheet.AcitveRefresh = true;
  495. }
  496. },
  497. _defineColCellType: function (sheet, col, colSetting) {
  498. sheet.AcitveComboRefresh = false;
  499. if(colSetting.cellType === 'ellipsis') {
  500. if (!sheet.extendCellType.ellipsis) {
  501. sheet.extendCellType.ellipsis = this.CellType.getEllipsisTextCellType();
  502. }
  503. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsis);
  504. }
  505. if(colSetting.cellType === 'ellipsisAutoTip') {
  506. if (!sheet.extendCellType.ellipsisAutoTip) {
  507. sheet.extendCellType.ellipsisAutoTip = this.CellType.getEllipsisTextAutoTipCellType();
  508. }
  509. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.ellipsisAutoTip);
  510. }
  511. if(colSetting.cellType === 'html') {
  512. if (!sheet.extendCellType.html) {
  513. sheet.extendCellType.html = this.CellType.getHtmlCellType();
  514. }
  515. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.html);
  516. }
  517. if (colSetting.cellType === 'image') {
  518. if (!sheet.extendCellType.image) {
  519. sheet.extendCellType.image = this.CellType.getImageCellType();
  520. }
  521. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.image);
  522. }
  523. if (colSetting.cellType === 'imageBtn') {
  524. if (!sheet.extendCellType.imageBtn) {
  525. sheet.extendCellType.imageBtn = this.CellType.getImageButtonCellType();
  526. }
  527. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.imageBtn);
  528. }
  529. if (colSetting.cellType === 'activeImageBtn') {
  530. if (!sheet.extendCellType.activeImageBtn) {
  531. sheet.extendCellType.activeImageBtn = this.CellType.getActiveImageButtonCellType();
  532. }
  533. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.activeImageBtn);
  534. }
  535. if (colSetting.cellType === 'tree') {
  536. if (!sheet.extendCellType.tree) {
  537. sheet.extendCellType.tree = this.CellType.getTreeNodeCellType();
  538. }
  539. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tree);
  540. }
  541. if (colSetting.cellType === 'tip') {
  542. if (!sheet.extendCellType.tip) {
  543. sheet.extendCellType.tip = this.CellType.getTipCellType();
  544. }
  545. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.tip);
  546. }
  547. if (colSetting.cellType === 'autoTip') {
  548. if (!sheet.extendCellType.autoTip) {
  549. sheet.extendCellType.autoTip = this.CellType.getAutoTipCellType();
  550. }
  551. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.autoTip);
  552. }
  553. if (colSetting.cellType === 'checkbox') {
  554. if (!sheet.extendCellType.checkbox) {
  555. sheet.extendCellType.checkbox = new spreadNS.CellTypes.CheckBox();
  556. }
  557. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.checkbox);
  558. }
  559. if (colSetting.cellType === 'unit') {
  560. if (!sheet.extendCellType.unit) {
  561. sheet.extendCellType.unit = this.CellType.getUnitCellType();
  562. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.unit);
  563. }
  564. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.unit);
  565. }
  566. if (colSetting.cellType === 'customizeCombo') {
  567. const cellKey = colSetting.cellTypeKey ? 'customizeCombo-' + colSetting.cellTypeKey : 'customizeCombo';
  568. if (!sheet.extendCellType[cellKey]) {
  569. sheet.extendCellType[cellKey] = this.CellType.getCustomizeComboCellType(colSetting.comboItems);
  570. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType[cellKey]);
  571. }
  572. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType[cellKey]);
  573. }
  574. if (colSetting.cellType === 'datepicker') {
  575. if (!sheet.extendCellType.datepicker) {
  576. sheet.extendCellType.datepicker = this.CellType.getDatePickerCellType();
  577. SpreadJsObj._addActivePaintEvents(sheet, sheet.extendCellType.datepicker);
  578. }
  579. sheet.getRange(-1, col, -1, 1).cellType(sheet.extendCellType.datepicker);
  580. }
  581. if (colSetting.formatter) {
  582. sheet.getRange(-1, col, -1, 1).formatter(colSetting.formatter);
  583. }
  584. },
  585. /**
  586. * 整个sheet重新加载数据
  587. * @param {GC.Spread.Sheets.Worksheet} sheet
  588. */
  589. reLoadSheetData: function (sheet) {
  590. const self = this;
  591. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  592. this.beginMassOperation(sheet);
  593. try {
  594. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  595. sheet.getRange(0, 0, sheet.getRowCount(), sheet.getColumnCount()).backColor(sheet.getDefaultStyle().backColor);
  596. // 设置总行数
  597. const totalRow = sortData.length + sheet.zh_setting.emptyRows;
  598. sheet.setRowCount(totalRow, spreadNS.SheetArea.viewport);
  599. // 控制空白行
  600. const emptyRows = sheet.getRange(sortData.length, -1, sheet.zh_setting.emptyRows, -1);
  601. emptyRows.locked(sheet.zh_dataType === 'tree');
  602. if (sortData) {
  603. // 单元格写入数据
  604. sortData.forEach(function (data, i) {
  605. self._loadRowData(sheet, data, i);
  606. sheet.setRowVisible(i, data.visible);
  607. });
  608. for (let iRow = sortData.length - 1; iRow < totalRow; iRow++) {
  609. self._loadRowStyle(sheet, iRow);
  610. }
  611. }
  612. // 设置列单元格格式
  613. sheet.zh_setting.cols.forEach(function (col, j) {
  614. //if (!col.cellType) { return; }
  615. self._defineColCellType(sheet, j, col);
  616. });
  617. this.endMassOperation(sheet);
  618. } catch (err) {
  619. this.endMassOperation(sheet);
  620. }
  621. },
  622. /**
  623. * 重新加载部分数据行
  624. * @param {GC.Spread.Sheets.Worksheet} sheet
  625. * @param {Number} row
  626. * @param {Number} count
  627. */
  628. reLoadRowData: function (sheet, row, count = 1) {
  629. if (row < 0) { return; }
  630. const self = this;
  631. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  632. this.beginMassOperation(sheet);
  633. try {
  634. // 清空原单元格数据
  635. sheet.clear(row, -1, count, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  636. sheet.getRange(row, -1, count, -1).backColor(sheet.getDefaultStyle().backColor);
  637. // 单元格重新写入数据
  638. for (let i = row; i < row + count; i++) {
  639. const data = sortData[i];
  640. if (!data) { continue; }
  641. this._loadRowData(sheet, data, i);
  642. sheet.setRowVisible(i, data.visible);
  643. }
  644. this.endMassOperation(sheet);
  645. } catch (err) {
  646. this.endMassOperation(sheet);
  647. }
  648. },
  649. /**
  650. * 重新加载部分行数据
  651. * @param {GC.Spread.Sheets.Worksheet} sheet
  652. * @param {Array} rows
  653. */
  654. reLoadRowsData: function (sheet, rows) {
  655. const self = this;
  656. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  657. this.beginMassOperation(sheet);
  658. try {
  659. for (const row of rows) {
  660. if (row < 0) { continue; }
  661. // 清空原单元格数据
  662. sheet.clear(row, -1, 1, -1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  663. sheet.getRange(row, -1, 1, -1).backColor(sheet.getDefaultStyle().backColor);
  664. const data = sortData[row];
  665. // 单元格重新写入数据
  666. this._loadRowData(sheet, data, row);
  667. sheet.setRowVisible(row, data.visible);
  668. };
  669. this.endMassOperation(sheet);
  670. } catch (err) {
  671. this.endMassOperation(sheet);
  672. }
  673. },
  674. reloadRowsBackColor: function (sheet, rows) {
  675. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  676. this.beginMassOperation(sheet);
  677. try {
  678. for (const row of rows) {
  679. if (row < 0) { continue; }
  680. const data = sortData[row];
  681. for (const [iCol, col] of sheet.zh_setting.cols.entries()) {
  682. sheet.getCell(row, iCol).backColor(SpreadJsObj._getBackColor(sheet, data, row, col));
  683. }
  684. };
  685. this.endMassOperation(sheet);
  686. } catch (err) {
  687. this.endMassOperation(sheet);
  688. }
  689. },
  690. /**
  691. * 重新加载部分列数据
  692. * @param {GC.Spread.Sheets.Worksheet} sheet
  693. * @param {Array} cols
  694. */
  695. reLoadColsData: function (sheet, cols) {
  696. const self = this;
  697. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  698. this.beginMassOperation(sheet);
  699. try {
  700. for (const iCol of cols) {
  701. // 清空原单元格数据
  702. sheet.clear(-1, iCol, -1, 1, spreadNS.SheetArea.viewport, spreadNS.StorageType.data);
  703. const col = sheet.zh_setting.cols[iCol];
  704. sortData.forEach(function (data, i) {
  705. // 设置值
  706. const cell = sheet.getCell(i, iCol);
  707. if (col.field !== '' && data[col.field]) {
  708. cell.value(data[col.field]).locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  709. } else {
  710. cell.locked(col.readOnly || sheet.zh_setting.readOnly || false).vAlign(1).hAlign(col.hAlign);
  711. }
  712. // 设置单元格格式
  713. if (col.formatter) {
  714. cell.formatter(col.formatter);
  715. }
  716. });
  717. }
  718. this.endMassOperation(sheet);
  719. } catch (err) {
  720. this.endMassOperation(sheet);
  721. }
  722. },
  723. reLoadNodesData: function (sheet, nodes) {
  724. this.beginMassOperation(sheet);
  725. nodes = nodes instanceof Array ? nodes : [nodes];
  726. for (const node of nodes) {
  727. const sortData = sheet.zh_dataType === 'tree' ? sheet.zh_tree.nodes : sheet.zh_data;
  728. this._loadRowData(sheet, node, sortData.indexOf(node));
  729. }
  730. this.endMassOperation(sheet);
  731. },
  732. /**
  733. * 根据data加载sheet数据,合并了一般数据和树结构数据的加载
  734. * @param {GC.Spread.Sheets.Worksheet} sheet
  735. * @param {String} dataType - 1.'zh_data' 2.'zh_tree'
  736. * @param {Array|PathTree} data - 对dataType对应
  737. */
  738. loadSheetData: function (sheet, dataType, data){
  739. sheet.zh_dataType = dataType;
  740. if (dataType === 'tree') {
  741. sheet.zh_tree = data;
  742. } else {
  743. sheet.zh_data = data;
  744. }
  745. this.protectedSheet(sheet);
  746. this.reLoadSheetData(sheet);
  747. },
  748. /**
  749. * 获取复制数据HTML格式(过滤不可见单元格)
  750. * @param {GC.Spread.Sheets.Worksheet} sheet
  751. * @returns {string}
  752. */
  753. getFilterCopyHTML: function (sheet) {
  754. const sel = sheet.getSelections()[0];
  755. const html = [];
  756. html.push('<table>');
  757. for (let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  758. // 跳过隐藏行
  759. if (!sheet.getCell(i, -1).visible()) { continue; }
  760. const rowHtml = [];
  761. rowHtml.push('<tr>');
  762. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  763. const data = sheet.getText(i, j);
  764. rowHtml.push('<td>' + data + '</td>');
  765. }
  766. rowHtml.push('</tr>');
  767. html.push(rowHtml.join(''));
  768. }
  769. html.push('</table>');
  770. return html.join('');
  771. },
  772. /**
  773. * 获取复制数据Text格式(过滤不可见单元格)
  774. * @param {GC.Spread.Sheets.Worksheet} sheet
  775. * @returns {string}
  776. */
  777. getFilterCopyText: function (sheet) {
  778. const copyData = [];
  779. const sel = sheet.getSelections()[0];
  780. for(let i = sel.row, iLen = sel.row + sel.rowCount; i < iLen; i++) {
  781. // 跳过隐藏行
  782. if (!sheet.getCell(i, -1).visible()) { continue; }
  783. const rowText = [];
  784. for (let j = sel.col, jLen = sel.col + sel.colCount; j < jLen; j++) {
  785. const data = sheet.getText(i, j);
  786. rowText.push(data);
  787. }
  788. copyData.push(rowText.join('\t'));
  789. }
  790. return copyData.join('\n');
  791. },
  792. analysisPasteText: function (text) {
  793. const result = [];
  794. if (text === '') return result;
  795. const rows = text.split('\n');
  796. for (const r of rows) {
  797. const cols = r.split('\t');
  798. result.push(cols);
  799. }
  800. return result;
  801. },
  802. analysisPasteHtml: function (html) {
  803. const result = [];
  804. if (!html || html === '') return result;
  805. },
  806. /**
  807. * 树表结构,定位至指定的节点
  808. * @param {GC.Spread.Sheets.Worksheet} sheet - 需要定位的sheet
  809. * @param {Number} id - 定位节点的id
  810. */
  811. locateTreeNode: function (sheet, id, autoExpand = false) {
  812. const tree = sheet.zh_tree;
  813. if (!tree) { return }
  814. const node = tree.getItems(id);
  815. if (!node) { return }
  816. if (autoExpand && !node.visible) {
  817. const parents = tree.autoExpandNode(node);
  818. if (parents && parents.length > 0) {
  819. SpreadJsObj.reLoadNodesData(sheet, parents);
  820. SpreadJsObj.refreshTreeRowVisible(sheet);
  821. }
  822. }
  823. const index = tree.nodes.indexOf(node);
  824. const sels = sheet.getSelections();
  825. sheet.setSelection(index, sels[0].col, 1, 1);
  826. SpreadJsObj.reloadRowsBackColor(sheet, [index, sels[0].row]);
  827. sheet.getParent().focus();
  828. sheet.showRow(index, spreadNS.VerticalPosition.center);
  829. },
  830. saveTopAndSelect: function (sheet, cacheKey) {
  831. const sel = sheet.getSelections()[0];
  832. const top = sheet.getViewportTopRow(1);
  833. setLocalCache(cacheKey, JSON.stringify({top: top, sel: sel}));
  834. },
  835. loadTopAndSelect: function (sheet, cacheKey) {
  836. let ts = getLocalCache(cacheKey);
  837. if (ts !== '') {
  838. ts = JSON.parse(ts);
  839. if (ts === undefined || ts === null) return;
  840. if (ts.sel) {
  841. sheet.setSelection(ts.sel.row, ts.sel.col, ts.sel.rowCount, ts.sel.colCount);
  842. SpreadJsObj.reloadRowsBackColor(sheet, [0, ts.sel.row]);
  843. }
  844. if (ts.top) {
  845. sheet.showRow(ts.top, spreadNS.VerticalPosition.top);
  846. }
  847. }
  848. },
  849. resetTopAndSelect: function (sheet) {
  850. sheet.setSelection(0, 0, 1, 1);
  851. sheet.showRow(0, spreadNS.VerticalPosition.top);
  852. },
  853. /**
  854. * 获取当前选行的数据对象
  855. * @param {GC.Spread.Sheets.Worksheet} sheet
  856. * @returns {Object}
  857. */
  858. getSelectObject: function (sheet) {
  859. if (!sheet) {
  860. return null;
  861. } else if (sheet.zh_dataType) {
  862. const sel = sheet.getSelections()[0];
  863. if (!sel) {
  864. return null;
  865. }
  866. if (sheet.zh_dataType === this.DataType.Tree) {
  867. return sheet.zh_tree.nodes[sel.row];
  868. } else if (sheet.zh_dataType === this.DataType.Data) {
  869. return sheet.zh_data[sel.row];
  870. } else {
  871. return null;
  872. }
  873. }
  874. },
  875. /**
  876. * 刷新列显示
  877. * @param sheet
  878. */
  879. refreshColumnVisible: function (sheet) {
  880. if(sheet.zh_setting) {
  881. sheet.zh_setting.cols.forEach(function (col, index) {
  882. if (col.visible !== undefined && col.visible !== null) {
  883. sheet.setColumnVisible(index, col.visible);
  884. }
  885. });
  886. }
  887. },
  888. /**
  889. * 刷新行显示
  890. * @param sheet
  891. */
  892. refreshTreeRowVisible: function (sheet) {
  893. this.beginMassOperation(sheet);
  894. const sortData = sheet.zh_dataType === this.DataType.Data ? sheet.zh_data : sheet.zh_tree.nodes;
  895. for (const iRow in sortData) {
  896. const node = sortData[iRow];
  897. if (node.visible !== undefined && node.visible !== null) {
  898. sheet.setRowVisible(iRow, node.visible);
  899. } else {
  900. sheet.setRowVisible(iRow, true);
  901. }
  902. }
  903. // if (sheet.zh_tree) {
  904. // for (const iRow in sheet.zh_tree.nodes) {
  905. // const node = sheet.zh_tree.nodes[iRow];
  906. // if (node.visible !== undefined && node.visible !== null) {
  907. // sheet.setRowVisible(iRow, node.visible);
  908. // } else {
  909. // sheet.setRowVisible(iRow, true);
  910. // }
  911. // }
  912. // }
  913. this.endMassOperation(sheet);
  914. },
  915. refreshColumnAlign: function (sheet) {
  916. if (sheet.zh_setting) {
  917. for (const iCol in sheet.zh_setting.cols) {
  918. const col = sheet.zh_setting.cols[iCol];
  919. sheet.getRange(-1, iCol, -1, 1).hAlign(col.hAlign);
  920. }
  921. }
  922. },
  923. /**
  924. * 刷新列是否只读
  925. * @param sheet
  926. * @param field
  927. * @param readonly
  928. */
  929. resetFieldReadOnly: function (sheet, field, readonly) {
  930. const fields = field instanceof Array ? field : [field];
  931. if (sheet.zh_setting) {
  932. sheet.zh_setting.cols.forEach(function (col, i) {
  933. if (fields.indexOf(col.field) !== -1) {
  934. col.readOnly = readonly;
  935. for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
  936. sheet.getCell(iRow, i).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  937. }
  938. //sheet.getRange(-1, i, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(col.readOnly || sheet.zh_setting.readOnly || false);
  939. }
  940. });
  941. }
  942. },
  943. CellType: {
  944. /**
  945. * 获取树结构CellType
  946. * 通过SpreadJsObj.loadSheetData(sheet, 'tree', tree)加载树结构数据
  947. * 要求tree类型为PathTree, 节点必须含有{id, pid, level, order, is_leaf}数据
  948. * @returns {TreeNodeCellType}
  949. */
  950. getTreeNodeCellType: function () {
  951. const xOffset = 2 + 5;
  952. const indent = 20;
  953. const levelIndent = -5;
  954. const halfBoxLength = 5;
  955. const halfExpandLength = 3;
  956. const lineColor = '#515151';//'#b8b8b8';
  957. const expandBoxColor = '#434343';//'#808080';
  958. const dotLine = true;
  959. /**
  960. * 画一条点线段
  961. * @param canvas - 画布
  962. * @param x1 - 线段起点 x
  963. * @param y1 - 线段起点 y
  964. * @param x2 - 线段终点 x
  965. * @param y2 - 线段终点 y
  966. * @param color - 线段颜色
  967. */
  968. const drawDotLine = function (canvas, x1, y1, x2, y2, color) {
  969. canvas.save();
  970. // 设置偏移量
  971. canvas.translate(0.5, 0.5);
  972. canvas.beginPath();
  973. canvas.strokeStyle = color;
  974. canvas.dottedLine(x1, y1, x2, y2);
  975. canvas.stroke();
  976. canvas.restore();
  977. };
  978. /**
  979. * 画一条线段
  980. * @param canvas - 画布
  981. * @param x1 - 线段起点 x
  982. * @param y1 - 线段起点 y
  983. * @param x2 - 线段终点 x
  984. * @param y2 - 线段终点 y
  985. * @param color - 线段颜色
  986. */
  987. const drawLine = function (canvas, x1, y1, x2, y2, color) {
  988. canvas.save();
  989. // 设置偏移量
  990. canvas.translate(0.5, 0.5);
  991. canvas.beginPath();
  992. canvas.moveTo(x1, y1);
  993. canvas.lineTo(x2, y2);
  994. canvas.strokeStyle = color;
  995. canvas.stroke();
  996. canvas.restore();
  997. };
  998. /**
  999. * 画一个方框
  1000. * @param {Object} canvas - 画布
  1001. * @param {Object} rect - 方框区域
  1002. * @param {String} lineColor - 画线颜色
  1003. * @param {String} fillColor - 填充颜色
  1004. */
  1005. const drawBox = function (canvas, rect, lineColor, fillColor) {
  1006. canvas.save();
  1007. // 设置偏移量
  1008. canvas.translate(0.5, 0.5);
  1009. canvas.strokeStyle = lineColor;
  1010. canvas.beginPath();
  1011. canvas.moveTo(rect.left, rect.top);
  1012. canvas.lineTo(rect.left, rect.bottom);
  1013. canvas.lineTo(rect.right, rect.bottom);
  1014. canvas.lineTo(rect.right, rect.top);
  1015. canvas.lineTo(rect.left, rect.top);
  1016. canvas.stroke();
  1017. canvas.fillStyle = fillColor ? fillColor : 'white';
  1018. canvas.fill();
  1019. canvas.restore();
  1020. };
  1021. /**
  1022. * 画树结构-展开收起按钮
  1023. * @param {Object} canvas - 画布
  1024. * @param {Number} x - 单元格左顶点坐标 x
  1025. * @param {Number} y - 单元格左顶点坐标 y
  1026. * @param {Number} w - 单元格宽度
  1027. * @param {Number} h - 单元格高度
  1028. * @param {Number} centerX - 按钮中央坐标
  1029. * @param {Number} centerY - 按钮中央坐标
  1030. * @param {Boolean} expanded - 当前节点展开收起状态
  1031. */
  1032. const drawExpandBox = function (canvas, x, y, w, h, centerX, centerY, expanded, style) {
  1033. let rect = {
  1034. top: centerY - halfBoxLength,
  1035. bottom: centerY + halfBoxLength,
  1036. left: centerX - halfBoxLength,
  1037. right: centerX + halfBoxLength
  1038. };
  1039. let h1, h2, offset = 1;
  1040. if (rect.left < x + w) {
  1041. // 方框超出单元格宽度时,超出部分不画。
  1042. rect.right = Math.min(rect.right, x + w);
  1043. drawBox(canvas, rect, expandBoxColor, style.backColor);
  1044. // 画中心十字
  1045. // 画十字横线
  1046. h1 = centerX - halfExpandLength;
  1047. h2 = Math.min(centerX + halfExpandLength, x + w);
  1048. if (h2 > h1) {
  1049. drawLine(canvas, h1, centerY, h2, centerY, expandBoxColor);
  1050. }
  1051. // 画十字竖线
  1052. if (!expanded && (centerX < x + w)) {
  1053. drawLine(canvas, centerX, centerY - halfExpandLength, centerX, centerY + halfExpandLength, expandBoxColor);
  1054. }
  1055. }
  1056. };
  1057. let TreeNodeCellType = function (){};
  1058. TreeNodeCellType.prototype = new spreadNS.CellTypes.Text();
  1059. const proto = TreeNodeCellType.prototype;
  1060. /**
  1061. * 绘制方法
  1062. * @param {Object} canvas - 画布
  1063. * @param value - cell.value
  1064. * @param {Number} x - 单元格左顶点坐标 x
  1065. * @param {Number} y - 单元格左顶点坐标 y
  1066. * @param {Number} w - 单元格宽度
  1067. * @param {Number} h - 单元格高度
  1068. * @param {Object} style - cell.style
  1069. * @param {Object} options
  1070. */
  1071. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1072. // 清理 画布--单元格范围 旧数据
  1073. if (style.backColor) {
  1074. canvas.save();
  1075. canvas.fillStyle = style.backColor;
  1076. canvas.fillRect(x, y, w, h);
  1077. canvas.restore();
  1078. } else {
  1079. canvas.clearRect(x, y, w, h);
  1080. }
  1081. const tree = options.sheet.zh_tree;
  1082. // 使用TreeCellType前,需定义sheet.tree
  1083. if (tree) {
  1084. const node = options.row < tree.nodes.length ? tree.nodes[options.row] : null;
  1085. if (node) {
  1086. const showTreeLine = true;
  1087. const centerX = Math.floor(x) + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1088. const centerY = Math.floor((y + (y + h)) / 2);
  1089. // Draw Sibling Line
  1090. if (showTreeLine) {
  1091. // Draw Horizontal Line
  1092. if (centerX < x + w) {
  1093. const x1 = centerX + indent / 2;
  1094. if (dotLine) {
  1095. drawDotLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1096. } else {
  1097. drawLine(canvas, centerX, centerY, Math.min(x1, x + w), centerY, lineColor);
  1098. }
  1099. }
  1100. // Draw Vertical Line
  1101. if (centerX < x + w) {
  1102. const y1 = tree.isLastSibling(node) ? centerY : y + h;
  1103. const parent = tree.getParent(node);
  1104. const y2 = y1 - centerY;
  1105. if (node.order === 1 && !parent) {
  1106. if (dotLine) {
  1107. drawDotLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1108. } else {
  1109. drawLine(canvas, centerX, centerY, centerX, y1, lineColor);
  1110. }
  1111. } else {
  1112. if (dotLine) {
  1113. drawDotLine(canvas, centerX, y, centerX, y1, lineColor);
  1114. } else {
  1115. drawLine(canvas, centerX, y, centerX, y1, lineColor);
  1116. }
  1117. }
  1118. }
  1119. }
  1120. // Draw Expand Box
  1121. if (!node.is_leaf) {
  1122. drawExpandBox(canvas, x, y, w, h, centerX, centerY, node.expanded, style);
  1123. }
  1124. // Draw Parent Line
  1125. if (showTreeLine) {
  1126. let parent = tree.getParent(node), parentCenterX = centerX - indent - levelIndent;
  1127. while (parent) {
  1128. if (!tree.isLastSibling(parent)) {
  1129. if (parentCenterX < x + w) {
  1130. if (dotLine) {
  1131. drawDotLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1132. } else {
  1133. drawLine(canvas, parentCenterX, y, parentCenterX, y + h, lineColor);
  1134. }
  1135. }
  1136. }
  1137. parent = tree.getParent(parent);
  1138. parentCenterX -= (indent + levelIndent);
  1139. }
  1140. };
  1141. // 重定位x
  1142. const move = (node.level) * indent + (node.level) * levelIndent + xOffset;
  1143. x = x + move;
  1144. w = w - move;
  1145. }
  1146. }
  1147. // Drawing Text
  1148. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1149. };
  1150. /**
  1151. * 获取点击信息
  1152. * @param {Number} x
  1153. * @param {Number} y
  1154. * @param {Object} cellStyle
  1155. * @param {Object} cellRect
  1156. * @param {Object} context
  1157. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1158. */
  1159. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1160. return {
  1161. x: x,
  1162. y: y,
  1163. row: context.row,
  1164. col: context.col,
  1165. cellStyle: cellStyle,
  1166. cellRect: cellRect,
  1167. sheet: context.sheet,
  1168. sheetArea: context.sheetArea
  1169. };
  1170. };
  1171. /**
  1172. * 鼠标点击 树结构按钮 响应展开收起(未加载子节点时,先加载子节点)
  1173. * @param {Object} hitinfo - 见getHitInfo
  1174. */
  1175. proto.processMouseDown = function (hitinfo) {
  1176. const offset = -1;
  1177. const tree = hitinfo.sheet.zh_tree;
  1178. if (!tree) { return; }
  1179. const node = tree.nodes[hitinfo.row];
  1180. if (!node) { return; }
  1181. let centerX = hitinfo.cellRect.x + offset + (node.level - 1) * indent + (node.level) * levelIndent + indent / 2 + xOffset;
  1182. let centerY = (hitinfo.cellRect.y + offset + (hitinfo.cellRect.y + offset + hitinfo.cellRect.height)) / 2;
  1183. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1184. if (Math.abs(hitinfo.x - centerX) < halfBoxLength && Math.abs(hitinfo.y - centerY) < halfBoxLength) {
  1185. const children = tree.getChildren(node);
  1186. if (!node.expanded && !node.is_leaf && children.length === 0 && tree.loadChildren) {
  1187. tree.loadChildren(node, function () {
  1188. node.expanded = true;
  1189. const children = tree.getChildren(node);
  1190. hitinfo.sheet.addRows(hitinfo.row + 1, children.length);
  1191. SpreadJsObj.reLoadRowData(hitinfo.sheet, hitinfo.row + 1, children.length);
  1192. });
  1193. } else {
  1194. tree.setExpanded(node, !node.expanded);
  1195. SpreadJsObj.massOperationSheet(hitinfo.sheet, function () {
  1196. const posterity = tree.getPosterity(node);
  1197. for (const child of posterity) {
  1198. hitinfo.sheet.setRowVisible(tree.nodes.indexOf(child), child.visible, hitinfo.sheetArea);
  1199. }
  1200. });
  1201. hitinfo.sheet.repaint();
  1202. }
  1203. }
  1204. };
  1205. return new TreeNodeCellType();
  1206. },
  1207. /**
  1208. * 获取 带悬浮提示的CellType
  1209. * @returns {TipCellType}
  1210. */
  1211. getTipCellType: function () {
  1212. const maxHintWidth = 200, indent = 15, borderIndent = 10;
  1213. const TipCellType = function () {};
  1214. // 继承 SpreadJs定义的 普通的TextCellType
  1215. TipCellType.prototype = new spreadNS.CellTypes.Text();
  1216. const proto = TipCellType.prototype;
  1217. proto.getTextDisplayWidth = function(hitinfo, str, font) {
  1218. const xs = hitinfo.sheet.getParent().xs;
  1219. const ctx = xs.childNodes[0].getContext("2d");
  1220. if (font && font !== '') {
  1221. ctx.font = font;
  1222. } else {
  1223. ctx.font = hitinfo.cellStyle.font;
  1224. }
  1225. return ctx.measureText(str).width;
  1226. };
  1227. proto.showTip = function (hitinfo, text) {
  1228. return text && text !== '';
  1229. };
  1230. /**
  1231. * 获取点击信息
  1232. * @param {Number} x
  1233. * @param {Number} y
  1234. * @param {Object} cellStyle
  1235. * @param {Object} cellRect
  1236. * @param {Object} context
  1237. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1238. */
  1239. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1240. return {
  1241. x: x,
  1242. y: y,
  1243. row: context.row,
  1244. col: context.col,
  1245. cellStyle: cellStyle,
  1246. cellRect: cellRect,
  1247. sheet: context.sheet,
  1248. sheetArea: context.sheetArea,
  1249. ctx: context.sheet.getParent().xs,
  1250. };
  1251. };
  1252. /**
  1253. * 鼠标进入单元格事件 - 显示悬浮提示
  1254. * @param {Object} hitinfo - 见getHitInfo返回值
  1255. */
  1256. proto.processMouseEnter = function (hitinfo) {
  1257. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  1258. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1259. if (col.getTip && Object.prototype.toString.apply(col.getTip) === "[object Function]") {
  1260. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1261. text = col.getTip(sortData[hitinfo.row]);
  1262. }
  1263. const pos = SpreadJsObj.getObjPos(hitinfo.sheet.getParent().qo);
  1264. if (pos && this.showTip(hitinfo, text)) {
  1265. if (!this._toolTipElement) {
  1266. let div = $('#autoTip')[0];
  1267. if (!div) {
  1268. div = document.createElement("div");
  1269. $(div).css("position", "absolute")
  1270. .css("border", "1px #C0C0C0 solid")
  1271. .css("box-shadow", "1px 2px 5px rgba(0,0,0,0.4)")
  1272. .css("font", "9pt Arial")
  1273. .css("background", "white")
  1274. .css("padding", 5)
  1275. .css("z-index", 999)
  1276. .css("max-width", maxHintWidth)
  1277. .css("word-wrap", "break-word")
  1278. .attr("id", 'autoTip');
  1279. document.body.insertBefore(div, null);
  1280. }
  1281. const validWidth = $(window).width() - (pos.x + hitinfo.x + indent) - borderIndent;
  1282. const textWidth = this.getTextDisplayWidth(hitinfo, text, "9pt Arial");
  1283. if (validWidth >= maxHintWidth || textWidth <= validWidth) {
  1284. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x + indent);
  1285. } else if (textWidth > maxHintWidth) {
  1286. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - maxHintWidth);
  1287. } else {
  1288. $(div).text(text).css("top", pos.y + hitinfo.y + indent).css("left", pos.x + hitinfo.x - indent - textWidth);
  1289. }
  1290. this._toolTipElement = div;
  1291. $(div).show("fast");
  1292. }
  1293. }
  1294. };
  1295. /**
  1296. * 鼠标移出单元格事件 - 隐藏悬浮提示
  1297. * @param {Object} hitinfo - 见getHitInfo返回值
  1298. */
  1299. proto.processMouseLeave = function (hitinfo) {
  1300. if (this._toolTipElement) {
  1301. $(this._toolTipElement).hide();
  1302. this._toolTipElement = null;
  1303. }
  1304. };
  1305. return new TipCellType();
  1306. },
  1307. getAutoTipCellType: function () {
  1308. const AutoTipCellType = function () {};
  1309. // 继承 TipCellType
  1310. AutoTipCellType.prototype = SpreadJsObj.CellType.getTipCellType();
  1311. const proto = AutoTipCellType.prototype;
  1312. proto.showTip = function (hitinfo, text) {
  1313. return text && text !== '' && this.getTextDisplayWidth(hitinfo, text) > hitinfo.cellRect.width;
  1314. };
  1315. return new AutoTipCellType();
  1316. },
  1317. /**
  1318. * 获取 带图片的cellType(图片需在document中定义好img,并写入col的img属性)
  1319. *
  1320. * img:
  1321. * 1. 整列固定,则传入img的select
  1322. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = '#attachment-img'}
  1323. *
  1324. * 2. 各单元格自定义,则
  1325. * e.g. {title: '附件', field: 'attachment', cellType: 'image', img = getAttachmentImage}
  1326. * function getAttachmentImage (data) {
  1327. * $('#attachment-img').url = data.attachmentImageUrl;
  1328. * return $('#attachment-img')[0];
  1329. * }
  1330. *
  1331. * @returns {ImageCellType}
  1332. */
  1333. getImageCellType: function () {
  1334. const ImageCellType = function (){};
  1335. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1336. const proto = ImageCellType.prototype;
  1337. proto.getImage = function (sheet, iRow, iCol) {
  1338. const col = sheet.zh_setting.cols[iCol];
  1339. let imgSource = col.img;
  1340. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1341. const sortData = SpreadJsObj.getSortData(sheet);
  1342. const data = sortData ? sortData[iRow] : null;
  1343. return data ? imgSource(data) : null;
  1344. } else {
  1345. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1346. }
  1347. };
  1348. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1349. const col = options.sheet.zh_setting.cols[options.col];
  1350. const img = this.getImage(options.sheet, options.row, options.col);
  1351. const indent = col.indent ? col.indent : 10;
  1352. if (img) {
  1353. if (style.backColor) {
  1354. canvas.save();
  1355. canvas.fillStyle = style.backColor;
  1356. canvas.fillRect(x, y, indent + img.width, h);
  1357. canvas.restore();
  1358. }
  1359. canvas.drawImage(img, x + indent, y + (h - img.height) / 2);
  1360. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1361. style.hAlign = spreadNS.HorizontalAlign.left;
  1362. }
  1363. x = x + indent + img.width;
  1364. w = w - indent - img.width;
  1365. }
  1366. // Drawing Text
  1367. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1368. };
  1369. /**
  1370. * 获取点击信息
  1371. * @param {Number} x
  1372. * @param {Number} y
  1373. * @param {Object} cellStyle
  1374. * @param {Object} cellRect
  1375. * @param {Object} context
  1376. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1377. */
  1378. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1379. return {
  1380. x: x,
  1381. y: y,
  1382. row: context.row,
  1383. col: context.col,
  1384. cellStyle: cellStyle,
  1385. cellRect: cellRect,
  1386. sheet: context.sheet,
  1387. sheetArea: context.sheetArea
  1388. };
  1389. };
  1390. /**
  1391. * 鼠标点击
  1392. * @param {Object} hitinfo - 见getHitInfo
  1393. */
  1394. proto.processMouseDown = function (hitinfo) {
  1395. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1396. if (img) {
  1397. const halfX = img.width / 2, halfY = img.height / 2;
  1398. const centerX = hitinfo.cellRect.x + indent + halfX;
  1399. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1400. // 点击展开节点时,如果已加载子项,则展开,反之这加载子项,展开
  1401. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1402. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1403. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1404. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1405. const data = sortData ? sortData[hitinfo.row] : null;
  1406. imageClick(data, hitinfo);
  1407. }
  1408. }
  1409. }
  1410. };
  1411. return new ImageCellType();
  1412. },
  1413. /**
  1414. *
  1415. * 获取 带normal-hover-active按钮的cellType(需定义三张图片,须在document中定义好img,并写入col的normalImg, hoverImg, activeImg属性)
  1416. * 其中:normalImg必需,向下套用(不存在activeImg则使用hoverImg,不存在hoverImg则使用normalImg)
  1417. * 三个img均可像getImageCellType一样动态获取,参见getImageCellType注释
  1418. *
  1419. * @returns {ImageCellType}
  1420. */
  1421. getImageButtonCellType: function () {
  1422. let hover = 1, active = 2;
  1423. const ImageCellType = function (){};
  1424. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1425. const proto = ImageCellType.prototype;
  1426. proto.getImage = function (sheet, iRow, iCol) {
  1427. const col = sheet.zh_setting.cols[iCol];
  1428. let imgSource = col.normalImg;
  1429. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1430. if (tag === active) {
  1431. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1432. } else if (tag === hover) {
  1433. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1434. }
  1435. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1436. const sortData = SpreadJsObj.getSortData(sheet);
  1437. const data = sortData ? sortData[iRow] : null;
  1438. return data ? imgSource(data) : null;
  1439. } else {
  1440. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1441. }
  1442. };
  1443. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1444. const col = options.sheet.zh_setting.cols[options.col];
  1445. const sortData = SpreadJsObj.getSortData(options.sheet);
  1446. const data = sortData ? sortData[options.row] : null;
  1447. let showImage = true;
  1448. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1449. showImage = col.showImage(data);
  1450. }
  1451. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1452. const indent = col.indent ? col.indent : 10;
  1453. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1454. if (img) {
  1455. if (style.backColor) {
  1456. canvas.save();
  1457. canvas.fillStyle = style.backColor;
  1458. canvas.fillRect(x + w - indent - img.width, y, img.width, h);
  1459. canvas.restore();
  1460. }
  1461. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1462. w = w - indent - img.width;
  1463. }
  1464. // Drawing Text
  1465. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1466. } else {
  1467. if (img) {
  1468. if (style.backColor) {
  1469. canvas.save();
  1470. canvas.fillStyle = style.backColor;
  1471. canvas.fillRect(x, y, indent + img.width, h);
  1472. canvas.restore();
  1473. }
  1474. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1475. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1476. style.hAlign = spreadNS.HorizontalAlign.left;
  1477. }
  1478. x = x + indent + img.width;
  1479. w = w - indent - img.width;
  1480. }
  1481. // Drawing Text
  1482. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1483. }
  1484. };
  1485. /**
  1486. * 获取点击信息
  1487. * @param {Number} x
  1488. * @param {Number} y
  1489. * @param {Object} cellStyle
  1490. * @param {Object} cellRect
  1491. * @param {Object} context
  1492. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1493. */
  1494. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1495. return {
  1496. x: x,
  1497. y: y,
  1498. row: context.row,
  1499. col: context.col,
  1500. cellStyle: cellStyle,
  1501. cellRect: cellRect,
  1502. sheet: context.sheet,
  1503. sheetArea: context.sheetArea
  1504. };
  1505. };
  1506. proto.processMouseEnter = function (hitinfo) {
  1507. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1508. // Drawing Image
  1509. if (col.hoverImg) {
  1510. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1511. cell.tag(hover);
  1512. hitinfo.sheet.repaint(hitinfo.cellRect);
  1513. }
  1514. };
  1515. proto.processMouseLeave = function (hitinfo) {
  1516. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1517. // Drawing Image
  1518. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1519. cell.tag(null);
  1520. hitinfo.sheet.repaint(hitinfo.cellRect);
  1521. };
  1522. // proto.processMouseDown = function (hitinfo) {
  1523. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1524. // if (col.activeImg) {
  1525. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1526. // cell.tag(active);
  1527. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1528. // }
  1529. // };
  1530. // proto.processMouseUp = function (hitinfo) {
  1531. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1532. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1533. // const data = sortData ? sortData[hitinfo.row] : null;
  1534. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1535. // if (!col.showImage(data)) {
  1536. // return;
  1537. // }
  1538. // }
  1539. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1540. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1541. // imageClick(data);
  1542. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1543. // cell.tag(null);
  1544. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1545. // }
  1546. // };
  1547. /*
  1548. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1549. */
  1550. proto.processMouseDown = function (hitinfo) {
  1551. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1552. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1553. const halfX = img.width / 2, halfY = img.height / 2;
  1554. const indent = col.indent ? col.indent : 10;
  1555. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1556. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1557. : hitinfo.cellRect.x + indent + halfX;
  1558. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1559. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1560. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1561. cell.tag(active);
  1562. hitinfo.sheet.repaint(hitinfo.cellRect);
  1563. }
  1564. };
  1565. proto.processMouseUp = function (hitinfo) {
  1566. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1567. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1568. const halfX = img.width / 2, halfY = img.height / 2;
  1569. const indent = col.indent ? col.indent : 10;
  1570. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1571. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1572. : hitinfo.cellRect.x + indent + halfX;
  1573. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1574. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1575. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1576. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1577. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1578. const data = sortData ? sortData[hitinfo.row] : null;
  1579. imageClick(data, hitinfo);
  1580. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1581. cell.tag(null);
  1582. hitinfo.sheet.repaint(hitinfo.cellRect);
  1583. }
  1584. }
  1585. };
  1586. proto.processMouseMove = function (hitinfo) {
  1587. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1588. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1589. const halfX = img.width / 2, halfY = img.height / 2;
  1590. const indent = col.indent ? col.indent : 10;
  1591. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1592. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1593. : hitinfo.cellRect.x + indent + halfX;
  1594. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1595. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1596. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1597. if (cell.tag() !== hover) {
  1598. cell.tag(hover);
  1599. hitinfo.sheet.repaint(hitinfo.cellRect);
  1600. }
  1601. } else {
  1602. if (cell.tag() === hover) {
  1603. cell.tag(null);
  1604. hitinfo.sheet.repaint(hitinfo.cellRect);
  1605. }
  1606. }
  1607. };
  1608. return new ImageCellType();
  1609. },
  1610. getActiveImageButtonCellType: function () {
  1611. let show = 1, hover = 2, active = 2;
  1612. const ImageCellType = function (){};
  1613. ImageCellType.prototype = new spreadNS.CellTypes.Text();
  1614. const proto = ImageCellType.prototype;
  1615. proto.getImage = function (sheet, iRow, iCol) {
  1616. const col = sheet.zh_setting.cols[iCol];
  1617. let imgSource = null;
  1618. const cell = sheet.getCell(iRow, iCol), tag = cell.tag();
  1619. if (tag === show) {
  1620. imgSource = col.normalImg;
  1621. } else if (tag === active) {
  1622. imgSource = col.activeImg ? col.activeImg : (col.hoverImg ? col.hoverImg : col.normalImg);
  1623. } else if (tag === hover) {
  1624. imgSource = col.hoverImg ? col.hoverImg : col.normalImg;
  1625. }
  1626. if (imgSource && Object.prototype.toString.apply(imgSource) === "[object Function]") {
  1627. const sortData = SpreadJsObj.getSortData(sheet);
  1628. const data = sortData ? sortData[iRow] : null;
  1629. return data ? imgSource(data) : null;
  1630. } else {
  1631. return $(imgSource)[0] ? $(imgSource)[0] : null;
  1632. }
  1633. };
  1634. proto.paint = function (canvas, value, x, y, w, h, style, options) {
  1635. const col = options.sheet.zh_setting.cols[options.col];
  1636. const sortData = SpreadJsObj.getSortData(options.sheet);
  1637. const data = sortData ? sortData[options.row] : null;
  1638. let showImage = true;
  1639. if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1640. showImage = col.showImage(data);
  1641. }
  1642. const img = showImage ? this.getImage(options.sheet, options.row, options.col) : null;
  1643. const indent = col.indent ? col.indent : 10;
  1644. if (style.hAlign === spreadNS.HorizontalAlign.right) {
  1645. if (img) {
  1646. if (style.backColor) {
  1647. canvas.save();
  1648. canvas.fillStyle = style.backColor;
  1649. canvas.fillRect(x + w - indent - img.width, y, img.width + indent, h);
  1650. canvas.restore();
  1651. }
  1652. canvas.drawImage(img, x + w - indent - img.width, y + (h - img.height) / 2);
  1653. w = w - indent - img.width;
  1654. }
  1655. // Drawing Text
  1656. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1657. } else {
  1658. if (img) {
  1659. if (style.backColor) {
  1660. canvas.save();
  1661. canvas.fillStyle = style.backColor;
  1662. canvas.fillRect(x, y, indent + img.width, h);
  1663. canvas.restore();
  1664. }
  1665. canvas.drawImage(img, x + 10, y + (h - img.height) / 2);
  1666. if (style.hAlign !== spreadNS.HorizontalAlign.left) {
  1667. style.hAlign = spreadNS.HorizontalAlign.left;
  1668. }
  1669. x = x + indent + img.width;
  1670. w = w - indent - img.width;
  1671. }
  1672. // Drawing Text
  1673. spreadNS.CellTypes.Text.prototype.paint.apply(this, [canvas, value, x, y, w, h, style, options]);
  1674. }
  1675. };
  1676. /**
  1677. * 获取点击信息
  1678. * @param {Number} x
  1679. * @param {Number} y
  1680. * @param {Object} cellStyle
  1681. * @param {Object} cellRect
  1682. * @param {Object} context
  1683. * @returns {{x: *, y: *, row: *, col: *|boolean|*[]|number|{}|UE.dom.dtd.col, cellStyle: *, cellRect: *, sheet: *|StyleSheet, sheetArea: *}}
  1684. */
  1685. proto.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  1686. return {
  1687. x: x,
  1688. y: y,
  1689. row: context.row,
  1690. col: context.col,
  1691. cellStyle: cellStyle,
  1692. cellRect: cellRect,
  1693. sheet: context.sheet,
  1694. sheetArea: context.sheetArea
  1695. };
  1696. };
  1697. proto.processMouseEnter = function (hitinfo) {
  1698. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1699. // Drawing Image
  1700. if (col.normalImg) {
  1701. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1702. cell.tag(show);
  1703. hitinfo.sheet.repaint(hitinfo.cellRect);
  1704. }
  1705. };
  1706. proto.processMouseLeave = function (hitinfo) {
  1707. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1708. cell.tag(null);
  1709. hitinfo.sheet.repaint(hitinfo.cellRect);
  1710. };
  1711. // proto.processMouseDown = function (hitinfo) {
  1712. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1713. // if (col.activeImg) {
  1714. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1715. // cell.tag(active);
  1716. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1717. // }
  1718. // };
  1719. // proto.processMouseUp = function (hitinfo) {
  1720. // const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1721. // const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1722. // const data = sortData ? sortData[hitinfo.row] : null;
  1723. // if (col.showImage && Object.prototype.toString.apply(col.showImage) === "[object Function]") {
  1724. // if (!col.showImage(data)) {
  1725. // return;
  1726. // }
  1727. // }
  1728. // const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1729. // if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1730. // imageClick(data);
  1731. // const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1732. // cell.tag(null);
  1733. // hitinfo.sheet.repaint(hitinfo.cellRect);
  1734. // }
  1735. // };
  1736. /*
  1737. 注释部分以进入鼠标进入图片,点击图片为基准更新图片,鼠标快速移动时,可能失效
  1738. */
  1739. proto.processMouseDown = function (hitinfo) {
  1740. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1741. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1742. const halfX = img.width / 2, halfY = img.height / 2;
  1743. const indent = col.indent ? col.indent : 10;
  1744. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1745. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1746. : hitinfo.cellRect.x + indent + halfX;
  1747. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1748. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1749. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1750. cell.tag(active);
  1751. hitinfo.sheet.repaint(hitinfo.cellRect);
  1752. }
  1753. };
  1754. proto.processMouseUp = function (hitinfo) {
  1755. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1756. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1757. const halfX = img.width / 2, halfY = img.height / 2;
  1758. const indent = col.indent ? col.indent : 10;
  1759. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1760. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1761. : hitinfo.cellRect.x + indent + halfX;
  1762. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1763. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1764. const imageClick = hitinfo.sheet.zh_setting ? hitinfo.sheet.zh_setting.imageClick : null;
  1765. if (imageClick && Object.prototype.toString.apply(imageClick) === "[object Function]") {
  1766. const sortData = SpreadJsObj.getSortData(hitinfo.sheet);
  1767. const data = sortData ? sortData[hitinfo.row] : null;
  1768. imageClick(data, hitinfo);
  1769. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1770. cell.tag(hover);
  1771. hitinfo.sheet.repaint(hitinfo.cellRect);
  1772. }
  1773. }
  1774. };
  1775. proto.processMouseMove = function (hitinfo) {
  1776. const col = hitinfo.sheet.zh_setting.cols[hitinfo.col];
  1777. const img = this.getImage(hitinfo.sheet, hitinfo.row, hitinfo.col);
  1778. const halfX = img.width / 2, halfY = img.height / 2;
  1779. const indent = col.indent ? col.indent : 10;
  1780. const centerX = hitinfo.cellStyle.hAlign === spreadNS.HorizontalAlign.right
  1781. ? hitinfo.cellRect.x + hitinfo.cellRect.width - indent -halfX
  1782. : hitinfo.cellRect.x + indent + halfX;
  1783. const centerY = hitinfo.cellRect.y + hitinfo.cellRect.height / 2;
  1784. const cell = hitinfo.sheet.getCell(hitinfo.row, hitinfo.col);
  1785. if (Math.abs(hitinfo.x - centerX) < halfX && Math.abs(hitinfo.y - centerY) < halfY) {
  1786. if (cell.tag() !== hover) {
  1787. cell.tag(hover);
  1788. hitinfo.sheet.repaint(hitinfo.cellRect);
  1789. }
  1790. } else {
  1791. if (cell.tag() === hover) {
  1792. cell.tag(show);
  1793. hitinfo.sheet.repaint(hitinfo.cellRect);
  1794. }
  1795. }
  1796. };
  1797. return new ImageCellType();
  1798. },
  1799. /**
  1800. * 获取 嵌入Html的cellType
  1801. * @returns {HTMLCellType}
  1802. */
  1803. getHtmlCellType: function () {
  1804. const HTMLCellType = function (){};
  1805. HTMLCellType.prototype = new spreadNS.CellTypes.Text;
  1806. const proto = ImageCellType.prototype;
  1807. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1808. let DOMURL = window.URL || window.webkitURL || window;
  1809. let cell = context.sheet.getCell(context.row, context.col);
  1810. let img = cell.tag();
  1811. if (img) {
  1812. try {
  1813. ctx.save();
  1814. ctx.rect(x, y, w, h);
  1815. ctx.clip();
  1816. ctx.drawImage(img, x + 2, y + 2)
  1817. ctx.restore();
  1818. cell.tag(null);
  1819. return;
  1820. }
  1821. catch (err) {
  1822. GC.Spread.Sheets.CustomCellType.prototype.paint.apply(this, [ctx, "#HTMLError", x, y, w, h, style, context])
  1823. cell.tag(null);
  1824. return;
  1825. }
  1826. }
  1827. let svgPattern = '<svg xmlns="http://www.w3.org/2000/svg" width="{0}" height="{1}">' +
  1828. '<foreignObject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml" style="font:{2}">{3}</div></foreignObject></svg>';
  1829. let data = svgPattern.replace("{0}", w).replace("{1}", h).replace("{2}", style.font).replace("{3}", value);
  1830. let doc = document.implementation.createHTMLDocument("");
  1831. doc.write(data);
  1832. // Get well-formed markup
  1833. data = (new XMLSerializer()).serializeToString(doc.body.children[0]);
  1834. img = new Image();
  1835. //var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  1836. //var url = DOMURL.createObjectURL(svg);
  1837. //img.src = url;
  1838. img.src = 'data:image/svg+xml;base64,' + window.btoa(data);
  1839. cell.tag(img);
  1840. img.onload = function () {
  1841. context.sheet.repaint(new GC.Spread.Sheets.Rect(x, y, w, h));
  1842. }
  1843. };
  1844. return new HTMLCellType();
  1845. },
  1846. /**
  1847. * 获取 字符超长缩略的cellType
  1848. * @returns {EllipsisTextCellType}
  1849. */
  1850. getEllipsisTextCellType: function () {
  1851. const EllipsisTextCellType = function (){};
  1852. EllipsisTextCellType.prototype = new spreadNS.CellTypes.Text;
  1853. const proto = EllipsisTextCellType.prototype;
  1854. proto.getEllipsisText = function(c, str, maxWidth) {
  1855. var width = c.measureText(str).width;
  1856. var ellipsis = '…';
  1857. var ellipsisWidth = c.measureText(ellipsis).width;
  1858. if (width <= maxWidth || width <= ellipsisWidth) {
  1859. return str;
  1860. } else {
  1861. var len = str.length;
  1862. while (width >= maxWidth - ellipsisWidth && len-- > 0) {
  1863. str = str.substring(0, len);
  1864. width = c.measureText(str).width;
  1865. }
  1866. return str + ellipsis;
  1867. }
  1868. };
  1869. proto.paint = function (ctx, value, x, y, w, h, style, context) {
  1870. ctx.font = style.font;
  1871. value = this.getEllipsisText(ctx, value, w - 2);
  1872. spreadNS.CellTypes.Text.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context]);
  1873. };
  1874. return new EllipsisTextCellType();
  1875. },
  1876. getEllipsisTextAutoTipCellType: function () {
  1877. const CellType = function () {};
  1878. // 继承 TipCellType
  1879. CellType.prototype = SpreadJsObj.CellType.getAutoTipCellType();
  1880. const proto = CellType.prototype;
  1881. const ellipsisTextCellType = SpreadJsObj.CellType.getEllipsisTextCellType();
  1882. proto.getEllipsisText = ellipsisTextCellType.getEllipsisText;
  1883. proto.paint = ellipsisTextCellType.paint;
  1884. return new CellType();
  1885. },
  1886. /**
  1887. * 获取 动态显示ComboBox的cellType
  1888. * @returns {ActiveComboCellType}
  1889. */
  1890. getActiveComboCellType: function () {
  1891. const ActiveComboCellType = function () {};
  1892. ActiveComboCellType.prototype = new spreadNS.CellTypes.ComboBox();
  1893. const proto = ActiveComboCellType.prototype;
  1894. proto.paintValue = function (ctx, value, x, y, w, h, style, options) {
  1895. const sheet = options.sheet;
  1896. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1897. && !sheet.getCell(options.row, options.col).locked()) {
  1898. spreadNS.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  1899. } else {
  1900. spreadNS.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  1901. }
  1902. };
  1903. proto.processMouseEnter = function (hitinfo) {
  1904. hitinfo.sheet.repaint(hitinfo.cellRect);
  1905. };
  1906. proto.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  1907. const sheet = options.sheet;
  1908. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex()
  1909. && !sheet.getCell(options.row, options.col).locked()) {
  1910. return spreadNS.CellTypes.ComboBox.prototype.getHitInfo.apply(this, [x, y, cellStyle, cellRect, options]);
  1911. } else {
  1912. return {
  1913. x: x,
  1914. y: y,
  1915. row: options.row,
  1916. col: options.col,
  1917. cellStyle: cellStyle,
  1918. cellRect: cellRect,
  1919. sheetArea: options.sheetArea
  1920. };
  1921. }
  1922. };
  1923. return new ActiveComboCellType();
  1924. },
  1925. /**
  1926. * 获取 单位的CellType
  1927. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1928. */
  1929. getUnitCellType: function () {
  1930. let combo = this.getActiveComboCellType();
  1931. combo.itemHeight(10).items(['m', 'km', 'm2', 'm3', 'dm3', 'kg', 't', 'm3·km',
  1932. '总额', '月' ,'项', '处' ,'个', '根', '棵', '块', '台', '系统', '延米', '每一试桩',
  1933. '桥长米', '公路公里', '株', '组', '座', '元', '工日', '套', '台班', '艘班', '亩',
  1934. 'm/处', 'm/道', 'm/座', 'm2/m', 'm3/m', 'm3/处', '根/米', 'm3/m2']);
  1935. return combo;
  1936. },
  1937. /**
  1938. * 获取 自定义的CellType
  1939. * @returns {GC.Spread.Sheets.CellTypes.ComboBox}
  1940. */
  1941. getCustomizeComboCellType: function (items) {
  1942. let combo = this.getActiveComboCellType();
  1943. combo.itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  1944. return combo;
  1945. },
  1946. /**
  1947. * 获取 日期选择 CellType
  1948. * @returns {DatePickerCellType}
  1949. */
  1950. getDatePickerCellType: function () {
  1951. let datepicker;
  1952. const DatePickerCellType = function () {};
  1953. DatePickerCellType.prototype = new spreadNS.CellTypes.Text();
  1954. const proto = DatePickerCellType.prototype;
  1955. proto.createEditorElement = function (context) {
  1956. return document.createElement("input");
  1957. };
  1958. proto.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  1959. const self = this;
  1960. if (editorContext) {
  1961. const $editor = $(editorContext);
  1962. //spreadNS.CellTypes.Text.prototype.activateEditor.apply(this, arguments);
  1963. $editor.css("position", "absolute");
  1964. datepicker = $editor.datepicker({
  1965. language: 'zh',
  1966. dateFormat: 'yyyy-MM-DD'
  1967. }).data('datepicker');
  1968. datepicker.show();
  1969. }
  1970. };
  1971. proto.deactivateEditor = function (editorContext, context) {
  1972. datepicker.hide();
  1973. datepicker.destroy();
  1974. spreadNS.CellTypes.Base.prototype.deactivateEditor.apply(this, arguments);
  1975. };
  1976. proto.setEditorValue = function (editor, value, context) {
  1977. if (datepicker) {
  1978. datepicker.selectDate(value);
  1979. }
  1980. };
  1981. proto.getEditorValue = function (editor, context) {
  1982. return datepicker ? datepicker.focused : null;
  1983. };
  1984. proto.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  1985. if (editorContext) {
  1986. const $editor = $(editorContext);
  1987. $editor.css("width", cellRect.width + 1);
  1988. $editor.css("height", cellRect.height + 3);
  1989. }
  1990. };
  1991. return new DatePickerCellType();
  1992. }
  1993. },
  1994. Formatter: {
  1995. baseNumberFormatter: (function (){
  1996. const formatter = {};
  1997. formatter.prototype = GC.Spread.Formatter.FormatterBase;
  1998. /**
  1999. * 格式化数字显示方式
  2000. * 用法
  2001. * formatNumber(12345.999,'#,##0.00');
  2002. * formatNumber(12345.999,'#,##0.##');
  2003. * formatNumber(123,'000000');
  2004. * @param num
  2005. * @param pattern
  2006. */
  2007. formatter.formatNum = function (num, pattern) {
  2008. var strarr = num?num.toString().split('.'):['0'];
  2009. var fmtarr = pattern?pattern.split('.'):[''];
  2010. var retstr='';
  2011. // 整数部分
  2012. var str = strarr[0];
  2013. var fmt = fmtarr[0];
  2014. var i = str.length-1;
  2015. var comma = false;
  2016. for(var f=fmt.length-1;f>=0;f--){
  2017. switch(fmt.substr(f,1)){
  2018. case '#':
  2019. if(i>=0 ) retstr = str.substr(i--,1) + retstr;
  2020. break;
  2021. case '0':
  2022. if(i>=0) retstr = str.substr(i--,1) + retstr;
  2023. else retstr = '0' + retstr;
  2024. break;
  2025. case ',':
  2026. comma = true;
  2027. retstr=','+retstr;
  2028. break;
  2029. }
  2030. }
  2031. if(i>=0){
  2032. if(comma){
  2033. var l = str.length;
  2034. for(;i>=0;i--){
  2035. retstr = str.substr(i,1) + retstr;
  2036. if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr;
  2037. }
  2038. }
  2039. else retstr = str.substr(0,i+1) + retstr;
  2040. }
  2041. retstr = retstr+'.';
  2042. // 处理小数部分
  2043. str=strarr.length>1?strarr[1]:'';
  2044. fmt=fmtarr.length>1?fmtarr[1]:'';
  2045. i=0;
  2046. for(var f=0;f<fmt.length;f++){
  2047. switch(fmt.substr(f,1)){
  2048. case '#':
  2049. if(i<str.length) retstr+=str.substr(i++,1);
  2050. break;
  2051. case '0':
  2052. if(i<str.length) retstr+= str.substr(i++,1);
  2053. else retstr+='0';
  2054. break;
  2055. }
  2056. }
  2057. return retstr.replace(/^,+/,'').replace(/\.$/,'');
  2058. };
  2059. formatter.format = function (obj, conditionalForeColor) {
  2060. if (this.pattern) {
  2061. return this.formatNum(obj, this.pattern);
  2062. } else {
  2063. return obj;
  2064. }
  2065. };
  2066. return formatter;
  2067. })(),
  2068. _numFormatter: [],
  2069. getNumberFormatter(pattern) {
  2070. let formatter = this._numFormatter.find(function (f) {
  2071. return f.pattern === pattern;
  2072. });
  2073. if (!formatter) {
  2074. formatter = {};
  2075. formatter.prototype = this.baseNumberFormatter.prototype;
  2076. formatter.pattern = pattern;
  2077. this._numFormatter.push(formatter);
  2078. }
  2079. return formatter;
  2080. },
  2081. },
  2082. Clipboard: (function () {
  2083. let cText = '', cHtml;
  2084. const setCopyData = function (text, html) {
  2085. cText = text;
  2086. cHtml = html;
  2087. };
  2088. const getPasteData = function () {
  2089. return {text: cText, html: cHtml};
  2090. };
  2091. const clearData = function () {
  2092. cText = '';
  2093. cHtml = undefined;
  2094. };
  2095. const setSheetFilterCopyData = function (sheet) {
  2096. cText = SpreadJsObj.getFilterCopyText(sheet);
  2097. cHtml = SpreadJsObj.getFilterCopyHTML(sheet);
  2098. };
  2099. const getAnalysisPasteText = function () {
  2100. return SpreadJsObj.analysisPasteText(cText);
  2101. };
  2102. const getAnalysisPasteHtml = function () {
  2103. return SpreadJsObj.analysisPasteHtml(cHtml);
  2104. };
  2105. return {
  2106. setCopyData, getPasteData, clearData,
  2107. setSheetFilterCopyData,
  2108. getAnalysisPasteText, getAnalysisPasteHtml,
  2109. }
  2110. })(),
  2111. };