Search code examples
google-sheetscheckboxgoogle-sheets-formula

Checkbox in Google Sheet


Is there a way where a checkbox will automatically appear when new data is entered in google sheet? So this will save time for me that whenever new data is entered, there's a corresponding checkbox to it.

Thank you!


Solution

  • You can use this script to do so:

    function myFunction() {
      var ss = SpreadsheetApp.getActiveSpreadsheet(); 
      var sheet = ss.getActiveSheet(); 
      var range = sheet.getRange(1,1,sheet.getLastRow(),1);
      var values = range.getValues(); //gets the values on ColA
    
      for(var i = 1; i < values.length; i++){
        if(values[i] != ""){ //this is the condition to check if Column A values is not blank.
          sheet.getRange(2,3,i,1).insertCheckboxes(); //inserts the checkbox on column C per iteration.
        }
      }
    }
    

    What this does is that it checks Column A if it's not blank, it will add a checkbox on Column C per value on Column A.

    Before running the script:

    enter image description here

    After running the script:

    enter image description here

    For best results, you can associate this function on a trigger by going to App Script > Triggers page like so:

    enter image description here

    You have options for onChange, onEdit, onFormSubmit, or onOpen event driven triggers.

    References:

    https://developers.google.com/apps-script/guides/triggers/installable https://developers.google.com/apps-script/reference/spreadsheet/range#insertcheckboxes