How to Embed a Google Form in Email in Google App Script

纵饮孤独 提交于 2019-12-10 18:43:12

问题


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,
  });
...

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

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


回答1:


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.

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.

Script

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


来源:https://stackoverflow.com/questions/27028826/how-to-embed-a-google-form-in-email-in-google-app-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!