通常情況下,你可以使用函數像每一個細胞被編輯時自動觸發如下:
function onEdit(e){
var range = e.range; //Get edited range
if (range.getA1Notation() == 'C2'){
var calculatedValue = range.offset(0,1); //Assumes that the calculated value is in the cell to the right of the cell where the area is entered
var body = 'You want carpeting for a '+ range.getValue() + ' location. This will cost ' + calculatedValue.getValue();
try{
GmailApp.sendEmail('[email protected]', 'new carpet request', body);
}
catch(e)
{
Logger.log(e)
}
}
}
不幸的是你,當一個電子表格編輯不允許腳本onEdit()引發火災發送電子郵件。見https://developers.google.com/apps-script/guides/sheets/functions#advanced
你的下一個最好的辦法是用一個菜單項夫婦它通過添加的OnOpen功能如下所示:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Scripts')
.addItem('Send Email', 'sendEmail')
.addToUi();
}
它調用一個sendEmail功能是這樣的:
function sendEmail(){
var range = SpreadsheetApp.getActiveSpreadsheet().getRange('C2');
var calculatedValue = range.offset(0,1);
var body = 'You want carpeting for a '+ range.getValue() + ' location. This will cost ' + calculatedValue.getValue();
try{
GmailApp.sendEmail('[email protected]', 'new carpet request', body);
}
catch(e)
{
Logger.log(e)
}
}
您顯然還需要添加一些錯誤檢查和漂亮的語法,讓您的電子郵件以單位和所有內容顯示出來,除非您打算在電子表格中這樣做。
非常感謝你!它的工作。我已經擴展了腳本以獲得更多的值和它的工作。謝謝你的明確回答。 – 2014-11-01 14:00:41