Fayt Leingod
Fayt Leingod

Reputation: 79

Google Apps Script Issue parsing number into variable

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. */
}

Upvotes: 0

Views: 481

Answers (1)

random-parts
random-parts

Reputation: 2225

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

Upvotes: 1

Related Questions