How to set validation method in Google spreadsheets API

后端 未结 2 1273
梦谈多话
梦谈多话 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);
    
    0 讨论(0)
  • 2021-01-16 13:15

    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())
    }
    
    • Sheets API advanced service will have to be enabled
    0 讨论(0)
提交回复
热议问题