sheet_common.js 54 KB

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