找回密码
 立即注册

QQ登录

只需一步,快速开始

mabh

注册会员

5

主题

7

帖子

49

积分

注册会员

积分
49
mabh
注册会员   /  发表于:2018-12-3 15:05  /   查看:2431  /  回复:3
本帖最后由 mabh 于 2018-12-3 15:40 编辑

问题描述:   1. 我定义了函数 =MABH(232,34,34,454)   参数值是逗号分割字符
    2. 单元格输入完成会把当前单元格转换成 下拉列表。现在已经完成了。
   3.处于下拉列表的单元格 ,双击自动转换成 公式单元格的编辑状态。现在转换成功了,但是转换完成后,需要点击别的框,才会变成文本输入框,而且内容值也没有了。




  1. <!DOCTYPE >
  2. <html>
  3.         <head>
  4.                 <meta name="spreadjs culture" content="zh-cn" />
  5.                 <meta charset="utf-8" />
  6.                 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7.                 <link rel="styleSheet" href="gc.spread.sheets.excel2013white.12.0.0.css" />
  8.                 <script src="TutorialSample/js/jquery-1.8.2.min.js"></script>
  9.                 <script src="gc.spread.sheets.all.12.0.0.min.js" type="text/javascript"></script>
  10.                 <script src="TutorialSample/js/FileSaver.js"></script>
  11.                 <script src="TutorialSample/js/spread/plugins/gc.spread.excelio.12.0.0.min.js"></script>
  12.                 <script src="gc.spread.sheets.resources.zh.12.0.0.min.js" type="text/javascript"></script>


  13.                 <style type="text/css">
  14.                         .sample-tutorial {
  15.                                 position: relative;
  16.                                 height: 100%;
  17.                                 overflow: hidden;
  18.                         }

  19.                         .sample-spreadsheets {
  20.                                 width: calc(100% - 280px);
  21.                                 height: 100%;
  22.                                 overflow: hidden;
  23.                                 float: left;
  24.                         }

  25.                         .options-container {
  26.                                 float: right;
  27.                                 width: 280px;
  28.                                 padding: 12px;
  29.                                 height: 100%;
  30.                                 box-sizing: border-box;
  31.                                 background: #fbfbfb;
  32.                                 overflow: auto;
  33.                         }

  34.                         .sample-options {
  35.                                 z-index: 1000;
  36.                         }

  37.                         .inputContainer {
  38.                                 width: 100%;
  39.                                 height: auto;
  40.                                 border: 1px solid #eee;
  41.                                 padding: 6px 12px;
  42.                                 margin-bottom: 10px;
  43.                                 box-sizing: border-box;
  44.                         }

  45.                         .input {
  46.                                 font-size: 14px;
  47.                                 height: 20px;
  48.                                 border: 0;
  49.                                 outline: none;
  50.                                 background: transparent;
  51.                         }

  52.                         .button {
  53.                                 height: 30px;
  54.                                 padding: 6px 12px;
  55.                                 width: 80px;
  56.                                 margin-top: 6px;
  57.                         }

  58.                         .group {
  59.                                 padding: 12px;
  60.                         }

  61.                         .group input {
  62.                                 padding: 4px 12px;
  63.                         }
  64.                 </style>

  65.         </head>

  66.         <body style='font-size:14px;'>
  67.                 <div class="sample-tutorial">
  68.                         <div id="ss" style="width:100%; height:360px;border: 1px solid gray;"></div>
  69.                         <div class="options-container">
  70.                                 <div class="option-row">
  71.                                         <div class="inputContainer">
  72.                                                 <input type="file" id="fileDemo" class="input">
  73.                                                 <input type="button" id="loadExcel" value="import" class="button">
  74.                                         </div>
  75.                                         <div class="inputContainer">
  76.                                                 <input id="exportFileName" value="export.xlsx" class="input">
  77.                                                 <input type="button" id="saveExcel" value="export" class="button">
  78.                                         </div>
  79.                                 </div>
  80.                                 <div class="option-row">
  81.                                         <div class="group">
  82.                                                 <label>Password:
  83.                                                         <input type="password" id="password">
  84.                                                 </label>
  85.                                         </div>
  86.                                 </div>
  87.                         </div>
  88.                 </div>
  89.                 <script type="text/javascript">
  90.                         var spread;
  91.                         var excelIo;
  92.                         var sheet;
  93.                         window.onload = function() {
  94.                                 //, { sheetCount: 1 }  导入execel去掉这个参数
  95.                                 spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {
  96.                                         sheetCount: 1
  97.                                 });
  98.                                 excelIo = new GC.Spread.Excel.IO();

  99.                                 //导入要用活动的 sheet
  100.                                 // sheet = spread.getActiveSheet();
  101.                                 sheet = spread.getSheet(0);
  102.                                 myFN();
  103.                         };

  104.                         var curRow, curCol;

  105.                         function myFN() {
  106.                                 //自定义函数
  107.                                 function FactorialFunction() {
  108.                                         this.name = "MABH";
  109.                                         this.maxArgs = 5;
  110.                                         this.minArgs = 1;
  111.                                 }
  112.                                 FactorialFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
  113.                                 FactorialFunction.prototype.evaluate = function(arg) {
  114.                                         if (arguments.length >= 1) {
  115.                                                 var items = [];
  116.                                                 for (var i = 0; i < arguments.length; i++) {
  117.                                                         items.push({
  118.                                                                 text: arguments[i],
  119.                                                                 value: arguments[i]
  120.                                                         });
  121.                                                 }
  122.                                                 return setComboBox(items, this.name);
  123.                                         }
  124.                                         return "#VALUE";
  125.                                 };
  126.                                 var factorial = new FactorialFunction();
  127.                                 sheet.addCustomFunction(factorial);

  128.                                 sheet.bind(GC.Spread.Sheets.Events.CellDoubleClick, function(sender, args) {
  129.                                         curRow = args.row;
  130.                                         curCol = args.col;
  131.                                         if (sender.type == 'CellDoubleClick') {
  132.                                                 var cellType = sheet.getCellType(args.row, args.col, GC.Spread.Sheets.SheetArea.viewport);
  133.                                                 if (cellType instanceof GC.Spread.Sheets.CellTypes.ComboBox) {
  134.                                                         var texts = [];
  135.                                                         for (var i = 0; i < cellType._items.length; i++) {
  136.                                                                 texts.push(cellType._items[i].text);
  137.                                                         }
  138.                                                         var type = new GC.Spread.Sheets.CellTypes.Text();
  139.                                                         sheet.setCellType(args.row, args.col, type ,GC.Spread.Sheets.SheetArea.viewport);
  140.                                                         
  141.                                                         var value = '=' + cellType.factorialName + '(' + texts.join(',') + ')';
  142.                                                         sheet.setValue(args.row, args.col, "====", GC.Spread.Sheets.SheetArea.viewport);
  143.                                                 }
  144.                                         }
  145.                                 });

  146.                         }

  147.                         function setComboBox(items, factorialName) {
  148.                                 var combo = new GC.Spread.Sheets.CellTypes.ComboBox();
  149.                                 combo.factorialName = factorialName;
  150.                                 sheet.setCellType(curRow, curCol, combo, GC.Spread.Sheets.SheetArea.viewport);
  151.                                 combo.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.text);
  152.                                 combo.items(items);
  153.                                 editableCombo.editable(false);
  154.                                 combo.itemHeight(24);
  155.                                 return combo;
  156.                         }

  157.                         function importOrExport() {
  158.                                 document.getElementById('loadExcel').onclick = function() {
  159.                                         var excelFile = document.getElementById("fileDemo").files[0];
  160.                                         var password = document.getElementById('password').value;
  161.                                         // here is excel IO API
  162.                                         excelIo.open(excelFile, function(json) {
  163.                                                 var workbookObj = json;
  164.                                                 spread.fromJSON(workbookObj);
  165.                                         }, function(e) {
  166.                                                 // process error
  167.                                                 alert(e.errorMessage);
  168.                                                 if (e.errorCode === 2 /*noPassword*/ || e.errorCode === 3 /*invalidPassword*/ ) {
  169.                                                         document.getElementById('password').onselect = null;
  170.                                                 }
  171.                                         }, {
  172.                                                 password: password
  173.                                         });
  174.                                 };
  175.                                 document.getElementById('saveExcel').onclick = function() {

  176.                                         var fileName = document.getElementById('exportFileName').value;
  177.                                         var password = document.getElementById('password').value;
  178.                                         if (fileName.substr(-5, 5) !== '.xlsx') {
  179.                                                 fileName += '.xlsx';
  180.                                         }

  181.                                         var json = spread.toJSON();

  182.                                         // here is excel IO API
  183.                                         excelIo.save(json, function(blob) {
  184.                                                 saveAs(blob, fileName);
  185.                                         }, function(e) {
  186.                                                 // process error
  187.                                                 console.log(e);
  188.                                         }, {
  189.                                                 password: password
  190.                                         });

  191.                                 };
  192.                         }
  193.                 </script>
  194.         </body>
  195. </html>
