Google apps script 在充满电子表格的文件夹中锁定每个电子表格的一张表格

Google apps script 在充满电子表格的文件夹中锁定每个电子表格的一张表格,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我在一所大学工作,为52名学生建立了52个电子表格。每周,我需要在这52张电子表格中的每一张上锁定一张表格。这些工作表都包含在一个文件夹中。每个学生的电子表格都有名为“1”到“33”的表格(代表33周),以及一些额外的表格 我正在考虑另一个电子表格,它有一个脚本,可以让我锁定或解锁一周。在这个电子表格上,我将按如下方式进行设置: +---+-----------+---------------------+---------------------+ | | A |

我在一所大学工作,为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”,因此我是唯一的编辑。我在想,我可以先删除所有其他人,然后在编辑器数组中添加回这些人。然而,它们并没有像我希望的那个样通过语句保护得到补充;错误显示:无效用户: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循环几次后,changesheets数组是changesheets[36][36][36],editors是editors[13][13]

功能增强保护(idFolder、sheetNames){


}

要从工作表中获取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
如果要传递数组而不是遍历数组,还需要使用
加法器
而不是
加法器


我认为这一个工作,但一些细节(如编辑设置在主表或每个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;
}
函数myFunction(){
var idFolder='0B_9l84KvUJBWRXhPd0lLRUN1WWs';
var folder=DriveApp.getFolderById(idFolder);
var contents=folder.getFiles();
var currentWeek=SpreadsheetApp.getActiveSheet().getRange(1,1.getValue();//从此主控制表获取周值
while(contents.hasNext()){
var ss=SpreadsheetApp.openById(contents.next().getId());//获取文件夹中的ss
var sheet=ss.getSheetByName(currentWeek);//按周名称获取工作表
var editors=cleanArray(sheet.getRange('B2:N2').getValues()[0]);//将B1:N1的值作为数组获取--不确定是在本地工作表还是在主控件工作表上设置
var protection=sheet.protect().setDescription('no Dana only Zuul');
protection.removeditors(protection.getEditors());
if(protection.canDomainEdit()){
protection.setDomainEdit(false);
}
保护.增补者(编辑者);
sheet.getRange('A1').setBackground('red');//只是一个视觉标记,可以看到它穿过所有的点
}
}
//从阵列中清除空插槽http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript
函数数组(实际){
var newArray=newArray();
对于(变量i=0;i
解释在给定脚本上失败的部分。它(当然)无法通过:var editors=[???????B1:N1的范围?????]此外,我不知道我是否正确编写了说明保护的部分。addEditor(editors)Tom,我在最初的问题帖子中对上述问题添加了一些描述。我修复了上面的编辑器部分。看看它把东西放在哪里。还请记住,您可以将
Logger.log(您的变量)
放入日志,了解传递的数据。这对找出哪一块工作不正常非常有帮助。好吧,我想它就快到了!它成功地遍历该文件夹中的所有文件并锁定正确的工作表。它还将所有电子邮件地址正确分配为编辑器。但是,它不会删除以前有权限的人。也就是说,每个文件都与学生共享。脚本运行后,学生不应具有访问权限。这就是为什么我试图写几行关于首先删除所有编辑器(并使其仅可由我自己编辑),然后根据电子邮件列表分配编辑器的内容。这就是为什么我试图按顺序首先使我自己成为唯一的编辑器,然后再将编辑器列表添加回。保护。加法器(me);protection.removeditors(protection.getEditors());if(protection.canDomainEdit()){protection.setDomainEdit(false);}protection.addEditors(editors);
   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;
}