user1188867
user1188867

Reputation: 3978

How to send a google form using google script with all the styles

I am trying to send google form using google script, it is sending form embedded mail but not along with styles of the form and without styles it is not looking good. See my code below:

function testEmail() {
  // Create a new form, then add a checkbox question, a multiple choice question,
// a page break, then a date question and a grid of questions.
var form = FormApp.create('New Form');
var item = form.addCheckboxItem();
item.setTitle('What condiments would you like on your hot dog?');
item.setChoices([
        item.createChoice('Ketchup'),
        item.createChoice('Mustard'),
        item.createChoice('Relish')
    ]);
form.addMultipleChoiceItem()
    .setTitle('Do you prefer cats or dogs?')
    .setChoiceValues(['Cats','Dogs'])
    .showOtherOption(true);
form.addPageBreakItem()
    .setTitle('Getting to know you');
form.addDateItem()
    .setTitle('When were you born?');
form.addGridItem()
    .setTitle('Rate your interests')
    .setRows(['Cars', 'Computers', 'Celebrities'])
    .setColumns(['Boring', 'So-so', 'Interesting']);
console.log('Published UR1L: ' + form.getPublishedUrl());
console.log('Editor UR1L: ' + form.getEditUrl());
var url = form.getPublishedUrl();  
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
var subject = form.getTitle();
  MailApp.sendEmail('[email protected]',
                    subject,
                    'This message requires HTML support to view.',
                    {
                      name: 'Form Email Test',
                      htmlBody: htmlBody
                    });  


return ContentService.createTextOutput(JSON.stringify({"result":"success"})).setMimeType(ContentService.MimeType.JSON);
}

How can I fix this issue?

Upvotes: 0

Views: 133

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76599

you would need to use the HTML as string:

var htmlBody = HtmlService.createHtmlOutput(response).getContent();

parse the DOM and then attach the style-sheet into the header

(or truncate </body></html>, attach the stylesheet and then add </body></html> again).

not exactly certain which one it is, only know of one for add-ons.

UrlFetchApp could also fetch it, if you want to inline the styles.

Upvotes: 1

Related Questions