spreadjs_zh.js 98 KB

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