sheet_common.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var sheetCommonObj = {
  5. // CSL.2017.06.05
  6. // createSpread、initSheet 在一个Spread多个Sheet分别调用时的情况下使用。
  7. createSpread: function(container, SheetCount){
  8. var me = this;
  9. var spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: SheetCount });
  10. spreadBook.options.tabStripVisible = false;
  11. spreadBook.options.showHorizontalScrollbar = true;
  12. spreadBook.options.showVerticalScrollbar = true;
  13. spreadBook.options.allowCopyPasteExcelStyle = false;
  14. spreadBook.options.allowUserDragDrop = true;
  15. spreadBook.options.allowContextMenu = false;
  16. spreadBook.options.allowUserEditFormula = false;
  17. spreadBook.options.showDragFillSmartTag = false;
  18. return spreadBook;
  19. },
  20. initSheet: function(sheet, setting, rowCount) {
  21. var me = this;
  22. var spreadNS = GC.Spread.Sheets;
  23. sheet.suspendPaint();
  24. sheet.suspendEvent();
  25. if(setting.frozenCols) sheet.frozenColumnCount(setting.frozenCols);//冻结列
  26. sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
  27. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  28. if (setting && setting.view && setting.view.colHeaderHeight) {
  29. sheet.setRowHeight(0, setting.view.colHeaderHeight, spreadNS.SheetArea.colHeader);
  30. };
  31. if (setting && setting.view && setting.view.rowHeaderWidth) {
  32. sheet.setColumnWidth(0, setting.view.rowHeaderWidth, spreadNS.SheetArea.rowHeader);
  33. };
  34. if (setting.emptyRowHeader) {
  35. sheet.setColumnWidth(0, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  36. }
  37. sheet.options.colHeaderAutoTextIndex = 1;
  38. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  39. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  40. sheet.options.protectionOptions = {
  41. allowResizeColumns: true
  42. };
  43. sheet.showRowOutline(false);
  44. sheet.options.allowCellOverflow = false;
  45. me.buildHeader(sheet, setting);
  46. if (rowCount > 0)
  47. sheet.setRowCount(rowCount);
  48. else
  49. sheet.setRowCount(1);
  50. sheet.resumeEvent();
  51. sheet.resumePaint();
  52. },
  53. // buildSheet 在一个Spread、一个Sheet的情况下使用。
  54. buildSheet: function(container, setting, rowCount) {
  55. var me = this;
  56. var spreadBook = me.createSpread(container, { sheetCount: 1 });
  57. var sheet = spreadBook.getSheet(0);
  58. me.initSheet(sheet, setting, rowCount);
  59. return spreadBook;
  60. },
  61. buildHeader: function(sheet, setting){
  62. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  63. for (var i = 0; i < setting.header.length; i++) {
  64. sheet.setValue(0, i, setting.header[i].headerName, ch);
  65. sheet.getCell(0, i, ch).wordWrap(true);
  66. sheet.setColumnWidth(i, setting.header[i].headerWidth?setting.header[i].headerWidth:100);
  67. sheet.setColumnVisible(i,setting.header[i].visible === false ? false:true);
  68. }
  69. },
  70. cleanSheet: function(sheet, setting, rowCount) {
  71. sheet.suspendPaint();
  72. sheet.suspendEvent();
  73. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  74. if (rowCount > 0) sheet.setRowCount(rowCount);
  75. sheet.clearSelection();
  76. sheet.resumeEvent();
  77. sheet.resumePaint();
  78. },
  79. cleanData: function (sheet, setting, rowCount) {
  80. sheet.suspendPaint();
  81. sheet.suspendEvent();
  82. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  83. if (rowCount > 0) sheet.setRowCount(rowCount);
  84. sheet.resumeEvent();
  85. sheet.resumePaint();
  86. },
  87. setAreaAlign: function(area, hAlign, vAlign){
  88. if (!(hAlign) || hAlign === "left") {
  89. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  90. } else if (hAlign === "right") {
  91. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  92. } else if (hAlign === "center") {
  93. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  94. } else {
  95. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  96. }
  97. if (!(vAlign) || vAlign === "center") {
  98. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  99. } else if (vAlign === "top") {
  100. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  101. } else if (vAlign === "bottom") {
  102. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  103. } else {
  104. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  105. }
  106. },
  107. showData: function(sheet, setting, data,distTypeTree) {
  108. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  109. sheet.suspendPaint();
  110. sheet.suspendEvent();
  111. //sheet.addRows(row, 1);
  112. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  113. if(sheet.getRowCount()<data.length){
  114. data.length<30? sheet.setRowCount(30):sheet.setRowCount(data.length);
  115. }else if(sheet.getRowCount()==0){
  116. sheet.setRowCount(30);
  117. }
  118. for (var col = 0; col < setting.header.length; col++) {
  119. var hAlign = "left", vAlign = "center";
  120. if (setting.header[col].hAlign) {
  121. hAlign = setting.header[col].hAlign;
  122. } else if (setting.header[col].dataType !== "String"){
  123. hAlign = "right";
  124. }
  125. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  126. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  127. if (setting.header[col].formatter) {
  128. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  129. }
  130. if(setting.header[col].cellType === "checkBox"||setting.header[col].cellType === "button"){//clear and reset
  131. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  132. sheet.deleteColumns(col,1);
  133. sheet.addColumns(col, 1);
  134. sheet.setValue(0, col, setting.header[col].headerName, header);
  135. sheet.setColumnWidth(col, setting.header[col].headerWidth?setting.header[col].headerWidth:100);
  136. }
  137. if(setting.header[col].visible!==null&&setting.header[col].visible!==undefined){
  138. sheet.setColumnVisible(col,setting.header[col].visible);
  139. }
  140. sheet.getCell(0, col, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
  141. }
  142. for (var row = 0; row < data.length; row++) {
  143. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  144. this.showRowData(sheet,setting,row,data,distTypeTree);
  145. if(setting.getStyle && setting.getStyle(data[row])){
  146. sheet.setStyle(row, -1, setting.getStyle(data[row]));
  147. }
  148. }
  149. if(setting.emptyRowHeader){
  150. let rowCount = sheet.getRowCount();
  151. for (let row = 0; row < rowCount; row++) {
  152. sheet.setValue(row, 0, '', GC.Spread.Sheets.SheetArea.rowHeader);
  153. }
  154. }
  155. this.lockCells(sheet,setting);
  156. sheet.resumeEvent();
  157. sheet.resumePaint();
  158. //me.shieldAllCells(sheet);
  159. },
  160. showRowData:function (sheet,setting,row,data,distTypeTree=null) {
  161. let ch = GC.Spread.Sheets.SheetArea.viewport;
  162. for (var col = 0; col < setting.header.length; col++) {
  163. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  164. var val = data[row][setting.header[col].dataCode];
  165. if(val&&setting.header[col].dataType === "Number"){
  166. if(setting.header[col].hasOwnProperty('tofix')){
  167. val =scMathUtil.roundToString(val,setting.header[col].tofix);
  168. }
  169. else if(setting.header[col].hasOwnProperty('decimalField')){
  170. var decimal = getDecimal(setting.header[col].decimalField);
  171. val =scMathUtil.roundToString(val,decimal);
  172. sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  173. }else {
  174. val =val+'';
  175. }
  176. }
  177. if(val!=null&&setting.header[col].cellType === "checkBox"){
  178. this.setCheckBoxCell(row,col,sheet,val)
  179. }
  180. if(setting.header[col].cellType === "comboBox"){
  181. this.setComboBox(row,col,sheet,setting.header[col].options,setting.header[col].editorValueType);
  182. }
  183. if(setting.header[col].cellType === "selectButton"){
  184. this.setSelectButton(row,col,sheet,setting.header[col]);
  185. }
  186. if(setting.header[col].cellType === "replaceButton"){
  187. this.setReplaceButton(row,col,sheet,setting.header[col]);
  188. }
  189. if(setting.header[col].cellType === "tipsCell"){
  190. this.setTipsCell(row,col,sheet,setting.header[col]);
  191. }
  192. if(setting.owner==='gljTree'){
  193. if(setting.header[col].cellType === "checkBox"){
  194. val==1?val:0;
  195. this.setCheckBoxCell(row,col,sheet,val);
  196. }
  197. if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
  198. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  199. val=distTypeVal;
  200. }
  201. }
  202. if(setting.header[col].getText){
  203. val = setting.getText[setting.header[col].getText](data[row],val)
  204. }
  205. sheet.setValue(row, col, val, ch);
  206. }
  207. this.setRowStyle(row,sheet,data[row].bgColour);
  208. if(setting.autoFit==true){//设置自动行高
  209. if(setting.fitRow && setting.fitRow.length > 0){//如果有设置特定的某些列才需要自动行高就按设置的来,没有设置就默认所有列
  210. for(let dataCode of setting.fitRow){
  211. let col = _.findIndex(setting.header,{dataCode:dataCode})
  212. sheet.getCell(row,col).wordWrap(true);
  213. }
  214. }else {
  215. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  216. }
  217. sheet.autoFitRow(row);
  218. }
  219. },
  220. checkData : function(col,setting, value) {
  221. let result = true;
  222. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  223. if (validator === null) {
  224. return result;
  225. }
  226. switch (validator) {
  227. case 'number':
  228. let regular = /^\d+(\.\d+)?$/;
  229. result = regular.test(value);
  230. break;
  231. case 'boolean':
  232. let booleanValue = [true, false];
  233. result = booleanValue.indexOf(value) >= 0;
  234. break;
  235. }
  236. return result;
  237. },
  238. //todo
  239. analyzePasteData: function(setting, pastedInfo) {
  240. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  241. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  242. if (pastedInfo.pasteData.text[i] === "\n") {
  243. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  244. preStrIdx = i + 1;
  245. rst.push(itemObj);
  246. if (i < pastedInfo.pasteData.text.length - 1) {
  247. itemObj = {};
  248. }
  249. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  250. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  251. propId++;
  252. preStrIdx = i + 1;
  253. //if the last copied-cell were empty, should check whether the end of text
  254. if (i == pastedInfo.pasteData.text.length - 1) {
  255. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  256. rst.push(itemObj);
  257. }
  258. } else if (i == pastedInfo.pasteData.text.length - 1) {
  259. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  260. rst.push(itemObj);
  261. }
  262. }
  263. return rst;
  264. },
  265. combineRowData: function(sheet, setting, row) {
  266. var rst = {};
  267. for (var col = 0; col < setting.header.length; col++) {
  268. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  269. }
  270. return rst;
  271. },
  272. shieldAllCells: function(sheet) {
  273. sheet.options.isProtected = true;
  274. },
  275. unShieldAllCells: function(sheet) {
  276. sheet.options.isProtected = false;
  277. },
  278. lockCells: function(sheet, setting){
  279. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  280. sheet.options.isProtected = true;
  281. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  282. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  283. let col = setting.view.lockColumns[i];
  284. if(_.isString(col)){//如果是dataCode 进行转换
  285. col = _.findIndex(setting.header,{dataCode:col})
  286. }
  287. sheet.getRange(-1,col, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  288. }
  289. }
  290. },
  291. setCheckBoxCell(row,col,sheet,val){
  292. var c = new GC.Spread.Sheets.CellTypes.CheckBox();
  293. c.isThreeState(false);
  294. sheet.setCellType(row, col,c,GC.Spread.Sheets.SheetArea.viewport);
  295. sheet.getCell(row, col).value(val);
  296. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  297. },
  298. setComboBox(row,col,sheet,options,editorValueType){
  299. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  300. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  301. if(options){
  302. dynamicCombo.items(options);
  303. if(editorValueType==true){
  304. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  305. }
  306. }
  307. sheet.setCellType(row, col,dynamicCombo,GC.Spread.Sheets.SheetArea.viewport);
  308. },
  309. setRowStyle(row,sheet,bgColour) {
  310. if(bgColour){
  311. let style = new GC.Spread.Sheets.Style();
  312. style.backColor = bgColour;
  313. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  314. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  315. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  316. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  317. sheet.setStyle(row, -1, style);
  318. }
  319. },
  320. getCustomerCoeCellType: function (htmlGenerator,setEditorValue,updateCallback) {
  321. let me = this;
  322. function CustomerCoeCellType() {
  323. this.isEscKey=false;
  324. this.displayText='';
  325. }
  326. CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  327. CustomerCoeCellType.prototype.createEditorElement = function (context) {
  328. console.log("create editor")
  329. let element = document.createElement("div");//这里创建的,会自动销毁
  330. return element
  331. };
  332. CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  333. if (editorContext) {
  334. $editor = $(editorContext);
  335. $editor.css("position", "fixed");
  336. $editor.css("background", "white");
  337. //$editor.css("width", cellRect.width); 2018-11-15 改成固定列宽
  338. $editor.css("width", 160);
  339. $editor.attr("gcUIElement", "gcEditingInput");
  340. if(htmlGenerator) htmlGenerator(context,cellRect,$editor);
  341. }
  342. }
  343. CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
  344. };
  345. CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
  346. console.log("set editor value");
  347. this.displayText = value;
  348. };
  349. CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
  350. console.log("get value");
  351. if(this.isEscKey !=true&& updateCallback){
  352. updateCallback();
  353. }
  354. this.isEscKey = false;
  355. return this.displayText;
  356. };
  357. CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  358. console.log(" update editor");
  359. if( setEditorValue){//不是esc时才更新
  360. setEditorValue(context);
  361. }
  362. };
  363. CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
  364. //cell type handle tab key by itself
  365. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  366. return false;
  367. };
  368. return new CustomerCoeCellType();
  369. },
  370. scrollSheetForOption:function (sheet,cxt,cellRect,row,options){
  371. let topRow = sheet.getViewportTopRow(1);
  372. if(row == topRow) return;//已经是最顶行了
  373. let length = options&&options.length>0?options.length:1;
  374. let height = cxt.canvas.height;
  375. let startY = cellRect.y+cellRect.height;//下拉框的起始显示位置
  376. let endY = startY + length * cellRect.height;//下拉框的结束显示位置
  377. if(endY <= height) return; //如果没有超出显示位置,直接返回
  378. let overRow = Math.ceil((endY - height)/cellRect.height);//超出的行数
  379. let showRow = topRow + overRow > row?row:topRow + overRow;
  380. sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
  381. },
  382. setSelectButton(row,col,sheet,header){
  383. /* let getSelectButton = function (cellWidth=100) {
  384. function moreButton() {
  385. }
  386. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  387. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  388. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  389. let buttonW = cellWidth/5;
  390. let endX = x+w-2;
  391. if(value){
  392. let textWidth = ctx.measureText(value).width;
  393. let spaceWidth = cellWidth - buttonW;
  394. let textEndX = x+2+textWidth;
  395. if(spaceWidth<textWidth){
  396. for(let i = value.length-1;i>1;i--){
  397. let newValue = value.substr(0,i);
  398. let newTestWidth = ctx.measureText(newValue).width;
  399. if(spaceWidth>newTestWidth){
  400. value = newValue;
  401. textEndX = x+2+newTestWidth;
  402. break;
  403. }
  404. }
  405. }
  406. ctx.fillText(value,textEndX,y+h-5);
  407. }
  408. //画三个点
  409. ctx.save();
  410. ctx.beginPath();
  411. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  412. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  413. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  414. ctx.fillStyle="black";//填充颜色,默认是黑色
  415. ctx.fill();//画实心圆
  416. ctx.closePath();
  417. ctx.restore();
  418. };
  419. moreButton.prototype.processMouseLeave= function (hitinfo) {
  420. let newCell = new selectButton();
  421. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  422. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  423. };
  424. function selectButton() {
  425. }
  426. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  427. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  428. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  429. };
  430. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  431. return {
  432. x: x,
  433. y: y,
  434. row: context.row,
  435. col: context.col,
  436. cellStyle: cellStyle,
  437. cellRect: cellRect,
  438. sheetArea: context.sheetArea
  439. };
  440. };
  441. selectButton.prototype.processMouseDown = function (hitinfo){
  442. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  443. let b1 = new moreButton();
  444. b1.marginLeft(cellWidth*4/5);
  445. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  446. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  447. }
  448. };
  449. return new selectButton();
  450. };*/
  451. sheet.setCellType(row, col,this.getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  452. },
  453. getSelectButton(cellWidth=100){
  454. function moreButton() {
  455. }
  456. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  457. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  458. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  459. ctx.font = '14px Calibri';
  460. let buttonW = cellWidth/5;
  461. let endX = x+w-2;
  462. if(value){
  463. let textWidth = ctx.measureText(value).width;
  464. let spaceWidth = cellWidth - buttonW;
  465. let textEndX = x+textWidth+2;
  466. if(spaceWidth<textWidth){
  467. for(let i = value.length-1;i>1;i--){
  468. let newValue = value.substr(0,i);
  469. let newTestWidth = ctx.measureText(newValue).width;
  470. if(spaceWidth>newTestWidth){
  471. value = newValue;
  472. textEndX = x+newTestWidth+2;
  473. break;
  474. }
  475. }
  476. }
  477. ctx.fillText(value,textEndX,y+h-6);
  478. }
  479. //画三个点
  480. ctx.save();
  481. ctx.beginPath();
  482. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  483. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  484. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  485. ctx.fillStyle="black";//填充颜色,默认是黑色
  486. ctx.fill();//画实心圆
  487. ctx.closePath();
  488. ctx.restore();
  489. };
  490. moreButton.prototype.processMouseLeave= function (hitinfo) {
  491. let newCell = new selectButton();
  492. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  493. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  494. };
  495. function selectButton() {
  496. }
  497. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  498. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  499. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  500. };
  501. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  502. return {
  503. x: x,
  504. y: y,
  505. row: context.row,
  506. col: context.col,
  507. cellStyle: cellStyle,
  508. cellRect: cellRect,
  509. sheetArea: context.sheetArea
  510. };
  511. };
  512. selectButton.prototype.processMouseDown = function (hitinfo){
  513. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  514. let b1 = new moreButton();
  515. b1.marginLeft(cellWidth*4/5);
  516. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  517. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  518. }
  519. };
  520. return new selectButton();
  521. },
  522. setReplaceButton(row,col,sheet){
  523. let replaceButton = function(){
  524. };
  525. replaceButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  526. replaceButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  527. GC.Spread.Sheets.CellTypes.Button.prototype.paint.apply(this,arguments);
  528. if(value){
  529. ctx.save();
  530. ctx.fillStyle = "white";
  531. let fh = options.fontInfo&&options.fontInfo.fontSize?options.fontInfo.fontSize:h-6;
  532. ctx.fillText(value,x+(w+ctx.measureText(value).width)/2,y+(h+fh)/2-2);
  533. ctx.restore();
  534. }
  535. };
  536. let cellType = new replaceButton();
  537. cellType.buttonBackColor("#07A0FF");
  538. sheet.setCellType(row, col,cellType,GC.Spread.Sheets.SheetArea.viewport);
  539. },
  540. setTipsCell(row,col,sheet,header){
  541. let TipCellType = function () {};
  542. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  543. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  544. return {
  545. x: x,
  546. y: y,
  547. row: context.row,
  548. col: context.col,
  549. cellStyle: cellStyle,
  550. cellRect: cellRect,
  551. sheet: context.sheet,
  552. sheetArea: context.sheetArea
  553. };
  554. };
  555. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  556. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  557. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  558. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  559. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  560. zoom = hitinfo.sheet.zoom();
  561. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  562. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  563. let setting = {};
  564. if(textLength <= cellWidth){
  565. return;
  566. }
  567. if(sheet && sheet.getParent().qo){
  568. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  569. }
  570. TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
  571. };
  572. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  573. TREE_SHEET_HELPER.tipDiv = 'hide';
  574. if (TREE_SHEET_HELPER._toolTipElement) {
  575. $(TREE_SHEET_HELPER._toolTipElement).hide();
  576. TREE_SHEET_HELPER._toolTipElement = null;
  577. };
  578. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  579. };
  580. sheet.setCellType(row, col,new TipCellType(),GC.Spread.Sheets.SheetArea.viewport);
  581. },
  582. chkIfEmpty: function(rObj, setting) {
  583. var rst = true;
  584. if (rObj) {
  585. for (var i = 0; i < setting.header.length; i++) {
  586. if (rObj[setting.header[i].dataCode]) {
  587. rst = false;
  588. break;
  589. }
  590. }
  591. }
  592. return rst;
  593. },
  594. //add by zhong 2017-10-10
  595. //动态下拉框,配合EnterCell, args.sheet.repaint();
  596. getDynamicCombo: function (forLocked) {
  597. let ComboCellForActiveCell = function () { };
  598. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  599. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  600. let sheet = options.sheet;
  601. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  602. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  603. } else {
  604. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  605. }
  606. };
  607. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  608. let sheet = options.sheet;
  609. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  610. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  611. } else {
  612. return {
  613. x: x,
  614. y: y,
  615. row: options.row,
  616. col: options.col,
  617. cellStyle: cellStyle,
  618. cellRect: cellRect,
  619. sheetArea: options.sheetArea
  620. };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
  621. }
  622. };
  623. return new ComboCellForActiveCell();
  624. },
  625. getTipsCombo:function (forLocked,tips,setting,node) {
  626. let getTipsCombo = function () {
  627. this.clickCom=false;
  628. };
  629. getTipsCombo.prototype = sheetCommonObj.getDynamicCombo(forLocked);
  630. if(tips && tips !=""){
  631. getTipsCombo.prototype.processMouseEnter = function(hitinfo){
  632. if(this.clickCom == true){ //点击了下拉框的三角形,则不用再显示悬浮框了
  633. this.clickCom = false;
  634. return;
  635. }
  636. let text = typeof tips == 'function'?tips(node):tips;
  637. TREE_SHEET_HELPER.delayShowTips(hitinfo,setting,text);
  638. };
  639. getTipsCombo.prototype.processMouseLeave = function (hitinfo) {
  640. TREE_SHEET_HELPER.hideTipsDiv();
  641. };
  642. getTipsCombo.prototype.processMouseDown = function (hitinfo){
  643. if(hitinfo.isReservedLocation == true){//这里是点击了下拉框的三角形才会有这个属性
  644. TREE_SHEET_HELPER.hideTipsDiv();
  645. this.clickCom = true;
  646. }
  647. GC.Spread.Sheets.CellTypes.ComboBox.prototype.processMouseDown.apply(this, arguments);
  648. };
  649. getTipsCombo.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context){
  650. TREE_SHEET_HELPER.hideTipsDiv();
  651. GC.Spread.Sheets.CellTypes.ComboBox.prototype.updateEditor.apply(this, arguments);
  652. };
  653. }
  654. return new getTipsCombo();
  655. },
  656. getTreeNodeCellType:function (datas,row,parentMap) {// 2018-09-26 不用spreadjs默认的树结构,自定义控件
  657. var ns = GC.Spread.Sheets;
  658. let rectW = 10;
  659. let rectH = 10;
  660. let margin = 3;
  661. function TreeNodeCellType() {
  662. this.collapsed = gljUtil.isDef(datas[row].collapsed)?datas[row].collapsed: true; //默认是折叠的
  663. this. treeNodeType = true;
  664. this.rectInfo = {};
  665. }
  666. TreeNodeCellType.prototype = new ns.CellTypes.Text();
  667. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  668. let offset = 0;
  669. let step = 7;
  670. let level = getTreeLevel(datas[row],datas);//从0开始,取当前节点是第几级的
  671. let tem = offset+margin+ rectW/2+step;//两条线之间的间隔
  672. let t_offset = offset;
  673. let temParentID = datas[row].ParentID;
  674. if(this.treeNodeType == true){
  675. for(let i = level;i>0;i--){//这里是画子节点前面的竖线,从第二级开始
  676. let temParent = getParent(temParentID,datas);
  677. if(temParent){//父节点有下一个兄弟节点才需要画
  678. if(hasNextBrother(parentMap,temParent)) sheetCommonObj.drawLine(ctx,x+t_offset+tem*i,y,x+t_offset+tem*i,y+h);
  679. temParentID = temParent.ParentID;
  680. }
  681. offset +=tem;
  682. }
  683. }
  684. offset+=step; //这个没法移动,所以要两个判断
  685. if(this.treeNodeType == true){
  686. if(hasChildern(datas[row].ID,datas)){//如果是有子节点
  687. //第一条 或者没有父节点(如费率子表综合里程项)不用画方框头上那条竖线其它都要
  688. if(row !=0 && gljUtil.isDef(datas[row].ParentID)) sheetCommonObj.drawLine(ctx,x+offset+ rectW/2+margin,y,x+offset+ rectW/2+margin,y + Math.round(h / 2) - rectH / 2);
  689. //画方框下面的那条竖线,如果没有下一个兄弟节点,则不用画
  690. if(hasNextBrother(parentMap,datas[row])) sheetCommonObj.drawLine(ctx,x+offset+ rectW/2+margin,y + Math.round(h / 2) + rectH / 2,x+offset+ rectW/2+margin,y + h);
  691. sheetCommonObj.drowRect(ctx, x+offset, y, w, h,rectW,rectH,margin);
  692. sheetCommonObj.drowSymbol(ctx, x+offset, y, w, h,rectW,rectH,margin, this.collapsed);
  693. this.rectInfo = {x:x+offset+margin,rectW:rectW}//计录一下可点击位置
  694. }else {
  695. let hasNext = datas[row+1]?datas[row+1].ParentID == datas[row].ParentID:false;
  696. sheetCommonObj.drowSubItem(ctx, x, y, w, h, offset,hasNext,margin+ rectW/2);
  697. }
  698. }
  699. offset += step;
  700. offset += rectW;
  701. x = x + offset;//设置偏移
  702. w = w - offset;
  703. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  704. };
  705. // override getHitInfo to allow cell type get mouse messages
  706. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  707. return {
  708. x: x,
  709. y: y,
  710. row: context.row,
  711. col: context.col,
  712. cellStyle: cellStyle,
  713. cellRect: cellRect,
  714. sheetArea: context.sheetArea
  715. };
  716. }
  717. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  718. if (!_.isEmpty(this.rectInfo)&&hitinfo.x < this.rectInfo.x+this.rectInfo.rectW && hitinfo.x > this.rectInfo.x) {
  719. this.collapsed = !this.collapsed;
  720. datas[row].collapsed = this.collapsed;
  721. this.refreshChildrenVisible(hitinfo.sheet);
  722. hitinfo.sheet.invalidateLayout();
  723. hitinfo.sheet.repaint();
  724. }
  725. };
  726. TreeNodeCellType.prototype.refreshChildrenVisible = function (sheet) {
  727. sheet.suspendPaint();
  728. sheet.suspendEvent();
  729. refreshVisible(datas[row]);
  730. sheet.resumeEvent();
  731. sheet.resumePaint();
  732. function refreshVisible(item){
  733. if(parentMap[item.ID]){
  734. for(let sub of parentMap[item.ID]){
  735. refreshVisible(sub)
  736. }
  737. }
  738. let visible = getVisible(item);
  739. let trow = datas.indexOf(item);
  740. sheet.getRange(trow , -1, 1, -1).visible(visible);
  741. }
  742. function getVisible(item) {
  743. if(item.ParentID){
  744. let parent = getParent(item.ParentID,datas);
  745. if(!parent) return true;
  746. let p_row= datas.indexOf(parent);
  747. let visible = !sheet.getCellType(p_row,0).collapsed;
  748. if(visible == true){ //如果是显示的,则要再往父节点的父节点检查,只要有一个节点是隐藏的,则都是隐藏
  749. return getVisible(parent);
  750. }else {
  751. return visible
  752. }
  753. }else {//如果parentID 为空则是最根节点
  754. return true;
  755. }
  756. }
  757. };
  758. return new TreeNodeCellType()
  759. function getTreeLevel(item,data) {
  760. if(item.ParentID){
  761. let pitem = _.find(data,{'ID':item.ParentID});
  762. return getTreeLevel(pitem,data) + 1;
  763. }else {
  764. return 0
  765. }
  766. }
  767. function hasChildern(ID,data) {//返回是否有子项
  768. let p = _.find(data,{'ParentID':ID});
  769. if(p) return true;
  770. return false
  771. }
  772. function getParent(ParentID,data) {
  773. let p = _.find(data,{'ID':ParentID});
  774. return p;
  775. }
  776. function hasNextBrother(parentMap,item){
  777. let children =parentMap[item.ParentID];
  778. if(!gljUtil.isDef(children)|| children.indexOf(item) == children.length -1) return false;
  779. return true
  780. }
  781. },
  782. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  783. let me = this;
  784. sheet.suspendPaint();
  785. let combo = me.getDynamicCombo();
  786. for(let i = 0, len = rowCount; i < len; i++){
  787. if(itemsHeight) {
  788. combo.itemHeight(itemsHeight);
  789. combo._maxDropDownItems = itemsHeight + 5;
  790. }
  791. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  792. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  793. else combo.items(items);
  794. sheet.getCell(beginRow + i, col).cellType(combo);
  795. }
  796. sheet.resumePaint();
  797. },
  798. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  799. sheet.suspendPaint();
  800. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  801. for(let i = 0, len = rowCount; i < len; i++){
  802. if(itemsHeight) combo.itemHeight(itemsHeight);
  803. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  804. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  805. else combo.items(items);
  806. sheet.getCell(beginRow + i, col).cellType(combo);
  807. }
  808. sheet.resumePaint();
  809. },
  810. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  811. copyTextToClipboard: function(text) {
  812. let textArea = document.createElement("textarea");
  813. textArea.style.position = 'fixed';
  814. textArea.style.top = 0;
  815. textArea.style.left = 0;
  816. textArea.style.width = '2em';
  817. textArea.style.height = '2em';
  818. textArea.style.padding = 0;
  819. textArea.style.border = 'none';
  820. textArea.style.outline = 'none';
  821. textArea.style.boxShadow = 'none';
  822. textArea.style.background = 'transparent';
  823. textArea.value = text;
  824. document.body.appendChild(textArea);
  825. textArea.select();
  826. try {
  827. let successful = document.execCommand('copy');
  828. let msg = successful ? 'successful' : 'unsuccessful';
  829. console.log('Copying text command was ' + msg);
  830. } catch (err) {
  831. console.log('Oops, unable to copy');
  832. }
  833. document.body.removeChild(textArea);
  834. },
  835. //获取选中区域的表格类型数据(可粘贴到excel)
  836. getTableData: function (sheet, colSettings = null) {
  837. let rst = '';
  838. let sel = sheet.getSelections()[0];
  839. let pasteText = [];
  840. for(let row = sel.row; row < sel.row + sel.rowCount; row++){
  841. if(!sheet.getCell(row, -1).visible())
  842. continue;
  843. let rowText = [];
  844. for(let j = 0; j < sel.colCount; j++){
  845. let col = sel.col + j;
  846. if(!sheet.getCell(-1, col).visible())
  847. continue;
  848. if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  849. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  850. else
  851. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
  852. }
  853. pasteText.push(rowText.join('\t'));
  854. }
  855. return pasteText.join('\n');
  856. },
  857. transferToTreeSetting:function(setting,treeSetting,treeCol){
  858. for(let h of setting.header){
  859. treeSetting.cols.push(getSettingCol(h))
  860. }
  861. for(let l of setting.view.lockColumns){
  862. treeSetting.cols[l].readOnly = true;
  863. }
  864. return treeSetting;
  865. function getSettingCol(header) {
  866. let aMap ={left:0,center:1,right:2};
  867. let hAlign = header.hAlign?aMap[header.hAlign]:0;
  868. let col = {
  869. "width":header.headerWidth?header.headerWidth:100,
  870. "head":{
  871. "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
  872. "spanCols":header.spanCols?header.spanCols:[1],
  873. "spanRows":header.spanRows?header.spanRows:[1],
  874. "vAlign":[1],
  875. "hAlign":[1],
  876. "font":["Arial"]
  877. },
  878. "data": {
  879. "field": header.dataCode,
  880. "vAlign": 1,
  881. "hAlign": hAlign,
  882. "font": "Arial"
  883. }
  884. };
  885. if(header.showHint == true){
  886. col.showHint = true;
  887. }
  888. if(header.cellType){
  889. col.data.cellType = getCellType(header);
  890. }
  891. if(header.decimalField){//设置formatter
  892. let decimal = getDecimal(header.decimalField);
  893. col.formatter = getFormatter(decimal);
  894. }
  895. if(header.getText && treeCol){
  896. col.data.getText = treeCol.getEvent(header.getText);
  897. }
  898. /*col.readOnly = function (node) {
  899. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  900. return true;
  901. }
  902. return false;
  903. };*/
  904. return col;
  905. }
  906. function getCellType(header) {
  907. return function () {
  908. if(header.cellType === "checkBox"){
  909. return new GC.Spread.Sheets.CellTypes.CheckBox();
  910. }
  911. if(header.cellType === "comboBox"){
  912. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  913. if(header.options){
  914. dynamicCombo.itemHeight(header.options.length).items(header.options);
  915. if(header.editorValueType==true){
  916. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  917. }
  918. }
  919. return dynamicCombo
  920. }
  921. }
  922. }
  923. },
  924. //注册自定义回车键事件
  925. bindEnterKey: function (workBook, operation) {
  926. workBook.commandManager().register('myEnter', operation);
  927. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  928. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  929. },
  930. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  931. bindEscKey: function (workBook, sheets) {
  932. function isDef(v){
  933. return typeof v !== 'undefined' && v !== null;
  934. }
  935. workBook.commandManager().register('myEsc', function () {
  936. let activeSheet = workBook.getActiveSheet();
  937. let hasTheSheet = false;
  938. for(let sheetObj of sheets){
  939. let sheet = sheetObj.sheet;
  940. if(sheet === activeSheet){
  941. hasTheSheet = true;
  942. let editStarting = sheetObj.editStarting;
  943. let editEnded = sheetObj.editEnded;
  944. if(editStarting){
  945. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  946. }
  947. if(editEnded){
  948. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  949. }
  950. let row = sheet.getActiveRowIndex();
  951. let col = sheet.getActiveColumnIndex();
  952. let orgV = sheet.getValue(row, col);
  953. let orgText = sheet.getText(row, col);
  954. if(!isDef(orgV)){
  955. orgV = '';
  956. }
  957. if(sheet.isEditing()){
  958. sheet.endEdit();
  959. sheet.setValue(row, col, orgV);
  960. }
  961. if(editStarting){
  962. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  963. }
  964. if(editEnded){
  965. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  966. }
  967. }
  968. }
  969. //容错处理,以防没把所有工作簿的表格信息传入参数
  970. if(!hasTheSheet){
  971. if(activeSheet.isEditing()){
  972. activeSheet.endEdit();
  973. }
  974. }
  975. });
  976. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  977. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  978. },
  979. //设置默认样式
  980. spreadDefaultStyle: function (workBook) {
  981. let defaultStyle = new GC.Spread.Sheets.Style();
  982. defaultStyle.font = '14px Calibri';
  983. let sheetCount = workBook.getSheetCount();
  984. for(let i = 0; i < sheetCount; i++){
  985. let sheet = workBook.getSheet(i);
  986. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  987. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  988. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  989. }
  990. },
  991. //动态根据工作簿宽度和各列宽度比例设置宽度
  992. setColumnWidthByRate: function (workBookWidth, workBook, headers){
  993. if(workBook){
  994. const sheet = workBook.getActiveSheet();
  995. sheet.suspendEvent();
  996. sheet.suspendPaint();
  997. for(let col = 0; col < headers.length; col++){
  998. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  999. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1000. }
  1001. else {
  1002. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  1003. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1004. }
  1005. }
  1006. }
  1007. sheet.resumeEvent();
  1008. sheet.resumePaint();
  1009. }
  1010. },
  1011. drowRect:function (ctx, x, y, w, h,rectW,rectH,margin) {
  1012. ctx.save();
  1013. ctx.strokeStyle = "gray";
  1014. ctx.translate(0.5, 0.5);
  1015. ctx.beginPath();
  1016. let rectX = x + margin;
  1017. let rectY = y + Math.round(h / 2) - rectH / 2;
  1018. ctx.moveTo(rectX, rectY);
  1019. ctx.lineTo(rectX, rectY + rectH);
  1020. ctx.lineTo(rectX + rectW, rectY + rectH);
  1021. ctx.lineTo(rectX + rectW, rectY);
  1022. ctx.lineTo(rectX, rectY);
  1023. ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
  1024. ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
  1025. ctx.stroke();
  1026. ctx.restore();
  1027. },
  1028. drawLine : function (ctx, x1, y1, x2, y2, color) {
  1029. let l_color = color?color:"gray";
  1030. ctx.save();
  1031. ctx.translate(0.5, 0.5);
  1032. ctx.beginPath();
  1033. ctx.moveTo(x1, y1);
  1034. ctx.lineTo(x2, y2);
  1035. ctx.strokeStyle = l_color;
  1036. ctx.stroke();
  1037. ctx.restore();
  1038. },
  1039. drowSymbol :function (ctx, x, y, w, h,rectW,rectH,margin,collapsed) {
  1040. ctx.save();
  1041. ctx.strokeStyle = "#000000";
  1042. ctx.translate(0.5, 0.5);
  1043. ctx.beginPath();
  1044. ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
  1045. ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
  1046. let rectY = y + Math.round(h / 2) - rectH / 2;
  1047. if (collapsed) {
  1048. ctx.moveTo(x + margin + rectW / 2, rectY + 2);
  1049. ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
  1050. }
  1051. ctx.stroke();
  1052. ctx.restore();
  1053. },
  1054. drowTriangle:function(ctx,x,y){//画向下三角形
  1055. ctx.save();
  1056. ctx.fillStyle = "black";
  1057. ctx.beginPath();
  1058. ctx.moveTo(x, y);
  1059. ctx.lineTo(x-3, y -6);
  1060. ctx.lineTo(x+3, y -6);
  1061. ctx.fill();
  1062. ctx.restore();
  1063. },
  1064. drowSubItem: function (ctx, x, y, w, h, offset, hasNext,step) {
  1065. let t_step = step?step:6;
  1066. offset += t_step;
  1067. ctx.save();
  1068. ctx.strokeStyle = "gray";
  1069. ctx.translate(0.5, 0.5);
  1070. ctx.beginPath();
  1071. ctx.moveTo(x + offset, y);
  1072. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1073. offset += 9;
  1074. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1075. if (hasNext) {
  1076. ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
  1077. ctx.lineTo(x + offset - 9, y + h);
  1078. }
  1079. ctx.stroke();
  1080. ctx.restore();
  1081. return offset;
  1082. },
  1083. checkData:function(col,setting, value) {
  1084. let result = true;
  1085. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  1086. if (validator === null) {
  1087. return result;
  1088. }
  1089. switch (validator) {
  1090. case 'number':
  1091. let regular = /^\d+(\.\d+)?$/;
  1092. result = regular.test(value);
  1093. break;
  1094. case 'boolean':
  1095. let booleanValue = [true, false];
  1096. result = booleanValue.indexOf(value) >= 0;
  1097. break;
  1098. }
  1099. return result;
  1100. }
  1101. }