sheet_common.js 61 KB

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