sheet_common.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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.getStyle && setting.getStyle(data[row])){
  143. sheet.setStyle(row, -1, setting.getStyle(data[row]));
  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 === "replaceButton"){
  178. this.setReplaceButton(row,col,sheet,setting.header[col]);
  179. }
  180. if(setting.header[col].cellType === "tipsCell"){
  181. this.setTipsCell(row,col,sheet,setting.header[col]);
  182. }
  183. if(setting.owner==='gljTree'){
  184. if(setting.header[col].cellType === "checkBox"){
  185. val==1?val:0;
  186. this.setCheckBoxCell(row,col,sheet,val);
  187. }
  188. if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
  189. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  190. val=distTypeVal;
  191. }
  192. }
  193. if(setting.header[col].getText){
  194. val = setting.getText[setting.header[col].getText](data[row],val)
  195. }
  196. sheet.setValue(row, col, val, ch);
  197. }
  198. this.setRowStyle(row,sheet,data[row].bgColour);
  199. if(setting.autoFit==true){
  200. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  201. sheet.autoFitRow(row);
  202. }
  203. },
  204. //todo
  205. analyzePasteData: function(setting, pastedInfo) {
  206. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  207. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  208. if (pastedInfo.pasteData.text[i] === "\n") {
  209. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  210. preStrIdx = i + 1;
  211. rst.push(itemObj);
  212. if (i < pastedInfo.pasteData.text.length - 1) {
  213. itemObj = {};
  214. }
  215. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  216. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  217. propId++;
  218. preStrIdx = i + 1;
  219. //if the last copied-cell were empty, should check whether the end of text
  220. if (i == pastedInfo.pasteData.text.length - 1) {
  221. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  222. rst.push(itemObj);
  223. }
  224. } else if (i == pastedInfo.pasteData.text.length - 1) {
  225. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  226. rst.push(itemObj);
  227. }
  228. }
  229. return rst;
  230. },
  231. combineRowData: function(sheet, setting, row) {
  232. var rst = {};
  233. for (var col = 0; col < setting.header.length; col++) {
  234. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  235. }
  236. return rst;
  237. },
  238. shieldAllCells: function(sheet) {
  239. sheet.options.isProtected = true;
  240. },
  241. unShieldAllCells: function(sheet) {
  242. sheet.options.isProtected = false;
  243. },
  244. lockCells: function(sheet, setting){
  245. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  246. sheet.options.isProtected = true;
  247. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  248. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  249. sheet.getRange(-1,setting.view.lockColumns[i], -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  250. }
  251. }
  252. },
  253. setCheckBoxCell(row,col,sheet,val){
  254. var c = new GC.Spread.Sheets.CellTypes.CheckBox();
  255. c.isThreeState(false);
  256. sheet.setCellType(row, col,c,GC.Spread.Sheets.SheetArea.viewport);
  257. sheet.getCell(row, col).value(val);
  258. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  259. },
  260. setComboBox(row,col,sheet,options,editorValueType){
  261. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  262. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  263. if(options){
  264. dynamicCombo.itemHeight(options.length).items(options);
  265. if(editorValueType==true){
  266. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  267. }
  268. }
  269. sheet.setCellType(row, col,dynamicCombo,GC.Spread.Sheets.SheetArea.viewport);
  270. },
  271. setRowStyle(row,sheet,bgColour) {
  272. if(bgColour){
  273. let style = new GC.Spread.Sheets.Style();
  274. style.backColor = bgColour;
  275. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  276. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  277. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  278. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  279. sheet.setStyle(row, -1, style);
  280. }
  281. },
  282. getCustomerCoeCellType: function (htmlGenerator,setEditorValue,updateCallback) {
  283. let me = this;
  284. function CustomerCoeCellType() {
  285. this.isEscKey=false;
  286. this.displayText='';
  287. }
  288. CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  289. CustomerCoeCellType.prototype.createEditorElement = function (context) {
  290. console.log("create editor")
  291. let element = document.createElement("div");//这里创建的,会自动销毁
  292. return element
  293. };
  294. CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  295. if (editorContext) {
  296. $editor = $(editorContext);
  297. $editor.css("position", "fixed");
  298. $editor.css("background", "white");
  299. $editor.css("width", cellRect.width);
  300. $editor.attr("gcUIElement", "gcEditingInput");
  301. if(htmlGenerator) htmlGenerator(context,cellRect,$editor);
  302. }
  303. }
  304. CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
  305. };
  306. CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
  307. console.log("set editor value");
  308. this.displayText = value;
  309. };
  310. CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
  311. console.log("get value");
  312. if(this.isEscKey !=true&& updateCallback){
  313. updateCallback();
  314. }
  315. this.isEscKey = false;
  316. return this.displayText;
  317. };
  318. CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  319. console.log(" update editor");
  320. if( setEditorValue){//不是esc时才更新
  321. setEditorValue(context);
  322. }
  323. };
  324. CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
  325. //cell type handle tab key by itself
  326. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  327. return false;
  328. };
  329. return new CustomerCoeCellType();
  330. },
  331. scrollSheetForOption:function (sheet,cxt,cellRect,row,options){
  332. let topRow = sheet.getViewportTopRow(1);
  333. if(row == topRow) return;//已经是最顶行了
  334. let length = options&&options.length>0?options.length:1;
  335. let height = cxt.canvas.height;
  336. let startY = cellRect.y+cellRect.height;//下拉框的起始显示位置
  337. let endY = startY + length * cellRect.height;//下拉框的结束显示位置
  338. if(endY <= height) return; //如果没有超出显示位置,直接返回
  339. let overRow = Math.ceil((endY - height)/cellRect.height);//超出的行数
  340. let showRow = topRow + overRow > row?row:topRow + overRow;
  341. sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
  342. },
  343. setSelectButton(row,col,sheet,header){
  344. let getSelectButton = function (cellWidth=100) {
  345. function moreButton() {
  346. }
  347. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  348. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  349. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  350. let buttonW = cellWidth/5;
  351. let endX = x+w-2;
  352. if(value){
  353. let textWidth = ctx.measureText(value).width;
  354. let spaceWidth = cellWidth - buttonW;
  355. let textEndX = x+2+textWidth;
  356. if(spaceWidth<textWidth){
  357. for(let i = value.length-1;i>1;i--){
  358. let newValue = value.substr(0,i);
  359. let newTestWidth = ctx.measureText(newValue).width;
  360. if(spaceWidth>newTestWidth){
  361. value = newValue;
  362. textEndX = x+2+newTestWidth;
  363. break;
  364. }
  365. }
  366. }
  367. ctx.fillText(value,textEndX,y+h-5);
  368. }
  369. //画三个点
  370. ctx.save();
  371. ctx.beginPath();
  372. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  373. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  374. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  375. ctx.fillStyle="black";//填充颜色,默认是黑色
  376. ctx.fill();//画实心圆
  377. ctx.closePath();
  378. ctx.restore();
  379. };
  380. moreButton.prototype.processMouseLeave= function (hitinfo) {
  381. let newCell = new selectButton();
  382. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  383. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  384. };
  385. function selectButton() {
  386. }
  387. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  388. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  389. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  390. };
  391. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  392. return {
  393. x: x,
  394. y: y,
  395. row: context.row,
  396. col: context.col,
  397. cellStyle: cellStyle,
  398. cellRect: cellRect,
  399. sheetArea: context.sheetArea
  400. };
  401. };
  402. selectButton.prototype.processMouseDown = function (hitinfo){
  403. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  404. let b1 = new moreButton();
  405. b1.marginLeft(cellWidth*4/5);
  406. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  407. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  408. }
  409. };
  410. return new selectButton();
  411. };
  412. sheet.setCellType(row, col,getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  413. },
  414. setReplaceButton(row,col,sheet){
  415. let replaceButton = function(){
  416. };
  417. replaceButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  418. replaceButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  419. GC.Spread.Sheets.CellTypes.Button.prototype.paint.apply(this,arguments);
  420. if(value){
  421. ctx.save();
  422. ctx.fillStyle = "white";
  423. let fh = options.fontInfo&&options.fontInfo.fontSize?options.fontInfo.fontSize:h-6;
  424. ctx.fillText(value,x+(w+ctx.measureText(value).width)/2,y+(h+fh)/2-2);
  425. ctx.restore();
  426. }
  427. };
  428. let cellType = new replaceButton();
  429. cellType.buttonBackColor("#07A0FF");
  430. sheet.setCellType(row, col,cellType,GC.Spread.Sheets.SheetArea.viewport);
  431. },
  432. setTipsCell(row,col,sheet,header){
  433. let TipCellType = function () {};
  434. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  435. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  436. return {
  437. x: x,
  438. y: y,
  439. row: context.row,
  440. col: context.col,
  441. cellStyle: cellStyle,
  442. cellRect: cellRect,
  443. sheet: context.sheet,
  444. sheetArea: context.sheetArea
  445. };
  446. };
  447. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  448. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  449. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  450. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  451. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  452. zoom = hitinfo.sheet.zoom();
  453. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  454. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  455. let setting = {};
  456. if(textLength <= cellWidth){
  457. return;
  458. }
  459. if(sheet && sheet.getParent().qo){
  460. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  461. }
  462. TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
  463. };
  464. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  465. TREE_SHEET_HELPER.tipDiv = 'hide';
  466. if (TREE_SHEET_HELPER._toolTipElement) {
  467. $(TREE_SHEET_HELPER._toolTipElement).hide();
  468. TREE_SHEET_HELPER._toolTipElement = null;
  469. };
  470. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  471. };
  472. sheet.setCellType(row, col,new TipCellType(),GC.Spread.Sheets.SheetArea.viewport);
  473. },
  474. chkIfEmpty: function(rObj, setting) {
  475. var rst = true;
  476. if (rObj) {
  477. for (var i = 0; i < setting.header.length; i++) {
  478. if (rObj[setting.header[i].dataCode]) {
  479. rst = false;
  480. break;
  481. }
  482. }
  483. }
  484. return rst;
  485. },
  486. //add by zhong 2017-10-10
  487. //动态下拉框,配合EnterCell, args.sheet.repaint();
  488. getDynamicCombo: function (forLocked) {
  489. let ComboCellForActiveCell = function () { };
  490. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  491. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  492. let sheet = options.sheet;
  493. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  494. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  495. } else {
  496. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  497. }
  498. };
  499. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  500. let sheet = options.sheet;
  501. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  502. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  503. } else {
  504. return {
  505. x: x,
  506. y: y,
  507. row: options.row,
  508. col: options.col,
  509. cellStyle: cellStyle,
  510. cellRect: cellRect,
  511. sheetArea: options.sheetArea
  512. };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
  513. }
  514. };
  515. return new ComboCellForActiveCell();
  516. },
  517. getTipsCombo:function (forLocked,tips,setting,node) {
  518. let getTipsCombo = function () {
  519. this.clickCom=false;
  520. };
  521. getTipsCombo.prototype = sheetCommonObj.getDynamicCombo(forLocked);
  522. if(tips && tips !=""){
  523. getTipsCombo.prototype.processMouseEnter = function(hitinfo){
  524. if(this.clickCom == true){ //点击了下拉框的三角形,则不用再显示悬浮框了
  525. this.clickCom = false;
  526. return;
  527. }
  528. let text = typeof tips == 'function'?tips(node):tips;
  529. TREE_SHEET_HELPER.delayShowTips(hitinfo,setting,text);
  530. };
  531. getTipsCombo.prototype.processMouseLeave = function (hitinfo) {
  532. TREE_SHEET_HELPER.hideTipsDiv();
  533. };
  534. getTipsCombo.prototype.processMouseDown = function (hitinfo){
  535. if(hitinfo.isReservedLocation == true){//这里是点击了下拉框的三角形才会有这个属性
  536. TREE_SHEET_HELPER.hideTipsDiv();
  537. this.clickCom = true;
  538. }
  539. GC.Spread.Sheets.CellTypes.ComboBox.prototype.processMouseDown.apply(this, arguments);
  540. };
  541. getTipsCombo.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context){
  542. TREE_SHEET_HELPER.hideTipsDiv();
  543. GC.Spread.Sheets.CellTypes.ComboBox.prototype.updateEditor.apply(this, arguments);
  544. };
  545. }
  546. return new getTipsCombo();
  547. },
  548. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  549. let me = this;
  550. sheet.suspendPaint();
  551. let combo = me.getDynamicCombo();
  552. for(let i = 0, len = rowCount; i < len; i++){
  553. if(itemsHeight) {
  554. combo.itemHeight(itemsHeight);
  555. combo._maxDropDownItems = itemsHeight + 5;
  556. }
  557. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  558. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  559. else combo.items(items);
  560. sheet.getCell(beginRow + i, col).cellType(combo);
  561. }
  562. sheet.resumePaint();
  563. },
  564. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  565. sheet.suspendPaint();
  566. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  567. for(let i = 0, len = rowCount; i < len; i++){
  568. if(itemsHeight) combo.itemHeight(itemsHeight);
  569. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  570. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  571. else combo.items(items);
  572. sheet.getCell(beginRow + i, col).cellType(combo);
  573. }
  574. sheet.resumePaint();
  575. },
  576. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  577. copyTextToClipboard: function(text) {
  578. let textArea = document.createElement("textarea");
  579. textArea.style.position = 'fixed';
  580. textArea.style.top = 0;
  581. textArea.style.left = 0;
  582. textArea.style.width = '2em';
  583. textArea.style.height = '2em';
  584. textArea.style.padding = 0;
  585. textArea.style.border = 'none';
  586. textArea.style.outline = 'none';
  587. textArea.style.boxShadow = 'none';
  588. textArea.style.background = 'transparent';
  589. textArea.value = text;
  590. document.body.appendChild(textArea);
  591. textArea.select();
  592. try {
  593. let successful = document.execCommand('copy');
  594. let msg = successful ? 'successful' : 'unsuccessful';
  595. console.log('Copying text command was ' + msg);
  596. } catch (err) {
  597. console.log('Oops, unable to copy');
  598. }
  599. document.body.removeChild(textArea);
  600. },
  601. //获取选中区域的表格类型数据(可粘贴到excel)
  602. getTableData: function (sheet, colSettings = null) {
  603. let rst = '';
  604. let sel = sheet.getSelections()[0];
  605. let pasteText = [];
  606. for(let row = sel.row; row < sel.row + sel.rowCount; row++){
  607. if(!sheet.getCell(row, -1).visible())
  608. continue;
  609. let rowText = [];
  610. for(let j = 0; j < sel.colCount; j++){
  611. let col = sel.col + j;
  612. if(!sheet.getCell(-1, col).visible())
  613. continue;
  614. if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  615. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  616. else
  617. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
  618. }
  619. pasteText.push(rowText.join('\t'));
  620. }
  621. return pasteText.join('\n');
  622. },
  623. transferToTreeSetting:function(setting,treeSetting,treeCol){
  624. for(let h of setting.header){
  625. treeSetting.cols.push(getSettingCol(h))
  626. }
  627. for(let l of setting.view.lockColumns){
  628. treeSetting.cols[l].readOnly = true;
  629. }
  630. return treeSetting;
  631. function getSettingCol(header) {
  632. let aMap ={left:0,center:1,right:2};
  633. let hAlign = header.hAlign?aMap[header.hAlign]:0;
  634. let col = {
  635. "width":header.headerWidth?header.headerWidth:100,
  636. "head":{
  637. "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
  638. "spanCols":header.spanCols?header.spanCols:[1],
  639. "spanRows":header.spanRows?header.spanRows:[1],
  640. "vAlign":[1],
  641. "hAlign":[1],
  642. "font":["Arial"]
  643. },
  644. "data": {
  645. "field": header.dataCode,
  646. "vAlign": 1,
  647. "hAlign": hAlign,
  648. "font": "Arial"
  649. }
  650. };
  651. if(header.showHint == true){
  652. col.showHint = true;
  653. }
  654. if(header.cellType){
  655. col.data.cellType = getCellType(header);
  656. }
  657. if(header.decimalField){//设置formatter
  658. let decimal = getDecimal(header.decimalField);
  659. col.formatter = getFormatter(decimal);
  660. }
  661. if(header.getText && treeCol){
  662. col.data.getText = treeCol.getEvent(header.getText);
  663. }
  664. /*col.readOnly = function (node) {
  665. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  666. return true;
  667. }
  668. return false;
  669. };*/
  670. return col;
  671. }
  672. function getCellType(header) {
  673. return function () {
  674. if(header.cellType === "checkBox"){
  675. return new GC.Spread.Sheets.CellTypes.CheckBox();
  676. }
  677. if(header.cellType === "comboBox"){
  678. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  679. if(header.options){
  680. dynamicCombo.itemHeight(header.options.length).items(header.options);
  681. if(header.editorValueType==true){
  682. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  683. }
  684. }
  685. return dynamicCombo
  686. }
  687. }
  688. }
  689. },
  690. //注册自定义回车键事件
  691. bindEnterKey: function (workBook, operation) {
  692. workBook.commandManager().register('myEnter', operation);
  693. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  694. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  695. },
  696. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  697. bindEscKey: function (workBook, sheets) {
  698. function isDef(v){
  699. return typeof v !== 'undefined' && v !== null;
  700. }
  701. workBook.commandManager().register('myEsc', function () {
  702. let activeSheet = workBook.getActiveSheet();
  703. let hasTheSheet = false;
  704. for(let sheetObj of sheets){
  705. let sheet = sheetObj.sheet;
  706. if(sheet === activeSheet){
  707. hasTheSheet = true;
  708. let editStarting = sheetObj.editStarting;
  709. let editEnded = sheetObj.editEnded;
  710. if(editStarting){
  711. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  712. }
  713. if(editEnded){
  714. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  715. }
  716. let row = sheet.getActiveRowIndex();
  717. let col = sheet.getActiveColumnIndex();
  718. let orgV = sheet.getValue(row, col);
  719. let orgText = sheet.getText(row, col);
  720. if(!isDef(orgV)){
  721. orgV = '';
  722. }
  723. if(sheet.isEditing()){
  724. sheet.endEdit();
  725. sheet.setValue(row, col, orgV);
  726. }
  727. if(editStarting){
  728. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  729. }
  730. if(editEnded){
  731. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  732. }
  733. }
  734. }
  735. //容错处理,以防没把所有工作簿的表格信息传入参数
  736. if(!hasTheSheet){
  737. if(activeSheet.isEditing()){
  738. activeSheet.endEdit();
  739. }
  740. }
  741. });
  742. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  743. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  744. },
  745. //设置默认样式
  746. spreadDefaultStyle: function (workBook) {
  747. let defaultStyle = new GC.Spread.Sheets.Style();
  748. defaultStyle.font = '14px Calibri';
  749. let sheetCount = workBook.getSheetCount();
  750. for(let i = 0; i < sheetCount; i++){
  751. let sheet = workBook.getSheet(i);
  752. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  753. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  754. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  755. }
  756. },
  757. //动态根据工作簿宽度和各列宽度比例设置宽度
  758. setColumnWidthByRate: function (workBookWidth, workBook, headers){
  759. if(workBook){
  760. const sheet = workBook.getActiveSheet();
  761. sheet.suspendEvent();
  762. sheet.suspendPaint();
  763. for(let col = 0; col < headers.length; col++){
  764. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  765. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  766. }
  767. else {
  768. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  769. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  770. }
  771. }
  772. }
  773. sheet.resumeEvent();
  774. sheet.resumePaint();
  775. }
  776. },
  777. drowRect:function (ctx, x, y, w, h,rectW,rectH,margin) {
  778. ctx.save();
  779. ctx.strokeStyle = "gray";
  780. ctx.translate(0.5, 0.5);
  781. ctx.beginPath();
  782. let rectX = x + margin;
  783. let rectY = y + Math.round(h / 2) - rectH / 2;
  784. ctx.moveTo(rectX, rectY);
  785. ctx.lineTo(rectX, rectY + rectH);
  786. ctx.lineTo(rectX + rectW, rectY + rectH);
  787. ctx.lineTo(rectX + rectW, rectY);
  788. ctx.lineTo(rectX, rectY);
  789. ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
  790. ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
  791. ctx.stroke();
  792. ctx.restore();
  793. },
  794. drawLine : function (ctx, x1, y1, x2, y2, color) {
  795. let l_color = color?color:"gray";
  796. ctx.save();
  797. ctx.translate(0.5, 0.5);
  798. ctx.beginPath();
  799. ctx.moveTo(x1, y1);
  800. ctx.lineTo(x2, y2);
  801. ctx.strokeStyle = l_color;
  802. ctx.stroke();
  803. ctx.restore();
  804. },
  805. drowSymbol :function (ctx, x, y, w, h,rectW,rectH,margin,collapsed) {
  806. ctx.save();
  807. ctx.strokeStyle = "#000000";
  808. ctx.translate(0.5, 0.5);
  809. ctx.beginPath();
  810. ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
  811. ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
  812. let rectY = y + Math.round(h / 2) - rectH / 2;
  813. if (collapsed) {
  814. ctx.moveTo(x + margin + rectW / 2, rectY + 2);
  815. ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
  816. }
  817. ctx.stroke();
  818. ctx.restore();
  819. },
  820. drowTriangle:function(ctx,x,y){//画向下三角形
  821. ctx.save();
  822. ctx.fillStyle = "black";
  823. ctx.beginPath();
  824. ctx.moveTo(x, y);
  825. ctx.lineTo(x-3, y -6);
  826. ctx.lineTo(x+3, y -6);
  827. ctx.fill();
  828. ctx.restore();
  829. },
  830. drowSubItem: function (ctx, x, y, w, h, offset, hasNext,step) {
  831. let t_step = step?step:6;
  832. offset += t_step;
  833. ctx.save();
  834. ctx.strokeStyle = "gray";
  835. ctx.translate(0.5, 0.5);
  836. ctx.beginPath();
  837. ctx.moveTo(x + offset, y);
  838. ctx.lineTo(x + offset, y + Math.round(h / 2));
  839. offset += 9;
  840. ctx.lineTo(x + offset, y + Math.round(h / 2));
  841. if (hasNext) {
  842. ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
  843. ctx.lineTo(x + offset - 9, y + h);
  844. }
  845. ctx.stroke();
  846. ctx.restore();
  847. return offset;
  848. }
  849. }