将电子表格中的单个工作表锁定在一个充满电子表格的文件夹中

时间:2016-10-25 16:56:46

标签: google-apps-script google-sheets

我在一所大学工作,为52名学生设置了52个电子表格。每周,我需要在这52个电子表格中的每一个上锁定一张表。工作表都包含在一个文件夹中。每个学生的电子表格都有名为“1”到“33”(代表33周)的表格,以及一些额外的表格。

我正在考虑使用另一个带有脚本的电子表格,让我可以锁定或解锁一周。在这个电子表格中,我会按如下方式进行设置:

+---+-----------+---------------------+---------------------+
|   |     A     |        B            |    C                |
+---+-----------+---------------------+---------------------+
| 1 | 1         |user1@university.edu |user2@university.edu |
| 2 | Lock      |                     |                     |
+---+-----------+---------------------+---------------------+

A1将是我键入哪一周锁定的地方。在这个例子中,它是'1'周。第1行,单元格B1到N1将包含应继续具有编辑权限的其他大学教授的电子邮件地址。 B2将指示“锁定”或“解锁”

如何循环浏览此文件夹中的每个文件并设置编辑权限?这是我尝试失败的原因。这是对某人帮助我的另一个脚本的修改,目的是在每个学生的电子表格中循环并编辑特定范围。

var idFolder = 'xxxxxxxxxxxxxxxxZLaXVPZzQ'; 
var folder = DriveApp.getFolderById(idFolder);
var contents = folder.getFiles();
var file;
var sheet;
var sheets;
var sheetName;
var range;
var strRangeProtect;
var protections;
var protection;
var editors = [???????? Range of B1:N1??????????];
var app = SpreadsheetApp;
var currentweekneedstobedefined = app.getActiveSheet().getRange(1, 1);
var thisweek = currentweekneedstobedefined.getValue();

while(contents.hasNext()) {
  file = app.openById(contents.next().getId());
  sheets = file.getSheets();

  var thisweek = currentweekneedstobedefined;
  protections = file.getProtections(app.ProtectionType.SHEET);
  protection = protections(thisweek);
  protection.addEditor(editors);
  protection.removeEditors(protection.getEditors());
  if (protection.canDomainEdit()) {
    protection.setDomainEdit(false);

所以我取得了一些进展。这是我在10月27日的情况。

问题1:它现在将保护工作表'1',因此我是唯一的编辑器。我以为我可以先删除其他人,然后在编辑器数组中添加人员。但是,因为我希望使用语句protection.addEditors(编辑),所以它们不会被添加回来。错误说:无效用户:“ixxxx @ university.edu,nxxxxr @ university.edu,sxxxxx0 @ university.edu,sxxxxx2 @ university.edu,nxxxxxt @ university.edu,gxxxxx4 @ university.edu,yxxxxxv @ university.edu, kxxxxx7 @ university.edu,emxxxxxs @ university.edu,kpxxxxx3 @ university.edu”。

问题2:另一个问题是它不会遍历所有学生文件。相反,我注意到数组更改表和编辑器的尺寸不断增加。在经过while循环几次之后,changeheets数组是changeheets [36] [36] [36],编辑是编辑[13] [13] [13]。

function increaseProtection(idFolder,sheetNames){

var folder = DriveApp.getFolderById(idFolder);
var contents = folder.getFiles();
var file;  
var app = SpreadsheetApp;
var currentweekneedstobedefined = app.getActiveSheet().getRange(1, 1);
var thisweek = currentweekneedstobedefined.getValue();
var ss = SpreadsheetApp.getActiveSpreadsheet(); //get spreadsheet   
var sheet = ss.getActiveSheet();
var editors = sheet.getRange('B1:N1').getValues(); //get values of B1:N1 as array
while(contents.hasNext()) {
  file = app.openById(contents.next().getId());
  var fname = file.getName();
  var changesheets = file.getSheets();
  var thisweeksSheet = file.getSheetByName(thisweek);
  var me = Session.getEffectiveUser();
  var permissions = thisweeksSheet.getSheetProtection();
  var protection = thisweeksSheet.protect().setDescription('Supervisors Only');
  protection.addEditor(me);
  protection.removeEditors(protection.getEditors());
  if (protection.canDomainEdit()) {
    protection.setDomainEdit(false);
  }
  protection.addEditors(editors);    
}  

}

1 个答案:

答案 0 :(得分:0)

要从表单中获取B1:N1编辑器范围,我会做这样的事情。

 var ss = SpreadsheetApp.getActiveSpreadsheet(); //get spreadsheet   
 var sheet = ss.getActiveSheet(); //get sheet
 var editors = sheet.getRange('B1:N1').getValues()[0]; //get values of B1:N1 as array

如果您打算传递数组而不是迭代,那么您还需要使用addEditors而不是addEditor

我认为这个有用,但是一些细节(比如在主页或每个ss上设置的编辑器)让我感到困惑。

   function myFunction() {
  var idFolder = '0B_9l84KvUJBWRXhPd0lLRUN1WWs'; 
  var folder = DriveApp.getFolderById(idFolder);
  var contents = folder.getFiles();

  var currentWeek = SpreadsheetApp.getActiveSheet().getRange(1, 1).getValue();//get week value from this main control sheet


while(contents.hasNext()) {
  var ss = SpreadsheetApp.openById(contents.next().getId());//gets the ss in the folder
  var sheet = ss.getSheetByName(currentWeek); //get sheet by week name 
  var editors = cleanArray(sheet.getRange('B2:N2').getValues()[0]); //get values of B1:N1 as array --- not sure if you set this on the local sheet or the main control one

  var protection = sheet.protect().setDescription('no Dana only Zuul');
  protection.removeEditors(protection.getEditors());
 if (protection.canDomainEdit()) {
   protection.setDomainEdit(false);
 }
  protection.addEditors(editors);
  sheet.getRange('A1').setBackground('red'); //just a visual marker to see it went through all the spots

  }
}


//cleans empty slots from array http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript
function cleanArray(actual) {
  var newArray = new Array();
  for (var i = 0; i < actual.length; i++) {
    if (actual[i]) {
      newArray.push(actual[i]);
    }
  }
  return newArray;
}