sheet_common.js 62 KB

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