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

时间:2016-04-22 18:54:49

标签: google-apps-script google-sheets triggers google-form

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

研究:

  • 我问this question,这是一个绑定到工作表的脚本但是试图访问该表单。然后它成为绑定到表单的脚本,尝试访问表单。
  • 我发现this question看起来与我的问题有关(用例略有不同)。与我的类似,我认为在表单上获取电子表格方法会有问题。

由于两种必需的方法只能用于脚本或表格,所以我一直在打墙。现在我想我可能需要一个混合解决方案,需要将一些代码绑定到工作表,并将一些代码绑定到表单,并在两个脚本之间传递一个同时执行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

这就是我认为应该与表单绑定的内容

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并将其发送到工作表脚本。我可以使用日志吗?

2 个答案:

答案 0 :(得分:1)

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

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 Script项目发出POST请求,并将有效负载发送到文件。

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

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

答案 1 :(得分:0)

我不得不将表单和电子表格操作分开,因为如果我在同一个函数中使用其他SpreadsheetApp方法,那么使用FormApp方法获取formEditURL将无效,而FormApp方法只有在onFormSubmit函数中才有效。 / p>

以下是我成功使用的代码段

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