通过谷歌脚本提交表单

时间:2017-05-28 05:40:40

标签: javascript google-apps-script google-sheets google-apps

我需要在Google脚本中提交表单,但会收到此错误:

  

TypeError:无法调用未定义的方法“withItemResponse”

根据以下链接,这是应该如何设置https://developers.google.com/apps-script/reference/forms/form#createResponse()

代码:

 //Submit form

  var formID = row[24]; 

  var form = FormApp.openById(formID);
  Logger.log(form.getId());  //returns correct ID
  form.createResponse() ;
  form.FormResponse.withItemResponse('Core Teachers', logSummary);  
  //form has only two questions, a short text and a paragraph text
    form.FormResponse.submit(); 

1 个答案:

答案 0 :(得分:3)

form.createResponse()会返回FormResponse,您需要将其分配给变量。

同样,withItemResponse()期望类型为ItemResponse的对象。我不熟悉谷歌表格,但也许这会让你朝着正确的方向前进:

var formID = row[24]; 
var form = FormApp.openById(formID);
var formResponse = form.createResponse();
// get items of form and loop through
var items = form.getItems();
for (index = 0; index < a.length; ++index) {
  var item = items[index]
  // Cast the generic item to the text-item class. You will likely have to adjust this part. You can find the item classes in the documentation. https://developers.google.com/apps-script/reference/forms/item-type.
  if (item.getType() == 'TEXT') {
    var textItem = item.asTextItem();
    var itemresponse = textItem.createResponse('Core Teachers');
    formResponse.withItemResponse(itemresponse);  
  }
}
formResponse.submit();

通常,当方法的文档列为参数类型而不是像String或Boolean这样的基本类型时,您需要创建或获取该类型的对象,就像我使用createResponse一样。您需要熟悉这些原则和其他原则,因为GoogleAppsScript文档假定您已了解这些原则。