Синтаксис для MailApp.sendEmail листов приложения Google и правильное место в функции? - PullRequest
0 голосов
/ 09 июля 2020

Я создал систему, которая использует форму Google для ввода информации в электронную таблицу. В момент отправки формы я хотел бы отправить электронное письмо соответствующему утверждающему.

У меня проблемы с синтаксическим анализом значения eventObject.email в поле 'to в функции MailApp.sendEmail.

   MailApp.sendEmail({
   to: eventObject.email,
   subject: "A new request has been submitted",
   htmlBody: "Please review at the following link: arbritrary link"
      });

Если я определю адрес электронной почты в виде строки, он будет отправлен, но когда это будет eventObject.email, я не получу электронного письма и сообщение об ошибке «Исключение: не удалось отправить электронное письмо: нет получателя»

Код выглядит следующим образом:

    //Load the Moment.js library once.
var moment = Moment.load();

var GLOBAL = {
  //the id of the form we will use to create calendar events 
  formId : "arbritrary form ID",  
  
  //the id of the calendar we will create events on
  calendarId : "arbritrary calendar ID",
  
  //a mapping of form item titles to sections of the calendar event
  formMap : {
    eventTitle: "Person Name and Organisation",
    startTime : "Start Date / Time",
    endTime: "End Date / Time",
    description: "Description of work / Additional Notes",
    location: "Location of Work and/or Room ID",
    email: "Approver Email Address",
    
  },
}

function onFormSubmit() {
  var eventObject = getFormResponse();
  var event = createCalendarEvent(eventObject);
}

function getFormResponse() {
  // Get a form object by opening the form using the
  // form id stored in the GLOBAL variable object
  var form = FormApp.openById(GLOBAL.formId),
      //Get all responses from the form. 
      //This method returns an array of form responses
      responses = form.getResponses(),
      //find the length of the responses array
      length = responses.length,
      //find the index of the most recent form response
      //since arrays are zero indexed, the last response 
      //is the total number of responses minus one
      lastResponse = responses[length-1],
      //get an array of responses to every question item 
      //within the form for which the respondent provided an answer
      itemResponses = lastResponse.getItemResponses(),
      //create an empty object to store data from the last 
      //form response
      //that will be used to create a calendar event
      eventObject = {};
  //Loop through each item response in the item response array
  for (var i = 0, x = itemResponses.length; i<x; i++) {
    //Get the title of the form item being iterated on
    var thisItem = itemResponses[i].getItem().getTitle(),
        //get the submitted response to the form item being
        //iterated on
        thisResponse = itemResponses[i].getResponse();
    //based on the form question title, map the response of the 
    //item being iterated on into our eventObject variable
    //use the GLOBAL variable formMap sub object to match 
    //form question titles to property keys in the event object
    switch (thisItem) {
      case GLOBAL.formMap.eventTitle:
        eventObject.title = thisResponse;
        break;
      case GLOBAL.formMap.startTime:
        eventObject.startTime = thisResponse;
        break;
      case GLOBAL.formMap.endTime:
        eventObject.endTime = thisResponse;
        break; 
      case GLOBAL.formMap.description:
        eventObject.description = thisResponse;
        break;
      case GLOBAL.formMap.location:
        eventObject.location = thisResponse;
        break;
      case GLOBAL.formMap.email:
        eventObject.email = thisResponse;
        break;
        
   
      }
   
    
  return eventObject;
   
   MailApp.sendEmail({
   to: eventObject.email,
   subject: "A new request has been submitted",
   htmlBody: "Please review at the following link: arbritrary link"
      });
 }

}

Заранее спасибо!

1 Ответ

0 голосов
/ 09 июля 2020

thisItem - это вопрос, а не ответ

  • GLOBAL.formMap.email - это значение указанного ключа, поэтому "Approver Email Address"

  • Таким образом, switch (thisItem) ... case GLOBAL.formMap.email никогда не будет заполнен.

Решение:

Изменить

switch (thisItem)

на switch (thisResponse)

В этом случае адрес электронной почты будет настроен правильно, если значение отправки формы - «Адрес электронной почты утверждающего».

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...