简体   繁体   English

在两个函数之间传递变量

[英]Pass variable between two functions

I want to write a app which generates a spreadsheet in my google-drive with the user input data with google-app scripts. 我想编写一个应用程序,该应用程序会在Google驱动器中使用用户输入的数据以及google-app脚本在电子表格中生成电子表格。

For doing so I have several JavaScript-functions I want to execute one after the other: 为此,我要执行几个JavaScript函数,一个又一个地执行:

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> 

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

  • You want to create new Spreadsheet from HTML side. 您要从HTML端创建新的电子表格。
  • You want to put the value to the created Spreadsheet from HTML side. 您希望将值从HTML端放到创建的电子表格中。
  • In your situation, you don't want to put writeData() in createSpreadsheet() . 在您的情况下,您不想将writeData()放在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. 请认为这只是几个答案之一。

At first, the modification point is as follows. 首先,修改点如下。

Modification point: 修改点:

When you run the script as follows, 当您按如下方式运行脚本时,

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

Function of writeData() is run before the function of createSpreadsheet() is finished, because google.script.run works by the asynchronous process. createSpreadsheet()函数完成之前运行writeData()函数,因为google.script.run由异步进程运行。 In order to avoid this, in this sample script, withSuccessHandler() is used. 为了避免这种情况,在此示例脚本中,使用withSuccessHandler()

About the modified script, I introduce 2 patterns. 关于修改后的脚本,我介绍2种模式。 Please select one of them. 请选择其中之一。

Pattern 1: 模式1:

In this pattern, file ID is returned from createSpreadsheet() and the value is put to the created Spreadsheet using the file ID. 在这种模式下,文件ID从createSpreadsheet()返回,并且使用文件ID将值放入创建的电子表格中。

Code.gs: Google Apps Script 代码:Google Apps脚本

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 and Javascript 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>

Pattern 2: 模式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. 在此模式中,文件ID由PropertiesService保存,并使用从PropertiesService获得的文件ID将值放入创建的电子表格中。 In this case, the file ID is saved to PropertiesService. 在这种情况下,文件ID将保存到PropertiesService。 So the created Spreadsheet can be also used by other action of Javascript using the file ID got from PropertiesService. 因此,使用从PropertiesService获得的文件ID,创建的Spreadsheet也可以由Javascript的其他操作使用。

Code.gs: Google Apps Script 代码:Google Apps脚本

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 and Javascript 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>

References: 参考文献:

If I misunderstood your question and this answer was not what you want, I apologize. 如果我误解了您的问题,而这个答案不是您想要的,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM