sheet_common.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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 === "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. scrollSheetForOption:function (sheet,cxt,cellRect,row,options){
  329. let topRow = sheet.getViewportTopRow(1);
  330. if(row == topRow) return;//已经是最顶行了
  331. let length = options&&options.length>0?options.length:1;
  332. let height = cxt.canvas.height;
  333. let startY = cellRect.y+cellRect.height;//下拉框的起始显示位置
  334. let endY = startY + length * cellRect.height;//下拉框的结束显示位置
  335. if(endY <= height) return; //如果没有超出显示位置,直接返回
  336. let overRow = Math.ceil((endY - height)/cellRect.height);//超出的行数
  337. let showRow = topRow + overRow > row?row:topRow + overRow;
  338. sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
  339. },
  340. setSelectButton(row,col,sheet,header){
  341. let getSelectButton = function (cellWidth=100) {
  342. function moreButton() {
  343. }
  344. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  345. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  346. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  347. let buttonW = cellWidth/5;
  348. let endX = x+w-2;
  349. if(value){
  350. let textWidth = ctx.measureText(value).width;
  351. let spaceWidth = cellWidth - buttonW;
  352. let textEndX = x+2+textWidth;
  353. if(spaceWidth<textWidth){
  354. for(let i = value.length-1;i>1;i--){
  355. let newValue = value.substr(0,i);
  356. let newTestWidth = ctx.measureText(newValue).width;
  357. if(spaceWidth>newTestWidth){
  358. value = newValue;
  359. textEndX = x+2+newTestWidth;
  360. break;
  361. }
  362. }
  363. }
  364. ctx.fillText(value,textEndX,y+h-5);
  365. }
  366. //画三个点
  367. ctx.save();
  368. ctx.beginPath();
  369. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  370. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  371. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  372. ctx.fillStyle="black";//填充颜色,默认是黑色
  373. ctx.fill();//画实心圆
  374. ctx.closePath();
  375. ctx.restore();
  376. };
  377. moreButton.prototype.processMouseLeave= function (hitinfo) {
  378. let newCell = new selectButton();
  379. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  380. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  381. };
  382. function selectButton() {
  383. }
  384. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  385. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  386. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  387. };
  388. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  389. return {
  390. x: x,
  391. y: y,
  392. row: context.row,
  393. col: context.col,
  394. cellStyle: cellStyle,
  395. cellRect: cellRect,
  396. sheetArea: context.sheetArea
  397. };
  398. };
  399. selectButton.prototype.processMouseDown = function (hitinfo){
  400. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  401. let b1 = new moreButton();
  402. b1.marginLeft(cellWidth*4/5);
  403. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  404. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  405. }
  406. };
  407. return new selectButton();
  408. };
  409. sheet.setCellType(row, col,getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  410. },
  411. setTipsCell(row,col,sheet,header){
  412. let TipCellType = function () {};
  413. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  414. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  415. return {
  416. x: x,
  417. y: y,
  418. row: context.row,
  419. col: context.col,
  420. cellStyle: cellStyle,
  421. cellRect: cellRect,
  422. sheet: context.sheet,
  423. sheetArea: context.sheetArea
  424. };
  425. };
  426. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  427. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  428. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  429. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  430. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  431. zoom = hitinfo.sheet.zoom();
  432. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  433. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  434. let setting = {};
  435. if(textLength <= cellWidth){
  436. return;
  437. }
  438. if(sheet && sheet.getParent().qo){
  439. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  440. }
  441. TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
  442. };
  443. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  444. TREE_SHEET_HELPER.tipDiv = 'hide';
  445. if (TREE_SHEET_HELPER._toolTipElement) {
  446. $(TREE_SHEET_HELPER._toolTipElement).hide();
  447. TREE_SHEET_HELPER._toolTipElement = null;
  448. };
  449. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  450. };
  451. sheet.setCellType(row, col,new TipCellType(),GC.Spread.Sheets.SheetArea.viewport);
  452. },
  453. chkIfEmpty: function(rObj, setting) {
  454. var rst = true;
  455. if (rObj) {
  456. for (var i = 0; i < setting.header.length; i++) {
  457. if (rObj[setting.header[i].dataCode]) {
  458. rst = false;
  459. break;
  460. }
  461. }
  462. }
  463. return rst;
  464. },
  465. //add by zhong 2017-10-10
  466. //动态下拉框,配合EnterCell, args.sheet.repaint();
  467. getDynamicCombo: function (forLocked) {
  468. let ComboCellForActiveCell = function () { };
  469. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  470. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  471. let sheet = options.sheet;
  472. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  473. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  474. } else {
  475. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  476. }
  477. };
  478. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  479. let sheet = options.sheet;
  480. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  481. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  482. } else {
  483. return {
  484. x: x,
  485. y: y,
  486. row: options.row,
  487. col: options.col,
  488. cellStyle: cellStyle,
  489. cellRect: cellRect,
  490. sheetArea: options.sheetArea
  491. };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
  492. }
  493. };
  494. return new ComboCellForActiveCell();
  495. },
  496. getTipsCombo:function (forLocked,tips,setting) {
  497. let getTipsCombo = function () {
  498. this.clickCom=false;
  499. };
  500. getTipsCombo.prototype = sheetCommonObj.getDynamicCombo(forLocked);
  501. if(tips && tips !=""){
  502. getTipsCombo.prototype.processMouseEnter = function(hitinfo){
  503. if(this.clickCom == true){ //点击了下拉框的三角形,则不用再显示悬浮框了
  504. this.clickCom = false;
  505. return;
  506. }
  507. TREE_SHEET_HELPER.delayShowTips(hitinfo,setting,tips);
  508. };
  509. getTipsCombo.prototype.processMouseLeave = function (hitinfo) {
  510. TREE_SHEET_HELPER.hideTipsDiv();
  511. };
  512. getTipsCombo.prototype.processMouseDown = function (hitinfo){
  513. if(hitinfo.isReservedLocation == true){//这里是点击了下拉框的三角形才会有这个属性
  514. TREE_SHEET_HELPER.hideTipsDiv();
  515. this.clickCom = true;
  516. }
  517. GC.Spread.Sheets.CellTypes.ComboBox.prototype.processMouseDown.apply(this, arguments);
  518. };
  519. getTipsCombo.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context){
  520. TREE_SHEET_HELPER.hideTipsDiv();
  521. GC.Spread.Sheets.CellTypes.ComboBox.prototype.updateEditor.apply(this, arguments);
  522. };
  523. }
  524. return new getTipsCombo();
  525. },
  526. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  527. let me = this;
  528. sheet.suspendPaint();
  529. let combo = me.getDynamicCombo();
  530. for(let i = 0, len = rowCount; i < len; i++){
  531. if(itemsHeight) {
  532. combo.itemHeight(itemsHeight);
  533. combo._maxDropDownItems = itemsHeight + 5;
  534. }
  535. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  536. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  537. else combo.items(items);
  538. sheet.getCell(beginRow + i, col).cellType(combo);
  539. }
  540. sheet.resumePaint();
  541. },
  542. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  543. sheet.suspendPaint();
  544. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  545. for(let i = 0, len = rowCount; i < len; i++){
  546. if(itemsHeight) combo.itemHeight(itemsHeight);
  547. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  548. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  549. else combo.items(items);
  550. sheet.getCell(beginRow + i, col).cellType(combo);
  551. }
  552. sheet.resumePaint();
  553. },
  554. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  555. copyTextToClipboard: function(text) {
  556. let textArea = document.createElement("textarea");
  557. textArea.style.position = 'fixed';
  558. textArea.style.top = 0;
  559. textArea.style.left = 0;
  560. textArea.style.width = '2em';
  561. textArea.style.height = '2em';
  562. textArea.style.padding = 0;
  563. textArea.style.border = 'none';
  564. textArea.style.outline = 'none';
  565. textArea.style.boxShadow = 'none';
  566. textArea.style.background = 'transparent';
  567. textArea.value = text;
  568. document.body.appendChild(textArea);
  569. textArea.select();
  570. try {
  571. let successful = document.execCommand('copy');
  572. let msg = successful ? 'successful' : 'unsuccessful';
  573. console.log('Copying text command was ' + msg);
  574. } catch (err) {
  575. console.log('Oops, unable to copy');
  576. }
  577. document.body.removeChild(textArea);
  578. },
  579. //获取选中区域的表格类型数据(可粘贴到excel)
  580. getTableData: function (sheet, colSettings = null) {
  581. let rst = '';
  582. let sel = sheet.getSelections()[0];
  583. let pasteText = [];
  584. for(let row = sel.row; row < sel.row + sel.rowCount; row++){
  585. if(!sheet.getCell(row, -1).visible())
  586. continue;
  587. let rowText = [];
  588. for(let j = 0; j < sel.colCount; j++){
  589. let col = sel.col + j;
  590. if(!sheet.getCell(-1, col).visible())
  591. continue;
  592. if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  593. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  594. else
  595. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
  596. }
  597. pasteText.push(rowText.join('\t'));
  598. }
  599. return pasteText.join('\n');
  600. },
  601. transferToTreeSetting:function(setting,treeSetting,treeCol){
  602. for(let h of setting.header){
  603. treeSetting.cols.push(getSettingCol(h))
  604. }
  605. for(let l of setting.view.lockColumns){
  606. treeSetting.cols[l].readOnly = true;
  607. }
  608. return treeSetting;
  609. function getSettingCol(header) {
  610. let aMap ={left:0,center:1,right:2};
  611. let hAlign = header.hAlign?aMap[header.hAlign]:0;
  612. let col = {
  613. "width":header.headerWidth?header.headerWidth:100,
  614. "head":{
  615. "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
  616. "spanCols":header.spanCols?header.spanCols:[1],
  617. "spanRows":header.spanRows?header.spanRows:[1],
  618. "vAlign":[1],
  619. "hAlign":[1],
  620. "font":["Arial"]
  621. },
  622. "data": {
  623. "field": header.dataCode,
  624. "vAlign": 1,
  625. "hAlign": hAlign,
  626. "font": "Arial"
  627. }
  628. };
  629. if(header.showHint == true){
  630. col.showHint = true;
  631. }
  632. if(header.cellType){
  633. col.data.cellType = getCellType(header);
  634. }
  635. if(header.decimalField){//设置formatter
  636. let decimal = getDecimal(header.decimalField);
  637. col.formatter = getFormatter(decimal);
  638. }
  639. if(header.getText && treeCol){
  640. col.data.getText = treeCol.getEvent(header.getText);
  641. }
  642. /*col.readOnly = function (node) {
  643. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  644. return true;
  645. }
  646. return false;
  647. };*/
  648. return col;
  649. }
  650. function getCellType(header) {
  651. return function () {
  652. if(header.cellType === "checkBox"){
  653. return new GC.Spread.Sheets.CellTypes.CheckBox();
  654. }
  655. if(header.cellType === "comboBox"){
  656. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  657. if(header.options){
  658. dynamicCombo.itemHeight(header.options.length).items(header.options);
  659. if(header.editorValueType==true){
  660. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  661. }
  662. }
  663. return dynamicCombo
  664. }
  665. }
  666. }
  667. },
  668. //注册自定义回车键事件
  669. bindEnterKey: function (workBook, operation) {
  670. workBook.commandManager().register('myEnter', operation);
  671. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  672. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  673. },
  674. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  675. bindEscKey: function (workBook, sheets) {
  676. function isDef(v){
  677. return typeof v !== 'undefined' && v !== null;
  678. }
  679. workBook.commandManager().register('myEsc', function () {
  680. let activeSheet = workBook.getActiveSheet();
  681. let hasTheSheet = false;
  682. for(let sheetObj of sheets){
  683. let sheet = sheetObj.sheet;
  684. if(sheet === activeSheet){
  685. hasTheSheet = true;
  686. let editStarting = sheetObj.editStarting;
  687. let editEnded = sheetObj.editEnded;
  688. if(editStarting){
  689. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  690. }
  691. if(editEnded){
  692. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  693. }
  694. let row = sheet.getActiveRowIndex();
  695. let col = sheet.getActiveColumnIndex();
  696. let orgV = sheet.getValue(row, col);
  697. let orgText = sheet.getText(row, col);
  698. if(!isDef(orgV)){
  699. orgV = '';
  700. }
  701. if(sheet.isEditing()){
  702. sheet.endEdit();
  703. sheet.setValue(row, col, orgV);
  704. }
  705. if(editStarting){
  706. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  707. }
  708. if(editEnded){
  709. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  710. }
  711. }
  712. }
  713. //容错处理,以防没把所有工作簿的表格信息传入参数
  714. if(!hasTheSheet){
  715. if(activeSheet.isEditing()){
  716. activeSheet.endEdit();
  717. }
  718. }
  719. });
  720. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  721. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  722. },
  723. //设置默认样式
  724. spreadDefaultStyle: function (workBook) {
  725. let defaultStyle = new GC.Spread.Sheets.Style();
  726. defaultStyle.font = '0.9rem Calibri';
  727. let sheetCount = workBook.getSheetCount();
  728. for(let i = 0; i < sheetCount; i++){
  729. let sheet = workBook.getSheet(i);
  730. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  731. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  732. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  733. }
  734. },
  735. //动态根据工作簿宽度和各列宽度比例设置宽度
  736. setColumnWidthByRate: function (workBookWidth, workBook, headers){
  737. if(workBook){
  738. const sheet = workBook.getActiveSheet();
  739. sheet.suspendEvent();
  740. sheet.suspendPaint();
  741. for(let col = 0; col < headers.length; col++){
  742. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  743. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  744. }
  745. else {
  746. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  747. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  748. }
  749. }
  750. }
  751. sheet.resumeEvent();
  752. sheet.resumePaint();
  753. }
  754. },
  755. drowRect:function (ctx, x, y, w, h,rectW,rectH,margin) {
  756. ctx.save();
  757. ctx.strokeStyle = "gray";
  758. ctx.translate(0.5, 0.5);
  759. ctx.beginPath();
  760. let rectX = x + margin;
  761. let rectY = y + Math.round(h / 2) - rectH / 2;
  762. ctx.moveTo(rectX, rectY);
  763. ctx.lineTo(rectX, rectY + rectH);
  764. ctx.lineTo(rectX + rectW, rectY + rectH);
  765. ctx.lineTo(rectX + rectW, rectY);
  766. ctx.lineTo(rectX, rectY);
  767. ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
  768. ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
  769. ctx.stroke();
  770. ctx.restore();
  771. },
  772. drawLine : function (ctx, x1, y1, x2, y2, color) {
  773. let l_color = color?color:"gray";
  774. ctx.save();
  775. ctx.translate(0.5, 0.5);
  776. ctx.beginPath();
  777. ctx.moveTo(x1, y1);
  778. ctx.lineTo(x2, y2);
  779. ctx.strokeStyle = l_color;
  780. ctx.stroke();
  781. ctx.restore();
  782. },
  783. drowSymbol :function (ctx, x, y, w, h,rectW,rectH,margin,collapsed) {
  784. ctx.save();
  785. ctx.strokeStyle = "#000000";
  786. ctx.translate(0.5, 0.5);
  787. ctx.beginPath();
  788. ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
  789. ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
  790. let rectY = y + Math.round(h / 2) - rectH / 2;
  791. if (collapsed) {
  792. ctx.moveTo(x + margin + rectW / 2, rectY + 2);
  793. ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
  794. }
  795. ctx.stroke();
  796. ctx.restore();
  797. },
  798. drowSubItem: function (ctx, x, y, w, h, offset, hasNext,step) {
  799. let t_step = step?step:6;
  800. offset += t_step;
  801. ctx.save();
  802. ctx.strokeStyle = "gray";
  803. ctx.translate(0.5, 0.5);
  804. ctx.beginPath();
  805. ctx.moveTo(x + offset, y);
  806. ctx.lineTo(x + offset, y + Math.round(h / 2));
  807. offset += 9;
  808. ctx.lineTo(x + offset, y + Math.round(h / 2));
  809. if (hasNext) {
  810. ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
  811. ctx.lineTo(x + offset - 9, y + h);
  812. }
  813. ctx.stroke();
  814. ctx.restore();
  815. return offset;
  816. }
  817. }