sheet_common.js 44 KB

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