0
votes

I'm trying to "upgrade" an old script from DocsList to DriveApp and having a problem with a getFolderById call. The script had worked previously using DocsList.getFolder. I have a (previously-created) folder named "Email Archive" that I'm trying to access in script. Through using Logs and Execution Transcript, I've verified that I'm correctly passing the string "Email Archive" in the baseFolder parameter to the following statement:

var baseFolderObject = DriveApp.getFolderById(baseFolder);

When that statement is executed, I get the following error:

Execution failed: No item with the given ID could be found, or you do not have permission to access it.

The "Email Archive" folder was created previously by the same script with a script call to docsList.CreateFolder, and no permissions were specified.

Do I need to do something to change the folder permissions to allow my script to access it now that I'm using DriveApp instead of DocsList? Do I need to create a new folder using DriveApp and specify permissions, and if so what permissions do I set to allow my script to access the folder and create subfolders and files without allowing it to be visible to the entire Internet?

Thanks in advance for any help,

~ Jim Fennell

2
if you haven't done so, be sure and run the code from the code editor at least one time where it should ask for needed permissions to be grantedScampMichael
@ScampMichael, Thanks for the suggestion, but unfortunately it didn't work. I tried creating a new copy of the sheet, which DID prompt me for permissions, but when I allowed it, I still got the error.I'm even getting the error with this, which makes me suspect I'm writing something wrong: DriveApp.createFolder("EmailArchive"); var baseFolderObject=DriveApp.getFolderById("EmailArchive"); Not much there and I still get: Execution failed: No item with the given ID could be found, or you do not have permission to access it.Jim Fennell
ID is not name. DriveApp.getFolderById("EmailArchive"); check the docs and see @Spencer Eastons's answerScampMichael
@ScampMichael, Thanks again - that is what I was doing wrong. Apologies for not providing the full detail at first.Jim Fennell

2 Answers

1
votes

In DriveApp you can get a folder by ID or by name. You are passing the name to the getFolderById() method. Try this below:

var myFile = DriveApp.getFilesByName(baseFolder).next();

The getFoldersByName returns an iterator that has the standard hasNext()/next() syntax that iterators carry. The code above saves the first folder returned with the name provided in 'baseFolder' to the myFile variable. Remember that in drive multiple folders can have the same name, hence returning an iterator vs a single folder.

-1
votes

var myFolder = DriveApp.getFoldersByName("Email Archive").next();