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 want to create the checkbox in the cells of spreadsheet using the scripts. If my understanding is correct, how about this workaround? Unfortunately, the Class SpreadsheetApp has no methods for creating the checkbox yet. (When such methods are tried to be used, the error occurs.) So I would like to propose to create it using Sheets API.
When I saw ConditionType
of dataValidation
, the document of BOOLEAN says
The cell's value must be TRUE/FALSE or in the list of condition values. Supported by data validation. Renders as a cell checkbox. ...
From this, I could understand how to create the checkbox using Sheets API. The following script is a sample script. This creates 6 checkboxes to "A1:C3". When you use this script, please enable Sheets API at Advanced Google Services and API console as follows.
If now you are opening the script editor with the script for using Sheets API, you can enable Sheets API for the project by accessing this URL https://console.cloud.google.com/apis/library/sheets.googleapis.com/
In this sample script, the checkboxes are created to "A1:C3" of Sheet1. Please use this script as the container-bound script.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetId = ss.getSheetByName("Sheet1").getSheetId();
var resource = {"requests": [
{
"repeatCell": {
"cell": {"dataValidation": {"condition": {"type": "BOOLEAN"}}},
"range": {"sheetId": sheetId, "startRowIndex": 0, "endRowIndex": 3, "startColumnIndex": 0, "endColumnIndex": 3},
"fields": "dataValidation"
}
},
{
"updateCells": {
"rows": [
{"values": [{"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": false}}, {"userEnteredValue": {"boolValue": false}}]},
{"values": [{"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": false}}]},
{"values": [{"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": true}}, {"userEnteredValue": {"boolValue": true}}]}
],
"start": {"rowIndex": 0, "columnIndex": 0, "sheetId": sheetId},
"fields": "userEnteredValue"
}
}
]};
Sheets.Spreadsheets.batchUpdate(resource, ss.getId());
Flow :
dataValidation
is set using repeatCell
.boolValue
is set using updateCells
.At June 22, 2018, this script returns an error of the server error yet.
var rule = SpreadsheetApp.newDataValidation().withCriteria(SpreadsheetApp.DataValidationCriteria.CHECKBOX, ["TRUE", "FALSE"]).build();
SpreadsheetApp.getActiveSheet().getRange("A1").setDataValidation(rule);
If I misunderstand your question, I'm sorry.