sheet_common.js 29 KB

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