Create a Shortcut to file under a folder(s) in Google Drive with Apps Script

Ames Computer Geek Corner News Create a Shortcut to file under a folder(s) in Google Drive with Apps Script NYC New York City North Bergen County

Working with Google Drive one quickly realizes there are many good reasons to automate many tasks. Simple tasks, like creating, gathering and displaying folders is more than tedious and time consuming. Here we utilize a function from another blog 'Search for folders and display' to search for folders and permission users as Editors in my project team.

This functionality is built on top of previous blogs such as, 'Create a Folder in the Root Drive' and 'Iterate and Display folder's full path'.

I often work with data sets that are populated from a database or spreadsheets to analyze big chunks of data. This requires creation of folders and files on demand that will hold my new spreadsheets and data. The folders and files are created and manipulated dynamically; therefore the data used needs to be cleaned up and removed from my Google Drive when the process has completed. Once I have my spreadsheet or docs built out from my datasets, I automate many of the tasks using my Google Apps Script code base. In our example we will employ the DriveApp Class to manage, create and find files and folders in Google Drive. Since we are working primarily in folder context, we will need to manipulate folders and listing files using Folder Class, which creates, deletes and manages folders and files.

Teams working on projects have different levels of permission requirements. Some team members require read only permissions, others require editing rights. If you are looking for read only viewer permissions check out my other blog: Add Viewers Permission to Folder(s)

In this blog we will focus on creating a shortcut to a new file within a new folder. If you are interested in reading further on Drive API, you can peruse the following article '`Create a shortcut to a Drive file'

We will begin to first dynamically creating a new file and a new folder using 'DriveApp Class'. Once the new sub-folder is created, we will use the new sub-folder and create a new google document using 'MimeType.GOOGLE_DOCS'. For a list of all google MimeTypes see the following URL: 'Enum MimeType'.

In order to create an actual google document we have to utilize the 'Advanced Google services'. Follow the guide in order to instal 'Advanced Google services'. Unfortunately just calling 'createFile(name, content, mimeType) ' with 'MimeType.GOOGLE_DOCS' will not work.

To start, we must first search for our parent folder. The DriveApp Class function allows us to narrow down a specific set of folders using a name search: 'DriveApp.getFoldersByName("folder-string-name")'. This function returns a 'FolderIterator' object that we will use to iterate over found folders. Using the first folder found in the folder-iterator we will create a new sub-folder.

newFolderObj = folderObj.createFolder("new-sub-folder");
We now have the new sub-folder in which we will create a new google document. Let’s proceed with the next function 'crtFileInFolder()' that will accept three parameters: First parameter, 'fileName' a name of the file we are creating; Second parameter, 'folderObj' returned from calling the function 'createFolder()'; Third parameter, fileMimeType such as 'MimeType.GOOGLE_DOCS' .

function crtFileInFolder(fileName, folderObj, fileMimeType)

We use the three (3) parameters to create a new file object 'newFile'. The new object will be passed to the Advanced Google services 'Drive.Files' by calling the 'insert()' function as a parameter.

  var ssId = Drive.Files.insert(newFile).id;

We now have a new folder and a new file created that we will use in order to create a new shortcut. Since the call to function crtFileInFolder() returns 'fileID', let's use this file ID to create a shortcut in the parent folder to the new file located in the new sub-folder.

The code at this point is pretty simple and looks like this:

crtShortCutToFileInFolder(folderObj, fileID)

Putting it all together:

function crtFileInFolder(fileName, folderObj, fileMimeType ){
  var newFile = {
      title: fileName,
      mimeType: fileMimeType,
      parents: [{id: folderObj.getId()}]
  var fileID = Drive.Files.insert(newFile).id;

function crtShortCutToFileInFolder(folderObj, fileID) {

function myMainBlog02() {
  var folderObj = null;
  var newFolderObj = null;
  var folderNameIter = DriveApp.getFoldersByName("test00");
    folderObj =;
    newFolderObj = folderObj.createFolder("new-sub-folder");
    var fileID = crtFileInFolder('NewGDoc', newFolderObj, MimeType.GOOGLE_DOCS );
    crtShortCutToFileInFolder(folderObj, fileID);