sheet_common.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. sheet.setRowCount(1, spreadNS.SheetArea.colHeader);
  26. sheet.setColumnCount(setting.header.length, spreadNS.SheetArea.viewport);
  27. if (setting && setting.view && setting.view.colHeaderHeight) {
  28. sheet.setRowHeight(0, setting.view.colHeaderHeight, spreadNS.SheetArea.colHeader);
  29. };
  30. if (setting && setting.view && setting.view.rowHeaderWidth) {
  31. sheet.setColumnWidth(0, setting.view.rowHeaderWidth, spreadNS.SheetArea.rowHeader);
  32. };
  33. sheet.options.colHeaderAutoTextIndex = 1;
  34. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  35. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  36. sheet.options.protectionOptions = {
  37. allowResizeColumns: true
  38. };
  39. sheet.showRowOutline(false);
  40. sheet.options.allowCellOverflow = false;
  41. me.buildHeader(sheet, setting);
  42. if (rowCount > 0)
  43. sheet.setRowCount(rowCount);
  44. else
  45. sheet.setRowCount(1);
  46. sheet.resumeEvent();
  47. sheet.resumePaint();
  48. },
  49. // buildSheet 在一个Spread、一个Sheet的情况下使用。
  50. buildSheet: function(container, setting, rowCount) {
  51. var me = this;
  52. var spreadBook = me.createSpread(container, { sheetCount: 1 });
  53. var sheet = spreadBook.getSheet(0);
  54. me.initSheet(sheet, setting, rowCount);
  55. return spreadBook;
  56. },
  57. buildHeader: function(sheet, setting){
  58. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  59. for (var i = 0; i < setting.header.length; i++) {
  60. sheet.setValue(0, i, setting.header[i].headerName, ch);
  61. sheet.setColumnWidth(i, setting.header[i].headerWidth?setting.header[i].headerWidth:100);
  62. sheet.setColumnVisible(i,setting.header[i].visible === false ? false:true);
  63. }
  64. },
  65. cleanSheet: function(sheet, setting, rowCount) {
  66. sheet.suspendPaint();
  67. sheet.suspendEvent();
  68. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  69. if (rowCount > 0) sheet.setRowCount(rowCount);
  70. sheet.clearSelection();
  71. sheet.resumeEvent();
  72. sheet.resumePaint();
  73. },
  74. cleanData: function (sheet, setting, rowCount) {
  75. sheet.suspendPaint();
  76. sheet.suspendEvent();
  77. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  78. if (rowCount > 0) sheet.setRowCount(rowCount);
  79. sheet.resumeEvent();
  80. sheet.resumePaint();
  81. },
  82. setAreaAlign: function(area, hAlign, vAlign){
  83. if (!(hAlign) || hAlign === "left") {
  84. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  85. } else if (hAlign === "right") {
  86. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  87. } else if (hAlign === "center") {
  88. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  89. } else {
  90. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  91. }
  92. if (!(vAlign) || vAlign === "center") {
  93. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  94. } else if (vAlign === "top") {
  95. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  96. } else if (vAlign === "bottom") {
  97. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  98. } else {
  99. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  100. }
  101. },
  102. showData: function(sheet, setting, data,distTypeTree) {
  103. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  104. sheet.suspendPaint();
  105. sheet.suspendEvent();
  106. //sheet.addRows(row, 1);
  107. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  108. if(sheet.getRowCount()<data.length){
  109. data.length<30? sheet.setRowCount(30):sheet.setRowCount(data.length);
  110. }else if(sheet.getRowCount()==0){
  111. sheet.setRowCount(30);
  112. }
  113. for (var col = 0; col < setting.header.length; col++) {
  114. var hAlign = "left", vAlign = "center";
  115. if (setting.header[col].hAlign) {
  116. hAlign = setting.header[col].hAlign;
  117. } else if (setting.header[col].dataType !== "String"){
  118. hAlign = "right";
  119. }
  120. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  121. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  122. if (setting.header[col].formatter) {
  123. //var style = new GC.Spread.Sheets.Style();
  124. //style.formatter = setting.header[col].formatter;
  125. //sheet.setStyle(row,col,style,GC.Spread.Sheets.SheetArea.viewport);
  126. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  127. }
  128. if(setting.header[col].cellType === "checkBox"||setting.header[col].cellType === "button"){//clear and reset
  129. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  130. sheet.deleteColumns(col,1);
  131. sheet.addColumns(col, 1);
  132. sheet.setValue(0, col, setting.header[col].headerName, header);
  133. sheet.setColumnWidth(col, setting.header[col].headerWidth?setting.header[col].headerWidth:100);
  134. }
  135. if(setting.header[col].visible === false){
  136. sheet.setColumnVisible(col,false);
  137. }
  138. }
  139. for (var row = 0; row < data.length; row++) {
  140. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  141. this.showRowData(sheet,setting,row,data,distTypeTree);
  142. }
  143. this.lockCells(sheet,setting);
  144. sheet.resumeEvent();
  145. sheet.resumePaint();
  146. //me.shieldAllCells(sheet);
  147. },
  148. showRowData:function (sheet,setting,row,data,distTypeTree=null) {
  149. let ch = GC.Spread.Sheets.SheetArea.viewport;
  150. for (var col = 0; col < setting.header.length; col++) {
  151. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  152. var val = data[row][setting.header[col].dataCode];
  153. if(val&&setting.header[col].dataType === "Number"){
  154. if(setting.header[col].hasOwnProperty('tofix')){
  155. val =scMathUtil.roundToString(val,setting.header[col].tofix);
  156. }
  157. else if(setting.header[col].hasOwnProperty('decimalField')){
  158. var decimal = getDecimal(setting.header[col].decimalField);
  159. val =scMathUtil.roundToString(val,decimal);
  160. sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  161. }else {
  162. val =val+'';
  163. }
  164. }
  165. if(val!=null&&setting.header[col].cellType === "checkBox"){
  166. this.setCheckBoxCell(row,col,sheet,val)
  167. }
  168. if(setting.header[col].cellType === "comboBox"){
  169. this.setComboBox(row,col,sheet,setting.header[col].options,setting.header[col].editorValueType);
  170. }
  171. if(setting.header[col].cellType === "selectButton"){
  172. this.setSelectButton(row,col,sheet,setting.header[col]);
  173. }
  174. if(setting.owner==='gljTree'){
  175. if(setting.header[col].cellType === "checkBox"){
  176. val==1?val:0;
  177. this.setCheckBoxCell(row,col,sheet,val);
  178. }
  179. if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
  180. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  181. val=distTypeVal;
  182. }
  183. }
  184. if(setting.header[col].getText){
  185. val = setting.getText[setting.header[col].getText](data[row],val)
  186. }
  187. sheet.setValue(row, col, val, ch);
  188. }
  189. this.setRowStyle(row,sheet,data[row].bgColour);
  190. if(setting.autoFit==true){
  191. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  192. sheet.autoFitRow(row);
  193. }
  194. },
  195. //todo
  196. analyzePasteData: function(setting, pastedInfo) {
  197. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  198. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  199. if (pastedInfo.pasteData.text[i] === "\n") {
  200. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  201. preStrIdx = i + 1;
  202. rst.push(itemObj);
  203. if (i < pastedInfo.pasteData.text.length - 1) {
  204. itemObj = {};
  205. }
  206. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  207. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  208. propId++;
  209. preStrIdx = i + 1;
  210. //if the last copied-cell were empty, should check whether the end of text
  211. if (i == pastedInfo.pasteData.text.length - 1) {
  212. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  213. rst.push(itemObj);
  214. }
  215. } else if (i == pastedInfo.pasteData.text.length - 1) {
  216. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  217. rst.push(itemObj);
  218. }
  219. }
  220. return rst;
  221. },
  222. combineRowData: function(sheet, setting, row) {
  223. var rst = {};
  224. for (var col = 0; col < setting.header.length; col++) {
  225. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  226. }
  227. return rst;
  228. },
  229. shieldAllCells: function(sheet) {
  230. sheet.options.isProtected = true;
  231. },
  232. unShieldAllCells: function(sheet) {
  233. sheet.options.isProtected = false;
  234. },
  235. lockCells: function(sheet, setting){
  236. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  237. sheet.options.isProtected = true;
  238. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  239. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  240. sheet.getRange(-1,setting.view.lockColumns[i], -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  241. }
  242. }
  243. },
  244. setCheckBoxCell(row,col,sheet,val){
  245. var c = new GC.Spread.Sheets.CellTypes.CheckBox();
  246. c.isThreeState(false);
  247. sheet.setCellType(row, col,c,GC.Spread.Sheets.SheetArea.viewport);
  248. sheet.getCell(row, col).value(val);
  249. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  250. },
  251. setComboBox(row,col,sheet,options,editorValueType){
  252. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  253. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  254. if(options){
  255. dynamicCombo.itemHeight(options.length).items(options);
  256. if(editorValueType==true){
  257. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  258. }
  259. }
  260. sheet.setCellType(row, col,dynamicCombo,GC.Spread.Sheets.SheetArea.viewport);
  261. },
  262. setRowStyle(row,sheet,bgColour) {
  263. if(bgColour){
  264. let style = new GC.Spread.Sheets.Style();
  265. style.backColor = bgColour;
  266. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  267. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  268. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  269. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  270. sheet.setStyle(row, -1, style);
  271. }
  272. },
  273. setSelectButton(row,col,sheet,header){
  274. let getSelectButton = function (cellWidth=100) {
  275. function moreButton() {
  276. }
  277. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  278. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  279. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  280. let buttonW = cellWidth/5;
  281. let endX = x+w-2;
  282. if(value){
  283. let textWidth = ctx.measureText(value).width;
  284. let spaceWidth = cellWidth - buttonW;
  285. let textEndX = x+2+textWidth;
  286. if(spaceWidth<textWidth){
  287. for(let i = value.length-1;i>1;i--){
  288. let newValue = value.substr(0,i);
  289. let newTestWidth = ctx.measureText(newValue).width;
  290. if(spaceWidth>newTestWidth){
  291. value = newValue;
  292. textEndX = x+2+newTestWidth;
  293. break;
  294. }
  295. }
  296. }
  297. ctx.fillText(value,textEndX,y+h-5);
  298. }
  299. //画三个点
  300. ctx.save();
  301. ctx.beginPath();
  302. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  303. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  304. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  305. ctx.fillStyle="black";//填充颜色,默认是黑色
  306. ctx.fill();//画实心圆
  307. ctx.closePath();
  308. ctx.restore();
  309. };
  310. moreButton.prototype.processMouseLeave= function (hitinfo) {
  311. let newCell = new selectButton();
  312. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  313. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  314. };
  315. function selectButton() {
  316. }
  317. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  318. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  319. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  320. };
  321. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  322. return {
  323. x: x,
  324. y: y,
  325. row: context.row,
  326. col: context.col,
  327. cellStyle: cellStyle,
  328. cellRect: cellRect,
  329. sheetArea: context.sheetArea
  330. };
  331. };
  332. selectButton.prototype.processMouseDown = function (hitinfo){
  333. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  334. let b1 = new moreButton();
  335. b1.marginLeft(cellWidth*4/5);
  336. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  337. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  338. }
  339. };
  340. return new selectButton();
  341. };
  342. sheet.setCellType(row, col,getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  343. },
  344. chkIfEmpty: function(rObj, setting) {
  345. var rst = true;
  346. if (rObj) {
  347. for (var i = 0; i < setting.header.length; i++) {
  348. if (rObj[setting.header[i].dataCode]) {
  349. rst = false;
  350. break;
  351. }
  352. }
  353. }
  354. return rst;
  355. },
  356. //add by zhong 2017-10-10
  357. //动态下拉框,配合EnterCell, args.sheet.repaint();
  358. getDynamicCombo: function (forLocked) {
  359. let ComboCellForActiveCell = function () { };
  360. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  361. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  362. let sheet = options.sheet;
  363. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  364. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  365. } else {
  366. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  367. }
  368. };
  369. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  370. let sheet = options.sheet;
  371. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  372. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  373. } else {
  374. return GC.Spread.Sheets.CellTypes.Base.prototype.getHitInfo.apply(this, arguments);
  375. }
  376. };
  377. return new ComboCellForActiveCell();
  378. },
  379. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  380. let me = this;
  381. sheet.suspendPaint();
  382. let combo = me.getDynamicCombo();
  383. for(let i = 0, len = rowCount; i < len; i++){
  384. if(itemsHeight) combo.itemHeight(itemsHeight);
  385. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  386. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  387. else combo.items(items);
  388. sheet.getCell(beginRow + i, col).cellType(combo);
  389. }
  390. sheet.resumePaint();
  391. },
  392. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  393. sheet.suspendPaint();
  394. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  395. for(let i = 0, len = rowCount; i < len; i++){
  396. if(itemsHeight) combo.itemHeight(itemsHeight);
  397. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  398. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  399. else combo.items(items);
  400. sheet.getCell(beginRow + i, col).cellType(combo);
  401. }
  402. sheet.resumePaint();
  403. },
  404. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  405. copyTextToClipboard: function(text) {
  406. let textArea = document.createElement("textarea");
  407. textArea.style.position = 'fixed';
  408. textArea.style.top = 0;
  409. textArea.style.left = 0;
  410. textArea.style.width = '2em';
  411. textArea.style.height = '2em';
  412. textArea.style.padding = 0;
  413. textArea.style.border = 'none';
  414. textArea.style.outline = 'none';
  415. textArea.style.boxShadow = 'none';
  416. textArea.style.background = 'transparent';
  417. textArea.value = text;
  418. document.body.appendChild(textArea);
  419. textArea.select();
  420. try {
  421. let successful = document.execCommand('copy');
  422. let msg = successful ? 'successful' : 'unsuccessful';
  423. console.log('Copying text command was ' + msg);
  424. } catch (err) {
  425. console.log('Oops, unable to copy');
  426. }
  427. document.body.removeChild(textArea);
  428. },
  429. //获取选中区域的表格类型数据(可粘贴到excel)
  430. getTableData: function (sheet, colSettings = null) {
  431. let rst = '';
  432. let sel = sheet.getSelections()[0];
  433. let pasteText = [];
  434. for(let row = sel.row; row < sel.row + sel.rowCount; row++){
  435. if(!sheet.getCell(row, -1).visible())
  436. continue;
  437. let rowText = [];
  438. for(let j = 0; j < sel.colCount; j++){
  439. let col = sel.col + j;
  440. if(!sheet.getCell(-1, col).visible())
  441. continue;
  442. if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  443. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  444. else
  445. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
  446. }
  447. pasteText.push(rowText.join('\t'));
  448. }
  449. return pasteText.join('\n');
  450. },
  451. transferToTreeSetting:function(setting,treeSetting,treeCol){
  452. for(let h of setting.header){
  453. treeSetting.cols.push(getSettingCol(h))
  454. }
  455. for(let l of setting.view.lockColumns){
  456. treeSetting.cols[l].readOnly = true;
  457. }
  458. return treeSetting;
  459. function getSettingCol(header) {
  460. let aMap ={left:0,center:1,right:2};
  461. let hAlign = header.hAlign?aMap[header.hAlign]:0;
  462. let col = {
  463. "width":header.headerWidth?header.headerWidth:100,
  464. "head":{
  465. "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
  466. "spanCols":header.spanCols?header.spanCols:[1],
  467. "spanRows":header.spanRows?header.spanRows:[1],
  468. "vAlign":[1],
  469. "hAlign":[1],
  470. "font":["Arial"]
  471. },
  472. "data": {
  473. "field": header.dataCode,
  474. "vAlign": 1,
  475. "hAlign": hAlign,
  476. "font": "Arial"
  477. }
  478. };
  479. if(header.showHint == true){
  480. col.showHint = true;
  481. }
  482. if(header.cellType){
  483. col.data.cellType = getCellType(header);
  484. }
  485. if(header.decimalField){//设置formatter
  486. let decimal = getDecimal(header.decimalField);
  487. col.formatter = getFormatter(decimal);
  488. }
  489. if(header.getText && treeCol){
  490. col.data.getText = treeCol.getEvent(header.getText);
  491. }
  492. /*col.readOnly = function (node) {
  493. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  494. return true;
  495. }
  496. return false;
  497. };*/
  498. return col;
  499. }
  500. function getCellType(header) {
  501. return function () {
  502. if(header.cellType === "checkBox"){
  503. return new GC.Spread.Sheets.CellTypes.CheckBox();
  504. }
  505. if(header.cellType === "comboBox"){
  506. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  507. if(header.options){
  508. dynamicCombo.itemHeight(header.options.length).items(header.options);
  509. if(header.editorValueType==true){
  510. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  511. }
  512. }
  513. return dynamicCombo
  514. }
  515. }
  516. }
  517. },
  518. //注册自定义回车键事件
  519. bindEnterKey: function (workBook, operation) {
  520. workBook.commandManager().register('myEnter', operation);
  521. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  522. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  523. },
  524. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  525. bindEscKey: function (workBook, sheets) {
  526. function isDef(v){
  527. return typeof v !== 'undefined' && v !== null;
  528. }
  529. workBook.commandManager().register('myEsc', function () {
  530. let activeSheet = workBook.getActiveSheet();
  531. let hasTheSheet = false;
  532. for(let sheetObj of sheets){
  533. let sheet = sheetObj.sheet;
  534. if(sheet === activeSheet){
  535. hasTheSheet = true;
  536. let editStarting = sheetObj.editStarting;
  537. let editEnded = sheetObj.editEnded;
  538. if(editStarting){
  539. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  540. }
  541. if(editEnded){
  542. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  543. }
  544. let row = sheet.getActiveRowIndex();
  545. let col = sheet.getActiveColumnIndex();
  546. let orgV = sheet.getValue(row, col);
  547. let orgText = sheet.getText(row, col);
  548. console.log(`orgV: ${orgV}`);
  549. console.log(`orgText: ${orgText}`);
  550. if(!isDef(orgV)){
  551. orgV = '';
  552. }
  553. if(sheet.isEditing()){
  554. sheet.endEdit();
  555. sheet.setValue(row, col, orgV);
  556. }
  557. if(editStarting){
  558. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  559. }
  560. if(editEnded){
  561. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  562. }
  563. }
  564. }
  565. //容错处理,以防没把所有工作簿的表格信息传入参数
  566. if(!hasTheSheet){
  567. if(activeSheet.isEditing()){
  568. activeSheet.endEdit();
  569. }
  570. }
  571. });
  572. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  573. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  574. }
  575. }