如何在 Google Drive Apps 脚本而不是多个父母中创建快捷方式 [英] How to create a shortcut in Google Drive Apps Script instead of multiple parents
问题描述
我一直在尝试在这里阅读:https://developers.google.com/drive/api/v3/shortcuts
而且我了解快捷方式的基础知识,但我不知道如何在代码中使用它——语法.
我不需要将文件夹放置在多个文件夹中,而是将快捷方式放置在与原始文件夹命名相同的文件夹中.
我在 Drafts 中有一个名为 Project1 的原始文件夹.此文件夹的快捷方式需要放在 Folder2/Drafts、Folder3/Drafts 和 Folder4/Drafts 中.
如果我之后更改原始文件夹 Project1 的名称,例如.到新建筑"我需要在文件夹 2,3 和 4 中找到快捷方式(按 ID? - 我可以将快捷方式的 ID 放在数据表中,从中可以遍历它们) - 然后将它们重命名为原始文件夹的新名称.
当我将 Project1 从草稿移至已确认时.我需要将快捷方式从 Folder2/Drafts 移动到 Folder2/Confirmed 等.
这主要是放置新文件夹的基本代码的样子
var folder1 = draft.createFolder("DRAFT - "+"Project1");var folder1Url = folder1.getUrl();var folder1Id = folder1.getId();folder2Draft.addFolder(folder1);
当更改文件夹的名称时,该名称(当然)会在其他地方发生变化.但据我了解,快捷方式并非如此.我已经使用手动创建的快捷方式对其进行了测试,证实了这一点.
我在确认项目时重命名的部分是这样的:
var folder1 = DriveApp.getFolderById(folder1Id);var folder1NameOld = folder1.getName();var folder1NameNew = folder1NameOld.replace("草稿 - ","");folder1.setName(folder1NameNew);
我在用简单的方式确认项目时移动文件:
确认.addFolder(folder1);DraftsFolder.removeFolder(folder1);
脚本是在电子表格中制作的,我已经将所有文件夹 ID 放在文件的数据表中,因此我可以非常轻松地引用不同的文件夹,如果需要,还可以收集快捷方式的 ID 以便能够重命名他们.
更新:提出一个更清楚的问题:如何使用快捷方式而不是多育儿来做到这一点?
函数快捷方式() {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);}
相信你的目标如下.
你想实现以下脚本作为快捷方式.
函数快捷方式() {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);}
在上面的脚本中,
folder1
被放在folder1DraftId
和folder2DraftId
的文件夹中.
对于这个,这个答案怎么样?
修改点:
- 本例使用Drive API中的files.create方法.但在现阶段,Drive API v2中的Files:insert方法也可以创建快捷方式.所以在这个答案中,使用了 Advanced Google services 的 Drive API v2.
修改脚本:
当你的脚本被修改后,它变成如下.在运行脚本之前,请在 Google 高级服务中启用 Drive API一个>.
从:var folder1 = DriveApp.getFolderById(projectFolderId);DriveApp.getFolderById(folder1DraftId).addFolder(folder1);DriveApp.getFolderById(folder2DraftId).addFolder(folder1);
到:
const folderIDs = [folder1DraftId, folder2DraftId];folderIDs.forEach(f => {Drive.Files.insert({快捷方式详细信息:{targetId:projectFolderId},父母:[{id:f}],标题:DriveApp.getFolderById(projectFolderId).getName(),mimeType:应用程序/vnd.google-apps.shortcut"});});
参考资料:
- 高级 Google 服务
- 创建云端硬盘文件的快捷方式
官方文档是这样说的.
- <块引用>
注意:使用
files.insert
创建快捷方式的应用必须指定 MIME 类型application/vnd.google-apps.drive-sdk
. 但是,当使用
application/vnd.google-apps.drive-sdk
时,无法创建快捷方式.我不确定这是错误还是当前规范.所以我使用application/vnd.google-apps.shortcut
作为 mimeType.
- <块引用>
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 offolder1DraftId
andfolder2DraftId
.
For this, how about this answer?
Modification points:
- In this case, the method of files.create in Drive API is used. But in the current stage, the method of Files: insert in Drive API v2 can also create the shortcut. So in this answer, Drive API v2 of Advanced Google services is used.
Modified script:
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"
});
});
References:
- Advanced Google services
- Create a shortcut to a Drive file
-
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 usedapplication/vnd.google-apps.shortcut
as the mimeType.
这篇关于如何在 Google Drive Apps 脚本而不是多个父母中创建快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!