2017-09-14 100 views
-3

我從電子表格發送電子郵件。我希望B2的價值是電子郵件的正文。這樣我可以在消息中輸入B2單元格。我已經將腳本設置爲發送工作表中包含的圖表和數據,並且工作正常。但是,當我試圖將電子郵件的正文設置爲單元格B2的值時,它只是發送一條說明「範圍」的消息,然後添加圖表。使單元格內的文本變量

我的代碼看起來像這樣迄今

function emailParentsUpdates(sheet,emails,emailSubject){ 
    var targetspreadsheet = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = targetspreadsheet.getActiveSheet(); 
    var emailSubject = 'Grades Update'; 
    var charts = sheet.getCharts(); 
    var startRow = 1; // First row of data to process 
    var numRows = 1; // Number of rows to process 
    var dataRange = sheet.getRange(startRow, 1, numRows, 1) 
    var data = dataRange.getValues(); 
    for (i in data) { 
    var row = data[i]; 
    var emailAddress = row[0]; // First column 
    var teacherEmail = "[email protected]" 

    if(charts.length==0){ 
     MailApp.sendEmail({ 
     to: teacherEmail, 
     subject: "ERROR:"+emailSubject, 
     htmlBody: "No charts in the spreadsheet"});  
     return; 
    } 

    var chartBlobs=new Array(charts.length); 
    //this is where the issue is. I need the email body to be the value, the actual words, of cell B2 
    var emailBody = sheet.getRange('B2'); 
    var emailImages={}; 
    for(var i=0;i<charts.length;i++){ 
     var builder = charts[i].modify(); 
     builder.setOption('vAxis.format', '#'); 
     var newchart = builder.build(); 
     chartBlobs[i]= newchart.getAs('image/png'); 
     emailBody= emailBody + "<p align='center'><img src='cid:chart"+i+"'></p>"; 
     emailImages["chart"+i]= chartBlobs[i]; 
    } 

    MailApp.sendEmail({ 
     to: emailAddress, 
     subject: emailSubject, 
     htmlBody: emailBody, 
     inlineImages:emailImages 
    }); 
    } 
} 

我知道它的粗糙,也許不是很簡潔或優雅,但它做什麼,我需要除整個電子郵件正文問題。

+3

發佈您的代碼here.From初步印象,解決辦法是,你必須得到的範圍內 –

+0

發佈您的代碼的價值。 –

回答

0
function getB2Value(){ 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var range = sheet.getRange('B2'); 
    return range.getValue(); 
} 

getRange()返回range,你需要調用getValue()才能到單元格的值的參考。即:

var emailBody = sheet.getRange('B2').getValue(); 
+0

我不確定將它放在代碼中的哪個位置,以便可變的emailBody可以等於B2的值。希望我的代碼很有意義。 –

+0

@LizFazekas我已經更新了我的答案,使之更加清晰。 – rckrd

相關問題