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:
Case 1: <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.
Case 2: <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.
Case 3: <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")
.
Case 4: <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?)