GAS - 更改文件夹权限

时间:2017-11-08 17:25:24

标签: javascript google-apps-script google-drive-api

我一直在编写一个脚本,列出驱动器中特定文件夹中所有文件夹的名称和文件夹ID。创建列表后,我运行该函数来获取id并将文件夹权限更改为private(仅限特定人员可以查看),删除没有公司域的编辑器并将其切换到查看者,然后它应该更改对文件夹中的任何文件的权限。创建id的第一步很好。不幸的是,updatePermissions()函数似乎只能无限循环遍历第一个文件夹,我不确定接下来要采取哪些措施来确保脚本从电子表格中的列表中提取下一个文件夹ID。任何帮助将不胜感激。

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

1 个答案:

答案 0 :(得分:0)

错误发生在子文件夹的逻辑检查中。如果没有子文件夹,则脚本完成。我在每个顶级文件夹上添加了两个条件检查,一个用于子文件夹,一个用于文件。该脚本具有一级深度。

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