如何使用具有所有样式的 google 脚本发送 google 表单

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

我正在尝试使用 google 脚本发送 google 表单,它发送的是表单嵌入的邮件,但没有发送表单样式,没有样式它看起来不太好。请参阅下面的代码:

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('myemailid@gmail.com',
                    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);
}

我该如何解决这个问题?

您需要使用 HTML 作为字符串:

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

解析 DOM 然后将 style-sheet 附加到 header

(或截断 </body></html>,附加样式表,然后再次添加 </body></html>)。

不确定是哪一个,add-ons只知道一个。

UrlFetchApp 也可以获取它,如果你想内联样式。