简体   繁体   English

用脚本替换受保护范围内的用户(Google Sheet)

[英]Replace user in a protected range by script (Google Sheet)

here my specific case:这是我的具体情况:

  1. I have some range protected in google sheets我在谷歌表格中保护了一些范围
  2. I need to replace some specific Editor if is editor of those range (var Editor2Replace and Editor2Add are emails)如果是这些范围的编辑器,我需要替换一些特定的编辑器(var Editor2Replace 和 Editor2Add 是电子邮件)
  3. Logically I tried to, for each sheet:从逻辑上讲,我试图为每张纸:
  • Cycle (FOR) of all the protected range (counter p)所有保护范围的循环(FOR)(计数器 p)
  • For each protected range catch current editors and have it in array对于每个受保护的范围,捕获当前编辑器并将其放入数组中
  • Of the Editors read the email ==> this is what generate the mistake编辑阅读 email ==> 这就是产生错误的原因
  • Cycle (FOR) all the editors looking if someone of those is == Editor2Replace (that is an email)循环(FOR)所有编辑,查看其中某人是否为 == Editor2Replace(即电子邮件)

Here the code, but something is logically wrong, I doubt in what is an array and what not..这里是代码,但在逻辑上有些错误,我怀疑什么是数组,什么不是..

   var Protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    for (var p = 0; p < Protections.length; p++) {
      var Protection_Desc = Protections[p].getDescription();
      var Protection_Editors = [];
      var Protection_Editors = [Protections[p].getEditors()];
      for (var r = 0; r < Protection_Editors.length; r++){
         var Protection_Email[r] = [Protection_Editors[r].getEmail()];
         if (Protection_Idontknow == Editor2Replace){
          Protections[p].addEditor = Editor2Add;
          Protections[p].removeEditor = Editor2Replace;

          var Protection_Range = Protections[p].getRange();
          var Protection_Row = Protection_Range.getRow();
          var Owner1 = sheet.getRange(Protection_Row,5).getValue();
          var Owner2 = sheet.getRange(Protection_Row,6).getValue();
          if (Owner1 == Editor2Replace){
              sheet.getRange(Protection_Row,5).setValue(Editor2Add);
          }
          if (Owner2 == Editor2Replace){
              sheet.getRange(Protection_Row,6).setValue(Editor2Add);
          }
        }
      }

Many thanks for hepling非常感谢您的帮助

There were a lot of issues in your script and I will enumerate them one by one.您的脚本中有很多问题,我将一一列举。 Also, I was able to replace a user in the protected sheet by modifying your script.此外,我可以通过修改您的脚本来替换受保护工作表中的用户。

Issues:问题:

  1. Duplicate declaration重复声明
var Protection_Editors = [];
var Protection_Editors = [Protections[p].getEditors()];
  1. Storing the returned value (array) in another array (which should not be done in your issue, it doesn't help you with anything)将返回的值(数组)存储在另一个数组中(这不应该在您的问题中完成,它对您没有任何帮助)
var Protection_Editors = [Protections[p].getEditors()];
...
var Protection_Email[r] = [Protection_Editors[r].getEmail()];
  1. Newly declared variable having an index (which I don't understand why)新声明的具有索引的变量(我不明白为什么)
var Protection_Email[r] = [Protection_Editors[r].getEmail()];
  1. Variable not declared Protection_Idontknow未声明的变量Protection_Idontknow
if (Protection_Idontknow == Editor2Replace){
  1. Incorrect usage of methods addEditor and removeEditor方法addEditorremoveEditor的错误使用
Protections[p].addEditor = Editor2Add;
Protections[p].removeEditor = Editor2Replace;

Below code should fix those issues (added some comments):下面的代码应该解决这些问题(添加了一些评论):

Code:代码:

  var Protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  for (var p = 0; p < Protections.length; p++) {
    var Protection_Desc = Protections[p].getDescription();
    // returned value of getEditors is already an array, return as is
    var Protection_Editors = Protections[p].getEditors();
    for (var r = 0; r < Protection_Editors.length; r++) {
      var Protection_Email = Protection_Editors[r].getEmail();
      // compare current email with the one you want to replace
      if (Protection_Email == Editor2Replace) {
        // add new and remove the one to replace
        Protections[p].addEditor(Editor2Add);
        Protections[p].removeEditor(Editor2Replace);
      }
    }
  }

Note:笔记:

  • I have removed anything that were unrelated to the replacement of editors.我已经删除了与更换编辑无关的任何内容。

Reference:参考:

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

相关问题 带有范围的 Google Script“替换”功能 - Google Script 'replace' function with range Google表格脚本帮助-存储/写入表格ID,表格名称,合并范围的单元格位置 - Google Sheet Script Help - Store/write sheet ID, sheet name, cell location for a combined range Google Sheet 数组范围 - Google Sheet array range Google Apps脚本替换和更新范围内的单元格 - Google Apps Script Replace and update cell within a range Google脚本-如何通过在工作表2中搜索那些值来替换工作表1中的单元格值,以及如何从工作表2的另一列返回相关数据 - Google Script - how to replace cell values in Sheet 1 by searching Sheet 2 for those values and returning correlating data from another Sheet 2 column 谷歌工作表命名范围多列 - Google Sheet Named Range Multiple Columns Google 工作表 - 如何使用自定义日期范围过滤数据 - Google sheet - how to filter the data with custom date range 我可以使用 GoogleTranslate 论坛翻译 Google Sheet 中的一系列数据吗? - Can I translate a range of data in Google Sheet with GoogleTranslate foruma? 使用Google脚本从Google表格中的单元格列表获取数组 - Using Google Script to get an array from a list of cells in google sheet 通过从 Google Sheet 生成的数组进行交互 | Google Apps 脚本 - Interact through an array generated from a Google Sheet | Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM