通过电子邮件发送预先填好的Google表单 [英] Send pre-filled Google Form via email

查看:198
本文介绍了通过电子邮件发送预先填好的Google表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

脚本

//表单已经建立...

var logs = ss.getDataRange()。getValues ();
var formResponse = form.createResponse(); (var j = 0; j <7; j ++){
formItem = items [j + 2] .asTextItem();


response = formItem.createResponse(weekData [i] [j]);
formResponse.withItemResponse(response);
}

var url = formResponse.toPrefilledUrl();

//标记为已发送,因此不会在下一次触发中捕获
logs.getRange(i + 1,logs.getLastColumn())。setValue('sent');

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

MailApp.sendEmail({
至:email@address.com,
主题:确认实习时间,
htmlBody:htmlBody
});

关于如何在电子邮件中将其作为功能表单发送的任何想法?如果最糟糕的是最差的,我可以包含一个缩短的链接,并让他们点击直播表单。 解决方案

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



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

 函数emailForm(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var form = FormApp.openByUrl(ss.getFormUrl());
var items = form.getItems();
//或者您的Multiple Choice项目在列表中的任何位置
var choice_item = items [0] .asMultipleChoiceItem();
var choice_opts = choice_item.getChoices();
var prefilled_set = [];

var logs = ss.getDataRange()。getValues();
var formResponse = form.createResponse(); (var j = 0; j <7; j ++){
formItem = items [j + 2] .asTextItem();


response = formItem.createResponse(weekData [i] [j]);
formResponse.withItemResponse(response);
}

var url = formResponse.toPrefilledUrl();

//为每个多选选项创建一个预填充链接
choice_opts.forEach(function(e){
var choice_response = choice_item.createResponse(e.getValue());
formResponse.withItemResponse(choice_response);

var prefill_url = formResponse.toPrefilledUrl();
//将URI从视图更改为响应并缩短URL
var tiny_url = form.shortenFormUrl(
prefill_url.replace(/ viewform?,/ formResponse?));
//建立HTML链接
var html_link ='< a href = ''+ tiny_url +'>'+ e.getValue()+'< / a>';

//将每个选项链接推送到一个数组
prefilled_set.push( html_link);
});

var response = UrlFetchApp.fetch(url);

//获取您的HTML输出并添加多选择链接到它
var htmlBody = HtmlService.createHtmlOutput(response + prefilled_set.toString())
.getContent();

MailApp.sendEmail({
至:email@address.com,
主题:确认实习时间,
htmlBody:htmlBody
});
}

这是预填充链接提交电子表格

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






另外,可能要格式化以某种方式链接,使他们更加突出。甚至用您自己的 response nofollow noreferrer> html模板


I have a script which creates a pre-filled Google Form from a spreadsheet. I can get the pre-filled URL easily enough and it's working as expected. I'm having a hard time sending the pre-filled form via email. I can get the HTML output using HtmlService and appending it to the body of the email, but it isn't interactive. I can fill in text fields, but cannot select multiple choice items or submit the form.

Script

// 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
});

Any ideas on how to send this as a functional form in an email? If worst comes to worst, I can include a shortened link and have them click out to the live form.

解决方案

If you are only looking for a pre-filled multiple choice reply to the form that is submitted from the email body - you can create a pre-filled link for each of the multiple choice items, add them to the html email, and then submit the pre-filled form using the link in the email.

If you also need the input text fields to be filled out by the user from the email, that would probably take a bit more work..but may be possible using 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
  });
}

This is the piece that makes the pre-filled links submit the form from email:

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


Also, will probably want to format the links in some way to make them stand out more. Or even replace response with your own html template

这篇关于通过电子邮件发送预先填好的Google表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