sheet_common.js 61 KB

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