spreadjs_zh.js 100 KB

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