I've been trying to read about it here: https://developers.google.com/drive/api/v3/shortcuts
And I understand the basics in shortcuts but I can't figure out how to use it in the code - the syntax.
I need to instead of placing a folder in multiple folders, placing shortcuts in those folders named the same as the original folder.
I have an original folder called Project1 placed in Drafts. Shortcuts for this folder needs to be places in Folder2/Drafts, Folder3/Drafts and Folder4/Drafts.
If I afterwards change the name of the original folder Project1 ex. to "New Building" I need to find the shortcuts in Folder 2,3 and 4 (by Id? - I can put the ID of the shortcuts in a datasheet from where I can iterate through them) - and then rename them as the original folder's new name.
And when I move the Project1 from Drafts to Confirmed. I need to move the shortcuts from Folder2/Drafts to Folder2/Confirmed etc.
This is mainly how the basic code looked like for placing the new folder
var folder1 = draft.createFolder("DRAFT - "+"Project1");
var folder1Url = folder1.getUrl();
var folder1Id = folder1.getId();
folder2Draft.addFolder(folder1);
When changing the name of the folder the name would (of course) change in the other places. But as I understand this is not the case with shortcuts. I've tested it with manually created shortcuts witch confirmed this.
The renaming part I do like this when confirming the project:
var folder1 = DriveApp.getFolderById(folder1Id);
var folder1NameOld = folder1.getName();
var folder1NameNew = folder1NameOld.replace("DRAFT - ","");
folder1.setName(folder1NameNew);
And I move the file when confirming the project with the simple:
confirmed.addFolder(folder1);
draftsFolder.removeFolder(folder1);
The script is made in a spreadsheet and I already put alle the folderIDs in a data sheet in the file so I can very easy make references to the different folders and if needed also collect the IDs of the shortcuts to be able to rename them.
Update: To make a more clear question: How to do this with shortcuts instead of multi-parenting?
function shortcut() {
var s = SpreadsheetApp.getActive();
var sheet = s.getActiveSheet();
var projectFolderId = sheet.getRange('B1').getValue();
var folder1DraftId = sheet.getRange('B2').getValue();
var folder2DraftId = sheet.getRange('B3').getValue();
var folder1 = DriveApp.getFolderById(projectFolderId);
DriveApp.getFolderById(folder1DraftId).addFolder(folder1);
DriveApp.getFolderById(folder2DraftId).addFolder(folder1);
}
I believe your goal is as follows.
You want to achieve the following script as the shortcut.
function shortcut() {
var s = SpreadsheetApp.getActive();
var sheet = s.getActiveSheet();
var projectFolderId = sheet.getRange('B1').getValue();
var folder1DraftId = sheet.getRange('B2').getValue();
var folder2DraftId = sheet.getRange('B3').getValue();
var folder1 = DriveApp.getFolderById(projectFolderId);
DriveApp.getFolderById(folder1DraftId).addFolder(folder1);
DriveApp.getFolderById(folder2DraftId).addFolder(folder1);
}
At above script, folder1
is put in the folders of folder1DraftId
and folder2DraftId
.
For this, how about this answer?
When your script is modified, it becomes as follows. Before you run the script, please enable Drive API at Advanced Google services.
From:var folder1 = DriveApp.getFolderById(projectFolderId);
DriveApp.getFolderById(folder1DraftId).addFolder(folder1);
DriveApp.getFolderById(folder2DraftId).addFolder(folder1);
To:
const folderIDs = [folder1DraftId, folder2DraftId];
folderIDs.forEach(f => {
Drive.Files.insert({
shortcutDetails: {targetId: projectFolderId},
parents: [{id: f}],
title: DriveApp.getFolderById(projectFolderId).getName(),
mimeType: "application/vnd.google-apps.shortcut"
});
});
The official document says as follows.
Note: Apps creating shortcuts with
files.insert
must specify the MIME typeapplication/vnd.google-apps.drive-sdk
.
But, when application/vnd.google-apps.drive-sdk
is used, the shortcut couldn't be created. I'm not sure whether this is the bug or the current specification. So I used application/vnd.google-apps.shortcut
as the mimeType.