Do Google Drive File IDs break? - google-drive-api

I'm playing with the Google Drive API and one thing that I keep wondering about is IDs in the case of an arbitrary file (non-Google App) like an image.
If a user is working locally, offline and they turn off the Google Drive client. And then they make some changes to a file, let's say they rename it, move it from one folder to the next and edit it.
Will the ID remain the same when the client comes back on, or will it break? I imagine the client will interpret either one or all of the move, rename and edit as delete and new file.
Would appreciate any help here!

The file id (within Google Drive) remains the same from the original creation to its deletion.

Related

Allow Anyone to Upload Files Directly to my Google Drive

I would like to allow my students to send me one or more files directly to my google drive via a form where they would just enter their name and choose the file(s) to send (drag and drop would be great). A folder with their name would be created in a specific folder on my drive and a warning email would be sent to me. Do you think this is feasible with google script? Thanks in advance.
An example here but paying ...
I've checked and Google Forms already allows for a File Upload kind of question (with drag and drop too).
It automatically creates a folder in your Google Drive (altough it doesnt create a folder for every student) (example here) and in the spreadsheet that it automatically generates there is a permanent link to that file (example here).
If that's not what you are looking for and you need the 'Folder per student' feature then yes, you can use google apps script to move files in folders quite easily.

How can I use Google Apps Script to move files between personal Google Drives and Team Drives?

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?)
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.

Prevent deletion of files in Google Drive

I am trying to prevent deletion of all files uploaded to specific folder in Google drive. The idea is several people will be able to upload files to this drive, but they should never be able to delete it.
I thought of putting a Google apps script to achieve this, like letting the script run every x interval and getTrashedFiles and set isTrashed to false. But what if the user is able to delete it from trash too?
Is there a foolproof way?
Or is Google Drive not the right answer at all?
Any other service can help me which is simple for people uploading the files?
TIA
its possible but the way you are doing it isnt practical or tamperproof.
instead write a little apps script webapp that allows uploading files to the given folder where you are owner and only writter.
publish it to run as you with permissions to anyone (or your domain). they will be able to add files and view but not change or remove them (and you will be the owner of all uploaded files).
to support uploading files already in drive (move them to the folder) is also possible but with more code.

How to provide a static link to a Google Doc that is unique to each user

This doesn't feel like a unique problem, but I can't find an existing solution.
Background:
I am building a course in an LMS. I can insert links, but they are static links and each user will see the same link.
As part of this course, there is a Google Doc study guide that the students should be filling out (and sharing with their instructor).
I was wondering about which is the most viable solution:
1. When joining the course, use Google (Drive API or Apps Script (?)) to create an instance of the file (presumably by either copying an existing one or using a named template) with the appropriate sharing permissions (the learner and their instructor would both be able to see the files). There would be no link to the file within the course, just instructions on how to access their already created file.
2. Have some sort of handler so that I could insert a static link to this handler into the course; the handler would check if the user was logged into Google Drive, if the file existed it would open it, and if it didn't exist it would create it (using similar technique to above). This would have the benefit of being able to insert the same link anywhere and the student could immediately go to their specific and personalized Google doc.
Any other suggestion or existing solution out there?
Much obliged,
David
Both would need the same code that makes personalized copies. The only difference is the moment where you create them.
2 is better because it has less chances of failing since you create at most one copy at a time, and is also more practical to the user.
The 1 option needs to make bulk copies and those take time (drive api to make copy is slow) and script could timeout or reach some quota.
going with 1, one way is to have a single webapp published as owner which receives the drive file ID as a url parameter. on start, the app figures if the copy has already been done (by finding the file by title or by using script or user properties).
if the file isnt created yet, create a copy and share it with the user. then display a link to the file in the webapp. note that the script wont be able to open the file automatically, thus you need to show it as an anchor for the user to click.
to clarify, with this way the user always makes two clicks. once from your CMS and once in your webapp.

Saving one file to two folders

In the Google Docs format, I could save one file to two (or more) folders by checking multiple boxes. Now that I've switched to Google Drive, I find that the files that were already in more than one folder stay that way, but I am unable to save a new file to two (or more) files/collections. Am I not doing something correctly or is this an intentional change?
Drag the file to the desired folder and press the Ctrl key before dropping it.
The action changes from Move to Add (mover/AƱadir in Spanish).
You get a linked file with joined share permission