通过电子邮件发送预填充的Google表单

时间:2017-05-08 16:41:42

标签: google-apps-script

我有一个脚本,可以从电子表格中创建预先填好的Google表单。我可以轻松地获得预填充的URL,并且它按预期工作。我很难通过电子邮件发送预先填好的表格。我可以使用HtmlService获取HTML输出并将其附加到电子邮件的正文中,但它不是交互式的。我可以填写文本字段,但不能选择多项选择项或提交表单。

脚本

// A form is already built...

var logs = ss.getDataRange().getValues();
var formResponse = form.createResponse();

for(var j=0;j<7; j++) {                        
  formItem = items[j+2].asTextItem();
  response = formItem.createResponse(weekData[i][j]);
  formResponse.withItemResponse(response);
}

var url = formResponse.toPrefilledUrl();

// Mark as sent so it isn't caught in the next trigger      
logs.getRange(i+1, logs.getLastColumn()).setValue('sent');

var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();

MailApp.sendEmail({
  to: "email@address.com",
  subject: "Confirm Intern Hours",
  htmlBody: htmlBody
});

有关如何将此作为功能表单发送到电子邮件中的任何想法?如果最坏的情况发生,我可以添加一个缩短的链接,让他们点击直播表格。

1 个答案:

答案 0 :(得分:0)

如果您只是在从电​​子邮件正文提交的表单中查找预先填写的多项选择回复,则可以为每个多项选项创建预先填写的链接,将它们添加到html电子邮件中,然后使用电子邮件中的链接提交预先填写的表单。

如果您还需要用户从电子邮件中填写输入文本字段,那可能需要更多工作..但可以使用replace()。

function emailForm () {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var form = FormApp.openByUrl(ss.getFormUrl());
  var items = form.getItems();
  // or wherever your Multiple Choice item is in the list
  var choice_item = items[0].asMultipleChoiceItem();
  var choice_opts = choice_item.getChoices();
  var prefilled_set = [];

  var logs = ss.getDataRange().getValues();
  var formResponse = form.createResponse();

  for(var j=0;j<7; j++) {                        
    formItem = items[j+2].asTextItem();
    response = formItem.createResponse(weekData[i][j]);
    formResponse.withItemResponse(response);
  }

  var url = formResponse.toPrefilledUrl();

  // Create a prefilled link for each multiple choice option
  choice_opts.forEach(function (e) {
    var choice_response = choice_item.createResponse(e.getValue());
    formResponse.withItemResponse(choice_response);

    var prefill_url = formResponse.toPrefilledUrl();
    // Change the URI from view to response and Shorten the URL
    var tiny_url = form.shortenFormUrl(
                       prefill_url.replace("/viewform?", "/formResponse?"));
    // Make HTML Link
    var html_link = '<a href="' + tiny_url + '"> ' + e.getValue() + ' </a>';

    // Push each option link into an array
    prefilled_set.push(html_link);
  });

  var response = UrlFetchApp.fetch(url);

  // Get your HTML Output and Add the Multiple Choice links to it
  var htmlBody = HtmlService.createHtmlOutput(response + prefilled_set.toString())
                            .getContent();

  MailApp.sendEmail({
    to: "email@address.com",
    subject: "Confirm Intern Hours",
    htmlBody: htmlBody
  });
}

这是预填充链接通过电子邮件提交表单的文章:

 prefill_url.replace("/viewform?", "/formResponse?")

此外,可能希望以某种方式格式化链接以使它们更突出。或者甚至用您自己的html template

替换response