我对新的Google表格API v4感到困惑。我的问题是:如何在电子表格中为指定的列设置验证规则? 没有有用的教程描述如何使用适当的方法。
结果应如下所示:
应在数据上传之前设置验证(工作正常)。
我目前的代码:
$client = getClient();
$service = new Google_Service_Sheets($client);
$fileId = 'my-document-id';
$body = new Google_Service_Sheets_SetDataValidationRequest(array(
'setRange' => new Google_Service_Sheets_GridRange(
array(
'sheetId'=>'List1',
'startColumnIndex'=>'0',
'endColumnIndex'=>'1'
)
),
'setRule' => new Google_Service_Sheets_DataValidationRule(
array(
'setValues'=>array('YES','NO')
)
)
));
$sheetReq = new Google_Service_Sheets_Request($client);
$sheetReq->setSetDataValidation($body);
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
'requests' => $sheetReq
));
$result = $service->spreadsheets->batchUpdate($fileId, $batchUpdateRequest);
答案 0 :(得分:3)
谢谢@ random-parts的帮助,它带我走上正轨。如果其他人试图在PHP中解决类似的问题,请参阅下面的完整工作示例:
$client = $this->getClient();
$service = new Google_Service_Sheets($client);
$ary_values = ['yes','nope','maybe','never ever'];
foreach( $ary_values AS $d ) {
$cellData = new Google_Service_Sheets_ConditionValue();
$cellData->setUserEnteredValue($d);
$values[] = $cellData;
}
$conditions = new Google_Service_Sheets_BooleanCondition();
$conditions->setType('ONE_OF_LIST');
$conditions->setValues($values);
$setRule= new Google_Service_Sheets_DataValidationRule();
$setRule->setCondition($conditions);
$setRule->setInputMessage('Please set correct value');
$setRule->setShowCustomUi(true);
$range = new Google_Service_Sheets_GridRange();
$range->setStartRowIndex(1);
$range->setEndRowIndex(5);
$range->setStartColumnIndex(1);
$range->setEndColumnIndex(2);
$range->setSheetId(YOUR_SHEET_ID); //replace this by your sheet ID
$valReq = new Google_Service_Sheets_SetDataValidationRequest();
$valReq->setRule($setRule);
$valReq->setRange($range);
$sheetReq = new Google_Service_Sheets_Request();
$sheetReq->setSetDataValidation($valReq);
$bodyReq = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$bodyReq->setRequests($sheetReq);
$result = $service->spreadsheets->batchUpdate($fileId, $bodyReq);
答案 1 :(得分:2)
DataValidationRule对象如下所示:
"rule": {
"condition": {
"type": "ONE_OF_LIST",
"values": [
{ userEnteredValue: "Yes"},
{ userEnteredValue: "No"}
],
},
"inputMessage": "",
"strict": true,
"showCustomUi": true,
}
您想使用rule.condition.type
ONE_OF_LIST,然后在列表中输入您想要的rule.condition.values
。 showCustomUi
会显示下拉列表
使用Sheets脚本编辑器中的google apps脚本的完整示例:
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())
}