stibb
stibb

Reputation: 11

getValue() function not returning anything in Google Scripts for Sheets

I'm trying to use google scripts to select a random row from google sheets and return some of the cell values in those rows to email to me.

Here's what I have so far:

function sendEmails() {

  var ss = SpreadsheetApp.openById('x');
  var sheet = ss.getSheetByName('report');

  var numRows = sheet.getLastRow();
  var randomRow = Math.floor(Math.random() * numRows); 

  var bodyTitle = sheet.getRange(3, randomRow).getValue();
  var bodyHighlight = sheet.getRange(4, randomRow).getValue();
  var bodyLink = SpreadsheetApp.getActiveSheet().getRange(2, randomRow).getValue();
  
  // var bodyTest = SpreadsheetApp.getActiveSheet().getRange(2, 2).getValue();

  var body = bodyTitle + " | " + bodyLink + " | " + bodyHighlight ; 
  var recipient = "[email protected]";
  var subject = "resurfaced highlights";

  MailApp.sendEmail(recipient, subject, body);

}

However when I receive the email all I get in the body is "| |" which makes it seem as though the getValue() functions for the bodyTitle, bodyHighlight, and bodyLink variables are not working. Any help would be appreciated!

Thanks.

Upvotes: 0

Views: 276

Answers (1)

Alfredo
Alfredo

Reputation: 792

The problem is in the order of the parameters.

It is supposed to be getRange(row, column) not getRange(column, row)

Just swap the order of the parameters.

Reference:

Upvotes: 2

Related Questions