我对新的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);
发布于 2017-11-08 16:41:26
谢谢你@随机-部分帮助,它已经把我带到了正确的轨道。如果其他人试图在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);
发布于 2017-11-02 17:51:33
DataValidationRule对象如下所示:
"rule": {
"condition": {
"type": "ONE_OF_LIST",
"values": [
{ userEnteredValue: "Yes"},
{ userEnteredValue: "No"}
],
},
"inputMessage": "",
"strict": true,
"showCustomUi": true,
}
您希望使用rule.condition.type
列表,然后在列表中输入所需的rule.condition.values
。showCustomUi
将显示下拉列表
从Sheets脚本编辑器中使用google应用程序脚本的完整示例:
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())
}
https://stackoverflow.com/questions/47079970
复制相似问题