![](/img/trans.png)
[英]How to submit a Google Form in Google Scripts with a pre-filled URL?
[英]Send pre-filled Google Form via email
我有一個腳本,可以從電子表格創建一個預先填寫的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
});
關於如何通過電子郵件將其作為功能形式發送的任何想法? 如果情況變得更糟,我可以添加一個縮短的鏈接,然后讓他們單擊實時表單。
如果您只希望對從電子郵件正文提交的表單進行預先填寫的多項選擇答復 ,則可以為多項選擇項中的每一項創建一個預先填寫的鏈接,將其添加到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模板替換response
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.