sheet_common.js 60 KB

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