Change Folder ownership

354 views
Skip to first unread message

Juan Cordoba

unread,
Jun 27, 2022, 10:01:18 PM6/27/22
to Google Apps Script Community
Hi guys!

I created this script that allows me to create a folder copy based on a template that we have. The user only needs to submit the ID of the DriveFolder's location into a Google Forms and the copy is created!

But one of the main issues with this is that every time a new user tries to copy a new folder with this tool the folders are created under my name.
Is there a way to add to my script the function ask the users from the google form their email and add this email as the owner of the file.

Thanks everyone!


Clark Lind

unread,
Jun 28, 2022, 12:17:41 PM6/28/22
to Google Apps Script Community
When the user submits the form, you should be able to grab their email address from their form input. Then after you create the folder copy, you can change ownership over to the email address that was submitted.

Juan Cordoba

unread,
Jun 28, 2022, 12:39:00 PM6/28/22
to Google Apps Script Community
Yes, that's the plan, but how can I do this with the new folder?
How can I tell App script to take the location where the user wants their folder, grab the new folder and change the ownership of the folder under his email?
Sorry I am just a newbie here with app script and java.

Clark Lind

unread,
Jun 28, 2022, 4:21:02 PM6/28/22
to Google Apps Script Community
Can you post your code snippet? I don't want to assume anything.. like are you using an onFormSubmit() trigger to run your code? etc etc. 

But that aside, I'll assume something triggered your code to run like the above trigger. 

function yourAwesomeFileCopyFunction(e) {   //pass in the event (e) that triggered your code
    var user = e.user.getEmail();  //something like this. You may have to research it.  
  //if you have trouble getting the email from the form submission, you can try getting it by grabbing whomever ran the script. In fact, that is probably the better way to go:
    var user = Session.getActiveUser().getEmail()
//    ....  your code to create the new folder....
//then
 newFolder.setOwner(user)

}

Juan Cordoba

unread,
Jun 29, 2022, 9:23:59 AM6/29/22
to Google Apps Script Community
Since I am creating a new folder I am not sure how I can invoke its script. I was thinking of assigning this new folder a very unique name and called by its name, but then I have an issue with ".setOwner". 
This is the script that I am using: 

function setUpTrigger(){
ScriptApp.newTrigger(makeCopy)
.forForm(('XXX'))
.onFormSubmit()
.create()
}

function makeCopy() {
var sourceFolder = "YYY";
let spreadsheet = SpreadsheetApp.openById('ZZZ').getActiveSheet();
let targetFolder = spreadsheet.getSheetValues(spreadsheet.getLastRow(),spreadsheet.getLastColumn(),1,1);

//add line with dynamic id as per number of copies required
var source = DriveApp.getFolderById(sourceFolder);
var dest=DriveApp.getFolderById(targetFolder);
var target = dest.createFolder(source.getName());
copyFolder(source, target);
}

function copyFolder(source, target) {
var folders = source.getFolders();
var files = source.getFiles();
while(files.hasNext()) {
var file = files.next();
file.makeCopy(file.getName(), target);
}
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
var targetFolder = target.createFolder(folderName);
copyFolder(subFolder, targetFolder);

}
}




Clark Lind

unread,
Jun 29, 2022, 4:31:12 PM6/29/22
to Google Apps Script Community
Since the Spreadsheet seems to be the central place, I would put your script there. I didn't change your script very much:

function makeCopy() {
var sourceFolder = "YOUR_FOLDERID";
let spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
let targetFolder = spreadsheet.getSheetValues(spreadsheet.getLastRow(),spreadsheet.getLastColumn(),1,1);

//add line with dynamic id as per number of copies required
var source = DriveApp.getFolderById(sourceFolder);
var dest = DriveApp.getFolderById(targetFolder);
var target = dest.createFolder(source.getName());
copyFolder(source, target);
}

function copyFolder(source, target) {
var folders = source.getFolders();
var files = source.getFiles();
while(files.hasNext()) {
var file = files.next();
file.makeCopy(file.getName(), target);
}
while(folders.hasNext()) {
var subFolder = folders.next();
var folderName = subFolder.getName();
var targetFolder = target.createFolder(folderName);
copyFolder(subFolder, targetFolder);

}
}

Then, in the script editor, go to the left menu and select Triggers. 
In the bottom right, click the "+" to add a new trigger. 
Select the MakeCopy function as the function to run, and select onFormSubmit as the event:

trigger.jpg

Juan Cordoba

unread,
Jun 30, 2022, 12:55:14 PM6/30/22
to Google Apps Script Community
But how can I make the person who submit the form the person who is assigned as the creator of these folders?

Clark Lind

unread,
Jul 1, 2022, 7:59:29 PM7/1/22
to Google Apps Script Community
You can't using Google Forms, unless everyone using the script and form is also and editor of the form. 

So if the creator truly matters after changing ownership, then consider using a webapp HTML form/interface instead of a Google Form.

Clark Lind

unread,
Jul 1, 2022, 8:00:12 PM7/1/22
to Google Apps Script Community
The problem is the permissions. You can't force someone to create or manipulate files and folders without their permission.

Juan Cordoba

unread,
Jul 2, 2022, 3:12:24 AM7/2/22
to Google Apps Script Community
Mmm that makes sense. I need to explore new options for this.
Thanks for you input !
Reply all
Reply to author
Forward
0 new messages