我有一个需要登录的表单,我正在尝试获取其html以嵌入电子邮件中。与问题Google Apps Script: how to access or call "Send this form to others"?
类似var form = FormApp.create('New Form');
form.setRequireLogin(true);
...
var url = form.getPublishedUrl();
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
MailApp.sendEmail({
to: email,
subject: subject,
htmlBody: htmlBody,
});
...
Howerver,URLFetchApp似乎没有正确的OAuth配置,我总是获得Google登录页面的HTML。
有没有办法设置正确的OAuth参数来获取表单HTML?
答案 0 :(得分:6)
您的问题询问了允许UrlFetch()
获取设置为requiresLogin()
的表单的HTML内容所需的OAuth参数。这个答案没有直接解决这个问题。
不需要OAuth,您可以简单地更改表单的登录要求,只需足够长时间从中获取HTML。在很短的时间内,您的表单可以由域外的个人访问,如果他们碰巧有URL,并且在您再次锁定表单之前足够快地填写表单以提交他们的回复。
以下sendForm()
功能适用于消费者& GApps域帐户,无论是否设置了requiresLogin()
。
/**
* Send user an email containing the given form, in HTML.
*
* @param {Form} form Form object.
* @param {String} email One or more email addresses, comma separated.
*/
function sendForm(form,email) {
var url = form.getPublishedUrl();
// Temporarily disable requiresLogin so UrlFetch will operate
if (form.requiresLogin()) {
var requiresLogin = true;
form.setRequireLogin(false);
}
// Fetch form's HTML
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
// Re-enable requireLogin, if necessary
if (requiresLogin) {
form.setRequireLogin(true);
}
var subject = form.getTitle();
MailApp.sendEmail(email,
subject,
'This message requires HTML support to view.',
{
name: 'Form Emailer Script',
htmlBody: htmlBody
});
}
为了完整起见,这是一个测试功能...
function test_sendForm() {
// Build new form for testing
var form = FormApp.create('New Form');
var formTitle = 'Form Name';
form.setTitle(formTitle)
.setDescription('Description of form')
.setConfirmationMessage('Thanks for responding!')
.setAllowResponseEdits(true)
.setAcceptingResponses(true)
// Require Login (for GApp Domain accounts only)
try {
form.setRequireLogin(true);
} catch (e) {
// Error is expected for consumer accounts - carry on.
}
// Just one question
form.addTextItem().setTitle("Q1");
// Send it to self
var email = Session.getEffectiveUser().getEmail();
sendForm(form,email)
}