Embed a google form in email with script the way google does it when option include form in email is used

冷暖自知 提交于 2020-05-10 08:53:06

问题


I have to send a form to some people and I do it with a google script. I try to embed the form in the email with this code : https://stackoverflow.com/a/23671529/4305236:

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

But it doesn't send the form the way google does it when the form is sent directly from the google forms interface.

Email sent by script;

The goal (Email sent by google form directly):

This question is already asked here but has no response.

Finally, the goal is to change the form every day, so it is not possible to use a template in an email previously sent.

Thank's in advance for any help.


回答1:


When Google sends the form to folks directly and embeds the form in the email they are using their new offering called AMP. You can read more about it here:

  • https://developers.google.com/amp/
  • https://www.blog.google/products/g-suite/bringing-power-amp-gmail/
  • https://developers.google.com/gmail/ampemail/

The code you're using gets the raw HTML of the form as it would be rendered for a user in a browser. This code is not in AMP format. Ergo it will not do what you want.

If you want that AMP experience you will need to create the AMP email yourself.

I don't think FormApp has a programatic way to send the form to folks, like you can from https://docs.google.com/forms/.

I hope that helps.




回答2:


I am sending pre-filled google form to users (partly filled and to be completed by him).

It does not send the form in the body of the email. But, it has a link which has to be clicked by the person to fill the form (some fields). Some fields are already filled by me (the sender of the email).

It is pretty straight forward.

Create the form

Fill the form values which you want to be pre-filled. Click "Get prefilled link"

Copy the url created.

USe the URL and fill the values from the google sheet.

I am enclosing the code I use

