sheet_common.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. if (setting.emptyRowHeader) {
  35. sheet.setColumnWidth(0, 1, GC.Spread.Sheets.SheetArea.rowHeader);
  36. }
  37. sheet.options.colHeaderAutoTextIndex = 1;
  38. sheet.options.colHeaderAutoText = spreadNS.HeaderAutoText.numbers;
  39. sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  40. sheet.options.protectionOptions = {
  41. allowResizeColumns: true
  42. };
  43. sheet.showRowOutline(false);
  44. sheet.options.allowCellOverflow = false;
  45. me.buildHeader(sheet, setting);
  46. if (rowCount > 0)
  47. sheet.setRowCount(rowCount);
  48. else
  49. sheet.setRowCount(1);
  50. sheet.resumeEvent();
  51. sheet.resumePaint();
  52. },
  53. // buildSheet 在一个Spread、一个Sheet的情况下使用。
  54. buildSheet: function(container, setting, rowCount) {
  55. var me = this;
  56. var spreadBook = me.createSpread(container, { sheetCount: 1 });
  57. var sheet = spreadBook.getSheet(0);
  58. me.initSheet(sheet, setting, rowCount);
  59. return spreadBook;
  60. },
  61. buildHeader: function(sheet, setting){
  62. var me = this, ch = GC.Spread.Sheets.SheetArea.colHeader;
  63. for (var i = 0; i < setting.header.length; i++) {
  64. sheet.setValue(0, i, setting.header[i].headerName, ch);
  65. sheet.getCell(0, i, ch).wordWrap(true);
  66. sheet.setColumnWidth(i, setting.header[i].headerWidth?setting.header[i].headerWidth:100);
  67. sheet.setColumnVisible(i,setting.header[i].visible === false ? false:true);
  68. }
  69. },
  70. cleanSheet: function(sheet, setting, rowCount) {
  71. sheet.suspendPaint();
  72. sheet.suspendEvent();
  73. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  74. if (rowCount > 0) sheet.setRowCount(rowCount);
  75. sheet.clearSelection();
  76. sheet.resumeEvent();
  77. sheet.resumePaint();
  78. },
  79. cleanData: function (sheet, setting, rowCount) {
  80. sheet.suspendPaint();
  81. sheet.suspendEvent();
  82. sheet.clear(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  83. if (rowCount > 0) sheet.setRowCount(rowCount);
  84. sheet.resumeEvent();
  85. sheet.resumePaint();
  86. },
  87. setAreaAlign: function(area, hAlign, vAlign){
  88. if (!(hAlign) || hAlign === "left") {
  89. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  90. } else if (hAlign === "right") {
  91. area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
  92. } else if (hAlign === "center") {
  93. area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  94. } else {
  95. area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
  96. }
  97. if (!(vAlign) || vAlign === "center") {
  98. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  99. } else if (vAlign === "top") {
  100. area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
  101. } else if (vAlign === "bottom") {
  102. area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
  103. } else {
  104. area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
  105. }
  106. },
  107. showData: function(sheet, setting, data,distTypeTree,callback) {//这个callback是为了在showdata后还做了引起重画表格的操作,在callback里调用能提高效率
  108. var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
  109. sheet.suspendPaint();
  110. sheet.suspendEvent();
  111. //sheet.addRows(row, 1);
  112. sheet.clear(0, 0, sheet.getRowCount(), sheet.getColumnCount(), GC.Spread.Sheets.SheetArea.viewport, GC.Spread.Sheets.StorageType.data);
  113. if(sheet.getRowCount()<data.length){
  114. data.length<30? sheet.setRowCount(30):sheet.setRowCount(data.length);
  115. }else if(sheet.getRowCount()==0){
  116. sheet.setRowCount(30);
  117. }
  118. for (var col = 0; col < setting.header.length; col++) {
  119. var hAlign = "left", vAlign = "center";
  120. if (setting.header[col].hAlign) {
  121. hAlign = setting.header[col].hAlign;
  122. } else if (setting.header[col].dataType !== "String"){
  123. hAlign = "right";
  124. }
  125. vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
  126. me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
  127. if (setting.header[col].formatter) {
  128. sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
  129. }
  130. if(setting.header[col].cellType === "checkBox"||setting.header[col].cellType === "button"){//clear and reset
  131. var me = this, header = GC.Spread.Sheets.SheetArea.colHeader;
  132. sheet.deleteColumns(col,1);
  133. sheet.addColumns(col, 1);
  134. sheet.setValue(0, col, setting.header[col].headerName, header);
  135. sheet.setColumnWidth(col, setting.header[col].headerWidth?setting.header[col].headerWidth:100);
  136. }
  137. if(setting.header[col].visible!==null&&setting.header[col].visible!==undefined){
  138. sheet.setColumnVisible(col,setting.header[col].visible);
  139. }
  140. sheet.getCell(0, col, GC.Spread.Sheets.SheetArea.colHeader).wordWrap(true);
  141. }
  142. for (var row = 0; row < data.length; row++) {
  143. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  144. this.showRowData(sheet,setting,row,data,distTypeTree);
  145. if(setting.getStyle && setting.getStyle(data[row])){
  146. sheet.setStyle(row, -1, setting.getStyle(data[row]));
  147. }
  148. }
  149. if(setting.emptyRowHeader){
  150. let rowCount = sheet.getRowCount();
  151. for (let row = 0; row < rowCount; row++) {
  152. sheet.setValue(row, 0, '', GC.Spread.Sheets.SheetArea.rowHeader);
  153. }
  154. }
  155. this.lockCells(sheet,setting);
  156. if(callback) callback();
  157. sheet.resumeEvent();
  158. sheet.resumePaint();
  159. //me.shieldAllCells(sheet);
  160. },
  161. showRowData:function (sheet,setting,row,data,distTypeTree=null) {
  162. let ch = GC.Spread.Sheets.SheetArea.viewport;
  163. for (var col = 0; col < setting.header.length; col++) {
  164. //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
  165. var val = data[row][setting.header[col].dataCode];
  166. if(val&&setting.header[col].dataType === "Number"){
  167. if(setting.header[col].hasOwnProperty('tofix')){
  168. val =scMathUtil.roundToString(val,setting.header[col].tofix);
  169. }
  170. else if(setting.header[col].hasOwnProperty('decimalField')){
  171. var decimal = getDecimal(setting.header[col].decimalField);
  172. val =scMathUtil.roundToString(val,decimal);
  173. sheet.setFormatter(-1, col,getFormatter(decimal), GC.Spread.Sheets.SheetArea.viewport);
  174. }else {
  175. val =val+'';
  176. }
  177. }
  178. if(val!=null&&setting.header[col].cellType === "checkBox"){
  179. this.setCheckBoxCell(row,col,sheet,val)
  180. }
  181. if(setting.header[col].cellType === "comboBox"){
  182. this.setComboBox(row,col,sheet,setting.header[col].options,setting.header[col].editorValueType);
  183. }
  184. if(setting.header[col].cellType === "selectButton"){
  185. this.setSelectButton(row,col,sheet,setting.header[col]);
  186. }
  187. if(setting.header[col].cellType === "replaceButton"){
  188. this.setReplaceButton(row,col,sheet,setting.header[col]);
  189. }
  190. if(setting.header[col].cellType === "tipsCell"){
  191. this.setTipsCell(row,col,sheet,setting.header[col]);
  192. }
  193. if(setting.owner==='gljTree'){
  194. if(setting.header[col].cellType === "checkBox"){
  195. val==1?val:0;
  196. this.setCheckBoxCell(row,col,sheet,val);
  197. }
  198. if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
  199. let distTypeVal = distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
  200. val=distTypeVal;
  201. }
  202. }
  203. if(setting.header[col].getText){
  204. val = setting.getText[setting.header[col].getText](data[row],val)
  205. }
  206. sheet.setValue(row, col, val, ch);
  207. }
  208. this.setRowStyle(row,sheet,data[row].bgColour);
  209. if(setting.autoFit==true){//设置自动行高
  210. if(setting.fitRow && setting.fitRow.length > 0){//如果有设置特定的某些列才需要自动行高就按设置的来,没有设置就默认所有列
  211. for(let dataCode of setting.fitRow){
  212. let col = _.findIndex(setting.header,{dataCode:dataCode})
  213. sheet.getCell(row,col).wordWrap(true);
  214. }
  215. }else {
  216. sheet.getRange(row, -1, 1, -1, GC.Spread.Sheets.SheetArea.viewport).wordWrap(true);
  217. }
  218. sheet.autoFitRow(row);
  219. }
  220. },
  221. checkData : function(col,setting, value) {
  222. let result = true;
  223. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  224. if (validator === null) {
  225. return result;
  226. }
  227. switch (validator) {
  228. case 'number':
  229. let regular = /^\d+(\.\d+)?$/;
  230. result = regular.test(value);
  231. break;
  232. case 'boolean':
  233. let booleanValue = [true, false];
  234. result = booleanValue.indexOf(value) >= 0;
  235. break;
  236. }
  237. return result;
  238. },
  239. //todo
  240. analyzePasteData: function(setting, pastedInfo) {
  241. var rst = [], propId = pastedInfo.cellRange.col, preStrIdx = 0, itemObj = {};//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  242. for (var i = 0; i < pastedInfo.pasteData.text.length; i++) {
  243. if (pastedInfo.pasteData.text[i] === "\n") {
  244. propId = pastedInfo.cellRange.col;//propId = 0 to proId = pastedInfo.cellRange.col, update by zhong
  245. preStrIdx = i + 1;
  246. rst.push(itemObj);
  247. if (i < pastedInfo.pasteData.text.length - 1) {
  248. itemObj = {};
  249. }
  250. } else if (pastedInfo.pasteData.text[i] === "\t" || pastedInfo.pasteData.text[i] === "\r") {
  251. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx, i);
  252. propId++;
  253. preStrIdx = i + 1;
  254. //if the last copied-cell were empty, should check whether the end of text
  255. if (i == pastedInfo.pasteData.text.length - 1) {
  256. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  257. rst.push(itemObj);
  258. }
  259. } else if (i == pastedInfo.pasteData.text.length - 1) {
  260. itemObj[setting.header[propId].dataCode] = pastedInfo.pasteData.text.slice(preStrIdx);
  261. rst.push(itemObj);
  262. }
  263. }
  264. return rst;
  265. },
  266. combineRowData: function(sheet, setting, row) {
  267. var rst = {};
  268. for (var col = 0; col < setting.header.length; col++) {
  269. rst[setting.header[col].dataCode] = sheet.getValue(row, col);
  270. }
  271. return rst;
  272. },
  273. shieldAllCells: function(sheet) {
  274. sheet.options.isProtected = true;
  275. },
  276. unShieldAllCells: function(sheet) {
  277. sheet.options.isProtected = false;
  278. },
  279. lockCells: function(sheet, setting){
  280. if (setting && setting.view.lockColumns && setting.view.lockColumns.length > 0) {
  281. sheet.options.isProtected = true;
  282. sheet.getRange(-1, 0, -1, setting.header.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);
  283. for (var i = 0; i < setting.view.lockColumns.length; i++) {
  284. let col = setting.view.lockColumns[i];
  285. if(_.isString(col)){//如果是dataCode 进行转换
  286. col = _.findIndex(setting.header,{dataCode:col})
  287. }
  288. sheet.getRange(-1,col, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
  289. }
  290. }
  291. },
  292. setCheckBoxCell(row,col,sheet,val){
  293. var c = new GC.Spread.Sheets.CellTypes.CheckBox();
  294. c.isThreeState(false);
  295. sheet.setCellType(row, col,c,GC.Spread.Sheets.SheetArea.viewport);
  296. sheet.getCell(row, col).value(val);
  297. sheet.getCell(row, col).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
  298. },
  299. setComboBox(row,col,sheet,options,editorValueType){
  300. //let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  301. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  302. if(options){
  303. dynamicCombo.items(options);
  304. if(editorValueType==true){
  305. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  306. }
  307. }
  308. sheet.setCellType(row, col,dynamicCombo,GC.Spread.Sheets.SheetArea.viewport);
  309. },
  310. setRowStyle(row,sheet,bgColour) {
  311. if(bgColour){
  312. let style = new GC.Spread.Sheets.Style();
  313. style.backColor = bgColour;
  314. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  315. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  316. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  317. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  318. sheet.setStyle(row, -1, style);
  319. }
  320. },
  321. getCustomerCoeCellType: function (htmlGenerator,setEditorValue,updateCallback) {
  322. let me = this;
  323. function CustomerCoeCellType() {
  324. this.isEscKey=false;
  325. this.displayText='';
  326. }
  327. CustomerCoeCellType.prototype = new GC.Spread.Sheets.CellTypes.Base();
  328. CustomerCoeCellType.prototype.createEditorElement = function (context) {
  329. console.log("create editor")
  330. let element = document.createElement("div");//这里创建的,会自动销毁
  331. return element
  332. };
  333. CustomerCoeCellType.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
  334. if (editorContext) {
  335. $editor = $(editorContext);
  336. $editor.css("position", "fixed");
  337. $editor.css("background", "white");
  338. //$editor.css("width", cellRect.width); 2018-11-15 改成固定列宽
  339. $editor.css("width", 160);
  340. $editor.attr("gcUIElement", "gcEditingInput");
  341. if(htmlGenerator) htmlGenerator(context,cellRect,$editor);
  342. }
  343. }
  344. CustomerCoeCellType.prototype.deactivateEditor = function (editorContext, context) {
  345. };
  346. CustomerCoeCellType.prototype.setEditorValue = function (editor, value, context) {
  347. console.log("set editor value");
  348. this.displayText = value;
  349. };
  350. CustomerCoeCellType.prototype.getEditorValue = function (editor, context) {
  351. console.log("get value");
  352. if(this.isEscKey !=true&& updateCallback){
  353. updateCallback();
  354. }
  355. this.isEscKey = false;
  356. return this.displayText;
  357. };
  358. CustomerCoeCellType.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context) {
  359. console.log(" update editor");
  360. if( setEditorValue){//不是esc时才更新
  361. setEditorValue(context);
  362. }
  363. };
  364. CustomerCoeCellType.prototype.isReservedKey = function (e, context) {
  365. //cell type handle tab key by itself
  366. this.isEscKey = e.keyCode === GC.Spread.Commands.Key.esc;
  367. return false;
  368. };
  369. return new CustomerCoeCellType();
  370. },
  371. scrollSheetForOption:function (sheet,cxt,cellRect,row,options){
  372. let topRow = sheet.getViewportTopRow(1);
  373. if(row == topRow) return;//已经是最顶行了
  374. let length = options&&options.length>0?options.length:1;
  375. let height = cxt.canvas.height;
  376. let startY = cellRect.y+cellRect.height;//下拉框的起始显示位置
  377. let endY = startY + length * cellRect.height;//下拉框的结束显示位置
  378. if(endY <= height) return; //如果没有超出显示位置,直接返回
  379. let overRow = Math.ceil((endY - height)/cellRect.height);//超出的行数
  380. let showRow = topRow + overRow > row?row:topRow + overRow;
  381. sheet.showRow(showRow, GC.Spread.Sheets.VerticalPosition.top);
  382. },
  383. setSelectButton(row,col,sheet,header){
  384. /* let getSelectButton = function (cellWidth=100) {
  385. function moreButton() {
  386. }
  387. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  388. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  389. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  390. let buttonW = cellWidth/5;
  391. let endX = x+w-2;
  392. if(value){
  393. let textWidth = ctx.measureText(value).width;
  394. let spaceWidth = cellWidth - buttonW;
  395. let textEndX = x+2+textWidth;
  396. if(spaceWidth<textWidth){
  397. for(let i = value.length-1;i>1;i--){
  398. let newValue = value.substr(0,i);
  399. let newTestWidth = ctx.measureText(newValue).width;
  400. if(spaceWidth>newTestWidth){
  401. value = newValue;
  402. textEndX = x+2+newTestWidth;
  403. break;
  404. }
  405. }
  406. }
  407. ctx.fillText(value,textEndX,y+h-5);
  408. }
  409. //画三个点
  410. ctx.save();
  411. ctx.beginPath();
  412. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  413. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  414. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  415. ctx.fillStyle="black";//填充颜色,默认是黑色
  416. ctx.fill();//画实心圆
  417. ctx.closePath();
  418. ctx.restore();
  419. };
  420. moreButton.prototype.processMouseLeave= function (hitinfo) {
  421. let newCell = new selectButton();
  422. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  423. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  424. };
  425. function selectButton() {
  426. }
  427. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  428. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  429. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  430. };
  431. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  432. return {
  433. x: x,
  434. y: y,
  435. row: context.row,
  436. col: context.col,
  437. cellStyle: cellStyle,
  438. cellRect: cellRect,
  439. sheetArea: context.sheetArea
  440. };
  441. };
  442. selectButton.prototype.processMouseDown = function (hitinfo){
  443. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  444. let b1 = new moreButton();
  445. b1.marginLeft(cellWidth*4/5);
  446. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  447. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  448. }
  449. };
  450. return new selectButton();
  451. };*/
  452. sheet.setCellType(row, col,this.getSelectButton(header.headerWidth),GC.Spread.Sheets.SheetArea.viewport);
  453. },
  454. getSelectButton(cellWidth=100){
  455. function moreButton() {
  456. }
  457. moreButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  458. moreButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  459. GC.Spread.Sheets.CellTypes.Button.prototype.paint.call(this, ctx, value, x, y, w, h, style, options);
  460. ctx.font = '14px Calibri';
  461. let buttonW = cellWidth/5;
  462. let endX = x+w-2;
  463. if(value){
  464. let textWidth = ctx.measureText(value).width;
  465. let spaceWidth = cellWidth - buttonW;
  466. let textEndX = x+textWidth+2;
  467. if(spaceWidth<textWidth){
  468. for(let i = value.length-1;i>1;i--){
  469. let newValue = value.substr(0,i);
  470. let newTestWidth = ctx.measureText(newValue).width;
  471. if(spaceWidth>newTestWidth){
  472. value = newValue;
  473. textEndX = x+newTestWidth+2;
  474. break;
  475. }
  476. }
  477. }
  478. ctx.fillText(value,textEndX,y+h-6);
  479. }
  480. //画三个点
  481. ctx.save();
  482. ctx.beginPath();
  483. ctx.arc(endX-buttonW/2,y+h/2,1,0,360,false);
  484. ctx.arc(endX-buttonW/2-4,y+h/2,1,0,360,false);
  485. ctx.arc(endX-buttonW/2+4,y+h/2,1,0,360,false);
  486. ctx.fillStyle="black";//填充颜色,默认是黑色
  487. ctx.fill();//画实心圆
  488. ctx.closePath();
  489. ctx.restore();
  490. };
  491. moreButton.prototype.processMouseLeave= function (hitinfo) {
  492. let newCell = new selectButton();
  493. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, newCell, GC.Spread.Sheets.SheetArea.viewport);
  494. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(false);
  495. };
  496. function selectButton() {
  497. }
  498. selectButton.prototype = new GC.Spread.Sheets.CellTypes.Text();
  499. selectButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  500. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this,arguments);
  501. };
  502. selectButton.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  503. return {
  504. x: x,
  505. y: y,
  506. row: context.row,
  507. col: context.col,
  508. cellStyle: cellStyle,
  509. cellRect: cellRect,
  510. sheetArea: context.sheetArea
  511. };
  512. };
  513. selectButton.prototype.processMouseDown = function (hitinfo){
  514. if(hitinfo.sheet.getCell(hitinfo.row,hitinfo.col).locked()!=true){
  515. let b1 = new moreButton();
  516. b1.marginLeft(cellWidth*4/5);
  517. hitinfo.sheet.setCellType(hitinfo.row, hitinfo.col, b1, GC.Spread.Sheets.SheetArea.viewport);
  518. hitinfo.sheet.getCell(hitinfo.row, hitinfo.col).locked(true);
  519. }
  520. };
  521. return new selectButton();
  522. },
  523. getCusButtonCellType:function (callback,readOnly=false) {
  524. var ns = GC.Spread.Sheets;
  525. function CusButtonCellType() {
  526. }
  527. CusButtonCellType.prototype = new ns.CellTypes.Text();
  528. CusButtonCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  529. if(!readOnly){
  530. if(options.sheet.getActiveRowIndex()==options.row&&options.sheet.getActiveColumnIndex()==options.col){
  531. var image = document.getElementById('f_btn'),imageMagin = 3;
  532. var imageHeight = 15;
  533. var imageWidth = 25;
  534. var imageX = x + w - imageWidth- imageMagin, imageY = y + h / 2 - imageHeight / 2;
  535. ctx.save();
  536. ctx.fillStyle = style.backColor;
  537. ctx.fillRect(x,y,w,h);
  538. ctx.drawImage(image, imageX, imageY,imageWidth,imageHeight);
  539. ctx.beginPath();
  540. ctx.arc(imageX+imageWidth/2,imageY+imageHeight/2,1,0,360,false);
  541. ctx.arc(imageX+imageWidth/2-4,imageY+imageHeight/2,1,0,360,false);
  542. ctx.arc(imageX+imageWidth/2+4,imageY+imageHeight/2,1,0,360,false);
  543. ctx.fillStyle="black";//填充颜色,默认是黑色
  544. ctx.fill();//画实心圆
  545. ctx.closePath();
  546. ctx.restore();
  547. w = w - imageWidth - imageMagin;
  548. //这里的左对齐的,当显示的字长度超过空白地方时,要改成右对齐
  549. if(style.hAlign == 0){
  550. if(value){
  551. let textWidth = ctx.measureText(value).width;
  552. let spaceWidth = w;
  553. if(spaceWidth<textWidth){
  554. style.hAlign = 2;
  555. }
  556. }
  557. }
  558. }
  559. }
  560. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  561. };
  562. CusButtonCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  563. return {
  564. x: x,
  565. y: y,
  566. row: context.row,
  567. col: context.col,
  568. cellStyle: cellStyle,
  569. cellRect: cellRect,
  570. sheetArea: context.sheetArea
  571. };
  572. };
  573. CusButtonCellType.prototype.processMouseDown = function (hitinfo) {
  574. if(hitinfo.sheet.getActiveRowIndex()==hitinfo.row&&hitinfo.sheet.getActiveColumnIndex()==hitinfo.col){
  575. var offset=hitinfo.cellRect.x+hitinfo.cellRect.width-6;
  576. var imageWidth = 25;
  577. if(hitinfo.x<offset&&hitinfo.x>offset-imageWidth){
  578. if(!readOnly){
  579. if(callback) callback(hitinfo)
  580. }
  581. }
  582. }
  583. };
  584. return new CusButtonCellType();
  585. },
  586. setReplaceButton(row,col,sheet){
  587. let replaceButton = function(){
  588. };
  589. replaceButton.prototype = new GC.Spread.Sheets.CellTypes.Button();
  590. replaceButton.prototype.paint = function (ctx, value, x, y, w, h, style, options){
  591. GC.Spread.Sheets.CellTypes.Button.prototype.paint.apply(this,arguments);
  592. if(value){
  593. ctx.save();
  594. ctx.fillStyle = "white";
  595. let fh = options.fontInfo&&options.fontInfo.fontSize?options.fontInfo.fontSize:h-6;
  596. ctx.fillText(value,x+(w+ctx.measureText(value).width)/2,y+(h+fh)/2-2);
  597. ctx.restore();
  598. }
  599. };
  600. let cellType = new replaceButton();
  601. cellType.buttonBackColor("#07A0FF");
  602. sheet.setCellType(row, col,cellType,GC.Spread.Sheets.SheetArea.viewport);
  603. },
  604. setTipsCell(row,col,sheet,header){
  605. let TipCellType = function () {};
  606. TipCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
  607. TipCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  608. return {
  609. x: x,
  610. y: y,
  611. row: context.row,
  612. col: context.col,
  613. cellStyle: cellStyle,
  614. cellRect: cellRect,
  615. sheet: context.sheet,
  616. sheetArea: context.sheetArea
  617. };
  618. };
  619. TipCellType.prototype.processMouseEnter = function (hitinfo) {
  620. let text = hitinfo.sheet.getText(hitinfo.row, hitinfo.col);
  621. let value = hitinfo.sheet.getValue(hitinfo.row, hitinfo.col);
  622. let tag = hitinfo.sheet.getTag(hitinfo.row, hitinfo.col);
  623. let acStyle = hitinfo.sheet.getActualStyle(hitinfo.row, hitinfo.col),
  624. zoom = hitinfo.sheet.zoom();
  625. let textLength = this.getAutoFitWidth(value, text, acStyle, zoom, {sheet: hitinfo.sheet, row: hitinfo.row, col: hitinfo.col, sheetArea: GC.Spread.Sheets.SheetArea.viewport});
  626. let cellWidth = hitinfo.sheet.getCell(-1, hitinfo.col).width();
  627. let setting = {};
  628. if(textLength <= cellWidth){
  629. return;
  630. }
  631. if(sheet && sheet.getParent().qo){
  632. setting.pos = SheetDataHelper.getObjPos(sheet.getParent().qo);
  633. }
  634. TREE_SHEET_HELPER.showTipsDiv(text,setting,hitinfo);
  635. };
  636. TipCellType.prototype.processMouseLeave = function (hitinfo) {
  637. TREE_SHEET_HELPER.tipDiv = 'hide';
  638. if (TREE_SHEET_HELPER._toolTipElement) {
  639. $(TREE_SHEET_HELPER._toolTipElement).hide();
  640. TREE_SHEET_HELPER._toolTipElement = null;
  641. };
  642. TREE_SHEET_HELPER.tipDivCheck();//延时检查:当tips正在show的时候,就调用了hide方法,会导致tips一直存在,所以设置一个超时处理
  643. };
  644. sheet.setCellType(row, col,new TipCellType(),GC.Spread.Sheets.SheetArea.viewport);
  645. },
  646. chkIfEmpty: function(rObj, setting) {
  647. var rst = true;
  648. if (rObj) {
  649. for (var i = 0; i < setting.header.length; i++) {
  650. if (rObj[setting.header[i].dataCode]) {
  651. rst = false;
  652. break;
  653. }
  654. }
  655. }
  656. return rst;
  657. },
  658. //add by zhong 2017-10-10
  659. //动态下拉框,配合EnterCell, args.sheet.repaint();
  660. getDynamicCombo: function (forLocked) {
  661. let ComboCellForActiveCell = function () { };
  662. ComboCellForActiveCell.prototype = new GC.Spread.Sheets.CellTypes.ComboBox();
  663. ComboCellForActiveCell.prototype.paintValue = function (ctx, value, x, y, w, h, style, options) {
  664. let sheet = options.sheet;
  665. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  666. GC.Spread.Sheets.CellTypes.ComboBox.prototype.paintValue.apply(this, arguments);
  667. } else {
  668. GC.Spread.Sheets.CellTypes.Base.prototype.paintValue.apply(this, arguments);
  669. }
  670. };
  671. ComboCellForActiveCell.prototype.getHitInfo = function (x, y, cellStyle, cellRect, options) {
  672. let sheet = options.sheet;
  673. if (options.row === sheet.getActiveRowIndex() && options.col === sheet.getActiveColumnIndex() && (!forLocked || forLocked && !sheet.getCell(options.row, options.col).locked())) {
  674. return GC.Spread.Sheets.CellTypes.ComboBox.prototype.getHitInfo.apply(this, arguments);
  675. } else {
  676. return {
  677. x: x,
  678. y: y,
  679. row: options.row,
  680. col: options.col,
  681. cellStyle: cellStyle,
  682. cellRect: cellRect,
  683. sheetArea: options.sheetArea
  684. };//GC.Spread.Sheets.CellTypes.Text.prototype.getHitInfo.apply(this, arguments);
  685. }
  686. };
  687. return new ComboCellForActiveCell();
  688. },
  689. getTipsCombo:function (forLocked,tips,setting,node) {
  690. let getTipsCombo = function () {
  691. this.clickCom=false;
  692. };
  693. getTipsCombo.prototype = sheetCommonObj.getDynamicCombo(forLocked);
  694. if(tips && tips !=""){
  695. getTipsCombo.prototype.processMouseEnter = function(hitinfo){
  696. if(this.clickCom == true){ //点击了下拉框的三角形,则不用再显示悬浮框了
  697. this.clickCom = false;
  698. return;
  699. }
  700. let text = typeof tips == 'function'?tips(node):tips;
  701. TREE_SHEET_HELPER.delayShowTips(hitinfo,setting,text);
  702. };
  703. getTipsCombo.prototype.processMouseLeave = function (hitinfo) {
  704. TREE_SHEET_HELPER.hideTipsDiv();
  705. };
  706. getTipsCombo.prototype.processMouseDown = function (hitinfo){
  707. if(hitinfo.isReservedLocation == true){//这里是点击了下拉框的三角形才会有这个属性
  708. TREE_SHEET_HELPER.hideTipsDiv();
  709. this.clickCom = true;
  710. }
  711. GC.Spread.Sheets.CellTypes.ComboBox.prototype.processMouseDown.apply(this, arguments);
  712. };
  713. getTipsCombo.prototype.updateEditor = function (editorContext, cellStyle, cellRect, context){
  714. TREE_SHEET_HELPER.hideTipsDiv();
  715. GC.Spread.Sheets.CellTypes.ComboBox.prototype.updateEditor.apply(this, arguments);
  716. };
  717. }
  718. return new getTipsCombo();
  719. },
  720. getTreeNodeCellType:function (datas,row,parentMap) {// 2018-09-26 不用spreadjs默认的树结构,自定义控件
  721. var ns = GC.Spread.Sheets;
  722. let rectW = 10;
  723. let rectH = 10;
  724. let margin = 3;
  725. function TreeNodeCellType() {
  726. this.collapsed = gljUtil.isDef(datas[row].collapsed)?datas[row].collapsed: true; //默认是折叠的
  727. this. treeNodeType = true;
  728. this.rectInfo = {};
  729. }
  730. TreeNodeCellType.prototype = new ns.CellTypes.Text();
  731. TreeNodeCellType.prototype.paint = function (ctx, value, x, y, w, h, style, options) {
  732. let offset = 0;
  733. let step = 7;
  734. let level = getTreeLevel(datas[row],datas);//从0开始,取当前节点是第几级的
  735. let tem = offset+margin+ rectW/2+step;//两条线之间的间隔
  736. let t_offset = offset;
  737. let temParentID = datas[row].ParentID;
  738. if(this.treeNodeType == true){
  739. for(let i = level;i>0;i--){//这里是画子节点前面的竖线,从第二级开始
  740. let temParent = getParent(temParentID,datas);
  741. if(temParent){//父节点有下一个兄弟节点才需要画
  742. if(hasNextBrother(parentMap,temParent)) sheetCommonObj.drawLine(ctx,x+t_offset+tem*i,y,x+t_offset+tem*i,y+h);
  743. temParentID = temParent.ParentID;
  744. }
  745. offset +=tem;
  746. }
  747. }
  748. offset+=step; //这个没法移动,所以要两个判断
  749. if(this.treeNodeType == true){
  750. if(hasChildern(datas[row].ID,datas)){//如果是有子节点
  751. //第一条 或者没有父节点(如费率子表综合里程项)不用画方框头上那条竖线其它都要
  752. if(row !=0 && gljUtil.isDef(datas[row].ParentID)) sheetCommonObj.drawLine(ctx,x+offset+ rectW/2+margin,y,x+offset+ rectW/2+margin,y + Math.round(h / 2) - rectH / 2);
  753. //画方框下面的那条竖线,如果没有下一个兄弟节点,则不用画
  754. if(hasNextBrother(parentMap,datas[row])) sheetCommonObj.drawLine(ctx,x+offset+ rectW/2+margin,y + Math.round(h / 2) + rectH / 2,x+offset+ rectW/2+margin,y + h);
  755. sheetCommonObj.drowRect(ctx, x+offset, y, w, h,rectW,rectH,margin);
  756. sheetCommonObj.drowSymbol(ctx, x+offset, y, w, h,rectW,rectH,margin, this.collapsed);
  757. this.rectInfo = {x:x+offset+margin,rectW:rectW}//计录一下可点击位置
  758. }else {
  759. let hasNext = datas[row+1]?datas[row+1].ParentID == datas[row].ParentID:false;
  760. sheetCommonObj.drowSubItem(ctx, x, y, w, h, offset,hasNext,margin+ rectW/2);
  761. }
  762. }
  763. offset += step;
  764. offset += rectW;
  765. x = x + offset;//设置偏移
  766. w = w - offset;
  767. GC.Spread.Sheets.CellTypes.Text.prototype.paint.apply(this, arguments);
  768. };
  769. // override getHitInfo to allow cell type get mouse messages
  770. TreeNodeCellType.prototype.getHitInfo = function (x, y, cellStyle, cellRect, context) {
  771. return {
  772. x: x,
  773. y: y,
  774. row: context.row,
  775. col: context.col,
  776. cellStyle: cellStyle,
  777. cellRect: cellRect,
  778. sheetArea: context.sheetArea
  779. };
  780. }
  781. TreeNodeCellType.prototype.processMouseDown = function (hitinfo) {
  782. ////方框外1像素内都有效
  783. if (!_.isEmpty(this.rectInfo)&&Math.floor(hitinfo.x) <= this.rectInfo.x+this.rectInfo.rectW+2 && Math.floor(hitinfo.x) >= this.rectInfo.x-2) {
  784. this.collapsed = !this.collapsed;
  785. datas[row].collapsed = this.collapsed;
  786. this.refreshChildrenVisible(hitinfo.sheet);
  787. hitinfo.sheet.invalidateLayout();
  788. hitinfo.sheet.repaint();
  789. }
  790. };
  791. TreeNodeCellType.prototype.refreshChildrenVisible = function (sheet) {
  792. sheet.suspendPaint();
  793. sheet.suspendEvent();
  794. refreshVisible(datas[row]);
  795. sheet.resumeEvent();
  796. sheet.resumePaint();
  797. function refreshVisible(item){
  798. if(parentMap[item.ID]){
  799. for(let sub of parentMap[item.ID]){
  800. refreshVisible(sub)
  801. }
  802. }
  803. let visible = getVisible(item);
  804. let trow = datas.indexOf(item);
  805. sheet.getRange(trow , -1, 1, -1).visible(visible);
  806. }
  807. function getVisible(item) {
  808. if(item.ParentID){
  809. let parent = getParent(item.ParentID,datas);
  810. if(!parent) return true;
  811. let p_row= datas.indexOf(parent);
  812. let visible = !sheet.getCellType(p_row,0).collapsed;
  813. if(visible == true){ //如果是显示的,则要再往父节点的父节点检查,只要有一个节点是隐藏的,则都是隐藏
  814. return getVisible(parent);
  815. }else {
  816. return visible
  817. }
  818. }else {//如果parentID 为空则是最根节点
  819. return true;
  820. }
  821. }
  822. };
  823. return new TreeNodeCellType()
  824. function getTreeLevel(item,data) {
  825. if(item.ParentID){
  826. let pitem = _.find(data,{'ID':item.ParentID});
  827. return getTreeLevel(pitem,data) + 1;
  828. }else {
  829. return 0
  830. }
  831. }
  832. function hasChildern(ID,data) {//返回是否有子项
  833. let p = _.find(data,{'ParentID':ID});
  834. if(p) return true;
  835. return false
  836. }
  837. function getParent(ParentID,data) {
  838. let p = _.find(data,{'ID':ParentID});
  839. return p;
  840. }
  841. function hasNextBrother(parentMap,item){
  842. let children =parentMap[item.ParentID];
  843. if(!gljUtil.isDef(children)|| children.indexOf(item) == children.length -1) return false;
  844. return true
  845. }
  846. },
  847. setDynamicCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  848. let me = this;
  849. sheet.suspendPaint();
  850. let combo = me.getDynamicCombo();
  851. for(let i = 0, len = rowCount; i < len; i++){
  852. if(itemsHeight) {
  853. combo.itemHeight(itemsHeight);
  854. combo._maxDropDownItems = itemsHeight + 5;
  855. }
  856. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  857. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  858. else combo.items(items);
  859. sheet.getCell(beginRow + i, col).cellType(combo);
  860. }
  861. sheet.resumePaint();
  862. },
  863. setStaticCombo: function (sheet, beginRow, col, rowCount, items, itemsHeight, itemsType) {
  864. sheet.suspendPaint();
  865. let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  866. for(let i = 0, len = rowCount; i < len; i++){
  867. if(itemsHeight) combo.itemHeight(itemsHeight);
  868. if(itemsType === 'value') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  869. else if(itemsType === 'text') combo.items(items).editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  870. else combo.items(items);
  871. sheet.getCell(beginRow + i, col).cellType(combo);
  872. }
  873. sheet.resumePaint();
  874. },
  875. //设置系统粘贴板数据,需要用户触发事件,直接调用会失败
  876. copyTextToClipboard: function(text) {
  877. let textArea = document.createElement("textarea");
  878. textArea.style.position = 'fixed';
  879. textArea.style.top = 0;
  880. textArea.style.left = 0;
  881. textArea.style.width = '2em';
  882. textArea.style.height = '2em';
  883. textArea.style.padding = 0;
  884. textArea.style.border = 'none';
  885. textArea.style.outline = 'none';
  886. textArea.style.boxShadow = 'none';
  887. textArea.style.background = 'transparent';
  888. textArea.value = text;
  889. document.body.appendChild(textArea);
  890. textArea.select();
  891. try {
  892. let successful = document.execCommand('copy');
  893. let msg = successful ? 'successful' : 'unsuccessful';
  894. console.log('Copying text command was ' + msg);
  895. } catch (err) {
  896. console.log('Oops, unable to copy');
  897. }
  898. document.body.removeChild(textArea);
  899. },
  900. //获取选中区域的表格类型数据(可粘贴到excel)
  901. getTableData: function (sheet, colSettings = null) {
  902. let rst = '';
  903. let sel = sheet.getSelections()[0];
  904. let pasteText = [];
  905. for(let row = sel.row; row < sel.row + sel.rowCount; row++){
  906. if(!sheet.getCell(row, -1).visible())
  907. continue;
  908. let rowText = [];
  909. for(let j = 0; j < sel.colCount; j++){
  910. let col = sel.col + j;
  911. if(!sheet.getCell(-1, col).visible())
  912. continue;
  913. if(colSettings && (colSettings[col]['data']['field'] === 'itemCharacterText' || colSettings[col]['data']['field'] === 'jobContentText'))
  914. rowText.push(sheet.getText(row, col) ? `"${sheet.getText(row, col)}"` : '');
  915. else
  916. rowText.push(sheet.getText(row, col) ? sheet.getText(row, col): '');
  917. }
  918. pasteText.push(rowText.join('\t'));
  919. }
  920. return pasteText.join('\n');
  921. },
  922. transferToTreeSetting:function(setting,treeSetting,treeCol){
  923. for(let h of setting.header){
  924. treeSetting.cols.push(getSettingCol(h))
  925. }
  926. for(let l of setting.view.lockColumns){
  927. treeSetting.cols[l].readOnly = true;
  928. }
  929. return treeSetting;
  930. function getSettingCol(header) {
  931. let aMap ={left:0,center:1,right:2};
  932. let hAlign = header.hAlign?aMap[header.hAlign]:0;
  933. let col = {
  934. "width":header.headerWidth?header.headerWidth:100,
  935. "head":{
  936. "titleNames":Array.isArray(header.headerName)?header.headerName:[header.headerName],
  937. "spanCols":header.spanCols?header.spanCols:[1],
  938. "spanRows":header.spanRows?header.spanRows:[1],
  939. "vAlign":[1],
  940. "hAlign":[1],
  941. "font":["Arial"]
  942. },
  943. "data": {
  944. "field": header.dataCode,
  945. "vAlign": 1,
  946. "hAlign": hAlign,
  947. "font": "Arial"
  948. }
  949. };
  950. if(header.showHint == true){
  951. col.showHint = true;
  952. }
  953. if(header.cellType){
  954. col.data.cellType = getCellType(header);
  955. }
  956. if(header.decimalField){//设置formatter
  957. let decimal = getDecimal(header.decimalField);
  958. col.formatter = getFormatter(decimal);
  959. }
  960. if(header.getText && treeCol){
  961. col.data.getText = treeCol.getEvent(header.getText);
  962. }
  963. /*col.readOnly = function (node) {
  964. if(node.data.ParentID == -1 || node.data.id == 'GJ'){//三材类别项不能编辑)
  965. return true;
  966. }
  967. return false;
  968. };*/
  969. return col;
  970. }
  971. function getCellType(header) {
  972. return function () {
  973. if(header.cellType === "checkBox"){
  974. return new GC.Spread.Sheets.CellTypes.CheckBox();
  975. }
  976. if(header.cellType === "comboBox"){
  977. let dynamicCombo = sheetCommonObj.getDynamicCombo(true);
  978. if(header.options){
  979. dynamicCombo.itemHeight(header.options.length).items(header.options);
  980. if(header.editorValueType==true){
  981. dynamicCombo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
  982. }
  983. }
  984. return dynamicCombo
  985. }
  986. }
  987. }
  988. },
  989. //注册自定义回车键事件
  990. bindEnterKey: function (workBook, operation) {
  991. workBook.commandManager().register('myEnter', operation);
  992. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.enter, false, false, false, false);
  993. workBook.commandManager().setShortcutKey('myEnter', GC.Spread.Commands.Key.enter, false, false, false, false);
  994. },
  995. //解决esc后触发了编辑结束的保存事件,显示与实际数据不同问题
  996. bindEscKey: function (workBook, sheets) {
  997. function isDef(v){
  998. return typeof v !== 'undefined' && v !== null;
  999. }
  1000. workBook.commandManager().register('myEsc', function () {
  1001. let activeSheet = workBook.getActiveSheet();
  1002. let hasTheSheet = false;
  1003. for(let sheetObj of sheets){
  1004. let sheet = sheetObj.sheet;
  1005. if(sheet === activeSheet){
  1006. hasTheSheet = true;
  1007. let editStarting = sheetObj.editStarting;
  1008. let editEnded = sheetObj.editEnded;
  1009. if(editStarting){
  1010. sheet.unbind(GC.Spread.Sheets.Events.EditStarting);
  1011. }
  1012. if(editEnded){
  1013. sheet.unbind(GC.Spread.Sheets.Events.EditEnded);
  1014. }
  1015. let row = sheet.getActiveRowIndex();
  1016. let col = sheet.getActiveColumnIndex();
  1017. let orgV = sheet.getValue(row, col);
  1018. let orgText = sheet.getText(row, col);
  1019. if(!isDef(orgV)){
  1020. orgV = '';
  1021. }
  1022. if(sheet.isEditing()){
  1023. sheet.endEdit();
  1024. sheet.setValue(row, col, orgV);
  1025. }
  1026. if(editStarting){
  1027. sheet.bind(GC.Spread.Sheets.Events.EditStarting, editStarting);
  1028. }
  1029. if(editEnded){
  1030. sheet.bind(GC.Spread.Sheets.Events.EditEnded, editEnded);
  1031. }
  1032. }
  1033. }
  1034. //容错处理,以防没把所有工作簿的表格信息传入参数
  1035. if(!hasTheSheet){
  1036. if(activeSheet.isEditing()){
  1037. activeSheet.endEdit();
  1038. }
  1039. }
  1040. });
  1041. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.esc, false, false, false, false);
  1042. workBook.commandManager().setShortcutKey('myEsc', GC.Spread.Commands.Key.esc, false, false, false, false);
  1043. },
  1044. //设置默认样式
  1045. spreadDefaultStyle: function (workBook) {
  1046. let defaultStyle = new GC.Spread.Sheets.Style();
  1047. defaultStyle.font = '14px Calibri';
  1048. let sheetCount = workBook.getSheetCount();
  1049. for(let i = 0; i < sheetCount; i++){
  1050. let sheet = workBook.getSheet(i);
  1051. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
  1052. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.colHeader);
  1053. sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.rowHeader);
  1054. }
  1055. },
  1056. //动态根据工作簿宽度和各列宽度比例设置宽度
  1057. setColumnWidthByRate: function (workBookWidth, workBook, headers){
  1058. if(workBook){
  1059. const sheet = workBook.getActiveSheet();
  1060. sheet.suspendEvent();
  1061. sheet.suspendPaint();
  1062. for(let col = 0; col < headers.length; col++){
  1063. if(headers[col]['rateWidth'] !== undefined && headers[col]['rateWidth'] !== null && headers[col]['rateWidth'] !== ''){
  1064. sheet.setColumnWidth(col, workBookWidth * headers[col]['rateWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1065. }
  1066. else {
  1067. if(headers[col]['headerWidth'] !== undefined && headers[col]['headerWidth'] !== null && headers[col]['headerWidth'] !== ''){
  1068. sheet.setColumnWidth(col, headers[col]['headerWidth'], GC.Spread.Sheets.SheetArea.colHeader)
  1069. }
  1070. }
  1071. }
  1072. sheet.resumeEvent();
  1073. sheet.resumePaint();
  1074. }
  1075. },
  1076. drowRect:function (ctx, x, y, w, h,rectW,rectH,margin) {
  1077. ctx.save();
  1078. ctx.strokeStyle = "gray";
  1079. ctx.translate(0.5, 0.5);
  1080. ctx.beginPath();
  1081. let rectX = x + margin;
  1082. let rectY = y + Math.round(h / 2) - rectH / 2;
  1083. ctx.moveTo(rectX, rectY);
  1084. ctx.lineTo(rectX, rectY + rectH);
  1085. ctx.lineTo(rectX + rectW, rectY + rectH);
  1086. ctx.lineTo(rectX + rectW, rectY);
  1087. ctx.lineTo(rectX, rectY);
  1088. ctx.moveTo(rectX + rectW, y + Math.round(h / 2));
  1089. ctx.lineTo(rectX + rectW + 5, y + Math.round(h / 2));
  1090. ctx.stroke();
  1091. ctx.restore();
  1092. },
  1093. drawLine : function (ctx, x1, y1, x2, y2, color) {
  1094. let l_color = color?color:"gray";
  1095. ctx.save();
  1096. ctx.translate(0.5, 0.5);
  1097. ctx.beginPath();
  1098. ctx.moveTo(x1, y1);
  1099. ctx.lineTo(x2, y2);
  1100. ctx.strokeStyle = l_color;
  1101. ctx.stroke();
  1102. ctx.restore();
  1103. },
  1104. drowSymbol :function (ctx, x, y, w, h,rectW,rectH,margin,collapsed) {
  1105. ctx.save();
  1106. ctx.strokeStyle = "#000000";
  1107. ctx.translate(0.5, 0.5);
  1108. ctx.beginPath();
  1109. ctx.moveTo(x + margin + 2, y + Math.round(h / 2));
  1110. ctx.lineTo(x + margin + 8, y + Math.round(h / 2));
  1111. let rectY = y + Math.round(h / 2) - rectH / 2;
  1112. if (collapsed) {
  1113. ctx.moveTo(x + margin + rectW / 2, rectY + 2);
  1114. ctx.lineTo(x + margin + rectW / 2, rectY + 2 + 6);
  1115. }
  1116. ctx.stroke();
  1117. ctx.restore();
  1118. },
  1119. drowTriangle:function(ctx,x,y){//画向下三角形
  1120. ctx.save();
  1121. ctx.fillStyle = "black";
  1122. ctx.beginPath();
  1123. ctx.moveTo(x, y);
  1124. ctx.lineTo(x-3, y -6);
  1125. ctx.lineTo(x+3, y -6);
  1126. ctx.fill();
  1127. ctx.restore();
  1128. },
  1129. drowSubItem: function (ctx, x, y, w, h, offset, hasNext,step) {
  1130. let t_step = step?step:6;
  1131. offset += t_step;
  1132. ctx.save();
  1133. ctx.strokeStyle = "gray";
  1134. ctx.translate(0.5, 0.5);
  1135. ctx.beginPath();
  1136. ctx.moveTo(x + offset, y);
  1137. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1138. offset += 9;
  1139. ctx.lineTo(x + offset, y + Math.round(h / 2));
  1140. if (hasNext) {
  1141. ctx.moveTo(x + offset - 9, y + Math.round(h / 2));
  1142. ctx.lineTo(x + offset - 9, y + h);
  1143. }
  1144. ctx.stroke();
  1145. ctx.restore();
  1146. return offset;
  1147. },
  1148. checkData:function(col,setting, value) {
  1149. let result = true;
  1150. let validator = setting.header[col].validator !== undefined ? setting.header[col].validator : null;
  1151. if (validator === null) {
  1152. return result;
  1153. }
  1154. switch (validator) {
  1155. case 'number':
  1156. let regular = /^\d+(\.\d+)?$/;
  1157. result = regular.test(value);
  1158. break;
  1159. case 'boolean':
  1160. let booleanValue = [true, false];
  1161. result = booleanValue.indexOf(value) >= 0;
  1162. break;
  1163. }
  1164. return result;
  1165. }
  1166. }