Script time trigger does not work properly, just manual execution

31 views
Skip to first unread message

Clovis Kuhn

unread,
Mar 27, 2020, 12:44:51 PM3/27/20
to Google Apps Script Community

Hello good afternoon.

I am new to using document and spreadsheet scripts and created a script for an inventory control spreadsheet in which emails are sent to me when a particular product reaches its minimum and critical capacity.

 

The script I programmed works normally when it is manually triggered by an execution in the script editor. However, when I create a time trigger for the same one that performs the sending of emails to me once a day, the trigger works and runs normally with the status of completed but the emails do not arrive as when it is executed manually.


This is the script:

/**

 * Envia email com dados da planilha atual.

 */

function sendEmails() {

  var sheet = SpreadsheetApp.getActiveSheet();

  var startRow = 2; // linha inicial de dados

  var numRows = 30; // número de linhas para processar

  // Busca no intervalo as células A2:F6

  var dataRange = sheet.getRange(startRow,1,numRows,6);

  // Busca valores para a linha no intervalo.

  var data = dataRange.getValues();

  for (var i in data) {

    var row = data[i];

    var emailAddress = row[0]; // coluna 1 - email

    var subject = row[1]; // coluna 2 - assunto

    var messagemin = row[2]; // coluna 3  - mensagem minimo

    var messagecrit = row[3]; // coluna 3  - mensagem critico

    var quanti = row[4]; //coluna 4 - quantidade atual crítica ou mínima

    var item = row[5]; //coluna 5 - item que corresponde aos dados anteriores

    if ((quanti<=3) && (quanti>2) && (item =='Álcool')) { //minimo

    MailApp.sendEmail(emailAddress, subject, messagemin);

    }

    if ((quanti<=2) && (quanti>=0) && (item =='Álcool')) { //critico

    MailApp.sendEmail(emailAddress, subject, messagecrit);

    }

  }

}



Alan Wells

unread,
Mar 27, 2020, 1:46:27 PM3/27/20
to Google Apps Script Community
Try changing:

var sheet = SpreadsheetApp.getActiveSheet();

to:

var sheet = SpreadsheetApp.openById('Put the Sheet file ID here');

Clovis Kuhn

unread,
Mar 27, 2020, 8:02:19 PM3/27/20
to google-apps-sc...@googlegroups.com
Hi, thanks.

so changing that, line 9 encounters an error:

 

 (String, number, number, number) do not match the method signature for SpreadsheetApp.Spreadsheet.getRange. (line 9, file "Codigo").


I don't know what it can be.

Andrew Roberts

unread,
Mar 28, 2020, 7:24:49 AM3/28/20
to google-apps-sc...@googlegroups.com
The first parameter should be a number not a string. Take a look at the value of the variable being passed into getRange() with the debugger.

Hi, thanks.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/4911525b-0ecb-4b24-8d68-d98ba778f08c%40googlegroups.com.

Alan Wells

unread,
Mar 28, 2020, 9:10:34 AM3/28/20
to Google Apps Script Community
I made a mistake.

the line:
var sheet = SpreadsheetApp.openById('Put the Sheet file ID here');
Gets the spreadsheet, but you also need to get the sheet tab.
Change to.
var sheet = SpreadsheetApp.openById('Put the Sheet file ID here').getSheetByName('Put Sheet Tab Name Here');
Reply all
Reply to author
Forward
0 new messages