sheet_common.js 60 KB

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