sheet_common.js 63 KB

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