GAS扫描工作表,查找值和发送电子邮件不会做任何事情.思潮

GAS to scan a sheet, look for values, and send e-mail wont do anything...thoughts?

本文关键字:电子邮件 任何事 思潮 工作 扫描 查找 GAS      更新时间:2023-09-26

基本上我的脚本应该:

  1. 打开某张纸
  2. 检查特定范围的数据值是否低于 60%
  3. 找到后,检查该列的第一行,看看它是否显示"已发送">
  4. 如果是这样,什么都不做
  5. 如果没有,请在脚本中发送值低于 60% 等的电子邮件。
  6. 然后用"已发送"编辑该列的第一行一旦它为该列中低于 60% 的所有值发送消息。(我还没有写这部分。

说它运行良好,但它没有发送任何东西。 我从头开始编写了大部分代码,我是一个初学者,所以我想知道我是否有错误阻止它工作。 顺便说一句,我将在定时触发器上运行此运行。如果您介意查看并给我一些反馈,我将不胜感激。 我在下面添加了代码。
节日快乐!布兰登

   function sendEmail() { 
    var ss = SpreadsheetApp.openById('1CvK-ALbc-_GZwX4pqadBb67AVAou6euk55OE1axfbAk');
    var sheet = ss.getSheets()[0];  // The first of the above spreadsheet
    var range = sheet.getRange(3, 10, 40, 40); // Get 2D range (Starting Row (3) ,Starting Column (J), # Rows (40),# Colmuns (40))
    var value = range.getValue(); // get values of all cells in that range
        if (value < 0.6) {  // if it it get values that are less than 60%
            var editedsheet = value.getsheet();  // get sheet that value < 60% is in
            var editedRow = value.getRow();  // get row that value < 60% is in
            var column = value.getColumn();  // get column that value < 60% is in
            var status = editedsheet.getRange(0, column).getValue() // check row 1 of that column and get value
            
            if (status != 'Sent') {  // if that value is sent, do nothing
                }
            else {  // if the value isnt sent then...
                   var studentData = editedsheet.getRange(editedRow, 1, 1, 9).getValues();  // email message details
                   var message = 'Assessment score: ' + Math.round((value * 100) * 10) / 10 + ' %' +
                      ''nStudentId: ' + studentData[0][0] +
                      ''nName: ' + studentData[0][1] +
                      ''nHR: ' + studentData[0][2] +
                      ''nTeacher: ' + studentData[0][3] +
                      ''nGrade: ' + studentData[0][4] +
                      ''nRace: ' + studentData[0][5] +
                      ''nG: ' + studentData[0][6] +
                      ''nEd: ' + studentData[0][7] +
                      ''nAVG: ' + Math.round((studentData[0][8] * 100) * 10) / 10 + ' %';
                var emailAddress = 'email address';  // email details
                var subject = 'ALERT - Assessment score below 60% inputted.';
                MailApp.sendEmail(emailAddress, subject, message);
           }
       }
   }
   

这是指向示例电子表格的链接,例如我正在使用的电子表格。示例电子表格

您的代码存在一些问题。 例如,如果要获取范围的值,请使用 .getValues(( 而不是 .getValue((。此外,您还必须遍历返回的值。我认为您的代码应该如下所示:注意:未经测试的代码!

function assessmentAlert() {
SpreadsheetApp.getActive().getSheets()
.forEach( function (s) {
var val = s.getDataRange().getValues();
//check to see if correct cells where found ==> check the log when the script finishes
var count = 0;
//start looping through the values, first loop = rows, second loop = columns
for (var i = 0, ilen = val.length; i < ilen; i++) {
    for (var j = 0, jlen = val[0].length; j < jlen; j++) {
      //conditions: column > 8, row > 2, value should be numeric and < 0.6 and the headerrow should not have 'Sent'
        if (j > 8 && i >2 && !isNaN(parseFloat(val[i][j])) && val[i][j] < 0.6 && val[0][j] != 'Sent') {
            count += 1
           //if all conditions are met, send the email             
            var message = 'Assessment score: ' + Math.round((val[i][j] * 100) * 10) / 10 + ' %' +
                ''nStudentId: ' + val[i][0] +
                ''nName: ' + val[i][1] +
                ''nHR: ' + val[i][2] +
                ''nTeacher: ' + val[i][3] +
                ''nGrade: ' + val[i][4] +
                ''nRace: ' + val[i][5] +
                ''nG: ' + val[i][6] +
                ''nEd: ' + val[i][7] +
                ''nAVG: ' + Math.round((val[i][8] * 100) * 10) / 10 + ' %';
            var emailAddress = 'email@email.com'; // email details
            var subject = 'ALERT - Assessment score below 60% inputted.';
            MailApp.sendEmail(emailAddress, subject, message);
            s.getRange(1, j+1).setValue('Sent').setFontColor('Red');
        }
    }
}
Logger.log('number of values found:' + count);
});
}

注意:上述代码未经测试。因此,我建议您注释掉 MailApp.sendEmail 行并运行代码并检查记录器是否找到的值数量正确。我希望这有帮助 ?

您对"已发送"的检查正在查看getRange(0,column)。虽然它返回的数组从 0 开始,但应将第 1 行指定为getRange(1,column)