2

我到处看了看,似乎 GAS 还没有赶上 Google 电子表格。是否有一种蛮力方法可以在工作表中的某些范围内设置保护..?(并且用所有公式制作一张受保护的表格并引用它们对我没有帮助。)

我通过谷歌找到了这个:https ://code.google.com/p/google-apps-script-issues/issues/detail?id=1721

我什至在底部评论。(更多的是抱怨而不是任何有用的东西。)但是我上面的那个人发布了这段代码:

//Function to Protect Target Sheet
function ProtectTargetSheet() {

      //Enter ID for each Worksheet
      var IDs = ["Sheeet_1", "Sheet_2"]

      //Enter Page to protect, in order of WorkSheets
      var Sheet_names = ["Page_1", "Page_2"]

      //For each sheet in the array
      for ( i = 0; i < IDs.length; i ++) {

        //id of sheet
        var sheet = IDs[i]

        //activate dedicated sheet
        var ActiveSheet = SpreadsheetApp.openById(sheet)

        //Find last row and column for each sheet
        var LastRow = ActiveSheet.getSheetByName(Sheet_names[i]).getLastRow();
        var LastCol = ActiveSheet.getSheetByName(Sheet_names[i]).getLastColumn();

        //Name of protectSheet
        var Name = "Protect_Sheet";

        //Range for Protection
        var Named_Range = ActiveSheet.getSheetByName(Sheet_names[i]).getRange(1, 1, LastRow, LastCol);

        //Impletment Protect Range
        var protected_Range = ActiveSheet.setNamedRange(Name, Named_Range);
      }
}

我看不出这如何在共享时为范围提供保护。似乎它只会创建一个命名范围。他确实说要先手动设置权限。但我想不通他到底是什么意思。

无论如何,我希望现在有人已经找到了一种方法来做到这一点,直到谷歌将 GAS 与其对应方同步。

我的愿望是,通过 100% 的代码,在工作表、电子表格中选择一个范围,并使其在我将整个电子表格共享给一个人时,他或她无法编辑该范围。他们必须能够编辑该工作表中的其他部分。只是没有那个范围。手动执行此操作很容易,但是当必须创建 100 个电子表格时,能够通过 GAS 执行此操作会有所帮助。

谢谢。

4

4 回答 4

1

The easiest approach I've found is to use Data Validation: that is, write a script which will examine each cell to be 'protected' and create and apply a validation rule which enforces entry of the existing content and rejects anything else. [Of course, this also implies that you have designed your spreadsheet such that all entered data is on sheet or sheets separate from those which have formula embedded. On these you use normal sheet protection.]

于 2013-09-16T20:44:52.590 回答
1

您的部分问题涉及保护工作表。请看这里:setProtected(protection)

至于以编程方式保护范围号。但是,您可以保护工作表,不需要在同一个电子表格中,然后创建一个 onEdit 触发器,该触发器将用原始源数据替换“受保护”范围中的任何更改。

像这样的东西:

function onLoad() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var protected = ss.getSheets()[1].getRange("A1:B2").getValues(); //You could use openByID to get from a different ss
  var target = ss.getSheets()[0].getRange("A1:B2").setValues(protected);
}

function onEdit(){ 
 onLoad();
}

每次对电子表格进行更改时,脚本都会根据您指定的范围重写工作表中的数据。

于 2013-08-12T04:49:46.340 回答
1

根据2015 年 2 月 19 日发布的使用 Apps 脚本控制 Google Sheets 中的受保护范围和工作表,现在可以保护 Google Sheets 范围。

来自https://developers.google.com/apps-script/reference/spreadsheet/protection

等级保护

访问和修改受保护的范围和工作表。受保护范围可以保护静态单元格范围或命名范围。受保护的片材可以包括未受保护的区域。对于使用旧版 Google 表格创建的电子表格,请改用PageProtection 类

 // Protect range A1:B10, then remove all other users from the list of editors.
 var ss = SpreadsheetApp.getActive();
 var range = ss.getRange('A1:B10');
 var protection = range.protect().setDescription('Sample protected range');

 // Ensure the current user is an editor before removing others. Otherwise, if the user's edit
 // permission comes from a group, the script will throw an exception upon removing the group.
 var me = Session.getEffectiveUser();
 protection.addEditor(me);
 protection.removeEditors(protection.getEditors());
 if (protection.canDomainEdit()) {
   protection.setDomainEdit(false);
 }
于 2016-02-13T15:03:47.117 回答
0

如果您要发送同一张纸的 100 份副本。然后创建一个模板表,手动保护其中的范围并发送模板的副本。它将保留保护。

抱歉,但正如其他人所说,没有在子表级别设置保护的脚本方法。

于 2014-08-16T00:56:07.107 回答