0

Trying to call a variable from a sub function, but "sheet" is not defined. Any suggestions on how to call it?

function getDate(){
var mnthYearDate = Utilities.formatDate(new Date,"EST","MMM yyyy");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mnthYearDate);
}

function getValue(){
getDate();
var target = sheet.getRange("A1").getValue();
}
Halen Lakes
  • 35
  • 2
  • 8

2 Answers2

1

Return it from your getValue() function and pass it as parameter to getValue(sheet) function.

Alex Dragnea
  • 174
  • 1
  • 6
1

The variable 'sheet' here is only known in your function getDate because you defined it there. To make it available to other functions, define it outside like what I did here.

var sheet;

function getDate(){
var mnthYearDate = Utilities.formatDate(new Date,"EST","MMM yyyy");
 sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(12345);
}

function getValue(){
    getDate();
    var target = sheet.getRange("A1").getValue();
    Logger.log(target);  
}
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56