-1
function lockCells(spreadsheet, cell, rangeToLock) {
  // Get the value of the specified cell
  var cellValue = spreadsheet.getRange(cell).getValue();
  // If the cell value is true, lock the specified range of cells
  if (cellValue === true) {
    spreadsheet.getRange(rangeToLock).setLocked(true);
  // Call the lockCells function to lock cells A1:A5 if cell B1 is set to TRUE
lockCells(SpreadsheetApp.getActiveSheet(),"B1","A1:A5");
  }
}

TypeError: Cannot read properties of undefined (reading 'getRange') lockCells @ LockCells.gs:3

How to correct the error.

Rubén
  • 34,714
  • 9
  • 70
  • 166
JCAN
  • 3
  • 1

2 Answers2

0

when calling lockCells you are not sending the value for spreadsheet which makes it undefined. Hence you are facing the following error

Make the following changes in the code:

var cellValue;
if(spreadsheet != undefined)
     cellValue = spreadsheet.getRange(cell).getValue();

if(spreadsheet != undefined)
    spreadsheet.getRange(rangeToLock).setLocked(true);
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
0

The primary issue with the code you quote is that it attempts to use a method called Range.setLocked() which does not exist.

To lock cells, use the Protection class.

doubleunary
  • 13,842
  • 3
  • 18
  • 51