Auto Add checkboxes

779 views
Skip to first unread message

Jason Newell

unread,
Sep 17, 2022, 9:08:16 AM9/17/22
to Google Apps Script Community
Hi im hoping someone can help me 

Ive found this script online somewhere and it does what i want to a degree but cant figure out how to extend it to work it 4 columns at ones

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Label Selection"); //change this to the name of your sheet
  ui = SpreadsheetApp.getUi();
  
  //PICK ONE & comment out the other one:
  //var names = ss.getRange("B2:E");//Use this if you are naming the range
  var names = ss.getRange("A2:A");//Use this if you are naming the ranges
  
  var namesValues = names.getValues(); //Get array of all the names
  
  //PICK ONE & comment out the other one:
  //var checkboxes = ss.getRange("B2:E"); //Use this if you are naming the range
  var checkboxes = ss.getRange ('B2:B'); //Use this if you want to hard-code your range

  var cbRows = checkboxes.getHeight(); //Get # of rows in the ranges
  var cbValues = checkboxes.getValues(); //Get array of all the checkbox column cell values
  //Logger.log(cbValues);
  
  var newCBValues = new Array(cbRows); //Create an array to store all the new checkboxes values before we edit the actual spreadsheet
  
  for (var row = 0; row < cbRows; row++) {
    newCBValues[row] = new Array(0); // Make the array 2 dimensional (even though it only has 1 column, it must be 2D).
    if (namesValues[row] == "" || namesValues[row] == " ") { //If the name cell of this row is empty or blank then...
      newCBValues[row][0] = " "; //Set the value to one space (which will make the cell NOT true or false, and thus NOT display a checkbox).
      //Logger.log("newCBValues[" + row + "][0]: " + newCBValues[row][0]);
    }else{ //otherwise, if the name cell isn't blank...
      if (cbValues[row][0] === true) {
        newCBValues[row][0] = true; //Keep the checkbox checked if it's already checked
      }else{ //If the name cell isn't blank, and it's not true...
        newCBValues[row][0] = false; //Then Keep it or set it to False (an empty checkbox):
        
      }   
    }
  }
  checkboxes.setValues(newCBValues); // now that we have a completed array of our new checkbox values, let's edit the sheet with them!
  
}

When i put text in A2 it put a checkbox in B2 which is great but i also want it to put a checkbox in C2, D2 & E2 but when i change the ss.getRange ('B2:B') to ('B2:E') it gives me this error

Exception: The number of columns in the data does not match the number of columns in the range. The data has 1 but the range has 4.

Im new to scripts so please speak slowly :)

Thank you 
Jason

cbmserv...@gmail.com

unread,
Sep 19, 2022, 10:59:12 PM9/19/22
to google-apps-sc...@googlegroups.com

Try this:

 

function onEdit(e) {

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Label Selection"); //change this to the name of your sheet

  var row = e.range.getRow();

  var col = e.range.getColumn();

  const template = [[false,false,false]]

  if(col == 1)  // only insert checkboxes if column A is changed

  {

    ss.getRange(row,2,1,3).setValues(template).insertCheckboxes();

--
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/4d60aec0-c134-4073-bbd1-b9aaca2982b8n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages