您可以在 Google 脚本中为多张纸发送通知吗?

Can you Sendnotification in Google Script for multiple sheets?

我想在特定单元格(即文档)更新时向某人发送通知。具体来说,当一行中的单元格被更新时,它会向同一行中列出的所有者发送通知。例如,B47 更新后,它会向 B3 发送一封电子邮件。

当脚本指向一张纸时,我已经让脚本开始工作。但是,当我尝试让脚本在多个工作表/标签上运行时,我收到错误 \\'找不到脚本函数:sendNotification\\' 是否可以让此脚本在同一个 Google 文档中为多个工作表/标签工作?更新来自不同的来源,所以我想继续使用 getSheetbyName 而不是 getActiveSheet.

任何帮助将不胜感激。这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function shellFunction() {
  var sheets = ['Arabic', 'Portuguese'];
  for (var s in sheets) {
    toTrigger(sheets[s]);
  }

  function toTrigger(sheetName) {
    function sendNotification() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName(sheets[s]);
      var cell = ss.getActiveCell().getA1Notation();
      var row = sheet.getActiveRange().getRow();
      var cellvalue = ss.getActiveCell().getValue().toString();
      var recipients = sheet.getRange('J' + sheet.getActiveCell().getRowIndex()).getValue();
      var message = '';

      if (cell.indexOf('B') != -1) {
        message = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue()
      }
      var subject = 'The ' + sheet.getRange('F' + sheet.getActiveCell().getRowIndex()).getValue() + ' ' + sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' needs your Translation';
      var body = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' has been updated. Can you please update ' + sheet.getRange('G' + sheet.getActiveCell().getRowIndex()).getValue() + '? Please remember to update the date column in the Resource Document when the translation is complete:' + ss.getUrl();
      MailApp.sendEmail(recipients, subject, body);
    }
  }
}


该不该行

1
var sheet = ss.getSheetByName(sheets[s]);

改为

1
var sheet = ss.getSheetByName(sheetName);

因为那是传递的表单的名称?

编辑

还有一些其他项目可能导致问题。我对此结果进行了编辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function shellFunction() {
  var sheets = ['Arabic', 'Portuguese'];
  for (var s in sheets) {
    toTrigger(sheets[s]);
  }
}

  function toTrigger(sheetName) {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName(sheetName);
      var cell = ss.getActiveCell().getA1Notation();
      var row = sheet.getActiveRange().getRow();
      var cellvalue = ss.getActiveCell().getValue().toString();
      var recipients = sheet.getRange('J' + sheet.getActiveCell().getRowIndex()).getValue();
      var message = '';

      if (cell.indexOf('B') != -1) {
        message = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue()
      }
      var subject = 'The ' + sheet.getRange('F' + sheet.getActiveCell().getRowIndex()).getValue() + ' ' + sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' needs your Translation';
      var body = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' has been updated. Can you please update ' + sheet.getRange('G' + sheet.getActiveCell().getRowIndex()).getValue() + '? Please remember to update the date column in the Resource Document when the translation is complete:' + ss.getUrl();
      MailApp.sendEmail(recipients, subject, body);
  }

我的第一个变化是不嵌套函数。为此,我在 toTrigger 函数之前添加了一个右大括号,并删除了代码中的最后一个。之后还有另一个嵌套函数,它实际上构成了所有的 toTrigger 函数。它被称为 sendNotifocation 匹配您的错误。我通过删除 toTrigger 函数声明之后的行和代码末尾的另一个大括号来删除它。

最后,我进行了上述编辑。在我的测试中,它似乎工作正常,只是我注释掉了实际发送电子邮件的调用,并且仅在该点之前调试到该行。