簡體   English   中英

從單元格值Google工作表發送電子郵件,以及電子郵件中的其他列

[英]Send email from cell value google sheet, with other columns in email

很抱歉,我已經問過幾次了,這是我從其他問題中學到的,但是我仍然很困惑,不確定如何繼續。

我正在嘗試制作一個Google腳本,根據單元格更改將電子郵件通知發送給一個以上的人,同時在同一行中向電子郵件發送多個單元格值?

例如,如果“ C”列發生更改,是否可以發送電子郵件,其中包含“ C”列更改的同一行中“ A”列和“ B”列的數據?

這是我一直在嘗試使用的腳本,但不確定如何添加多封電子郵件以及另外兩個單元格中的值。

function sendNotifications() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = email@domain.com";
var message = '';
var cellA = ''
if(cell.indexOf('B')!=-1){ 
message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'Update to '+sheet.getName();
var body = + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
};

下面的代碼檢查是否已編輯的單元格位於C列中,如果滿足條件,則從已編輯的行中獲取A列和B列的值。 在Google Apps腳本中,onEdit()是內置函數,當您在電子表格中進行更改時會自動觸發。 “ e”參數表示事件對象,您可以檢查該事件對象以獲取有關事件發生的上下文的更多詳細信息

function onEdit(e){

  var editRange = e.range; // e is the event object generated by the Edit event
  var editRow = editRange.getRow();   // get row and column for the edited cell
  var editCol = editRange.getColumn();


  var sheet = SpreadsheetApp.getActiveSpreadsheet()
                            .getSheetByName("Sheet1"); // put your sheet name between the brackets

  if (editCol == 3) {   // checking if the edited cell was in column C

      var range = sheet.getRange(editRow, 1, 1, editCol - 1); // Take 1st cell in the edited row and make it span 1 row and 2 columns
      var values = range.getValues(); // get values array for the range
      Logger.log(values);

  }

}

結果變量(“值”)是一個數組,因此必須遍歷它才能獲取字符串值。 至於電子郵件,您可以將多個電子郵件存儲在一個單元格中,並以逗號分隔。 在字符串上調用split()方法將獲得一個值(電子郵件)數組,然后可以將其作為第一個參數傳遞給sendEmail方法。

  var cellValue = "email@example.com,anotheremail@example.com,yetanotheremail@example.com";
  var recipients = cellValue.split(",");  
  MailApp.sendEmail(recipients, "From google-apps-script", "hello from google apps script")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM