Google Apps Script How to Disable the ability to write to a cell in Sheets with code

You can overwrite the content in a cell, you can’t specifically disable the ability to input into a cell.

To disable a cell or row in Google Sheets using Apps Script, you can use the setValues() method to overwrite the content of the cell or row with an empty value or a specific text indicating it is disabled. Here’s an example code that disables a single cell and a row:

function disableCellAndRow() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Replace "Sheet1" with your sheet name
  var cell = sheet.getRange("A1"); // Replace "A1" with the cell you want to disable
  var row = sheet.getRange("2:2"); // Replace "2:2" with the row you want to disable
    // Disable cell
  cell.setValue("Disabled"); // Replace "Disabled" with your preferred text or an empty value

  // Disable row
  var numRows = row.getNumRows();
  var numCols = sheet.getLastColumn();
  var emptyValues = new Array(numRows).fill("").map(function() { return new Array(numCols).fill(""); }); // Create a 2D array with empty values
  row.setValues(emptyValues);
}

In this code, we first get a reference to the sheet we want to disable the cell or row in using the getActiveSpreadsheet() and getSheetByName() methods.

To disable a single cell, we get a reference to the cell using the getRange() method and set its value to an empty string or a text indicating it is disabled.

To disable a row, we get a reference to the row using the getRange() method and get the number of rows and columns in the range using the getNumRows() and getLastColumn() methods, respectively. We then create a 2D array of empty values with the same number of rows and columns as the range and set the range values to this array using the setValues() method.