Home > OS >  Retrieve last row values for specific columns to send an email
Retrieve last row values for specific columns to send an email

Time:12-03

Google Sheet Data

I want to retrieve CPC and Spend column values of the last row and send that email on a daily basis. I have successfully retrieved CPC but I am unable to do it for spend column.

Code.gs

function sendEmail () {

var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var data = ss.getRange(lr,3);
var values = data.getValues();
Logger.log(values);

var message = "Today CPC is "   values   "and spend is " ;
var email = "[email protected]";
var subject = "CPC Today"
MailApp.sendEmail(email,subject, message);
}

CodePudding user response:

In your script, the value is retrieved from only one column. I thought that this is the reason for your issue. So in this case, how about the following modification?

From:

var data = ss.getRange(lr,3);
var values = data.getValues();
Logger.log(values);

var message = "Today CPC is "   values   "and spend is " ;

To:

var [[cpc, , spend]] = ss.getRange(lr, 3, 1, 3).getValues();
var message = "Today CPC is "   cpc   " and spend is "   spend;

Reference:

  • Related