I wrote this code with a trigger to send me emails upon a specific number of days remaining in the schedule. Everything works fine, but the content of my email is always 3 rows off for the variables fournir
and projet
. I think that it's probably because of the getRange
associated with these variables but since I'm fairly new with scripting, I don't know how to fix it.
Thanks in advance
function checkValue()
{
var NOMBREDELIGNES = 3000;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(3, 8, NOMBREDELIGNES); // ligne de départ, colonne, nombre de lignes
var values = range.getValues();
for (var row in values) {
for (var col in values[row]) {
var crissedevaleur = values[row][col];
if(crissedevaleur == 1 || crissedevaleur == 3 || crissedevaleur == 7) {
var projet = sheet.getRange(row,1).getValue();
var fournir = sheet.getRange(row,4).getValue();
MailApp.sendEmail("dufourlarouche@gotoast.ca","Rappel de votre compte en banque [" + projet + "]" ,"Échéance dans " + crissedevaleur + " jours pour \n\n" + fournir);
}
}
}
}
When you use range.getValues() in Google Apps Script, you get a double array. When you loop through the array the rows start at 0 and count up.
The row value corresponds to the row in the array, not the row on your sheet. To fix this, you will need to simply add range.getRow() or 3 to the row you are getting the value of.
Use:
var projet = sheet.getRange(row+range.getRow(),1).getValue();
var fournir = sheet.getRange(row+range.getRow(),4).getValue();
or
var projet = sheet.getRange(row+3,1).getValue();
var fournir = sheet.getRange(row+3,4).getValue();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.