简体   繁体   English

GAS-更改文件夹权限

[英]GAS - Change Folder Permissions

I've been working on a script that lists the name and folder ids of the all the folders in a specific folder in my drive. 我一直在研究一个脚本,该脚本列出了驱动器中特定文件夹中所有文件夹的名称和文件夹ID。 Once the list is created, I run the function to get the id and change the folder permissions to private (only specific people can view), remove editors who do not have the company domain and switches them to viewers, and then it should change the permissions for any files in the folder as well. 创建列表后,我运行函数以获取ID并将文件夹权限更改为私人(仅特定人员可以查看),删除不具有公司域的编辑器并将其切换到查看器,然后应更改文件夹中任何文件的权限。 The initial step of creating the ids works fine. 创建id的第一步工作正常。 Unfortunately, the updatePermissions() function only seems to infinitely loop through the first folder and I'm not sure what next steps to take to ensure that the script pulls the next folder id from the list in the spreadsheet. 不幸的是,updatePermissions()函数似乎只能无限循环地遍历第一个文件夹,而且我不确定该采取什么下一步步骤来确保脚本从电子表格的列表中提取下一个文件夹ID。 Any help would be greatly appreciated. 任何帮助将不胜感激。

function listSchoolFolders(){  

  var folderId = 'enter folder id here';

  var myspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var parentFolder = DriveApp.getFolderById(folderId);
  var childFolders = parentFolder.getFolders();

  // List folders inside the folder

  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    var data = [ 
      childFolder.getName(),
      childFolder.getId()
    ];

//Write
   myspreadsheet.appendRow(data)

  }   
}



//------------------------- New Function -------------------------//


function updatePermissions() {
  var myspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var schoolRows = myspreadsheet.getDataRange();
  var schoolNumRows = schoolRows.getNumRows();
  var schoolValues = schoolRows.getValues();

  //Loop through List of Schools
  var row_num = schoolValues[0][2];
  while(row_num<schoolNumRows){

  //Retrieve folder id and go to folder
    var folderId = schoolValues[row_num][1];
    var folderName = schoolValues[row_num][0];
    Logger.log(folderName);
    try {
      var schoolFolder = DriveApp.getFolderById(folderId);
    }
    catch (err){
      Logger.log(folderName + ": " + err.message);
      row_num = row_num+1;
      myspreadsheet.getRange(1,3).setValue(row_num);
      continue;
   };
 };

//Loop through folders and set permissions
  var childFolders = DriveApp.getFolderById(folderId).getFolders();
  while (childFolders.hasNext()) { 
    var childFolder = childFolders.next();
    var childFolderPermissions = childFolder.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.VIEW);

    var files = DriveApp.getFolderById(folderId).getFiles();
    while (files.hasNext()) {
      Logger.log(files.next().getName());
      var fileFolderPermissions = files.next().setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.VIEW);

//check for rogue editors    
    var viewEditors = schoolFolder.getEditors();
    for (i in viewEditors) {
      var email = viewEditors[i].getEmail();
      var emailSource = email.split("@")[1]
      if (emailSource != "tester.com") {
      // add as a viewer or remove completely?
        addViewer(email)
      };
    };  
   };
 };   
    // Recursive call for any sub-folders
  getChildFolders(childFolder);

The error was in the logic checking for child folders. 错误在于检查子文件夹的逻辑。 If there was no child folder, the script completed. 如果没有子文件夹,则脚本已完成。 I've added two conditional checks on each top-level folder, one for child folders and one for files. 我在每个顶级文件夹上添加了两个条件检查,一个用于子文件夹,一个用于文件。 This script functions with one-level depth. 该脚本具有一级深度的功能。

  function updatePermissions() {

  // define several variables to use throughout the script
  var editors, domain, email;

  var myspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var schoolValues = myspreadsheet.getDataRange().getValues();

  // Loop through List of folders
  // Skip row 1
  for(var i=1; i<schoolValues.length; i++) {

    //Retrieve folder id and go to folder
    var folderId = schoolValues[i][1];
    var folderName = schoolValues[i][0];
    Logger.log(folderName);

    var schoolFolder = DriveApp.getFolderById(folderId);

    // Get the children
    var childFolders = schoolFolder.getFolders();

    // test for child folders.
    if(!childFolders.hasNext()) {

      // There is no child folder, so test for files
      if(!schoolFolder.getFiles().hasNext()) {

        // There are no files, so get the folder editors and loop
        editors = schoolFolder.getEditors();
        for(var j=0; j<editors.length; j++) {
          email = editors[j].getEmail();
          domain = editors[j].getDomain(); // easier than using a split function

          // Check the domain. Remove if no match, add as viewer if there is a match
          if(domain !== "testdomain.com") {
            schoolFolder.removeEditor(email)
          } else {
            schoolFolder.removeEditor(email).addViewer(email);
          }
        }
      } 

      // There are child folders, loop through and change permissions
    } else {
      while (childFolders.hasNext()) { 
        var childFolder = childFolders.next();
        // Set permissions on the folder
        childFolder.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.VIEW);

        // Get the files in the child folder and loop
        var files = childFolder.getFiles();
        while (files.hasNext()) {

          files.next().setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.VIEW);

         var viewEditors = schoolFolder.getEditors();

        // Loop the array of editors
          for (var j=0; j<viewEditors.length; j++) {
            email = viewEditors[j].getEmail();
            domain = viewEditors[j].getDomain();

            if (domain !== "testdomain.com") {
            // add as a viewer or remove completely?
              Logger.log("add " + email + " as a viewer");
              files.next().addViewer(email);
            } else {
              // Remove the editor
              Logger.log("removed " + email + " from folder");
              files.next().removeEditor(email);
            }
          };  
         }
       }

       };
      }    
        // Recursive call for any sub-folders
      //getChildFolders(childFolder);
 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM