How to set validation method in Google spreadsheets API

 ̄綄美尐妖づ 提交于 2019-12-01 11:49:42

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!