L
L
ligisayan2017-08-11 13:07:27
Google Apps Script
ligisayan, 2017-08-11 13:07:27

How do I set up an autoresponder for filling out forms in different languages?

Hello! I set up, according to the instructions on Google Script, sending an autoresponder with a welcome letter like this.

function sendFormByEmail(e) 
{    
  // instance of Active Spreadsheet:
  var responceSheet = SpreadsheetApp.getActiveSheet(); 
  
  // number of rows in responce form:
  var rows = responceSheet.getLastRow();
  
  // range of names, e-mails:
  var namesRange = responceSheet.getRange(rows, 2, 1, 1);
  var surnamesRange = responceSheet.getRange(rows, 3, 1, 1);
  var emailsRange = responceSheet.getRange(rows, 8, 1, 1);
  
  // last sender's name, e-mail::
  var nameSender = namesRange.getValue();
  var surnameSender = surnamesRange.getValue();
  var emailSender = emailsRange.getValue();
  
  // message ReplyTo, subject, body:
  var emailReplyTo = "[email protected]";
  var subject = "Спасибо за Ваш интерес";
  var body = "<b>Здравствуйте, " + nameSender + surnameSender + "!</b> <br> Спасибо, что Вы с нами :)";
    
  // send the e-mails:
  MailApp.sendEmail({to: emailSender, replyTo: emailReplyTo, subject: subject, htmlBody: body});
}

Now I have a question: in the Google spreadsheet there are several tabs for different forms (Russian, English)
How can I now track which tab the table is filled in and, in accordance with this, send the desired letter format in Russian and English, respectively.
It seems like there is a hint in
the google docs script to interact with a specific tab,
// The code below will make the 2nd sheet active in the active spreadsheet
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 SpreadsheetApp.setActiveSheet(ss.getSheets()[1]);

but I don't even know if it is possible in google script to fulfill the conditions and in what format are they written then?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2017-08-11
@ligisayan

This code will never work for you. Read the comments on the resource you provided.
The main mistake is a reference to an active Table SpreadsheetApp.getActiveSpreadsheet();A trigger OnFormSubmitcannot have an active Table because no user has opened it. And even if he opened it, then he may not be a trigger registrar or its initiator.
Error Handling
Google Apps Script (we are talking about this point in time in the development of the language) does not have advanced debugging methods. So wrap the main call:

/**
* В Таблице должен быть лист "Лог".
* Чтобы лист существовал всегда
* https://github.com/oshliaer/google-apps-script-snippets#get-a-sheet-by-name
**/
function handler(e) {
  try {
    sendFormByEmail(getNewEmailMessage(e));
  } catch(err) {
    e.source.getSheetByName('Лог').appendRow([new Date(), err.message, err.stack]);
  }
}

Great, now everything that fell in runtime will be known to you.
Current Spreadsheet/Sheet
I urge not only the author of the topic, but everyone in general: "Please do not touch ActiveSpreadsheet, ActiveSheet and ActiveRange, because this is not VBA. This is #YOUR_FUCK_OTHER_UNIVERSE!". Take a look at the code above e.source.getSheetByName(). That's all it takes.
And here is an example of a switch:
/**
* Определение языка на основе ответа
**/
function getNewEmailMessage(e) {
  var sheet = e.range.getSheet();
  var lang = 'EN';
  switch(sheet.getName()){
    case 'RU':
       return 'Ответ на русском';
       break;
    case 'EN':
       return 'Ответ на нерусском';
       break;
  }
  return 'Ответ на русском';
}

It remains to add arguments and rewrite sendFormByEmailaccording to the accepted parameters.
Trigger on several Forms
Instead of a conclusion
Only high-quality and verified content
  • google-apps-script-english.gitlab.io

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question