I am confused about the new Google Sheets API v4. My question is: how can I set validation rules for specified column(s) in spreadsheet? There is no useful tutorial with des
The DataValidationRule object would look like the following:
"rule": {
"condition": {
"type": "ONE_OF_LIST",
"values": [
{ userEnteredValue: "Yes"},
{ userEnteredValue: "No"}
],
},
"inputMessage": "",
"strict": true,
"showCustomUi": true,
}
You want to use rule.condition.type
ONE_OF_LIST and then enter the rule.condition.values
you want in the list. showCustomUi
will show the dropdown
A full example using google apps script from the Sheets script editor:
function setDataVal () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var validation = {
"setDataValidation": {
"range": {
"sheetId": sheet.getSheetId(),
"startRowIndex": 1,
"endRowIndex": 5,
"startColumnIndex": 1,
"endColumnIndex": 5,
},
"rule": {
"condition": {
"type": "ONE_OF_LIST",
"values": [
{ userEnteredValue: "Yes"},
{ userEnteredValue: "No"}
],
},
"inputMessage": "",
"strict": true,
"showCustomUi": true,
}
},
}
var req = {
"requests": [validation],
"includeSpreadsheetInResponse": false,
}
Sheets.Spreadsheets.batchUpdate(req, ss.getId())
}