spreadjs_zh.js 98 KB

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