Home Ask Login Register

Developers Planet

Your answer is one click away!

Brady Burnsides February 2016

Are if and else if statements bugged in Google Apps Script?

I'm fairly new to GAS and Javascript in general and have searched far and wide to find an answer to why this isn't working, but haven't found a solution. I was wondering if one of you guys could find the problem. Here's my code:

function maintenanceRequest() {

var findFirstRequest = GmailApp.search("to:censored label:ems-request AND is:unread", 0, 1)[0];
  var firstRequest = findFirstRequest.getMessages()[0];
  var parseRequest = firstRequest.getPlainBody();
  var requestString = String(parseRequest);

  if ("Mark archived mail as read" == requestString) {
  else if ("Cleanup" == requestString) {
  else {
    GmailApp.sendEmail("censored", "Failure to parse command", "The EMS has recieved your request but has failed to recognize the command '" + parseRequest + "'. Please try again, using the terms 'Mark archived as read' or 'Cleanup'. If you would like to add an eligible command, please refer to function 'maintenanceRequest'.", {
      name: "Email Maintenance Service",
      from: "censored"
    //Add moveToArchive line here after debugging

The code always skips the if and else if statements and jumps to the else statement, regardless of the email's content. I've tried using both == and === to no avail, and have tried switching the sides that the arguments are on. To no avail. I even created a new var, requestString to convert parseRequest to a string, even though I'm like 99% certain that it already is a string.. so what gives? Where's the problem?


tanenbring February 2016

Try adding trim to the string: requestString = requestString.trim()

It's usually a safe bet to use trim on any string you're getting from another service.

Post Status

Asked in February 2016
Viewed 1,818 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life