0

I have a spreadsheet that I owner and I have 10 users with editor permission as there are quite a lot of cells to unlock or lock, it might be best to lock everything first and unlock the ones that they can safely use without accidentally editing a formula incorrectly

now I'm just guessing what the best solution would be I would like to avoid that users is deleted

if it matters then no one is in a group

these would be the ones that the editors can edit

['B3:U27', 'W3:AP27', 'E29:E31', 'I29:I31', 'M29:M31', 'Q29:Q31', 'U29:U31', 'Z29:Z31', 'AD29:AD31', 'AH29:AH31', 'AL29:AL31', 'AP29:AP31', 'B29', 'F29', 'J29', 'N29', 'R29', 'W29', 'AA29', 'AE29', 'AI29', 'AM29', 'C29', 'G29', 'K29', 'O29', 'S29', 'X29', 'AB29', 'AF29', 'AJ29', 'AN29', 'D29', 'H29', 'L29', 'P29', 'T29', 'Y29', 'AC29', 'AG29', 'AK29', 'AO29', 'B31', 'F31', 'J31', 'N31', 'R31', 'W31', 'AA31', 'AE31', 'AI31', 'AM31', 'C31', 'G31', 'K31', 'O31', 'S31', 'X31', 'AB31', 'AF31', 'AJ31', 'AN31', 'D31', 'H31', 'L31', 'P31', 'T31', 'Y31', 'AC31', 'AG31', 'AK31', 'AO31', 'B33:C33', 'F33:G33', 'J33:K33', 'N33:O33', 'R33:S33', 'W33:X33', 'AA33:AB33', 'AE33:AF33', 'AI33:AJ33', 'AM33:AN33' ,'D33:E33', 'H33:I33', 'L33:M33', 'P33:Q33', 'T33:U33', 'Y33:Z33', 'AC33:AD33', 'AG33:AH33', 'AK33:AL33', 'AO33:AP33'];

everything else can be locked

  1. will 1 or 2 extra sheets be created each day and will this apply to all sheets from now on?

  2. does it matter that already have a few sheets with manually protection (without a script)?

  3. users will be notified of this or is it all just happening in the background?

UPDATE:

I found the right script to protect the page and unlock the range and it works perfectly

link

function testProtect() {
var sheet = SpreadsheetApp.getActiveSheet();
var protection = sheet.protect().setDescription('Sample protected sheet');
var unprotected = sheet.getRange('B3:I27');
protection.setUnprotectedRanges([unprotected]);

var me = Session.getEffectiveUser();
protection.addEditor(me);
protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);

}
    }

but there were two things I couldn't figure out how can I apply it to all sheets and add more cells and ranges? because if I add another range it gives an error

var unprotected = sheet.getRange('B3:I27','F29:I29');

Exception: B3:I27 cannot be converted to int type (line 4 in the "Code" file)

thanks in advance for any help!

  • Have you tried your code? Does it work? – Rubén May 13 '21 at 15:56
  • @Rubén no I just picked these info's up from stackoverflow – Ádám Hegedüs May 13 '21 at 16:02
  • If you are new to programming, JavaScript or Google Apps Script, I think that you should mention that and that you picked the code or ideas from this site (include a link to the source) and ask if it's safe to run it, otherwise you should test the code before posting a question as [ask] suggest to show what was tried and add brief description of the search efforts done. P.S. If you are new to using Google Apps Script to extend Google Sheets, please read https://developers.google.com/apps-script/guides/sheets . – Rubén May 13 '21 at 16:49
  • @Rubén thanks for the advice yes, it would have been really useful for others to be able to help better but I don't know where I got those lines from so I deleted it from the post I think you can tell from all my questions that I'm a beginner – Ádám Hegedüs May 13 '21 at 17:15
  • Please show what you tried and add a brief description of your search efforts as is suggested in [ask]. – Rubén May 13 '21 at 18:25

1 Answers1

