sheet_common.js 63 KB

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