spreadjs_zh.js 92 KB

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