Abigail Bianchi
Abigail Bianchi

Reputation: 11

Why am I getting "TypeError: Cannot read property 'getRange' of undefined" for line 2 sheet.getRange?

This function was working perfectly until this morning and I can't find the reason for the error... I have edited the first line to include the Spreadsheet and it says it is executing normally now but there is no actual post

function createMeetingMessage(sheet) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var values = ss.getSheetByName("Post") .getRange("B2:D40").getValues(); // Modify this range, if you want to expand it.
  var message = values.reduce(function(s, e) {
    var SlackIDrow, YearsWorkedrow, Congratsrow, _,
    [SlackIDrow, YearsWorkedrow, Congratsrow, _, _, ] = e;
    if (SlackIDrow && YearsWorkedrow && Congratsrow) {
      s += "> *Human* " +"<"+ SlackIDrow +">"+"\n"
     + "> *Years worked as of today* " + YearsWorkedrow + "\n"
     + Congratsrow + "\n"
      +"\n";
    }
    return s;
  }, "> *Today's Workiversaries!*" +"\n");
  return message;
}

function triggerSlackWorkaversaryLog(channel, msg) {
 var slackWebhook = "https://hooks.slack.com/services/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";
  var payload = { "channel": channel, "text": msg, "link_names": 1, "username": "Time to Partyyyyyy", "icon_emoji": ":confetti_ball:" };
 var options = { "method": "post", "contentType": "application/json", "muteHttpExceptions": true, "payload": JSON.stringify(payload) };

Logger.log(UrlFetchApp.fetch(slackWebhook, options));
}

function notifySlackChannelofWorkaversaries() {
 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Post");
 if (!sheet.getRange("B2").getValue()) return;
var channel = getChannel();

var msg = createMeetingMessage(sheet);
 triggerSlackWorkaversaryLog(channel, msg);
}

function isTest() {
 return false;
}

function getChannel() {
 if(isTest()) {
 return "intergration_tests";
 } else {
 return "intergration_tests";
 }
}

Upvotes: 0

Views: 2344

Answers (2)

Cooper
Cooper

Reputation: 64032

If you wish to run that function from the script editor you could do this:

function testCreateMeetingMessage() {
  Logger.log(CreateMeetingMessage(SpreadsheetApp.getActive());
}

Upvotes: 1

doubleunary
doubleunary

Reputation: 18698

The problem is not with the function you quote but with the function that calls it. The sheet parameter must be a SpreadsheetApp.Sheet object (rather than a sheet name as text string or something like that.)

You will also get the error you quote if you run the function directly in the script editor.

Upvotes: 0

Related Questions