sheet_common.js 54 KB

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