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. */
}
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 ofmessage,content,readData,test,test2&outputalong the way? Are you able to get your code to work with just getting the values from the spreadsheet?Logger.log(daysOff)DOES display the correct data. For instance, if I manually setdaysOffas[5,6], the log will display[5.0,6.0]but if I grab the value5,6from 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.daysOffwhen populated from the email or spreadsheet? Incidentally, why isoutputa string, rather than an array? Does your auto-reply code expect an array?