My understanding is that placing a file in multiple parent folders is no longer allowed on Drive (per the announcement of the single-parent model).
Using Google Apps Script methods (specifically DriveApp methods) to find the parent of a file, it seems I still have to obtain a folder iterator first using file.getParents(). Then use the folder iterator to find the parent folder.
Is this correct? I don't understand why there isn't a file.getParent() method now that would directly provide the parent folder.
Yes. That is correct. Currently, There isn't a file.getParent() method to directly get the parent Folder. You can create a feature request here.
Related
Need to know how to do the same in shared drive.
I was able to create a subfolder in my drive using:
file_metadata = {
'supportsAllDrives':True,
'parents': ['twLBZwLdxfBKTEX0VUUr6'],
'name': "new folderrrrr sub",
'mimeType': 'application/vnd.google-apps.folder',
}
file = DRIVE.files().create(body=file_metadata,
fields='id', supportsAllDrives=True, supportsTeamDrives=True).execute()```
I grasp that you want to upload a folder filled with files in a shared drive. If that isn't your question, please forgive me. To accomplish that you'll need editing permission to be able to write into that shared drive. With that on mind, your operation can be divided into two steps:
First you want to create a folder inside the shared drive in a way similar as this one. As the parents[] you have to use the id property of the shared drive (you can get it by using the LIST method).
After that you'll have to upload every file, one by one, with a code akin this one. You'll need to use the identifier of the folder created on the previous step and the appropriate MIME type.
For both operation you must use the CREATE method with the supportsAllDrives parameter set to true (this requirement is only needed until the first of June of 2020 as described on the linked documentation). You can read here how to set up a working Drive API environment for Python if you don't have one already. If you have any doubt or request additional help, please don't hesitate to comment on my answer.
I want to 'hide' a folder from the users Drive root, as it contains mostly junk Google Docs. I don't want to actually trash it, or use the appData storage as I can't then convert things to Google Docs.
In Apps Script, I can call DriveApp.remove(folder) and the file is given no parents at all, not even the root. I've tried calling the create method with "parents": [] but the folder is still created in the root.
Is there a way to make this happen with the REST API?
Figured it out. Supplying an empty parents list means Google will assign the folder to the root of your Drive. The only way to change this is to supply an update request indicating that you want to remove the parents of the folder that fall under the alias root.
In Python, it's as simple as this:
service.files().update(fileId=id, removeParents='root').execute()
In the Google Drive interface, a file can be assigned multiple folders by using the Shift+Z feature. I am working a Google Apps Script project that will create a folder for each user in a spreadsheet. It will also make copies of selected documents into the newly created folders.
What I am trying to find out though, is I have a specific document that I would like to add to those new folders, but I do not want to simply make copies. I also do not want to simply move the file. The Shift+Z feature allows me to have the document in multiple places at once, changes to that document are reflected regardless of the folder. Can this be done with script?
In Google Drive, a file can have more than one parent folder. The addFile() method of the Folder class will add the file to the folder you are calling the method on.
https://developers.google.com/apps-script/reference/drive/folder#addFile(File)
Google Drive has a cool feature: one can add a file or folder from Shared With Me to My Drive.
Then I can delete this file from My Drive in the same manner (note that if I delete shared file in web version it won't go to Trash)
However, I am getting 403 Forbidden error while trying to delete this file using Google Drive API (because of insufficient permissions due to that I am not owner of this file). So, as one can see, this is not a simple Delete request. How can I implement this functionality?
Add to My Drive changes the parents collection for the item in question. To change this, you will want to unparent the item rather than delete.
Use the about.get call to retrieve the My Drive ID, then remove that ID from the list of parents in the file resource for the file/folder. Update the file with the new list of parents.
A late answer, but I ran into this issue too. This happens because the file isn't actually copied, it's the same instance. If you remove it from My Drive, it will be removed from the Shared Drive too. And you may lack permission to remove it from the Shared Drive.
If you want to just remove it from My Drive: click on the file. In the right panel, under Details, there is a list of locations. At least the Shared Drive and My Drive is listed there. Click the X to remove it from My Drive, and it will disappear there while preserved on the Shared Drive.
This is what I found. After removing the parent of an item shared with me, do a sharedWithMe search still return the item, even though the item's parent list is empty.
Here is the code:
string sItemId = (this is the Id of the item shared with me);
string sParentId = driveService.About.Get().Execute().RootFolderId;
driveService.Parents.Delete(sItemId, sParentId).Execute();
var parentList = m_driveService.Parents.List(sItemId).Execute();
At this point parentList.Items.Count is 0.
Now when I search sharedWithMe the item is in the return list.
Is it possible to restrict the reach of a Google Apps Script to a given Google Drive folder?
I'm creating a stand alone apps script that will be hosted as a web app.
The goal of the script is to list all the sub-folders contained inside the folder (the root folder) where the stand alone script is saved and return a JSON with information about those sub-folders and the files that they contain.
notes:
The name or the id of the root folder is unknown as the script will be distributed to Google Drive users and I want to allow them to save the script inside any folder they would like to.
Although the root folder can have any name or any hierarchical position inside the user's Google Drive folders tree, I'm planning on enforcing a predefined naming structure for the sub-folders.
I thought about two approaches to solve this, but I do not know which one is possible and what Classes and Methods to use.
1)The first approach would be to restrict the script reach to it's container folder, therefore the call to DocsList.getFolders(), would return an array of folders objects just for the folders (sub-folders) contained by the root folder.
Question: Does Google Apps Script provide a Class or Method to restrict the reach of a stand alone script, as described above?
2) Get the name or id of the root folder containing the script and using it retrieve the sub-folders and their files.
Question Does Google Apps Script provide a Method to get the name or the id of the folder that contains the script?
Thanks in advance for any help or tip.
None of those are currently possible. You can write a ui to ask the user for the folder. The google picker now supports picking folders.