My First Apps Script Program giving a Run Time error

21 views
Skip to first unread message

Ishantha Siribaddana

unread,
Feb 12, 2023, 6:04:28 AM2/12/23
to Google Apps Script Community
I have written a program to make range of cells on google sheet to read only. It was nicely deployed. but runtime error was given. Can you help me. code is shown as follows. 
function makeCellsReadOnly(range) {
  var sheetName = "Exchange Rate Solution";
  var sheet = SpreadsheetApp.getActive().getSheetByName('"Exchange Rate Solution"');
  if (sheet != null) {
    var protection = sheet.getRange("d12:d13").protect().setDescription('Read only cells');
    protection.setProtected(true);
  } else {
    Logger.log("Sheet '" + sheetName + "' not found.");
  }
}

claudio fernandes

unread,
Feb 13, 2023, 11:09:47 PM2/13/23
to google-apps-sc...@googlegroups.com
function makeCellsReadOnly(range) {
  var sheetName = "Exchange Rate Solution";
  var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
  if (sheet != null) {
    var protection = sheet.getRange(range).protect().setDescription('Read only cells');
    protection.setProtected(true);
  } else {
    Logger.log("Sheet '" + sheetName + "' not found.");
  }
}

Make sure to pass a valid range to the function when calling it. For example, makeCellsReadOnly("D12:D13") will protect the range D12:D13.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/d3903378-4ef7-4ae3-89dc-76dda8549a23n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages