设置onEdit()可安装触发器以识别IMPORTRANGE数据的编辑

时间:2014-12-18 16:04:08

标签: javascript google-apps-script google-sheets

" Rckrd"所以亲切/熟练地帮助我使用下面的第一个脚本,在电子表格中查找低于60%的数据,并发送一封包含数据信息的电子邮件。(这很棒!)此脚本设置为在可安装的onEdit上运行如果我手动进入电子表格并更改单元格值,则触发并运行良好。

但它不适用于我们的数据系统,因为我们的系统使用IMPORTRANGE使用其他电子表格中的数据填充列。现在我的理解是onEdit需要手动输入才能触发。是否可以添加监视工作表中列长度的第二个脚本(下面),将值保留在某处(例如在脚本属性中),如果添加了一列数据,则调用assessmentOnEdit()函数?我认为在帖子here中解释了类似的代码。我甚至不确定这是否可以用我上面的第一个代码实现,或者它是让onEdit识别IMPORTRANGE添加数据的最佳方法。 非常感谢您给予的任何帮助和节日快乐! 布兰登

这是他帮助我的原始帖子的链接。 Original Post



  function assessmentOnEdit(e) {
    var range = e.range;

    if (range.getColumn() >= 10) { // Only check column I and up
        var editedSheet = e.source.getActiveSheet();
        var editedRow = range.getRow();
        var value = range.getValue();
        if (typeof value === 'number') {
            if (value < 0.6) {
                var studentData = editedSheet.getRange(editedRow, 1, 1, 9).getValues();
                var message = 'Assessment score: ' + Math.round((value * 100) * 10) / 10 + ' %' +
                    '\nStudentId: ' + studentData[0][0] +
                    '\nName: ' + studentData[0][3] +
                    '\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 = 'john.doe@example.com';
                var subject = 'ALERT - Assessment score below 60% inputted.';
                MailApp.sendEmail(emailAddress, subject, message);
            }
        }
    }
}
&#13;
&#13;
&#13;

&#13;
&#13;
function lookatsheet(){
  var ss = SpreadsheetApp.openById('Spreadsheet ID');// the ID of the SS you want to look at
  var sh = ss.getSheets()[1];// Second Sheet
  var lastcolumn = sh.getLastColumn() -2;// Get the last column that was inputted (minus the 2 top header rows, not sure if this is right)
  var formertest = ScriptProperties.getProperty('lastTest');// recover the value from the last test (will need to get called once to initiate a valid value)
  if (formertest < lastcolumn){
    sheetWasEdited(lastcolumn);// call your function with lastRow as parameter
    ScriptProperties.setProperties({'lastTest': lastcolumn}, true);   // store for next time
}
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

如果您的用例是在电子表格(A)中发送电子邮件数据低于60%的电子邮件,那么工作表始终从不同电子表格(B)的ImportRange更新,而不是onEdit(e),您可以只需编辑lookatsheet()函数,即可通过以下方式从电子表格(A)中获取新的值范围:

    function lookatsheet(){
      var ss = SpreadsheetApp.openById('Spreadsheet ID');// the ID of the SS you want to look at
      var sh = ss.getSheets()[1];// Second Sheet
      var lastcolumn = sh.getLastColumn() -2;// Get the last column that was inputted (minus the 2 top header rows, not sure if this is right)
      var formertest = ScriptProperties.getProperty('lastTest');// recover the value from the last test (will need to get called once to initiate a valid value)
      if (formertest < lastcolumn){
        sheetWasEdited(lastcolumn);// call your function with lastRow as parameter
        ScriptProperties.setProperties({'lastTest': lastcolumn}, true);   // store for next time

// range would be the new range from the Sheet.

if (range.getColumn() >= 10) { // Only check column I and up
        var editedSheet = e.source.getActiveSheet();
        var editedRow = range.getRow();
        var value = range.getValue();
        if (typeof value === 'number') {
            if (value < 0.6) {
                var studentData = editedSheet.getRange(editedRow, 1, 1, 9).getValues();
                var message = 'Assessment score: ' + Math.round((value * 100) * 10) / 10 + ' %' +
                    '\nStudentId: ' + studentData[0][0] +
                    '\nName: ' + studentData[0][3] +
                    '\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 = 'john.doe@example.com';
                var subject = 'ALERT - Assessment score below 60% inputted.';
                MailApp.sendEmail(emailAddress, subject, message);
            }
        }
    }
    }
    }

希望有所帮助!