spreadjs_zh.js 92 KB

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