Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access or call "Send this form to others"?

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?

like image 315
ElBel Avatar asked Dec 27 '22 14:12

ElBel


2 Answers

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,
  });
...
like image 100
user3301366 Avatar answered Dec 30 '22 06:12

user3301366


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!

like image 27
Joris Avatar answered Dec 30 '22 08:12

Joris