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 found a reference regarding this in Google Apps Script documentation.
Currently I'm doing it manually but any suggestion using script will be much appreciated.
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
ofdataValidation
, the document of BOOLEAN saysFrom 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.
Enable Sheets API v4 at Advanced Google Services
Enable Sheets API v4 at API console
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/
Sample script :
In this sample script, the checkboxes are created to "A1:C3" of Sheet1. Please use this script as the container-bound script.
Flow :dataValidation
is set usingrepeatCell
.boolValue
is set usingupdateCells
.Result :
Note :
At June 22, 2018, this script returns an error of the server error yet.
References :
If I misunderstand your question, I'm sorry.
Short answer
Add the checkbox from the Google Sheets UI, then use one of the copyTo methods of Class Range.
Explanation
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
NOTE: If you don't want to pass all the cell properties (borders, formulas, background, etc. instead of
SpreadsheetApp.CopyPasteType.PASTE_NORMAL
useSpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION
.Related Q on Stack Overflow
Easy:
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:
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:
I agree that you have to workaround to create a checkbox. Another way maybe is to create a dropdown list.