如何在Google App脚本的电子邮件中嵌入Google表单 [英] How to Embed a Google Form in Email in Google App Script

查看:94
本文介绍了如何在Google App脚本的电子邮件中嵌入Google表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要登录的表格,我正在尝试获取其html以便嵌入到电子邮件中.类似于问题 Google Apps脚本:如何访问或调用将此表单发送给其他人"?

I have a form that requires login and I am trying to fetch its html to embed in an email. Similar to the question 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,
  });
...

但是,URLFetchApp似乎没有正确的OAuth配置,而且我总是会获得Google登录页面的HTML.

Howerver, the URLFetchApp does not seem to have the correct OAuth configuration and I always get the HTML for Google Login page.

是否可以设置正确的OAuth参数以获取HTML表单?

Is there a way to set the correct OAuth parameters to get the form HTML?

推荐答案

您的问题询问允许UrlFetch()获取设置了requiresLogin()的表单的HTML内容所需的OAuth参数.这个答案不能直接解决这个问题.

Your question asks about the OAuth parameters required to allow UrlFetch() to obtain the HTML content of a form with requiresLogin() set. This answer doesn't address that directly.

不需要OAuth,您可以短暂地更改表单的登录要求,只要足够长的时间即可从表单中获取HTML.在很短的时间内,您的域之外的个人可以访问您的表单(如果他们碰巧拥有该URL),并在再次锁定表单之前,以足够快的速度填充表单以提交其响应.

Without requiring OAuth, you can briefly change the login requirement for the form, just long enough to grab the HTML from it. For a small amount of time, your form could be accessed by individuals outside of your domain, if they happened to have the URL, and filled the form fast enough to submit their response before you locked the form up again.

以下sendForm()功能将适用于消费者& GApps域帐户,无论是否设置requiresLogin().

The following sendForm() function will work for consumer & GApps domain accounts, whether or not requiresLogin() is set.

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

为完整起见,这是一个测试功能...

For completeness, here's a test function...

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

这篇关于如何在Google App脚本的电子邮件中嵌入Google表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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