sheet_common.js 29 KB

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