sheet_common.js 26 KB

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