2014-10-30 274 views
0

我是Google腳本的初學者,我正在尋找一個possability來從我的電子表格中讀取數值,並編寫一個包含這些值的自動電子郵件。我試圖在谷歌和論壇上找到一些信息,但我對它的理解不是很清楚。閱讀電子表格,通過電子郵件發送價值

例如:在我的電子表格中,C2是用戶填寫的單元格(它是每平方米計算器的價格)。當C2填充例如6平方米時,D2將計算價格。例如€240。

現在我想爲此工作表添加一個腳本,它會自動創建併發送一封電子郵件給我,內容包括:'您希望在'C2'位置放置地毯。這將花費'D2'。

我知道這是可能的,但我不知道如何...有人可以幫我嗎?

回答

0

通常情況下,你可以使用函數像每一個細胞被編輯時自動觸發如下:

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) 
    } 
} 

您顯然還需要添加一些錯誤檢查和漂亮的語法,讓您的電子郵件以單位和所有內容顯示出來,除非您打算在電子表格中這樣做。

+0

非常感謝你!它的工作。我已經擴展了腳本以獲得更多的值和它的工作。謝謝你的明確回答。 – 2014-11-01 14:00:41