I work at a university, and have set up 52 spreadsheets for 52 students. Each week, I need to lock a single sheet on each of these 52 spreadsheets. The sheets are all contained in a single folder. Each student's spreadsheet has sheets named '1' through '33' (representing 33 weeks), along with a couple of additional sheets.
I am thinking of having another spreadsheet with a script that lets me lock or unlock a week. On this spreadsheet, I would set it up as follows:
+---+-----------+---------------------+---------------------+
| | A | B | C |
+---+-----------+---------------------+---------------------+
| 1 | 1 |[email protected] |[email protected] |
| 2 | Lock | | |
+---+-----------+---------------------+---------------------+
A1 will be where I type which week to lock. In this example, it's week '1'. Row 1, Cells B1 through N1 will contain the email addresses of other university professors who should continue to have editing access. B2 will be where I indicate either "Lock" or "Unlock"
How can I loop through each file in this folder and set the editing permissions? Here is what I am unsuccessfully trying. It is a modification of another script that someone helped me with for the purpose of looping through and editing a specific range in each students' spreadsheet.
var idFolder = 'xxxxxxxxxxxxxxxxZLaXVPZzQ';
var folder = DriveApp.getFolderById(idFolder);
var contents = folder.getFiles();
var file;
var sheet;
var sheets;
var sheetName;
var range;
var strRangeProtect;
var protections;
var protection;
var editors = [???????? Range of B1:N1??????????];
var app = SpreadsheetApp;
var currentweekneedstobedefined = app.getActiveSheet().getRange(1, 1);
var thisweek = currentweekneedstobedefined.getValue();
while(contents.hasNext()) {
file = app.openById(contents.next().getId());
sheets = file.getSheets();
var thisweek = currentweekneedstobedefined;
protections = file.getProtections(app.ProtectionType.SHEET);
protection = protections(thisweek);
protection.addEditor(editors);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
So I have made some good progress. Here is where I am as of 10/27.
Issue 1: It now will protect the sheet '1', so that I am the only editor. I was thinking that I can remove everyone else first, and then add back the people in the editors array. However, they don't get added back as I was hoping with the statement protection.addEditors(editors); The error says: Invalid user: "[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]".
Issue 2: The other issue is that it doesn't loop through all student files. Instead, I noticed the arrays changesheets and editors keep increasing in dimensions. After passing through the while loop a few times, the changesheets array is changesheets[36][36][36] and editors is editors[13][13][13].
function increaseProtection(idFolder, sheetNames) {
var folder = DriveApp.getFolderById(idFolder);
var contents = folder.getFiles();
var file;
var app = SpreadsheetApp;
var currentweekneedstobedefined = app.getActiveSheet().getRange(1, 1);
var thisweek = currentweekneedstobedefined.getValue();
var ss = SpreadsheetApp.getActiveSpreadsheet(); //get spreadsheet
var sheet = ss.getActiveSheet();
var editors = sheet.getRange('B1:N1').getValues(); //get values of B1:N1 as array
while(contents.hasNext()) {
file = app.openById(contents.next().getId());
var fname = file.getName();
var changesheets = file.getSheets();
var thisweeksSheet = file.getSheetByName(thisweek);
var me = Session.getEffectiveUser();
var permissions = thisweeksSheet.getSheetProtection();
var protection = thisweeksSheet.protect().setDescription('Supervisors Only');
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
protection.addEditors(editors);
}
}