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