简体   繁体   中英

Can I add multiple checkboxes in rows when a cell in each row contains a value?

Please help me out with this script that I found?

I'm not an experienced coder, so I'm just winging it at this point.

I want to simply make the process of adding check boxes to each new row automated with the following script:

 function onEdit() { var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); //change this to the name of your sheet ui = SpreadsheetApp.getUi(); //PICK ONE & comment out the other one: var names = ss.getRange("Ranger");//Use this if you are naming the range (Refer Range) //var names = ss.getRange("B3:B");//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("Checkboxes"); //Use this if you are naming the range //var checkboxes = ss.getRange("C2:E2"); //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! }

I'm only able to add check boxes into one column, I would like to add check boxes into like 10 separate columns based on the value of 1 Row's cell values. Spreadsheet I'm working on to capture data

You use [row][0] which refers to the 1st column of a range

If you want to perform an an action for all columns, you need to loop through those columns.

Sample:

if (cbValues[row][0] == true) {
  for( var j = 0; j < newCBValues.length; j++){
    newCBValues[row][j] = true;
   }
}

Btw., see here how to build checkboxes instead of copying them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM