Last active
December 31, 2018 14:04
-
-
Save phillypb/bc53075aee34d30296696eb6466d3424 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addPermissions() { | |
// get relevant Sheets | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var groupSheet = ss.getSheetByName('Groups'); | |
var groupSheetName = groupSheet.getSheetName(); | |
var groupSheetValues = groupSheet.getDataRange().getValues(); | |
var groupLastRow = groupSheet.getLastRow(); | |
var tutorSheet = ss.getSheetByName('Tutors'); | |
var tutorSheetValues = tutorSheet.getDataRange().getValues(); | |
var tutorLastRow = tutorSheet.getLastRow(); | |
ss.toast('Starting to add permissions to each Group'); | |
// loop through each Group ********************************************************************************* | |
for (var i=1;i<groupLastRow;i++) { | |
// loop through Tutor 1 and Tutor 2 ********************************************************************** | |
var tutorDetails = [] // create empty array to push tutor details into | |
for (var k=1;k<=2;k++) { | |
var tutorName = groupSheetValues[i][k]; // get Tutor name | |
Logger.log('Tutor name is: ' + tutorName); | |
tutorDetails.push(tutorName); // add details to array | |
// loop to get Tutor email address ********************************************************************** | |
for (var j=1;j<tutorLastRow;j++) { | |
if (tutorName == tutorSheetValues[j][1]) { | |
Logger.log(tutorName + ' matches ' + tutorSheetValues[j][1]); | |
// if match found then get corresponding email address from column 1 | |
var tutorEmail = tutorSheetValues[j][0]; | |
Logger.log('Tutor email is ' + tutorEmail); | |
tutorDetails.push(tutorEmail); // add details to array | |
Logger.log(tutorDetails); | |
} | |
}// end of loop to get Tutor email address ************************************************************** | |
}// end of loop through Tutor 1 and Tutor 2 *************************************************************** | |
// extract ID from file URL | |
var groupFileURL = groupSheet.getRange(i+1, 4).getFormula(); | |
// check Group File Link is not blank before proceeding | |
if (groupFileURL) { | |
var groupFileID = groupFileURL.match(/[-\w]{25,}/); // regular expression to get continious string for ID | |
Logger.log('Group File ID is ' + groupFileID); | |
var groupFile = DriveApp.getFileById(groupFileID); | |
// loop through Tutors adding them as editors *********************************************************** | |
Logger.log(tutorDetails); | |
for (var l=1;l<tutorDetails.length;l+=2) { | |
var tutor = tutorDetails[l]; | |
groupFile.addEditor(tutor); | |
}// end of loop through Tutors adding them as editors *************************************************** | |
// open Group Sheet to add relevant Tutor details ******************************************************* | |
var sheet = SpreadsheetApp.openById(groupFileID); | |
var newSheetGroup = sheet.getSheetByName('Group Feedback'); | |
var newSheetTutors = sheet.getSheetByName('Tutors'); | |
// add Tutor details into Tutors sheet from array | |
newSheetTutors.getRange(2, 1).setValue(tutorDetails[1]); | |
newSheetTutors.getRange(2, 2).setValue(tutorDetails[0]); | |
newSheetTutors.getRange(3, 1).setValue(tutorDetails[3]); | |
newSheetTutors.getRange(3, 2).setValue(tutorDetails[2]); | |
// add Tutor details into Group Feedback sheet Headings from array | |
// add Tutor details into Column headings | |
newSheetGroup.getRange(1, 3).setValue('Tutor 1: ' + tutorDetails[0]); | |
newSheetGroup.getRange(1, 7).setValue('Tutor 2: ' + tutorDetails[2]); | |
// end loop of open Group Sheet to add relevant Tutor details ******************************************* | |
ss.toast('Tutors added to ' + groupSheetValues[i][0] + ' file'); | |
} | |
else { | |
ss.toast('Group File Link missing for ' + groupSheetValues[i][0]); | |
} | |
}// end of loop through each Group ************************************************************************** | |
ss.toast('Task completed'); | |
} | |
function onOpen() { | |
SpreadsheetApp.getUi() | |
.createMenu('Feedback') | |
.addItem('Add Sheet Permissions', 'addPermissions') | |
.addToUi(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment