我们已经建立了一个工作流电子表格,其中包含工作信息,例如客户名称,工作详细信息等。如果您从该菜单的最后一列的下拉菜单中选择“完成”,那么我有一个漂亮的脚本可以隐藏整行行。
我现在想做的是发送一封电子邮件,其中包含我们要隐藏的行中的所有数据。
已更新:这是我到目前为止的代码:
/**
* TITLE:
* Hide a row if a value is inputted then send an email
*/
//**GLOBALS**
// Sheet the data is on.
var SHEET = "Live Jobs";
// The value that will cause the row to hide.
var VALUE = "Complete";
// The column we will be using
var COLUMN_NUMBER = 22
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
//Ensure on correct sheet.
if(SHEET == activeSheet.getName()){
var cell = ss.getActiveCell()
var cellValue = cell.getValue();
//Ensure we are looking at the correct column.
if(cell.getColumn() == COLUMN_NUMBER){
//If the cell matched the value we require,hide the row.
if(cellValue == VALUE){
activeSheet.hideRow(cell)
SendEmail(e);
};
};
};
}
/**
* Sends emails with data from the current spreadsheet.
*/
function SendEmail(e) {
// Fetch the email address
var correctSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Live Jobs');
var emailRange = correctSheet.getRange('Z1');
var emailAddress = emailRange.getValue();
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getActiveCell()
var rowValue = cell.getRow();
var startRow = rowValue; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, 1, 5);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var message = row; // Second column
var subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);
}
}
现在,隐藏行有效,并且如果我从Google脚本编辑器运行它,电子邮件功能也有效,但是由于某些原因,当我选择“完成”时,电子邮件不会触发,但是昨天可以正常工作! / p>
有什么提示吗?
M
答案 0 :(得分:1)
尝试一下:
您可能无法通过简单的触发器来执行此操作,因为发送电子邮件需要授权。
var SHEET = "Live Jobs";
var VALUE = "Complete";
var COLUMN_NUMBER = 22
function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()==SHEET){
if(e.range.columnStart==COLUMN_NUMBER && e.value==VALUE){
sh.hideRow(e.range.rowStart);
SendEmail(e);
}
}
}
}
function SendEmail(e) {
MailApp.sendEmail(e.range.getSheet().getRange('Z1').getValue(), 'Invoice Job Alert', e.range.getSheet(e.range.rowStart,1,1,e.range.getSheet().getLastColumn()).getValues()[0].join(','));
}