How to access or call “Send this form to others”?

2019-01-18 07:56发布

问题:

I have a form attached to a Google Apps spreadsheet. It's a form to let my coworkers submit agenda items to our weekly review meeting. I'm writing a script to automatically email a reminder to the relevant people.

To make it less annoying & tedious for them, I'd like to actually embed the form within the email. Google Docs provides a way to manually send a form: Spreadsheet > Form > Send form. However, I can't find any way in the Google Apps Scripts documentation that lets me trigger this functionality, e.g.

  • A method like sendFormInEmail
  • Access to the email-friendly form HTML, which I could assign to the htmlBody argument of the sendEmail method.
  • Trigger an arbitrary menu item in Google Apps
  • Something else?

I could do a workaround by extracting the generated HTML from an email and assigning it as the htmlBody argument, but then I'd have to manually update the html every time we want to make a change to the form -- not what I want to happen.

Any suggestions?

回答1:

Joris, you're right. You can use the method fetch() to send the form's html to the user's mailbox:

var form = FormApp.create('New Form');
....
var url = form.getPublishedUrl();
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
    MailApp.sendEmail({
    to: email,
    subject: subject,
    htmlBody: htmlBody,
  });
...


回答2:

I have the exact same requirement as you do, but unfortunately there doesn't seem to be an API call that does this.

What I think might work (though I have yet to actually try this) is to use the Spreadsheet.getFormUrl method to get the form URL, then use UrlFetchAp.fetch to obtain the HTML for the spreadsheet form, and then use that HTML as the e-mail body.

Like I said, I don't know if this will work (though on paper it should!), but I'd be very interested to know if it did!