I know that checkbox is a relatively new feature in Google Sheets, so I\'m trying to find a way to automatically create checkboxes in cells.
So far, I haven\'t foun
You can now directly insertCheckboxes
(or removeCheckboxes
) on a Range or RangeList without any workarounds. You can also change the checked value/unchecked value using alternate method signatures found in the documentation.
SpreadsheetApp.getActive()
.getRange('Sheet2!A2:A10')
.insertCheckboxes();
The checkbox is the recently added Data Validation criterion. Interestingly enough, when I attempt to call the 'getDataValidation()' method on the range that contains checkboxes, the following error is thrown:
var rule = range.getDataValidation();
We're sorry, a server error occurred. Please wait a bit and try again.
In the meantime, you can work around this by placing a single checkbox somewhere in your sheet and copying its Data Validation to the new range. For example, if "A1" is the cell containing the checkbox and the target range consists of a single column with 3 rows:
var range = sheet.getRange("A1"); //checkbox template cell
var targetRange = sheet.getRange(rowIdex, colIndex, numOfRows, numOfCols);
range.copyTo(col, SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION);
var values = [["true"], ["false"], ["false"]];
targetRange.setValues(values);
Easy:
//There are two ways, using Range or using current cell or sheet or similar
//First is using current cell
function VoF() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getCurrentCell().offset(1, 0, 499, 1).setDataValidation(SpreadsheetApp.newDataValidation()
.setAllowInvalid(true)
.setHelpText('TRUE or FALSE')
.requireCheckbox() //if you customize this is possible that you dont get the boxes and the verification data could fail,so keep them standar with TRUE and FALSE
.build());
};
//Second is using Spreedsheet ranges
function myFunction() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('G1:G11').activate();
spreadsheet.getRange('G1:G11').setDataValidation(SpreadsheetApp.newDataValidation()
.setAllowInvalid(false)
.requireCheckbox()
.build());
};
I'm not sure when they did it, but they've added this now.
Use class DataValidationBuilder
's requireCheckbox() method. Example:
function setCheckboxes() {
// Assumes there's only one sheet
var sheet = SpreadsheetApp.getActiveSheet();
// This represents ALL the data
var dataRange = sheet.getDataRange();
/* Get checkbox range from sheet data range. Assumes checkboxes are on the
left-most column
*/
var dataRangeRow = dataRange.getRow();
var dataRangeColumn = dataRange.getColumn();
var dataRangeLastRow = dataRange.getLastRow();
var checkboxRange = sheet.getRange(
dataRangeRow,
dataRangeColumn,
dataRangeLastRow
);
var enforceCheckbox = SpreadsheetApp.newDataValidation();
enforceCheckbox.requireCheckbox();
enforceCheckbox.setAllowInvalid(false);
enforceCheckbox.build();
checkboxRange.setDataValidation(enforceCheckbox);
}
I agree that you have to workaround to create a checkbox. Another way maybe is to create a dropdown list.
function myFunction() {
var cell = SpreadsheetApp.getActive().getRange('A1');
var rule = SpreadsheetApp.newDataValidation().requireValueInList(['☐','☑']).build();
cell.setDataValidation(rule);
}
Add the checkbox from the Google Sheets UI, then use one of the copyTo methods of Class Range.
The Google Apps Script Spreadsheet service doesn't include a methods for everything that could be done through the Google Sheets user interface. This is the case of the Insert > Checkbox which is a pretty new feature.
Even the Record macro feature can't do this. The following was recorded one momento ago
/** @OnlyCurrentDoc */
function InsertCheckbox() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1').activate();
/*
* Added to show the missing Insert > Checkbox step
*/
spreadsheet.getRange('B1').activate();
spreadsheet.getRange('A1').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
NOTE: If you don't want to pass all the cell properties (borders, formulas, background, etc. instead of SpreadsheetApp.CopyPasteType.PASTE_NORMAL
use SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION
.
Related Q on Stack Overflow