在 Google Sheet 中管理 Google 个受保护单元格

Managing Google Protected Cells in Google Sheet

我有一个 Google Drive 文件夹,里面有 30 多个 Google Sheet。在每个 sheet 中,我有 5 个以上的选项卡,并且这些选项卡中的每一个都有至少一组受保护的单元格,或者选项卡本身。我想知道,是否可以将受保护单元格的所有这些权限作为文本输入到一个 Google Sheet 中,以便能够快速查看并可能管理权限。我的长期目标是直接从那个 Google Sheet 管理受保护的单元格。我一直在寻找,但没有找到任何资源让我走上正轨。

我写这个脚本是为了完成你想要的任务,

到运行打开Spreadsheet或创建一个新脚本所需的脚本 转到工具->脚本编辑器创建它,然后copy/paste代码。

更改容器 ID 的“#########################”文件夹,要确定你的文件夹的ID,你可以打开文件夹,然后复制ID对应的URL部分 https://drive.google.com/drive/folders/#######################

添加菜单后,需要刷新才能看到。

使用:点击Custom Utilities->Get permisions list Here,然后会创建“sheet#”这将包含所有信息

代码如下:

function onOpen(){
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Utilities').addItem('Get permisions list Here','testfunction').addToUi();
}

function testfunction() {
  //Add a new sheet in the current Spreadsheet
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet().activate();
  activeSheet.appendRow(['FileName','ID','Protection Description','Range','Type','Users']);
  activeSheet.getRange("A1:F1").setFontWeight('bold');

  //get all the Google Spreadsheet's files
  var files = DriveApp.getFolderById("#########################").getFilesByType(MimeType.GOOGLE_SHEETS);
  while (files.hasNext()) {
   var file = files.next();
   var ss = SpreadsheetApp.openById(file.getId());

   //get the permisions in the current file, and print the data to the previous created sheet
   var protectionsRange = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    for (var i = 0; i < protectionsRange.length; i++) {
      var protection = protectionsRange[i];

      activeSheet.appendRow([file.getName(),file.getId(),protection.getDescription(),protection.getRange().getA1Notation(),protection.getProtectionType(),protection.getEditors().join(";")]);
      //Logger.log(file.getName() + " | " + file.getId() + " \n| " + protection.getDescription() + " | " + protection.getRange().getA1Notation() + " | " + protection.getProtectionType() + " | " + protection.getEditors().join(";"));
    }
    var protectionsSheet = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET);
    for (var i = 0; i < protectionsSheet.length; i++) {
      var protection = protectionsSheet[i];
      activeSheet.appendRow([file.getName(),file.getId(),protection.getDescription(),protection.getRange().getA1Notation(),protection.getProtectionType(),protection.getEditors().join(";")]);
      //Logger.log(file.getName() + " | " + file.getId() + " \n| " + protection.getDescription() + " | " + protection.getRange().getA1Notation() + " | " + protection.getProtectionType() + " | " + protection.getEditors().join(";"));
    }
  }
}