在绑定到 Google 表单的脚本和绑定到其目的地 sheet 的脚本之间传递变量

Pass a variable between a script bound to a Google Form and a script bound to its destination sheet

我在从 Google 表单获取信息到 Google Sheet 时遇到问题。我希望获得编辑 url onFormSubmit,然后将其设置为存储响应的列中记录的末尾。

研究:

由于这两个必需的方法只能用于脚本或表单,所以我一直碰壁。现在我在想我可能需要一个混合解决方案,它需要一些代码绑定到 sheet,一些代码绑定到表单,并在两个执行的脚本之间传递一个变量 onFormSubmit.

这是我认为我应该保持绑定的形式

    function onFormSubmit(e)
    {
      Logger.clear; //if I can use log to pass variable I want to clear out at the beginning of each submission 

      var form = FormApp.getActiveForm();
      var activeFormUrl = form.getEditUrl();//This is the variable I need to pass to the sheet

      Logger.log(activeFormUrl); //only to confirm what we are getting unless I can somehow access the log after the fact using sheet script

    }//This is the end of onFormSubmit function bound to the Form

这是我认为我应该保持绑定的 sheet

function onFormSubmit(e)
{
  var ss = SpreadsheetApp.getActiveSheet();
  var createDateColumn = ss.getMaxColumns(); //CreateDateColumn is currently in AX (Column 50) which is the last/max column position
  var urlColumn = createDateColumn-1; //urlColumn is currently in AX (Column 50) Calculating using it's relative position to createDateColumn Position

  if (ss.getActiveRange(urlColumn).getValue() == "") // so that subsequent edits to Google Form don't overwrite editResponseURL
  {
     var editResponseURL = setGoogleFormEditUrl(ss, createDateColumn, activeFormUrl);
     var createEditResponseUrl = ss.getActiveRange(urlColumn);
     createEditResponseUrl.setValue(activeFormUrl);  
  }
  else
  {
     if (ss.getActiveRange(urlColumn).getValue() != activeFormUrl)
     { 
         Logger.log("Something went wrong - URL doesn't match" + activeFormUrl);
         Logger.log(ss.getActiveRange(urlColumn).getValue());
         var checkLog2 = Logger.getLog();
     }
     else {}//do nothing
   }   
}//This is the end of the onFormSubmit function bound to the Sheet

我需要知道的是如何从表单脚本中取出 activeFormUrl 并将其发送到 sheet 脚本。我可以使用日志吗?

我不确定这是否适合您,但您可以使用 UrlFetchApp.fetch(url) 向 Apps 脚本项目发出 HTTPS GET 或 POST 请求。因此,您可以从 Form 项目向已发布的 Web 应用程序发出 HTTPS POST 请求。已发布的 Web 应用程序实际上可以从绑定到电子表格的项目中发布,如果您愿意的话。

Apps 脚本项目检测发送给它的 HTTPS GET 或 POST 请求的方式是使用 doGet()doPost() 函数。

var webAppUrl = "https://script.google.com/macros/s/123_My_FileID/exec";

var payload = {
  "url":"activeFormUrl"
};

var options = {"method":"post","payload":payload};

UrlFetchApp.fetch(webAppUrl, options);

以上代码向另一个 Apps 脚本项目发出 POST 请求,并将负载发送到文件。

function doPost(e) {
  var theUrl = e.parameter.url;
};

我假设您正在尝试创建一个从多个表单获取数据的电子表格?

我必须将表单和电子表格操作分开,因为如果我在同一函数中使用其他 SpreadsheetApp 方法,则使用 FormApp 方法获取 formEditURL 将不起作用,而 FormApp 方法仅在 onFormSubmit 函数中起作用.

这是我成功使用的代码片段

function onFormSubmit(e) 
{
  var rng = e.range; //Collects active range for event
  var ss = SpreadsheetApp.getActiveSpreadsheet();//collects active spreadsheet object

  var fUrl = ss.getFormUrl();//gets form url linked with active spreadsheet
  var f = FormApp.openByUrl(fUrl);//opens form using form url

  var rs = f.getResponses(); //gets responses from active form 
  var r = rs[rs.length - 1]; // Get last response made on active form

  var c = getCellRngByCol(rng, 'formEditURL');  //locates the cell which the form url will be stored by searching header name
  c.setValue(r.getEditResponseUrl());// sets form url value into correct cell for active form response

  var callSpreadsheetFunctions = spreadsheetFunctions(rng, ss); //method calls other spreadsheet functions. This had to be modularized as you can't get form url if the other functions are occuring in the same function
}//This is the end of the onFormSubmit function

function spreadsheetFunctions (rng, ss)
{
  var rowIndex = rng.getRowIndex();//gets row index for current response. This is used by tracking number

  var createDateCell = getCellRngByCol(rng, 'CreateDate'); //locates which cell the createdate will be stored in by searching header name
  var timestampCell = getCellRngByCol(rng, 'Timestamp'); //locates which cell the autogenerated timestamp is located in by searching header name
  var trackingNumberCell = getCellRngByCol(rng, 'Tracking ID#');//locates which cell the tracking ID# will be stored in by searching by header name

  var createDate = setCreateDate(rng, createDateCell, timestampCell); //method sets create date. NOTE: Function not included in code snippet but left here to demonstrate type of information used

  var trackingNumber = setTrackingNumber(rng, rowIndex, trackingNumberCell, createDateCell); //method sets tracking number. NOTE: Function not included in code snippet but left here to demonstrate type of information used

  return;
} //This is the end of the callSpreadsheetFunctions function

function getCellRngByCol(rng, col)//finds the cell associated with the active range and column
{
  var aRng = SpreadsheetApp.getActiveSheet().getDataRange();//gets the spreadsheet data range
  var hRng = aRng.offset(0, 0, 1, aRng.getNumColumns()).getValues();//finds the header row range by offsetting
  var colIndex = hRng[0].indexOf(col);// declares the column index in the header row

  return SpreadsheetApp.getActiveSheet().getRange(rng.getRow(), colIndex + 1); //returns the cell range at the position of the active row and column name passed into this method
}//This is the end of the getCellRngByCol function