0

I have created a script which automatically responds to emails that are sent to me on my days off. I recently decided to upgrade it so that I can update the days off by sending myself a specially formatted email, pulling the data from the body, storing that data in a Google Sheet, then pulling that value and using it as my days off value.

I have almost everything working the way I need it to, but when passing the value I read from the cell to the variable, it does not work. However when I set it as a static number in the code, it works fine. I thought it was because it was being read as a string so I broke it apart and made them specifically into numbers. However this also didn't work. So now I am stuck. Any help would be appreciated.

Here is my code:

function autoResponder(){
  var content;
  var myemail = Session.getActiveUser().getEmail();

  //Searches your Google Drive for the spreasdsheet that will hold your day off values.
  var searchDrive = DriveApp.searchFiles('title contains "AutoResponder Data"')
  if (searchDrive.hasNext()){
    var spreadsheet = SpreadsheetApp.open(searchDrive.next());
    var sheet = spreadsheet.getSheets()[0];
    var data = sheet.getRange("A1");

    // Searches for your updater email and retrieves the message.   
    var findlabel = GmailApp.getUserLabelByName('AutoResponderUpdate');
    var thread = findlabel.getThreads()[0];

    //Checks if an update email was found.
    if (thread != undefined){
      var threadId = thread.getId();
      var message = thread.getMessages()[0];

      //Copies the data from your email and pastes it into a spreadsheet, then deletes the email entirely.
      var content = message.getPlainBody();
      var writeData = data.setValue(content);
      Gmail.Users.Threads.remove(myemail, threadId); 
    } else {
      //Reads data from spreadsheet if no email messages are found.
      var readData = data.getValue();
      var content = readData;
    }
  } else {
    //Creates the spreadsheet that will hold your day off values if one is not found.
    SpreadsheetApp.create('AutoResponder Data');
    autoResponder();
  }  
  // Specifies which days you have off. Sun=0 Mon=1 Tue=2 Wed=3 Thurs=4 Fri=5 Sat=6
  //var daysOff = [content.toString().replace("char(10)", "")];
  //var daysOff = [5,6];

  var test = content.split(",");
  var test2 = test.length;
  var output = "";
  if (test2 > -1){
    for (var n = 0; n < test2; n++){
      if (n === (test2 - 1)){
        output = output + parseInt(test[n]);
      } else {
        output = output + parseInt(test[n]) + ",";
      }
    }
  }
  var daysOff = [output]; 
  Logger.log(daysOff); 

  /* There is some code after this to auto reply to the email,
     but I've left that out. */
}
3
  • When you say it's not working, I gather that you mean Logger.log(daysOff) logs nothing if you are getting the values from the email or the spreadsheet. What do the logs tell you if you log the values of message, content, readData, test, test2 & output along the way? Are you able to get your code to work with just getting the values from the spreadsheet? Commented Sep 19, 2017 at 0:33
  • @DeanRansevycz So, the Logger.log(daysOff) DOES display the correct data. For instance, if I manually set daysOff as [5,6], the log will display [5.0,6.0] but if I grab the value 5,6 from and email or spreadsheet cell, it will log as [5,6]. From this I assume it is acting like a string instead of numbers, but I don't know why or how to change that. Hopefully that clarified it. Commented Sep 20, 2017 at 1:51
  • Still not understanding your problem. Is it that the logic you have for firing your auto-response on your days off is not interpreting the values in daysOff when populated from the email or spreadsheet? Incidentally, why is output a string, rather than an array? Does your auto-reply code expect an array? Commented Sep 20, 2017 at 3:49

1 Answer 1

1

It's acting like a string because you are concatenating string + number:

output = output + parseInt(test[n])

resulting in a string instead of a number.

var test = content.split(",");
var test2 = test.length;
var output = [];

test.forEach(function(e) { output.push(parseInt(e)) });

var daysOff = output;
Logger.log(daysOff); 

The above should produce the array of number values you are looking for

Sign up to request clarification or add additional context in comments.

1 Comment

That was the magical line of coding I was hunting for, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.