Google function returning undefined

42
October 22, 2019, at 11:30 PM

I have an issue with a custom google script I'm making to generate a bunch of sheets with info based on other sheets. I can't figure out why this is happening..

I've tried including logs and the values before the return is correct.. however when its returned, I get the value undefined. it's regarding the function: getTournamentInfo(), called from tournamentInfo = getTournamentInfo(matchInfo[0]);

function getTournamentInfo(abbreviation) {
  var sheet = ss.getSheetByName("Tournaments");
  var tournaments = sheet.getRange("B2:B").getValues().filter(String);
  console.log("Fetching Abbreviation: " + abbreviation);
  var r = 2;
  tournaments.forEach(function (tournament) {
    if (tournament != "")
    {
      var tInfo = sheet.getRange("B"+r+":K"+r).getValues().toString().split(",");
      if (tInfo[0] == abbreviation) {
        console.log("Returning Info for: " + tInfo[0]);
        return tInfo;
      }
    }
  });
}
function generateSheets() {
  var sheet = ss.getSheetByName("Match Schedule");
  var matches = sheet.getRange("B5:B").getValues().filter(String);
  var r = 5;
  matches.forEach(function (match) {
    if (match != "")
    {
      var matchInfo = sheet.getRange("B"+r+":L"+r).getValues().toString().split(",");
      if (matchInfo[10] == "true") // Checks wether or not to generate the sheet
      {
        console.log("Generate = " + matchInfo[10]);
        console.log("Fetching Tournament Info: " + matchInfo);
        var tournamentInfo = "";
        try {
          tournamentInfo = getTournamentInfo(matchInfo[0]);
        } catch (e) {
          console.log(e);
        }
        console.log(tournamentInfo);
        var template = "1v1PlayerTemplate"; // Default Template
        if (tournamentInfo[3] == 2) {
          template = "1v1TeamTemplate";
        } else if (tournamentInfo[3] == 3) {
          template = "XvXTeamTaplte";
        }
        var sheetName = matchInfo[0] + " | " + matchInfo[1];
        var matchSheet = ss.getSheetByName(template).copyTo(ss.getSheetByName(template).getParent()).setName(sheetName);
      }
    }
    r++;
  });
}```
Answer 1

Your getTournamentInfo function is not returning your result. Your return statement only short-circuits the function supplied in forEach. This is one of the possible solutions (untested):

function getTournamentInfo(abbreviation) {
    var sheet = ss.getSheetByName("Tournaments");
    var tournaments = sheet.getRange("B2:B").getValues().filter(String);
    console.log("Fetching Abbreviation: " + abbreviation);
    var r = 2;
    let result; // <----
    tournaments.forEach(function (tournament) {
        if (tournament != "" && result == undefined) { // <-----
            var tInfo = sheet.getRange("B" + r + ":K" + r).getValues().toString().split(",");
            if (tInfo[0] == abbreviation) {
                console.log("Returning Info for: " + tInfo[0]);
                result = tInfo; // <----
            }
        }
    });
    return result; // <----
}

You can do it more simply with a for loop, instead of forEach:

function getTournamentInfo(abbreviation) {
    var sheet = ss.getSheetByName("Tournaments");
    var tournaments = sheet.getRange("B2:B").getValues().filter(String);
    console.log("Fetching Abbreviation: " + abbreviation);
    var r = 2;
    for (const tournament of tournaments) { // <==============
        if (tournament != "") {
            var tInfo = sheet.getRange("B" + r + ":K" + r).getValues().toString().split(",");
            if (tInfo[0] == abbreviation) {
                console.log("Returning Info for: " + tInfo[0]);
                return tInfo;
            }
        }
    }
}
READ ALSO
Can I develop Node.js (Express) app in both JavaScript and TypeScript together?

Can I develop Node.js (Express) app in both JavaScript and TypeScript together?

I have Nodejs project (Express REST API) written in JavaScript

36
Account Linking response for Actions On Google not correct in Hindi

Account Linking response for Actions On Google not correct in Hindi

I am making custom Google Action and in my app I am using account linkingThe response for when Google asks the user for confirmation in English is Correct but the same in Hindi is not

15
Mongoose ValidationError (Path is required)

Mongoose ValidationError (Path is required)

i am trying to write a post method in express

35