In a previous post I was helped to fix a mistake I made with the signature on the protection method setUnprotectedRanges. Today, I am facing the same problem but this time I do not make the mistake (as far as I can see).

I actually pass an array on ranges in A1 notation and the logs confirm that. I can assure you I am not drunk or drugged, may be only a bit tired, and I cannot see what is probably a trivial problem again.

Here is the script

  var thisSheet = ss.getActiveSheet()
  var template = thisSheet.getRange("TemplateLineRecurrent")
  var forms = template.getFormulas()
  Logger.log(forms)
  var insertPos=template.getRow()
  //insert a new line
  thisSheet.insertRowsBefore(insertPos,1)
  //copy the template
  destRange = thisSheet.getRange(insertPos,template.getColumn(),1,template.getLastColumn()-template.getColumn()+1)
  destRange.setFormulas(forms)
  var p = thisSheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
  var ranges = p.getUnprotectedRanges();
  var newRanges = [];
  for (var i = 0; i < ranges.length; i++) {
    newRanges.push(ranges[i].getA1Notation());
  } 
  r=thisSheet.getRange(insertPos,destRange.getColumn(),1,5).getA1Notation()
  Logger.log(r)
  newRanges.push(r)
  Logger.log(newRanges)
  p.setUnprotectedRanges(newRanges); 

and here is the matching log

Oct 28, 2023, 2:12:21 PM
Info
[[, , , , , =IF(E13>F13;E13-F13;0), =IF(F13>E13;F13-E13;0)]]
Oct 28, 2023, 2:12:22 PM
Info
B13:F13
Oct 28, 2023, 2:12:22 PM
Info
[C7:F12, B29, B7:B12, B13:F13]
Oct 28, 2023, 2:12:22 PM
Error
Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.Protection.setUnprotectedRanges.
    at AddRecurrentLine(WebAppScripts:73:5)
    at myMacros(WebAppScripts:36:7)
    at doGet(WebAppScripts:2:20)
1

There are 1 answers

5
Tanaike On

I think that the argument of setUnprotectedRanges(ranges) method is Range[]. In your script, it seems that an array including the A1Notation is used. I thought that this might be the reason for your current issue of The parameters (number[]) don't match the method signature for SpreadsheetApp.Protection.setUnprotectedRanges.. In this case, how about the following modification?

From:

p.setUnprotectedRanges(newRanges); 

To:

p.setUnprotectedRanges(thisSheet.getRangeList(newRanges).getRanges());
  • By this, thisSheet.getRangeList(newRanges).getRanges() is Range[] of thisSheet.

References: