如何在Google Drive Apps脚本中创建快捷方式而不是多个父级 [英] How to create a shortcut in Google Drive Apps Script instead of multiple parents

查看:139
本文介绍了如何在Google Drive Apps脚本中创建快捷方式而不是多个父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这里尝试阅读有关内容: https://developers. google.com/drive/api/v3/快捷键

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.

我在草稿中放置了一个名为Project1的原始文件夹.该文件夹的快捷方式必须位于Folder2/Drafts,Folder3/Drafts和Folder4/Drafts中.

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.

如果以后我更改原始文件夹Project1 ex的名称.到新建建筑物"中,我需要在文件夹2、3和4中找到快捷方式(通过ID?-我可以将快捷方式的ID放在数据表中进行迭代的位置)-然后将其重命名为原始名称文件夹的新名称.

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.

当我将Project1从草稿"移到已确认"时.我需要将快捷方式从Folder2/Drafts移至Folder2/Confirmed等.

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);

脚本是在电子表格中创建的,我已经将文件夹ID放在文件的数据表中,因此我可以非常轻松地引用不同的文件夹,并且如果需要,还可以收集快捷方式的ID以进行重命名他们.

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);
}

推荐答案

我相信您的目标如下.

  • 您想要实现以下脚本作为快捷方式.

  • 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);
}

  • 在上面的脚本中,folder1被放置在folder1DraftIdfolder2DraftId的文件夹中.

  • At above script, folder1 is put in the folders of folder1DraftId and folder2DraftId.

    为此,这个答案如何?

    • 在这种情况下,将使用Drive API中的files.create方法.但是在当前阶段,Drive API v2中的Files:insert方法也可以创建快捷方式.因此,在此答案中,使用了高级Google服务的Drive API v2.

    修改脚本后,它如下所示.在运行脚本之前,请在高级Google服务中启用Drive API .

    When your script is modified, it becomes as follows. Before you run the script, please enable Drive API at Advanced Google services.

    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({
        shortcutDetails: {targetId: projectFolderId},
        parents: [{id: f}],
        title: DriveApp.getFolderById(projectFolderId).getName(),
        mimeType: "application/vnd.google-apps.shortcut"
      });
    });
    

    参考文献:

    • 高级Google服务
    • 创建驱动器文件的快捷方式
    • 文件:插入Drive API v2

      References:

      • Advanced Google services
      • Create a shortcut to a Drive file
      • Files: insert of Drive API v2

        • 官方文件如下.

        • The official document says as follows.

        注意:使用files.insert创建快捷方式的应用必须指定MIME类型application/vnd.google-apps.drive-sdk.

        Note: Apps creating shortcuts with files.insert must specify the MIME type application/vnd.google-apps.drive-sdk.

      • 但是,使用application/vnd.google-apps.drive-sdk时,无法创建快捷方式.我不确定这是错误还是当前的规范.所以我用application/vnd.google-apps.shortcut作为mimeType.

      • 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.

        这篇关于如何在Google Drive Apps脚本中创建快捷方式而不是多个父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