sheet_common.js 73 KB

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