Google app script trigger not working

2020-08-22 15:40发布

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. image

here is the log enter image description here

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

4条回答
迷人小祖宗
2楼-- · 2020-08-22 16:18

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.

查看更多
别忘想泡老子
3楼-- · 2020-08-22 16:25

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.

查看更多
老娘就宠你
4楼-- · 2020-08-22 16:41

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 :).

查看更多
来,给爷笑一个
5楼-- · 2020-08-22 16:44

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.

查看更多
登录 后发表回答