谷歌表保护 - 基本需要将无法正常工作

时间:2016-05-13 19:43:52

标签: google-apps-script google-sheets

我有一个基本的问题,并一直在试图弄清楚我的头发。

我希望只有拥有该链接的任何人才能查看Google工作表。 我希望能够编辑任何东西作为所有者(duh) 我希望保护除某些单元格(例如A1)之外的工作表可编辑,并添加1个人使用Google帐户(例如george@gmail.com),这样他们只能编辑A1。

我的所有尝试都导致匿名用户只能按需查看,但george@gmail.com可以编辑每个单元格,但不限于A1。

我可能做错了什么,或者这种情况是不可能的?

我尝试过很多指南,包括: https://support.google.com/docs/answer/144687?hl=en http://www.appscare.com/2015/02/set-permissions-protected-sheets/

谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个。我将样本表上的共享设置给任何有链接的人都可以查看,然后添加了可以编辑的电子邮件地址。电子邮件地址的用户可以编辑“A1” 当然,我可以编辑所有内容。

function protectionTest(){
 // Protect the active sheet except A1, then remove all other users from the  list of editors.
 var sheet = SpreadsheetApp.getActiveSheet();
 var protection = sheet.protect().setDescription('Sample protected sheet');
 var unprotected = sheet.getRange('A1');
 protection.setUnprotectedRanges([unprotected]);

 // 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);
 }}

花了一段时间,但这将确定彩色范围并取消保护。设置设置可以如上编辑。

 function cellsToUnprotect(){
 //Find cells of specific color and create array of ranges.
 var ss = SpreadsheetApp.getActiveSpreadsheet()
 var sheet = ss.getSheetByName("Sheet1")// Change Sheet Name as needed.
 var rng = sheet.getRange("A1:C10").getBackgrounds()// Adjust Range as  needed.
 var arrayBG=[]
{
  for(var i=0;i<rng.length;i++){
  for(var j=0;j<rng[1].length;j++){
  //if(rng[i][j] != "#ffffff"){ //Any background color not equal to white. I  prefer this.
  if(rng[i][j] == "#ffff00"){ //background color defined ("#ffff00" is  yellow)
  var r=i+1
  var c=j+1
  var range=sheet.getRange(r, c)
  arrayBG.push(range)
    }}}
  unprotect(arrayBG)
  }}

function unprotect(arrayBG){
//Protect Sheet1 and unprotect ranges in passed array.
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName("Sheet1") // Change Sheet Name as needed.
var protection = sheet.protect().setDescription('Sample protected sheet');
protection.setUnprotectedRanges(arrayBG);
// 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);
}}

答案 1 :(得分:0)

抱歉,我花了很长时间才回答,我正在度假。这将处理多张纸。如果这对您有用,请批准答案。

function cellsToUnprotect(){
//Find cells of specific color and create array of ranges.
 var ss = SpreadsheetApp.getActiveSpreadsheet()
 var sheet = SpreadsheetApp.getActiveSheet();
 var sheetNameToWatch=[] //Array of Sheet names
 var names = ss.getSheets()
  for( j=0;j<names.length;j++) {
    var n= names[j].getSheetName();
      if(n!="Done"){ //If Sheet name not "Done" add to array. Change sheet  name to exclude sheets. 
       sheetNameToWatch.push(n)
      }}
   for(var k=0;k<sheetNameToWatch.length;k++){
      var rng =  ss.getSheetByName(sheetNameToWatch[k]).getRange("A1:C10").getBackgrounds()//  Adjust Range as needed.
      var arrayBG=[]
{
  for(var i=0;i<rng.length;i++){
  for(var j=0;j<rng[1].length;j++){
  //if(rng[i][j] != "#ffffff"){ //Any background color not equal to white. I   prefer this.
  if(rng[i][j] == "#ffff00"){ //background color defined ("#ffff00" is   yellow)
     var r=i+1
     var c=j+1
     var range=ss.getSheetByName(sheetNameToWatch[k]).getRange(r, c)
  arrayBG.push(range)
    }}}
  unprotect(arrayBG,sheetNameToWatch,k)
}}}

function unprotect(arrayBG,sheetNameToWatch,k){
 //Protect Sheet1 and unprotect ranges in passed array.
 var ss = SpreadsheetApp.getActiveSpreadsheet()
 var sheet = ss.getSheetByName(sheetNameToWatch[k]) // Change Sheet Name as needed.
var protection = sheet.protect().setDescription('Sample protected sheet');
protection.setUnprotectedRanges(arrayBG);
// 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);
}}