sheet_common.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. if(setting.style){
  143. sheet.setStyle(row, -1, setting.style);
  144. }
  145. }
  146. this.lockCells(sheet,setting);
  147. sheet.resumeEvent();
  148. sheet.resumePaint();
  149. //me.shieldAllCells(sheet);
  150. },
  151. showRowData:function (sheet,setting,row,data,distTypeTree=null) {
  152. let ch = GC.Spread.Sheets.SheetArea.viewport;
  153. for (var col = 0; col < setting.header.length; col++) {
  154. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  155. var val = data[row][setting.header[col].dataCode];
  156. if(val&&setting.header[col].dataType === "Number"){
  157. if(setting.header[col].hasOwnProperty('tofix')){
  158. val =scMathUtil.roundToString(val,setting.header[col].tofix);
  159. }
  160. else if(setting.header[col].hasOwnProperty('decimalField')){
  161. var decimal = getDecimal(setting.header[col].decimalField);
  162. val =scMathUtil.roundToString(val,decimal);
  163. sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  164. }else {
  165. val =val+'';
  166. }
  167. }
  168. if(val!=null&&setting.header[col].cellType === "checkBox"){
  169. this.setCheckBoxCell(row,col,sheet,val)
  170. }
  171. if(setting.header[col].cellType === "comboBox"){
  172. this.setComboBox(row,col,sheet,setting.header[col].options,setting.header[col].editorValueType);
  173. }
  174. if(setting.header[col].cellType === "selectButton"){
  175. this.setSelectButton(row,col,sheet,setting.header[col]);
  176. }
  177. if(setting.header[col].cellType === "tipsCell"){
  178. this.setTipsCell(row,col,sheet,setting.header[col]);
  179. }
  180. if(setting.owner==='gljTree'){
  181. if(setting.header[col].cellType === "checkBox"){
  182. val==1?val:0;
  183. this.setCheckBoxCell(row,col,sheet,val);
  184. }
  185. if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
  186. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  187. val=distTypeVal;
  188. }
  189. }
  190. if(setting.header[col].getText){
  191. val = setting.getText[setting.header[col].getText](data[row],val)
  192. }
  193. sheet.setValue(row, col, val, ch);
  194. }
  195. this.setRowStyle(row,sheet,data[row].bgColour);
  196. if(setting.autoFit==true){
  197. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  198. sheet.autoFitRow(row);
  199. }
  200. },
  201. //todo
  202. analyzePasteData: function(setting, pastedInfo) {
  203. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  204. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  205. if (pastedInfo.pasteData.text[i] === "\n") {
  206. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  207. preStrIdx = i + 1;
  208. rst.push(itemObj);
  209. if (i < pastedInfo.pasteData.text.length - 1) {
  210. itemObj = {};
  211. }
  212. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  213. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  214. propId++;
  215. preStrIdx = i + 1;
  216. //if the last copied-cell were empty, should check whether the end of text
  217. if (i == pastedInfo.pasteData.text.length - 1) {
  218. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  219. rst.push(itemObj);
  220. }
  221. } else if (i == pastedInfo.pasteData.text.length - 1) {
  222. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  223. rst.push(itemObj);
  224. }
  225. }
  226. return rst;
  227. },
  228. combineRowData: function(sheet, setting, row) {
  229. var rst = {};
  230. for (var col = 0; col < setting.header.length; col++) {
  231. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  232. }
  233. return rst;
  234. },
  235. shieldAllCells: function(sheet) {
  236. sheet.options.isProtected = true;
  237. },
  238. unShieldAllCells: function(sheet) {
  239. sheet.options.isProtected = false;
  240. },
  241. lockCells: function(sheet, setting){
  242. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  243. sheet.options.isProtected = true;
  244. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  245. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  246. sheet.getRange(-1,setting.view.lockColumns[i], -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  247. }
  248. }
  249. },
  250. setCheckBoxCell(row,col,sheet,val){
  251. var c = new GC.Spread.Sheets.CellTypes.CheckBox();
  252. c.isThreeState(false);
  253. sheet.setCellType(row, col,c,GC.Spread.Sheets.SheetArea.viewport);
  254. sheet.getCell(row, col).value(val);
  255. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  256. },
  257. setComboBox(row,col,sheet,options,editorValueType){
  258. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  259. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  260. if(options){
  261. dynamicCombo.itemHeight(options.length).items(options);
  262. if(editorValueType==true){
  263. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  264. }
  265. }
  266. sheet.setCellType(row, col,dynamicCombo,GC.Spread.Sheets.SheetArea.viewport);
  267. },
  268. setRowStyle(row,sheet,bgColour) {
  269. if(bgColour){
  270. let style = new GC.Spread.Sheets.Style();
  271. style.backColor = bgColour;
  272. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  273. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  274. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  275. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  276. sheet.setStyle(row, -1, style);
  277. }
  278. },
  279. getCustomerCoeCellType: function (htmlGenerator,setEditorValue,updateCallback) {
  280. let me = this;
  281. function CustomerCoeCellType() {
  282. this.isEscKey=false;
  283. this.displayText='';
  284. }
  285. CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  286. CustomerCoeCellType.prototype.createEditorElement = function (context) {
  287. console.log("create editor")
  288. let element = document.createElement("div");//这里创建的,会自动销毁
  289. return element
  290. };
  291. CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  292. if (editorContext) {
  293. $editor = $(editorContext);
  294. $editor.css("position", "fixed");
  295. $editor.css("background", "white");
  296. $editor.css("width", cellRect.width);
  297. $editor.attr("gcUIElement", "gcEditingInput");
  298. if(htmlGenerator) $editor.html(htmlGenerator(context,cellRect));
  299. }
  300. }
  301. CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
  302. };
  303. CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
  304. console.log("set editor value");
  305. this.displayText = value;
  306. };
  307. CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
  308. console.log("get value");
  309. if(this.isEscKey !=true&& updateCallback){
  310. updateCallback();
  311. }
  312. this.isEscKey = false;
  313. return this.displayText;
  314. };
  315. CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  316. console.log(" update editor");
  317. if( setEditorValue){//不是esc时才更新
  318. setEditorValue(context);
  319. }
  320. };
  321. CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
  322. //cell type handle tab key by itself
  323. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  324. return false;
  325. };
  326. return new CustomerCoeCellType();
  327. },
  328. setSelectButton(row,col,sheet,header){
  329. let getSelectButton = function (cellWidth=100) {
  330. function moreButton() {
  331. }
  332. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  333. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  334. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  335. let buttonW = cellWidth/5;
  336. let endX = x+w-2;
  337. if(value){
  338. let textWidth = ctx.measureText(value).width;
  339. let spaceWidth = cellWidth - buttonW;
  340. let textEndX = x+2+textWidth;
  341. if(spaceWidth<textWidth){
  342. for(let i = value.length-1;i>1;i--){
  343. let newValue = value.substr(0,i);
  344. let newTestWidth = ctx.measureText(newValue).width;
  345. if(spaceWidth>newTestWidth){
  346. value = newValue;
  347. textEndX = x+2+newTestWidth;
  348. break;
  349. }
  350. }
  351. }
  352. ctx.fillText(value,textEndX,y+h-5);
  353. }
  354. //画三个点
  355. ctx.save();
  356. ctx.beginPath();
  357. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  358. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  359. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  360. ctx.fillStyle="black";//填充颜色,默认是黑色
  361. ctx.fill();//画实心圆
  362. ctx.closePath();
  363. ctx.restore();
  364. };
  365. moreButton.prototype.processMouseLeave= function (hitinfo) {
  366. let newCell = new selectButton();
  367. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  368. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  369. };
  370. function selectButton() {
  371. }
  372. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  373. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  374. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  375. };
  376. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  377. return {
  378. x: x,
  379. y: y,
  380. row: context.row,
  381. col: context.col,
  382. cellStyle: cellStyle,
  383. cellRect: cellRect,
  384. sheetArea: context.sheetArea
  385. };
  386. };
  387. selectButton.prototype.processMouseDown = function (hitinfo){
  388. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  389. let b1 = new moreButton();
  390. b1.marginLeft(cellWidth*4/5);
  391. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  392. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  393. }
  394. };
  395. return new selectButton();
  396. };
  397. sheet.setCellType(row, col,getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  398. },
  399. setTipsCell(row,col,sheet,header){
  400. let TipCellType = function () {};
  401. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  402. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  403. return {
  404. x: x,
  405. y: y,
  406. row: context.row,
  407. col: context.col,
  408. cellStyle: cellStyle,
  409. cellRect: cellRect,
  410. sheet: context.sheet,
  411. sheetArea: context.sheetArea
  412. };
  413. };
  414. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  415. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  416. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  417. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  418. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  419. zoom = hitinfo.sheet.zoom();
  420. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  421. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  422. let setting = {};
  423. if(textLength <= cellWidth){
  424. return;
  425. }
  426. if(sheet && sheet.getParent().qo){
  427. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  428. }
  429. TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
  430. };
  431. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  432. TREE_SHEET_HELPER.tipDiv = 'hide';
  433. if (TREE_SHEET_HELPER._toolTipElement) {
  434. $(TREE_SHEET_HELPER._toolTipElement).hide();
  435. TREE_SHEET_HELPER._toolTipElement = null;
  436. };
  437. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  438. };
  439. sheet.setCellType(row, col,new TipCellType(),GC.Spread.Sheets.SheetArea.viewport);
  440. },
  441. chkIfEmpty: function(rObj, setting) {
  442. var rst = true;
  443. if (rObj) {
  444. for (var i = 0; i < setting.header.length; i++) {
  445. if (rObj[setting.header[i].dataCode]) {
  446. rst = false;
  447. break;
  448. }
  449. }
  450. }
  451. return rst;
  452. },
  453. //add by zhong 2017-10-10
  454. //动态下拉框,配合EnterCell, args.sheet.repaint();
  455. getDynamicCombo: function (forLocked) {
  456. let ComboCellForActiveCell = function () { };
  457. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  458. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  459. let sheet = options.sheet;
  460. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  461. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  462. } else {
  463. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  464. }
  465. };
  466. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  467. let sheet = options.sheet;
  468. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  469. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  470. } else {
  471. return GC.Spread.Sheets.CellTypes.Base.prototype.getHitInfo.apply(this, arguments);
  472. }
  473. };
  474. return new ComboCellForActiveCell();
  475. },
  476. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  477. let me = this;
  478. sheet.suspendPaint();
  479. let combo = me.getDynamicCombo();
  480. for(let i = 0, len = rowCount; i < len; i++){
  481. if(itemsHeight) combo.itemHeight(itemsHeight);
  482. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  483. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  484. else combo.items(items);
  485. sheet.getCell(beginRow + i, col).cellType(combo);
  486. }
  487. sheet.resumePaint();
  488. },
  489. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  490. sheet.suspendPaint();
  491. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  492. for(let i = 0, len = rowCount; i < len; i++){
  493. if(itemsHeight) combo.itemHeight(itemsHeight);
  494. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  495. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  496. else combo.items(items);
  497. sheet.getCell(beginRow + i, col).cellType(combo);
  498. }
  499. sheet.resumePaint();
  500. },
  501. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  502. copyTextToClipboard: function(text) {
  503. let textArea = document.createElement("textarea");
  504. textArea.style.position = 'fixed';
  505. textArea.style.top = 0;
  506. textArea.style.left = 0;
  507. textArea.style.width = '2em';
  508. textArea.style.height = '2em';
  509. textArea.style.padding = 0;
  510. textArea.style.border = 'none';
  511. textArea.style.outline = 'none';
  512. textArea.style.boxShadow = 'none';
  513. textArea.style.background = 'transparent';
  514. textArea.value = text;
  515. document.body.appendChild(textArea);
  516. textArea.select();
  517. try {
  518. let successful = document.execCommand('copy');
  519. let msg = successful ? 'successful' : 'unsuccessful';
  520. console.log('Copying text command was ' + msg);
  521. } catch (err) {
  522. console.log('Oops, unable to copy');
  523. }
  524. document.body.removeChild(textArea);
  525. },
  526. //获取选中区域的表格类型数据(可粘贴到excel)
  527. getTableData: function (sheet, colSettings = null) {
  528. let rst = '';
  529. let sel = sheet.getSelections()[0];
  530. let pasteText = [];
  531. for(let row = sel.row; row < sel.row + sel.rowCount; row++){
  532. if(!sheet.getCell(row, -1).visible())
  533. continue;
  534. let rowText = [];
  535. for(let j = 0; j < sel.colCount; j++){
  536. let col = sel.col + j;
  537. if(!sheet.getCell(-1, col).visible())
  538. continue;
  539. if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  540. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  541. else
  542. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
  543. }
  544. pasteText.push(rowText.join('\t'));
  545. }
  546. return pasteText.join('\n');
  547. },
  548. transferToTreeSetting:function(setting,treeSetting,treeCol){
  549. for(let h of setting.header){
  550. treeSetting.cols.push(getSettingCol(h))
  551. }
  552. for(let l of setting.view.lockColumns){
  553. treeSetting.cols[l].readOnly = true;
  554. }
  555. return treeSetting;
  556. function getSettingCol(header) {
  557. let aMap ={left:0,center:1,right:2};
  558. let hAlign = header.hAlign?aMap[header.hAlign]:0;
  559. let col = {
  560. "width":header.headerWidth?header.headerWidth:100,
  561. "head":{
  562. "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
  563. "spanCols":header.spanCols?header.spanCols:[1],
  564. "spanRows":header.spanRows?header.spanRows:[1],
  565. "vAlign":[1],
  566. "hAlign":[1],
  567. "font":["Arial"]
  568. },
  569. "data": {
  570. "field": header.dataCode,
  571. "vAlign": 1,
  572. "hAlign": hAlign,
  573. "font": "Arial"
  574. }
  575. };
  576. if(header.showHint == true){
  577. col.showHint = true;
  578. }
  579. if(header.cellType){
  580. col.data.cellType = getCellType(header);
  581. }
  582. if(header.decimalField){//设置formatter
  583. let decimal = getDecimal(header.decimalField);
  584. col.formatter = getFormatter(decimal);
  585. }
  586. if(header.getText && treeCol){
  587. col.data.getText = treeCol.getEvent(header.getText);
  588. }
  589. /*col.readOnly = function (node) {
  590. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  591. return true;
  592. }
  593. return false;
  594. };*/
  595. return col;
  596. }
  597. function getCellType(header) {
  598. return function () {
  599. if(header.cellType === "checkBox"){
  600. return new GC.Spread.Sheets.CellTypes.CheckBox();
  601. }
  602. if(header.cellType === "comboBox"){
  603. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  604. if(header.options){
  605. dynamicCombo.itemHeight(header.options.length).items(header.options);
  606. if(header.editorValueType==true){
  607. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  608. }
  609. }
  610. return dynamicCombo
  611. }
  612. }
  613. }
  614. },
  615. //注册自定义回车键事件
  616. bindEnterKey: function (workBook, operation) {
  617. workBook.commandManager().register('myEnter', operation);
  618. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  619. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  620. },
  621. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  622. bindEscKey: function (workBook, sheets) {
  623. function isDef(v){
  624. return typeof v !== 'undefined' && v !== null;
  625. }
  626. workBook.commandManager().register('myEsc', function () {
  627. let activeSheet = workBook.getActiveSheet();
  628. let hasTheSheet = false;
  629. for(let sheetObj of sheets){
  630. let sheet = sheetObj.sheet;
  631. if(sheet === activeSheet){
  632. hasTheSheet = true;
  633. let editStarting = sheetObj.editStarting;
  634. let editEnded = sheetObj.editEnded;
  635. if(editStarting){
  636. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  637. }
  638. if(editEnded){
  639. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  640. }
  641. let row = sheet.getActiveRowIndex();
  642. let col = sheet.getActiveColumnIndex();
  643. let orgV = sheet.getValue(row, col);
  644. let orgText = sheet.getText(row, col);
  645. if(!isDef(orgV)){
  646. orgV = '';
  647. }
  648. if(sheet.isEditing()){
  649. sheet.endEdit();
  650. sheet.setValue(row, col, orgV);
  651. }
  652. if(editStarting){
  653. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  654. }
  655. if(editEnded){
  656. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  657. }
  658. }
  659. }
  660. //容错处理,以防没把所有工作簿的表格信息传入参数
  661. if(!hasTheSheet){
  662. if(activeSheet.isEditing()){
  663. activeSheet.endEdit();
  664. }
  665. }
  666. });
  667. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  668. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  669. },
  670. //设置默认样式
  671. spreadDefaultStyle: function (workBook) {
  672. let defaultStyle = new GC.Spread.Sheets.Style();
  673. defaultStyle.font = '0.9rem Calibri';
  674. let sheetCount = workBook.getSheetCount();
  675. for(let i = 0; i < sheetCount; i++){
  676. let sheet = workBook.getSheet(i);
  677. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  678. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  679. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  680. }
  681. },
  682. //动态根据工作簿宽度和各列宽度比例设置宽度
  683. setColumnWidthByRate: function (workBookWidth, workBook, headers){
  684. if(workBook){
  685. const sheet = workBook.getActiveSheet();
  686. sheet.suspendEvent();
  687. sheet.suspendPaint();
  688. for(let col = 0; col < headers.length; col++){
  689. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  690. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  691. }
  692. else {
  693. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  694. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  695. }
  696. }
  697. }
  698. sheet.resumeEvent();
  699. sheet.resumePaint();
  700. }
  701. }
  702. }