Monday, 4 February 2019

Save email to Drive add-on - get user info

Overview blog post.

The following Google Apps Script code extracts the entered username from the event data and saves it as a variable:
var userId = e.formInput['userId'];
It checks to make sure the value is not blank before proceeding (ie the user has not just clicked the submit button) otherwise nothing else will occur. If a value has been entered we then call the getDetails function detailed in the next blog post, to get the corresponding name and folder ID. As this returns an array of student data we can then split the values appropriately - meaning we only need to interrogate the spreadsheet data once throughout the add-on:
var forename = details[0][0];
var surname = details[1][0];
var folderID = details[2][0];
var errorCode = details[3][0];
Next we need to determine the status of the error code in order to run the appropriate showCard(0) function, so we use an if statement. If the value is 0 then we run the showCard0 function and pass the above parameters through:
if (errorCode == 0) {
      Logger.log('errorCode is 0');
      //run showCard '0' function
      return showCard0(userId, forename, surname, folderID);
}
Else if the value is 1, 2, 3 or 5 we run the showCard function:
else if ( (errorCode == 1) || (errorCode == 2) || (errorCode == 3) || (errorCode == 5) ) {
      Logger.log('errorCode is: ' + errorCode);
      //run showCard function
      return showCard(errorCode);
}

function getUserInfo(e) {
//get user id from event data
var userId = e.formInput['userId'];
Logger.log('Inputted username is: ' + userId);
//check if entered username is not blank before proceeeding
if (userId != '') {
//call function to get the name + folderID for the user
var details = getDetails(userId);
//set variables from above function
var forename = details[0][0];
var surname = details[1][0];
var folderID = details[2][0];
var errorCode = details[3][0];
//check status of returned error code from getting users details
if (errorCode == 0) {
Logger.log('errorCode is 0');
//run showCard '0' function
return showCard0(userId, forename, surname, folderID);
}
else if ( (errorCode == 1) || (errorCode == 2) || (errorCode == 3) || (errorCode == 5) ) {
Logger.log('errorCode is: ' + errorCode);
//run showCard function
return showCard(errorCode);
}
}//end of check if entered username is not blank before proceeeding
else {
//do nothing if no username is entered
}
}

No comments:

Post a Comment