如何使用PHP中的Google表格API在一系列单元格上设置数据验证?

I have the following code:

$requests = '{
  "requests": [
    {
      "setDataValidation": {
        "range": {
          "sheetId": mySheetID,
          "startRowIndex": 0,
          "endRowIndex": 10,
          "startColumnIndex": 0,
          "endColumnIndex": 6
        },
        "rule": {
          "condition": {
            "type": "NUMBER_GREATER",
            "values": [
              {
                "userEnteredValue": "5"
              }
            ]
          },
          "inputMessage": "Value must be > 5",
          "strict": true
        }
      }
    }
  ]
}';

// $client is a confirmed, working authorized Google_Client object.
$serviceSheets      = new \Google_Service_Sheets($client);
// NOTE This is not the same object as in \App\GoogleSheets
$batchUpdateRequest = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest;

$batchUpdateRequest->setRequests($requests);

$serviceSheets->spreadsheets->batchUpdate(
    // $id is correct.
    $id,
    $batchUpdateRequest
);

…and I get the following error:

{
  "error": {
    "code": 400,
    "message": "Invalid value at 'requests' (type.googleapis.com/google.apps.sheets.v4.BatchUpdateSpreadsheetRequest.Request), \"{
 \"requests\": [
 {
 \"setDataValidation\": {
 \"range\": {
 \"sheetId\": \"1100288254\",
 \"startRowIndex\": 0,
 \"endRowIndex\": 10,
 \"startColumnIndex\": 0,
 \"endColumnIndex\": 6
 },
 \"rule\": {
 \"condition\": {
 \"type\": \"NUMBER_GREATER\",
 \"values\": [
 {
 \"userEnteredValue\": \"5\"
 }
 ]
 },
 \"inputMessage\": \"Value must be > 5\",
 \"strict\": true
 }
 }
 }
 ]
}\"",
    "errors": [
      {
        "message": "Invalid value at 'requests' (type.googleapis.com/google.apps.sheets.v4.BatchUpdateSpreadsheetRequest.Request), \"{
 \"requests\": [
 {
 \"setDataValidation\": {
 \"range\": {
 \"sheetId\": \"1100288254\",
 \"startRowIndex\": 0,
 \"endRowIndex\": 10,
 \"startColumnIndex\": 0,
 \"endColumnIndex\": 6
 },
 \"rule\": {
 \"condition\": {
 \"type\": \"NUMBER_GREATER\",
 \"values\": [
 {
 \"userEnteredValue\": \"5\"
 }
 ]
 },
 \"inputMessage\": \"Value must be > 5\",
 \"strict\": true
 }
 }
 }
 ]
}\"",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

I tried passing $requests as an array, but it says it needs JSON, so that's not the issue.

I've also tried passing the value of the requests key (array of objects) and the single setDataValidationrequest (object).

The request itself is copied from Google's docs.

That's invalid JSON:

     "sheetId": mySheetIdConfirmedCorrect,
                              ^---- unquoted string

Don't build your own JSON. Build a plain old regular PHP array structure, then json_encode() it.

JSON can also not contain "code", so if that mySheet... is actually a variable, that's again invalid JSON. You cannot pass anything other than data - expressions, code, etc... are all not valid in JSON.