Home > Back-end >  how do i email a quiz result from google forms using app script site
how do i email a quiz result from google forms using app script site

Time:01-10

I need a Google script (GAS) to send an email where the student who passed the quiz on google forms can find his grade, for example if he passed the quiz and his grade is 10/10 he will receive an email on his address-email: "Hi, you've already passed your quiz and you got a 10/10"

Thank you

function sendEmail(e) { //respond //getRespondentEmail() 
  var html = HtmlService.createTemplateFromFile("email.html"); 
  var htmlText = html.evaluate().getContent(); 
  var emailTo = e.response.getRespondentEmail(); 
  var subject = "Merci pour votre participation"; 
  var textBody = "This email requires HTML support. Please make sure you open with a client that support it." 
  var options = { htmlBody: htmlText }; Logger.log(emailTo); if(emailTo !== undefined){ GmailApp.sendEmail(emailTo, subject, textBody, options); 
  } 
}

CodePudding user response:

Email Results of Quiz to respondent

From the Form Trigger

function onMyFormSubmit(e) {
  const form = FormApp.getActiveForm();
  const r = e.response;
  r.getGradableItemResponses().forEach((item,i) => {
    Logger.log('Question: %s Response: %s Score: %s',item.getItem().asTextItem().getTitle(),item.getResponse(),item.getScore());
  });
  let email = r.getRespondentEmail();
  GmailApp.sendEmail(email,"Quiz Response",Logger.getLog())
  Logger.log(r);
}

function createOnFormSubmitTrigger() {
  const form = FormApp.getActiveForm();
  if(ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == "onMyFormSubmit").length == 0) {
    ScriptApp.newTrigger("onMyFormSubmit").forForm(form).onFormSubmit().create();
  }
}

From the Spreadsheet Trigger

function onMyFormSubmit(e) {
  Logger.log(JSON.stringify(e));
  const ss = SpreadsheetApp.getActive();
  const sh = e.range.getSheet();
  const hA = sh.getRange(1,2,1,6).getValues().flat();
  let s = '';
  hA.forEach(h => {
    s = `\nQuestion: ${h} Answer: ${e.namedValues[h][0]}`
  });
  s  = `\nYour Score is: ${e.namedValues.Score}`;
  //Logger.log(s);
  GmailApp.sendEmail(e.namedValues['Email Address'][0],"Quiz Result",s);
}

Email:

enter image description here

CodePudding user response:

I believe your goal is as follows.

  • When the form is submitted, you want to send an email when the grade is 10/10.
  • From function sendEmail(e) { and var emailTo = e.response.getRespondentEmail();, your script is the container-bound script of Google Form. And, your function of sendEmail is installed as OnSubmit trigger.

Modification point:

  • In order to check if he passed the quiz and his grade is 10/10, it is required to calculate the grade of all items.

When this point is reflected in your script, it becomes as follows.

Modified script:

In this script, it supposes that the function sendEmail is run by the OnSubmit trigger. So please confirm whether the function sendEmail has already been installed as OnSubmit trigger again.

function sendEmail(e) {
  var maxGrade = 10; // This is from "10/10" in your question.
  var grade = e.response.getGradableItemResponses().reduce((p, e) => p  = e.getScore(), 0);
  var emailTo = e.response.getRespondentEmail();
  if (grade < maxGrade || !emailTo) return;

  var subject = "Sample subject"; // Please set the subject.
  var textBody = "Hi, you've already passed your quiz and you got a 10/10.";
  GmailApp.sendEmail(emailTo, subject, textBody);
}

Note:

  • From function sendEmail(e) { and var emailTo = e.response.getRespondentEmail();, I understood that your script is the conteiner-bound script of Google Form. So please be careful about this.

  • This modified script can be used by the OnSubmit trigger. So when you test this, please submit the form. When you directly run this script, an error occurs. Please be careful about this.

References:

  •  Tags:  
  • Related