无法从电子表格提交表单回复

问题描述 投票:0回答:1

我正在尝试使用Google电子表格中的数据提交大量回复。我使用了以下代码:

function FillForm() {
  var wrkSht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  for(var i = 2; i <= wrkSht.getLastRow(); i++)
  {
    try{
      UrlFetchApp.fetch("https://docs.google.com/forms/u/0/d/e/SomeID/formResponse", {"method" : "post",
                                                                                                                                    "payload" : {
                                                                                                                                      "entry.1320223480" : wrkSht.getRange("A" + i).getValue(),
                                                                                                                                      "entry.1469028961_sentinel" : wrkSht.getRange("B" + i).getValue(),
                                                                                                                                    "entry.379679049" : wrkSht.getRange("C" + i).getValue()
                                                                                                                                   }});
    }
    catch(ex)
    {
      MailApp.sendEmail("[email protected]", "error at spreedsheet", "name: " + wrkSht.getRange("A" + i).getValue() + " had the error: " + ex)
      throw ex;
    }
  }
}

但是每次我尝试运行它时,都会出现以下错误:

Exception: Request failed for https://docs.google.com returned code 400. Truncated server response: 

<!DOCTYPE html><html lang="en" class="m2"><head><link rel="shortcut icon" sizes="16x16"

 href="https://ssl.gstatic.com/docs/spreadsheets/forms/favic... (use muteHttpExceptions option to examine full response) (line 16, file "Code")

我怀疑我的问题出在条目号上:形式中的一个问题是多项选择,另一个是一个选择,但可以选择多个选项。对于这两个问题,我找不到条目号,因此我输入了input type: hidden条目号,我认为该条目适用于希望通过代码提交响应的开发人员',但我不确定。

您能找到任何问题吗?

编辑:

具有选项而不是文本的两个问题是问题,因为我试图从中获取的值是字符串?这是代表选项之一的准确字符串,但是也许还有其他方法可以为选项报价输入值?

javascript json google-apps-script google-sheets-api google-form
1个回答
0
投票

我们可以尝试使用createResponse()创建和提交表单回复。

  1. 使用createResponse()创建对表单的新响应。
  2. 要回答问题项目,请从该项目创建ItemResponse
  3. 然后通过调用FormResponse.withItemResponse(response)将其附加到此表单响应中。
  4. 要保存组合响应,请调用FormResponse.submit()

https://developers.google.com/apps-script/reference/forms/form.html#createresponse

© www.soinside.com 2019 - 2024. All rights reserved.