1

If I understand your post correctly, here's your goal:

  1. Create a script to lock your sheet and only allow specific ranges to be editable for the users with edit access.
  2. Apply that script to all of your sheets on your spreadsheet file.

Recommended Solution:

You can refer to this sample script below where it locks your sheet and only unlock specific ranges you setup.

Sample script

[UPDATED]

function main(){ //Main function to run
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var disregard = ["Sheet3","Sheet4","Sheet5"]; //ADD SHEET NAMES HERE THAT YOU WANT TO BE DISREGARDED

  for(var x=0; x<sheets.length; x++){
    if(disregard.some(data => sheets[x].getName().includes(data))){ 
      //E.g. Disregard any sheet names added on the "disregard" array
    }else{
      unlockCertainRanges(sheets[x]);
    }
  }
}

function unlockCertainRanges(currentSheet){ //Function to unlock certain ranges on your spreadshseet
  var sheet = currentSheet;
  // Remove all range protections in the spreadsheet
  var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  for (var i = 0; i < protections.length; i++) {
    var protection = protections[i];
    protection.remove();
  }

  var protection = sheet.protect();
  //restrict editors to owner
  protection.getRange().getA1Notation();
  var eds = protection.getEditors();
  protection.removeEditors(eds);

  //set unprotected ranges
  var ranges = protection.getUnprotectedRanges();
  var data = ["A1:A5","B6:B10","C11:C15"]; // ADD YOUR RANGES HERE
  data.forEach(res => { //LOOPS INTO EVERY ARRAY CONTAINING SPECIFIC RANGES
    ranges.push(sheet.getRange(res));
    protection.setUnprotectedRanges(ranges); //REMOVES THE PROTECTION ON THE RANGE
  });
}

Note: Borrowed a snippet of script to unlock specific ranges from How to protect a sheet then unprotect specific cells as reference.

Result:

Sample Sheet

All cells are locked except the ranges "A1:A5","B6:B10" & "C11:C15" (contains the "Unlock" word for visibility)

enter image description here

Other cells are locked enter image description here

Unlocked range cells are editable enter image description here

SputnikDrunk2
  • 3,398
  • 1
  • 5
  • 17
  • works very cool, exactly what I was looking for thank you very much! – Ádám Hegedüs May 13 '21 at 19:48
  • could it be possible to ignore a few sheets? – Ádám Hegedüs May 13 '21 at 20:03
  • 1
    Yes, you can implement an if statement on the main method to check for a certain sheet name to be skipped. I've added a comment on my sample script. – SputnikDrunk2 May 13 '21 at 20:51
  • with this I can skip one but how can I skip more? I tried these: "List, List2" or these "List", "List2" but it didn't work – Ádám Hegedüs May 13 '21 at 21:30
  • 1
    I see, I've updated my main function sample script with an array where you can put names of sheets you want to be disregarded . – SputnikDrunk2 May 13 '21 at 21:50
  • sorry for the new question, i will open a new post if I have to. right now it takes 6 minutes to protect 16 sheets can it be something that has already been protected or have a padlock on them not to be touched? in test it was only 16 files but the end of the year up to 500. I tried to find a way to retrieve the names of the already protected sheets I mean something like getSheetName() but for the protected ones. Or maybe put it in the exceptions if there is already such protection on this description? setDescription('Already protected'); do you have any ideas? – Ádám Hegedüs May 14 '21 at 06:38
  • It is actually an expected behavior for the script to run that long, given that there are multiple ranges that will be processed on multiple sheets. If you want to workaround this, I'd highly suggest that you post a new question as this is no longer related to this post. – SputnikDrunk2 May 14 '21 at 17:53
  • Thanks, I took your advice, do you have any idea what colleagues think? [link] (https://stackoverflow.com/questions/67540617/how-to-ignore-already-protected-sheets-with-script) – Ádám Hegedüs May 15 '21 at 16:00