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

奥利弗斯顿

我是 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"
 };
Tanaike

在您的脚本中,如何进行以下修改?

从:

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

参考:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

在 Google Apps 脚本中更改图表边框颜色

Google Team Drive使用Apps脚本在团队驱动器文件夹之间移动文件

通过Apps脚本列出驱动器文件夹不显示权限

Google Apps脚本中的“共享驱动器”支持

使用 Google Apps 脚本更改单元格的颜色(基于“vLookup”)

Google Apps脚本根据文件名将我的驱动器文件移动到Team驱动器文件夹

Google Apps脚本:搜索文件夹中的所有文档

如何访问共享(团队)驱动器中Google Apps脚本的Stackdriver Logging?

Google Apps 脚本 - 无法对共享驱动器中的元素进行操作

Docs Apps脚本中按前景颜色计数的字数

Google Apps脚本对共享文件夹中已创建文件的权限

使用Google Apps脚本将文件上传到我的google驱动器(在GOOGLE中无格式)

使用驱动器服务的Apps脚本“文件”类和“ .setContent”方法的语法

使用Google Apps脚本将文件从Google驱动器上传到外部API

在Google Apps脚本中获取文件夹名称

使用Java脚本更改<a>标记的颜色

使用按钮Java脚本更改颜色

Drive.Files.get(fileid).LastModifiyingUser.emailaddress 无法使用 Google Apps 脚本在共享驱动器中找到该文件

Google Apps脚本无法更改项目属性中的时区

使用脚本将 HSL 中的颜色列表转换为 RGB 作为 Google Apps 脚本

使用 Apps 脚本在 Google Drive 上使用单元格值作为驱动器名称创建(团队)共享驱动器

如何使用python脚本在谷歌驱动器中创建和显示文件夹?

使用Google Apps脚本直接在文件夹中而不是在根文件夹中创建Google Doc文件

如何使用Google脚本获取驱动器中文件夹的文件夹ID?

用于删除学生驱动器内所有文件和文件夹的批处理脚本 (G)

如何使用Google文档中的Google Apps脚本设置表格中某行的背景颜色?

Google Apps脚本:从文件获取文件夹名称

从Google Apps脚本中电子表格的已发布URL获取驱动器文件ID