I Have a csv file
with first name
, ID number
and email id
data of a certain set of people. I want to take this information and with it, in a Google Drive parent folder I want to create subfolders with the name firstname_ID_number
and set the access permission of each folder to the corresponding email only.
In other words each person in the list should only be able to access their own folder in that drive and not others'.
My first thought was to use Google Colab
and Google Drive API
to get this done. But I do not know how or where to start with. I would appreciate some kind guidance.
CodePudding user response:
You could start by importing your csv file in Google Sheets and later use Apps Script for your task.
Therefore, assuming your sheet will end up looking something similar to this:
First Name | ID Number | Email ID |
---|---|---|
Name1 | N1 | E1 |
Name2 | N2 | E2 |
Name3 | N3 | E3 |
You can make use of the Drive
advanced service in order to create the folders and assign the permissions. You can take a look at the below script in order to achieve this:
Code
function mainFunction() {
let spreadsheet = SpreadsheetApp.openById("SPREADSHEET_ID");
let sheet = spreadsheet.getSheetByName("SHEET_NAME");
let nameCol = sheet.getRange(START_ROW, START_COL, NO_ROWS, NO_COLS).getValues();
let idCol = sheet.getRange(START_ROW, START_COL, NO_ROWS, NO_COLS).getValues();
let emailCol = sheet.getRange(START_ROW, START_COL, NO_ROWS, NO_COLS).getValues();
for (let i = 0; i < nameCol.length; i ) {
let fileName = nameCol[i 1][0] "_" idCol[i 1][0];
let folderId = createFolder(fileName);
createPermission(emailCol[i 1][0], folderId);
}
}
function createFolder(fileName) {
let optionalArgs = { supportsAllDrives: true };
let resource = {
title: fileName,
mimeType: 'application/vnd.google-apps.folder',
parents: [{
"id": "SHARED_DRIVE_ID"
}]
}
let folder = Drive.Files.insert(resource, null, optionalArgs);
return folder.id;
}
function createPermission(emailAddress, folderId) {
let optionalArgs = { supportsAllDrives: true };
let resource = {
role: "ROLE",
type: "TYPE",
emailAddress: emailAddress
}
Drive.Permissions.insert(resource, folderId);
}
Explanation
The script is composed of three functions: mainFunction
, createFolder
and createPermission
. The mainFunction
is used to gather all the values from the sheet while createFolder
and createPermission
are used to create the folder and assign the necessary permission to it. Since you are working with shared drives, the right approach here is to use the Drive
advanced service.