Google apps script - Folder as a script container - google-apps-script

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.

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.

create copy of google file in the same drive directory

I'm looking for a way to create a copy of a given file in the same Google Drive directory using Google Apps Script.
The goal is to iterate through a folder structure looking for files owned by a specific user; when such a file is found [doc, spreadsheet, PDF], create a copy in the same place and delete the original.
The reason for this is that we have files owned by users in other domains and Google currently does not allows the transfer of ownership outside the domain. Using a script like this, could help us solve the problem.
Any ideas?
Thanks,

Use Google Apps Script to Assign a File to Multiple Folders - Like Shift+Z Feature

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)

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.

add prefix to all files and folders in google drive

How can I add a prefix or a suffix to all files/ folders in Google Drive using Google Script. The objective is that whenever a users needs to be deleted to add a suffix to all files and folders and transfer to another user so that the documents of the user can easily be tracked at a later stage.
Regards
The File.setName() method in the Drive Service will do what you want. I'd be happy to help further if you post a sample of your code.