Pages

Tuesday, 12 May 2020

Create a Shortcut in Google Drive

The following Google Apps Script is designed to create a Shortcut to an existing Google Drive folder, within another folder.
Screenshot of Shortcut file in Google Drive
Screenshot of Shortcut file in Google Drive

Enable Drive API
In Script Editor go to Resources > Advanced Google services ... > scroll down and turn on Drive API > click OK.

The Code
Create a new file in Google Drive that will become the Shortcut:
var shortcut = Drive.newFile();
Provide the new file with a title and icon type:
shortcut.title = 'My Shortcut';
shortcut.mimeType = 'application/vnd.google-apps.shortcut';
Now add the ID of the parent Google folder where you want the Shortcut to be created. This is required as a list within an array so it looks more daunting than it needs to be:
shortcut.parents = [
    {
      "id": "ENTER ID HERE"
    }
];
Next we create some further details about the Shortcut - such as the ID of the folder we want it to point to:
var shortcutDetails = Drive.newFileShortcutDetails();
shortcutDetails.targetId = "ENTER ID HERE";
shortcut.shortcutDetails = shortcutDetails;
Finally we insert the Shortcut into Google Drive:
Drive.Files.insert(shortcut);
And that is it.

Create a Shortcut in Google Drive.gs

No comments:

Post a comment