sheet_common.js 40 KB

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