//
//
function appraisal_mail_to_all(){
  //https://docs.google.com/forms/d/e/..../viewform?usp=pp_url&entry.1559436995=name&entry.226490833=Satisfactory&entry.1002429742=comment1&entry.1373188131=Poor&entry.2023543437=comment2&entry.332797694=Indifferent&entry.928578055=comment3&entry.1637933262=Poor&entry.1733301737=comment4&entry.1465918066=job_descrip&entry.31857466=1234&entry.370551729=dept&entry.835006919=qualification&entry.914497805=joined&entry.976738938=hod
  var ss = SpreadsheetApp.getActiveSpreadsheet();  
  var activesheet=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  if (activesheet!=="Emp") {Browser.msgBox('Pl. click this option from "Emp" sheet');return;};   
  var response = Browser.msgBox('Mail Will go to all HOD.', "Are you sure ?" , Browser.Buttons.YES_NO);
  if (response == "yes") {} else {return};
  var empsht = ss.getSheetByName("Emp");  
  var emplr=empsht.getLastRow();
  var empdvals = empsht.getRange("A2:W"+emplr).getValues();//all
  var empcvals=ArrayLib.filterByText(empdvals, 18, "Yes");//active
  var empavals=ArrayLib.filterByText(empcvals, 21, "TRUE" );//appraised
  var hods = ArrayLib.unique(empavals, 16);//uniq
  var joined="";
  //var appss=SpreadsheetApp.openById("....");//appraisal
  //var appsht=appss.getSheetByName("Form Responses 1")  
  //var appvals = appsht.getDataRange().getValues();
  for (k=0;k<hods.length;k++){ 
      var link1="",link2="",link3="",link4="";
      var message = "Dear "+hods[k]+",<br><br>Pl. click the link to fill the performance appraisal of the people reporting to you (if pending). Pl. submit it only once per employee within two days. Once submitted, it can not be changed. Next year increment, promotion etc will be based on the appraisal. Your kind co-operation is requested.<br><br>";
      message += "<table cellspacing='2' cellpadding='2' dir='ltr' border='1' style='font-size:10pt;font-family:arial,sans,sans-serif;border-collapse:collapse;border:1px solid #ccc;font-weight:normal;color:black;background-color:white;text-align:center;text-decoration:none;font-style:normal'"
      +"<tr>"
      +"<td bgcolor = '#ffffff', Align = 'center'>"+"<b>"+" Emp No "+"</td>"
      +"<td bgcolor = '#ffffff', Align = 'center'>"+"<b>"+" Name "+"</td>"
      +"<td bgcolor = '#ffffff', Align = 'center'>"+"<b>"+" Desig "+"</td>"
      +"<td bgcolor = '#ffffff', Align = 'center'>"+"<b>"+" Dept "+"</td>"
      +"<td bgcolor = '#ffffff', Align = 'center'>"+"<b>Link (Submit once only)"+"</b>"+"</td>"
      +"</tr>"
      +"<tr>"
      var emps=ArrayLib.filterByText(empavals, 16, hods[k]);
      var hod=ArrayLib.filterByText(empdvals,1, hods[k]);
      //Browser.msgBox(hod);
      //if (hod.length > 1) {Browser.msgBox(hod[0][1] + " is appearing more than once in emp master. Pl. correct it");return};
      for (m=0;m<emps.length; m++){
        //if (emps[m][21]==true && emps[m][18]=="Yes") {//to be appraised and active
        if (emps[m][22]=="") {//appraisal not done
          //if ( ArrayLib.find(appvals, 12, emps[m][0]) == 0 ) { //emp no not found in appraisal data
          //if (appdone.length == 0) {//appraisal not done
          //if (emps[m][15]=="") {joined=""} else { joined=Utilities.formatDate( emps[m][15],"GMT+05:30", "dd-MM-YY")} ;
          link4= 'https://docs.google.com/forms/d/e/..../viewform?usp=pp_url&entry.1559436995='+emps[m][1] +'&entry.31857466='+emps[m][0]+'&entry.370551729='+emps[m][11] + '&entry.914497805='+ joined +'&entry.976738938='+emps[m][16] ; 
          link3 = link4.replace(/#/g, "");
          link2 = link3.replace(/,/g, "");
          link1 = link2.replace(/ /g, "%20");
          message += "<td bgcolor = '#ffffff', Align = 'center'>"+emps[m][0]+"</td>"
          +"<td bgcolor = '#ffffff', Align = 'center'>"+emps[m][1]+"</td>"
          +"<td bgcolor = '#ffffff', Align = 'center'>"+emps[m][3]+"</td>"
          +"<td bgcolor = '#ffffff', Align = 'center'>"+emps[m][11]+"</td>"
          +"<td bgcolor = '#ffffff', Align = 'center'><a href='" +link1+ "'>click to evaluate</a></td>"
          +"</tr>"
          +"<tr>"
          } else { //if appraisal done
          //if (emps[m][15]=="") {joined=""} else { joined=Utilities.formatDate( emps[m][15],"GMT+05:30", "dd-MM-YY")} ;
          message += "<td bgcolor = '#ffffff', Align = 'center'>"+emps[m][0]+"</td>"
          +"<td bgcolor = '#ffffff', Align = 'center'>"+emps[m][1]+"</td>"
          +"<td bgcolor = '#ffffff', Align = 'center'>"+emps[m][3]+"</td>"
          +"<td bgcolor = '#ffffff', Align = 'center'>"+emps[m][11]+"</td>"
          +"<td bgcolor = '#ffffff', Align = 'center'>Appraisal Completed</td>"
          +"</tr>"
          +"<tr>"
        }//if appraisal done
      //}//if to be appraised AND ACTIVE
      }//for each emp under the hod
      message+="</tr>"+  "</tr>"+ "</table>"+'<br>.<br><br>';
      var subject1="Performance Evaluation of people under you (Confidential)";
      if  (hod[0][7]=="") { var hodm="..."} else {var hodm=hod[0][7]};
      //var hodm="";
      MailApp.sendEmail({        to: hodm,        subject: subject1,        htmlBody: message      });
      //Browser.msgBox(hodm+subject1+message)
  }//for each hod
}
//






来源:https://stackoverflow.com/questions/50834532/embed-a-google-form-in-email-with-script-the-way-google-does-it-when-option-incl

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