2

Here's my script, but I can't get it to work for some weird reason??!!! Driving me nuts.

function removeThenSetProtection() {
  // Remove all range protections in the spreadsheet that the user has
  //permission to edit.
  var ss = SpreadsheetApp.getActive();
  var protections = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET);

  for (var i = 0; i < protections.length; i++) {
    var protection = protections[i];
    if (protection.canEdit()) {
      protection.remove();
    }
  }
}
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • 1
    The code worked for me. I set a protected range manually, ran your code, and it removed the protection. We need to have more info. Who is running the script? A user other than you? If so, what are their permissions? Also, use `Logger.log('Variable Name: ' + variableName)` statements, and then VIEW the LOGS. Or step through your code line by line with the Debugger. [Troubleshooting - Breakpoints](https://developers.google.com/apps-script/troubleshooting#using_the_debugger_and_breakpoints) – Alan Wells Feb 28 '15 at 14:59
  • 1
    Is the spreadsheet actually open when the code is run? Is the sheet that you want to act on, the active sheet? – Alan Wells Feb 28 '15 at 15:09

1 Answers1

1

Change:

var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);

To:

var protections = ss.getProtections(SpreadsheetApp.ProtectionType.SHEET);

That way, you'll be assured that you are getting a reference to everything that is protected, and future conflicts don't happen.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152