查找共享驱动器中的所有空文件夹并使用 Apps 脚本更改颜色?

Find all empty folders in a Shared Drive and change color with Apps Script?

我是 Apps 脚本的新手,我正在尝试制作一个脚本来查找所有空文件夹并更改它们的颜色。我设法做到了,当我在我的驱动器中尝试它时它运行得很好,但是当我在共享驱动器中尝试 运行 它时它给我这个错误:

Exception: Request failed for https://www.googleapis.com returned code 404. Truncated server response: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "File not found: 166gK63I72FZvqY0XjSE63z4SyQuSgGp... (use muteHttpExceptions option to examine full response)

我搜索了一些解决方案,唯一的解决方案是在 Google Cloud Platform 中创建一个项目。我这样做了,并将我的脚本链接到 GCP 中的项目,但它给出了同样的错误。 基本上,我希望我的脚本能够访问我的共享驱动器。 Shared Drive 挺大的,里面有很多文件夹和文件,不知道是api调用太多,还是权限问题。 我希望有人能帮帮忙!非常感谢!我把我的剧本留给你:

function findEmptyFolders() {
  var parentFolder = DriveApp.getFolderById('Shared Drive ID');
  var folders = parentFolder.getFolders();
  while (folders.hasNext()) {
    var childfolder = folders.next();
    recurseFolder(parentFolder, childfolder);
  }
}

function recurseFolder(parentFolder, folder) {
        var filecount = 0;
        var foldercount = 0;

        var files = folder.getFiles();               
        var childfolders = folder.getFolders();

        while (childfolders.hasNext()) {
            var childfolder = childfolders.next();
            recurseFolder(folder, childfolder);
            foldercount++;
        }

        while (files.hasNext()) { 
            var file = files.next();
            filecount++;
        }

       if (filecount == 0 && foldercount == 0) {            
            var id = folder.getId();
            Logger.log(folderColor.setColorByName(id,'Yellow cab'));
       }             
}

var folderColor = {};

folderColor.setColorByName = function(id,name){
  if(!colorPalette[name]){
    throw "Name is not valid, please check name in colorPalette.";
  }
  this.setColor(id,colorPalette[name]);
  return true;
}

folderColor.init = function(){
  return this;
}

folderColor.setColor = function(id,hexa){
  var url = 'https://www.googleapis.com/drive/v2/files/'+id+'?fields=folderColorRgb';
  var param = {
    method      : "patch",
    contentType: 'application/json',
    headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    payload: JSON.stringify({folderColorRgb:hexa})
  };
  var html = UrlFetchApp.fetch(url,param).getContentText();
  
  return html;
}

var colorPalette = {
  "Chocolate ice cream":"#ac725e",
  "Old brick red":"#d06b64",
  "Cardinal":"#f83a22",
  "Wild straberries":"#fa573c",
  "Mars orange":"#ff7537",
  "Yellow cab":"#ffad46",
  "Spearmint":"#42d692",
  "Vern fern":"#16a765",
  "Asparagus":"#7bd148",
  "Slime green":"#b3dc6c",
  "Desert sand":"#fbe983",
  "Macaroni":"#fad165",
  "Sea foam":"#92e1c0",
  "Pool":"#9fe1e7",
  "Denim":"#9fc6e7",
  "Rainy sky":"#4986e7",
  "Blue velvet":"#9a9cff",
  "Purple dino":"#b99aff",
  "Mouse":"#8f8f8f",
  "Mountain grey":"#cabdbf",
  "Earthworm":"#cca6ac",
  "Bubble gum":"#f691b2",
  "Purple rain":"#cd74e6",
  "Toy eggplant":"#a47ae2"
 };

在你的脚本中,做如下修改怎么样?

发件人:

var url = 'https://www.googleapis.com/drive/v2/files/'+id+'?fields=folderColorRgb';

收件人:

var url = 'https://www.googleapis.com/drive/v2/files/'+id+'?fields=folderColorRgb&supportsAllDrives=true';

参考: