How to set validation method in Google spreadsheets API

后端 未结 2 1272
梦谈多话
梦谈多话 2021-01-16 12:24

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

2条回答
  •  执笔经年
    2021-01-16 13:07

    Thank you @random-parts for help, it has brought me to the right track. If someone else will try to solve similar problem in PHP in feature, please find bellow fully working example:

        $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);
    

提交回复
热议问题