Reputation: 3
I want to be able to check a given folder that I've shared, for files that someone else put into the folder, and then take ownership of those files. I've tried this:
function changeUser() {
var folderID = "<folder ID here>"
var newUser = "<my email address>"
var folder = DriveApp.getFolderById(folderID);
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
if(file.getOwner() <> "DriveUser") {
file.setOwner(newUser);
}
}
}
I'm using "DriveUser" because that's the user it says it is, when I run Logger.log(file.getOwner()); But I'm certainly not sure that's right.
When I try to run this, it tells me that there's an unexpected token '>' in the IF statement line. Hey - I'm new to this. In any case, any suggestions as to how I could make this work?
Upvotes: 0
Views: 230
Reputation: 2072
I believe there are two issues with your code:
!=
for 'not equals' rather than the <>
used in some other languages.DriveUser
you are seeing logged is not a string value, but rather an instance of the User
class, documented here. To get a meaningful identifier of the user, you need to call this class's method .getEmail()
. You can then compare that to your own email address.So the updated code looks like this:
var folderID = "..."
var newUser = "(an email address)"
var folder = DriveApp.getFolderById(folderID);
var files = folder.getFiles();
console.log(folder.getOwner())
while (files.hasNext()) {
var file = files.next();
if(file.getOwner().getEmail() != newUser) {
file.setOwner(newUser);
}
}
But there's a third issue you'll encounter: This will only work if you are transferring ownership from you to another user (i.e. you own the files and newUser
is not your email address). Apps Script runs scripts on your behalf, which means it will not execute an action that you wouldn't have permission to do manually--only the owner of a file can transfer that ownership to another user. If you run the script on files you don't own, you'll get a vaguely worded error Action not allowed
.
Upvotes: 1