Reputation: 4603
Recently, I gained access to Google Team Drive via Google's Team Drive early adopter program.
I created a Google Docs file called Hello, world!
, and then wrote a short Google Apps Script function which uses an addFile()
method to update which Google Drive folder the file is attached to:
function move_or_link_file() {
var source = DriveApp.getFolderById("<sourceID>");
var fileiter = source.getFilesByName("Hello, world!");
var dest = DriveApp.getFolderById("<destID>");
while (fileiter.hasNext()) {
var file = fileiter.next();
dest.addFile(file);
}
}
Typically, a Google Drive folder has a URL which matches the following pattern: https://drive.google.com/drive/folders/<alphanumericID>
. Although it's perhaps a bit inelegant, I can test my code under various operating scenarios and conditions by simply opening different combinations of Google Drive folders in a web browser, selecting the <alphanumericID>
portion of the folder URL, and then manually copy-and-pasting values for this string into <sourceID>
and <destID>
.
After testing, I am able to identify four different input conditions which result in three distinct behaviors:
<sourceID>
and <destID>
are both folders in my personal Google Drive:In this case, the script behaves in effect as if it's creating a symbolic link: the Hello, world!
file now appears in both directories. Note that it really is the same file, not two identical copies: for example, if I open the document, then the document URL, like the folder URLs, also follows a pattern: https://docs.google.com/document/d/<documentID>/edit
. I can tell the file is the same because when I open it, the URL for the document shares the same <documentID>
, regardless of which parent folder I use to access it.
For case 1, I can also get the script to behave more like a mv
command by simply appending an additional line, source.removeFile(file);
to the end of the file iterator loop.
<sourceID>
is a folder in my personal Google Drive while <destID>
is a folder in a Team Drive:In this case, the script behaves like a mv
command by default, rather than as a symbolic link, even without the additional call to the removeFile()
method that I mentioned in case 1: i.e., the Hello, world!
file simply disappears from my personal drive and reappears in the Team Drive.
<sourceID>
and <destID>
are both folders in a Google Team Drive:This results in an error message from Google Apps Script: Cannot use this operation on a Team Drive item. (line 7, file "move_or_link_file")
.
<sourceID>
is a folder in a Team Drive while <destID>
is a folder in my personal Google Drive:Same error as for Case 3.
Now here is the really weird part: the GSuite graphical user interface (i.e., what you are using when you access Google Drive files and folders via the web browser) offers a Move
command via a popup window that appears when you right-click on a file. This GUI version of the Unix-like mv
command behaves identically for all four of the above cases: it doesn't matter whether you are moving a folder back and forth between a personal drive or team drive, or internally within a drive, it works correctly and moves the file to where you would expect it to go, every time.
So, I presume it must be possible to implement a mv
command via Google's API, somehow, given that they've evidently done it already for users of the GUI interface.
Thus my question: given that it's empirically possible to move files back and forth between arbitrary combinations of folders in personal drives and team drives, how would I actually do it, using only the API calls provided by Google Apps Script?
Also, a bonus question: suppose that, similar to Case 1, instead of moving a file between two different folders in the same Team Drive, I actually wanted to create a symbolic link attaching the file to both folders--how would I use the Google API to do that as well? (I.e., how can I get Case 3 to behave more like Case 1?)
Upvotes: 2
Views: 3606
Reputation: 2286
The Google Team Drives only recently started allowing scripts in general. I would imagine the file move you were able to achieve in case 2 is not even intended. There are still several limitations on the team drive (for example you cannot move folders).
For Case 1 I can simply point out that your script is not actually a move command. You should actually imagine Google Drive folders as Gmail tags. A file can have no folders at all. Your script merely assigns a tag to the file and as such it can appear in many folders (just like an email can have many Gmail tags and appear in each tags "folder").
It works in case 2 because Team Drive is a seperate entity from your personal drive. In essence, when you added it to the team drive you had to give up the ownership of the file. As far as I have seen, team drive considers that adding a file to it means that it should be removed from all other parents. I would assume that is why in cases 3 and 4 you cannot move any items. The owner is the team drive itself, however the commands are being sent as a regular Gsuite user.
Drive REST api was recently (~begining of March) updated to work with team drives: https://developers.google.com/drive/v3/web/about-teamdrives so I believe that technically what you are looking for can be done, however considering there are still several limitations on team drives, I don't think it will be documented as well as it could be.
Upvotes: 1