0

I have a problem with the .setUnprotectedRanges method

I want next ranges to be added in the loop. If the cell is not empty, another unlocked range should be added, which is in column 3. For example: In column 3, rows 5, 8 and 10 are not empty.

The code below works but is not flexible:

protection.setUnprotectedRanges (
                 [
                 spreadsheet.getRange ("A5: F5")
                 spreadsheet.getRange ("A8: F8")
                 spreadsheet.getRange ("A8: F8")
                 ])

This is my code:

function xx(){
var app = SpreadsheetApp;
var as = app.getActiveSpreadsheet().getActiveSheet()
var sr = 6
var lr = as.getRange(4,2).getValue()
var spreadsheet = SpreadsheetApp.getActive();
var aa = ''

  for (var i=sr;i<lr;i++){      
      if (as.getRange(i, 3).getValue() != ""){                   
                var allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.SHEET);
                var protection = allProtections[0]
                protection.setUnprotectedRanges([])
                protection.setUnprotectedRanges(
                [
                spreadsheet.getRange("A" + i + ":B" + i)
                ])             
      }
   }
}

How can I make that different ranges will be added after a certain condition?

protection.setUnprotectedRanges (
                 [
                 spreadsheet.getRange ("A" + i + ": B" + i)
                 ])
  • Welcome. I am funding your question a little hard to follow. I suggest that you add a some screenshots (or in-line tables) to show the spreadsheet layout, and also to show how a successful outcome would appear. – Tedinoz Mar 29 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.