在两个函数之间传递变量 [英] Pass variable between two functions

查看:40
本文介绍了在两个函数之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个应用程序,该应用程序会在Google驱动器中使用用户输入的数据以及google-app脚本在电子表格中生成电子表格.

I want to write a app which generates a spreadsheet in my google-drive with the user input data with google-app scripts.

为此,我有几个JavaScript函数要一个接一个地执行:

For doing so I have several JavaScript-functions I want to execute one after the other:

function createSpreadsheet(name){

  var ss=SpreadsheetApp.create(name);
  var ssID = ss.getID()

}

function writeData(data){

  var s = ss.getSheets()[0];
  s.getRange('A1').setValue(data);

}

由前端脚本通过以下方式调用:

which are called by the frond-end script via:

 <script>

  function parse_to_backend(){

      var name = document.getElementById("user_name").value;
      var data = document.getElementById("user_data").value;

      google.script.run.createSpreadsheet(name);
      google.script.run.writeData(data);

  };

  </script> 

如何实现writeData知道ssID电子表格的ID (我无法使用google.script.run将值返回到前端并将其解析为参数)?

how can I achieve that the writeData knows ssID the ID of the spreadsheet (I can not return values to the frontend with google.script.run and parse it as an argument)?

推荐答案

  • 您要从HTML端创建新的电子表格.
  • 您要将值从HTML端输入到已创建的电子表格中.
  • 根据您的情况,您不想将 writeData()放在 createSpreadsheet()中.即,您要分别使用这两个功能.
    • You want to create new Spreadsheet from HTML side.
    • You want to put the value to the created Spreadsheet from HTML side.
    • In your situation, you don't want to put writeData() in createSpreadsheet(). Namely, you want to individually use both functions.
    • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个答案之一.

      If my understanding is correct, how about this answer? Please think of this as just one of several answers.

      首先,修改点如下.

      按以下方式运行脚本时,

      When you run the script as follows,

      google.script.run.createSpreadsheet(name);
      google.script.run.writeData(data);
      

      writeData()的功能在 createSpreadsheet()的功能完成之前运行,因为 google.script.run 由异步过程.为了避免这种情况,在此示例脚本中,使用了 withSuccessHandler().

      Function of writeData() is run before the function of createSpreadsheet() is finished, because google.script.run works by the asynchronous process. In order to avoid this, in this sample script, withSuccessHandler() is used.

      关于修改后的脚本,我介绍2种模式.请选择其中之一.

      About the modified script, I introduce 2 patterns. Please select one of them.

      在这种模式下,文件ID从 createSpreadsheet()返回,并使用文件ID将值放入创建的电子表格中.

      In this pattern, file ID is returned from createSpreadsheet() and the value is put to the created Spreadsheet using the file ID.

      function createSpreadsheet(name){
        var ss = SpreadsheetApp.create(name);
        var ssID = ss.getId();
        return ssID;
      }
      
      function writeData(id, data){
        var ss = SpreadsheetApp.openById(id);
        var s = ss.getSheets()[0];
        s.getRange('A1').setValue(data);
      }
      

      index.html:HTML和Javascript

      <script>
        function parse_to_backend(){
          var name = document.getElementById("user_name").value;
          var data = document.getElementById("user_data").value;
          google.script.run.withSuccessHandler((id) => {
            google.script.run.writeData(id, data);
          }).createSpreadsheet(name);
        };
      </script>
      

      模式2:

      在此模式中,文件ID由PropertiesService保存,并使用从PropertiesService获得的文件ID将该值放入创建的电子表格中.在这种情况下,文件ID将保存到PropertiesService.因此,使用从PropertiesService获得的文件ID,创建的Spreadsheet也可以被Javascript的其他操作使用.

      Pattern 2:

      In this pattern, file ID is saved by PropertiesService and the value is put to the created Spreadsheet using the file ID got from PropertiesService. In this case, the file ID is saved to PropertiesService. So the created Spreadsheet can be also used by other action of Javascript using the file ID got from PropertiesService.

      function createSpreadsheet(name){
        var ss = SpreadsheetApp.create(name);
        var ssID = ss.getId();
        PropertiesService.getScriptProperties().setProperty("ssID", ssID);
      }
      
      function writeData(data){
        var id = PropertiesService.getScriptProperties().getProperty("ssID");
        var ss = SpreadsheetApp.openById(id);
        var s = ss.getSheets()[0];
        s.getRange('A1').setValue(data);
      }
      

      index.html:HTML和Javascript

      <script>
        function parse_to_backend(){
          var name = document.getElementById("user_name").value;
          var data = document.getElementById("user_data").value;
          google.script.run.withSuccessHandler(() => {
            google.script.run.writeData(data);
          }).createSpreadsheet(name);
        };
      </script>
      

      参考文献:

      • google.script.run类
      • withSuccessHandler()
      • 类PropertiesService
      • 如果我误解了您的问题,而这个答案不是您想要的,我表示歉意.

        If I misunderstood your question and this answer was not what you want, I apologize.

        这篇关于在两个函数之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