sheet_common.js 55 KB

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