我想根據單元格值將電子郵件發送給相應的人,并附上該行的表格。例如,在作業表中,當列 D==="New" 和 "Ongoing" 時,將向 C 列的收件人發送一封電子郵件,其中包含該行的生成表。我不想要 onedit 觸發器,因為我每周只發送一次電子郵件。當表格生成時,它只需要列資源、狀態、到期日期和鏈接。我已經撰寫了用于發送電子郵件和創建表格的代碼。問題是當我創建表時,它正在獲取狀態為新的所有資料。出于這個原因,所有收件人都會獲得同一個表格,但我希望表格的方式是收件人只會獲得他相應行的表格。我在這里附上了我的代碼。誰能建議我應該在這里添加什么?表鏈接:https://docs.google.com/spreadsheets/d/1GC59976VwB2qC-LEO2sH3o2xJaMeXfXLKdfOjRAQoiI/edit#gid=0
代碼:
function myFunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var sheet=ss.getSheetByName("Sheet3")
var sheetData=sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn()).getValues()
var filteredRows_new = sheetData.filter(function(row){
if (row[3] === 'New') {
return row
}
})
// create the html table
var header=sheet.getRange(1,1,1,sheet.getLastColumn()).getValues()
var resource=header[0][1]
var status_r=header[0][3]
var due_date_r=header[0][5]
var link_r=header[0][10]
var data=filteredRows_new.map(col=>[col[1],col[3],col[5],col[10]])
var htmltemplate=HtmlService.createTemplateFromFile("email")
htmltemplate.resource=resource
htmltemplate.status_r=status_r
htmltemplate.due_date_r=due_date_r
htmltemplate.link_r=link_r
htmltemplate.data=data
var htmlformail=htmltemplate.evaluate().getContent()
console.log(htmlformail)
// extract only the new status data for email
filteredRows_new.forEach(function(row) {
//Email address
var email = row[2];
GmailApp.sendEmail(email,"automation","open the link for html", { htmlBody: htmlformail })
})
//extract the rows for ongoing
var filteredRows_ongoing = sheetData.filter(function(col){
if (col[3] === 'ongoing') {
return col
}
})
//console.log(filteredRows_ongoing)
}
uj5u.com熱心網友回復:
我遇到了一個非常相似的問題,似乎應用程式腳本檔案中沒有專門的功能。這就是為什么我試圖創建一個能夠以優雅的方式完成它的庫。
您的檔案已鎖定,我們無法訪問!請參閱下面我可以提供的最佳答案,必須使用您的資料更新標題名稱等一些值。
為了能夠使用這些庫,您必須創建兩個新的 gs 腳本并復制粘貼兩個腳本 UtilsGSheetTableHelper.gs 和 UtilsGmailHelper 可用:https ://github.com/SolannP/UtilsAppSsript
然后在 code.gs 檔案上
function macro(){
// get range of cell with data from A1 to any cell near having value (call data region)
var table = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3").getRange("A1").getDataRegion();
// create custom table filtered by the column having header "State" where the value match "New"
var filterTable = new TableWithHeaderHelper(table)
.getTableWhereColumn("State").matchValueRegex(/(New)|(Ongoing)/);
// for each row matching the criteria
for(var i=0; i< filterTable.length() ; i ){
// Get cell range value at column Mail
var mail = filterTable.getWithinColumn("Mail").cellAtRow(i).getValue();
// Any other value of column Target Value
var anyOtherValue = filterTable.getWithinColumn("Target Value").cellAtRow(i).getValue();
// Send email
MailApp.sendEmail({
to:mail ,
subject: "BIKINI ASSEMBLY",
cc:cc,
htmlBody:`<h1>Sheet table weekly</h1><hr><p>${anyOtherValue}<p>`,
});
}
}
然后,您可以將腳本附加到一個按鈕,以便在一周結束時單擊。
作為進一步的改進,您可以使用上次發送郵件的日期更新單元格,甚至更多!
小心 !
更新
請參閱匹配您的請求的代碼:
function macro(){
// get range of cell with data from A1 to any cell near having value (call data region)
var table = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3").getRange("A1").getDataRegion();
// create custom table filtered by the column having header "State" where the value match "New"
var filterTable = new TableWithHeaderHelper(table)
.getTableWhereColumn("Status").matchValueRegex(/(New)/);
// for each row matching the criteria
for(var i=0; i< filterTable.length() ; i ){
// Get cell range value at column Mail
var mail = filterTable.getWithinColumn("Email").cellAtRow(i).getValue();
// Any other value of column Target Value
var bodyMail = filterTable.getWithinColumn("Description").cellAtRow(i).getValue();
var subjectMail = filterTable.getWithinColumn("Title").cellAtRow(i).getValue();
// Send email
MailApp.sendEmail({
to:mail ,
subject: subjectMail,
htmlBody:subjectMail,
});
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/516113.html
標籤:电子邮件谷歌应用脚本
下一篇:Python電子郵件發送
