Search code examples
google-apps-scriptcheckboxgoogle-sheetsgs-conditional-formatting

Create checkbox in cell by typing in another cell


I want to create checkbox in column E cell by typing in column A cell. Is it possible?

Get the day of each month entered by typing in column A. sheet here

function onEdit(event)
{ 
  var timezone = "GMT-3";
  var timestamp_format = "dd"; // Timestamp Format. 
  var updateColName = "NOME";
  var timeStampColName = "DIA ";
  var sheet = event.source.getActiveSheet()


  var actRng = event.source.getActiveRange();
  var editColumn = actRng.getColumn();
  var index = actRng.getRowIndex();
  var headers = sheet.getRange(5, 1, 1, sheet.getLastColumn()).getValues();
  var dateCol = headers[0].indexOf(timeStampColName);
  var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
  Logger.log(dateCol+" "+editColumn+" "+updateCol+" "+index)
  if (dateCol > -5 && index > 5 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
    var cell = sheet.getRange(index, dateCol + 1);
    var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
    cell.setValue(date);
  }
  }

Solution

    • You want to put a checkbox to the column "E" at the same row with the edit row, when the column "A" is edited.
      • In your current script, when a value is put to the column "A", a value is put to the column "D".
      • You want to add the script for putting the checkbox to this.

    I could understand like this. In order to achieve above, how about this modification? Please think of this as just one of several answers.

    Modified script:

    Please put the following script below cell.setValue(date);.

    cell.offset(0, 1).insertCheckboxes();
    

    Note:

    • If you want to put the checked checkbox, please replace it to the following script.

      cell.offset(0, 1).insertCheckboxes().check();
      

    References:

    Edit:

    Please modify the settings of the conditional formatting rules as follows.

    enter image description here