sheet_common.js 57 KB

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