Google app script trigger not working

自古美人都是妖i 提交于 2019-12-10 03:34:20

问题


I have a google app script which sends email, and i have set a trigger such that it sends email on every form submit. The problem is the trigger works perfectly fine for initial few minutes, but later even after entering correct data. The script does not send the mail, i have to manually press the execution button of the script. Here is my code

var EMAIL_SENT = "EMAIL_SENT";
function sendEmailsapp() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = sheet.getLastRow();   // Number of rows to process
  // Fetch the range of cells A2:B3

  var dataRange = sheet.getRange(startRow, 1, numRows,8)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[4];  // First column
    var message = row[5];       // Second column
    var emailSent = row[7];  // Third column
    var money=row[6]
    if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
      var subject = "You have been registered for follwoing events:-";
      var event;

      MailApp.sendEmail(emailAddress, subject, "Please bring your college id and copy of this mail either in phone or printed paper"+
                        "\n\n"+"Name:-"+row[1]+"\n"+"USN:-"+row[2]+"\n"+"Mobile:-"+row[3]+"\n"+"Event:-"+ message+"\nMoney status:-"+money+"You registered on"+row[0]);
        sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);


      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

The code works fine. The only problem is triggers.

Here is the image of my trigger. sendEmailsapp is the trigger, and sendEmailsweb is another trigger which also suffers from same problem.

here is the log

My only Problem is the trigger is not getting triggered, it is not with the email being sent.


回答1:


My trigger wasn't working just like yours, even though the script was correct.

The solution was very stupid. I deleted the trigger and added a new one. Exactly the same trigger.




回答2:


I had this issue with my script, but I was using Time-Driven as the Event. I set it on every minute but it just wouldn't do anything. Used the logging method written above and I found out that the trigger works fine - every minute as I set. But it just wouldn't actually do the same thing as when I explicitly click on the Run button.

I suspected that it had something to do with the following line:

var sheet = SpreadsheetApp.getActiveSheet();

So I played around with it and changed it to:

var spreadsheet = SpreadsheetApp.openById("INSERT_SPREADSHEET_ID_HERE");
var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);

And it finally worked.

Replace INSERT_SPREADSHEET_ID_HERE with the ID of your spreadsheet (from your URL https://docs.google.com/spreadsheets/d/INSERT_SPREADSHEET_ID_HERE/edit#gid=0).

And from the getSheets()[0], the 0 would be your first of possibly multiple sheets in that specific spreadsheet.

I hope this helps in any way.




回答3:


What I found useful was to set notifications to immediate in "Resources>Current Projects Triggers". I set it to send me an email on error. That email contains details of the problem which you can then solve :).




回答4:


I'd be willing to bet that the problem is not the trigger. Even though your code works, it could be conditions that are preventing the conditional section of your code to work. You can test to see that the function is actually triggering, even if it's not giving you the expected result. Put a Logger.log() statement immediately after the function:

function sendEmailsapp() {
  Logger.log('sendEmailsapp ran!');

Then VIEW the LOGS. I can't believe that the function isn't being triggered. The email might not be getting sent, but I'd be willing to bet that you are going to get that msg 'sendEmailsapp ran!' printed to the LOGS. So, that's the first thing you need to do.

The next thing you need to do is put a Logger.log() statement immediately after retrieving the email:

var emailSent = row[7];
Logger.log('value of the email is: ' + emailSent);

Then check the LOGS for what values are actually being returned. If every value being returned is "EMAIL_SENT", then no email will ever get sent.

We need to know what the actual results are. You need to provide what was printed to the LOGS.



来源:https://stackoverflow.com/questions/28701382/google-app-script-trigger-not-working

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