简体   繁体   中英

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 = "x@gmail.com";
  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM