spreadjs_zh.js 92 KB

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