复制代码


3 个回复

倒序浏览
mabh
注册会员   /  发表于:2018-12-3 17:59:33
推荐
KevinChen 发表于 2018-12-3 17:41
您好,看了您的代码,觉得您这个思路可能有问题,也没明白您到底要实现一个怎样的功能。

麻烦您描述一下 ...

需求:
我要自定义个公式  例如代码中的 MABH(123,456,789)  参数是逗号分割的。之后我会根据这个做个处理生成下拉列表。现在这一步已完成,但是我从下拉列表单元格,双击切换到编辑公式状态时 和 编辑完公式再切换到下拉列表时,有问题。
回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2018-12-3 17:41:10
沙发
您好,看了您的代码,觉得您这个思路可能有问题,也没明白您到底要实现一个怎样的功能。

麻烦您描述一下您具体的使用场景,具体需求。

也许我们可以给您提供一个更好的实现思路。
回复 使用道具 举报
KevinChen讲师达人认证 悬赏达人认证 SpreadJS 开发认证
论坛元老   /  发表于:2018-12-4 11:48:22
地板
您好,您要实现的双击切换编辑公式状态,可以用自定义单元格类型来实现。

我贴一个Demo,Demo中展示了如何自定义单元格。

CustomCellType.html

4.36 KB, 下载次数: 65

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部