使变量成为单元格内的文本

时间:2017-09-14 05:05:09

标签: email google-apps-script google-sheets

我正在发送电子表格中的电子邮件。我希望B2的值成为电子邮件的正文。这样我就可以在消息中输入我要说的单元格B2。我将脚本设置为发送工作表中包含的图表和数据,这是有效的。但是,当我尝试将电子邮件的正文设置为单元格B2的值时,它只是发送一条消息“Range”,然后添加图表。

到目前为止,我的代码看起来像这样

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 = "efazekas@dcsdk12.org"

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

我知道这很粗糙,可能不是很简洁或优雅,但除了整个电子邮件正文问题外,它还能满足我的需求。

1 个答案:

答案 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();