如何在 Google 电子表格 API 中设置验证方法

How to set validation method in Google spreadsheets API

我对新的 Google Sheets API 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);

DataValidationRule 对象如下所示:

"rule": {
  "condition": {
    "type": "ONE_OF_LIST",
    "values": [
      { userEnteredValue: "Yes"},
      { userEnteredValue: "No"}
    ],
  },
  "inputMessage": "",
  "strict": true,
  "showCustomUi": true,
}

您想使用rule.condition.type ONE_OF_LIST然后在列表中输入您想要的rule.condition.valuesshowCustomUi 将显示下拉菜单

使用表格脚本编辑器中的 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())
}
  • Sheets API 高级服务必须 enabled

感谢@random-parts 的帮助,它让我走上了正轨。如果其他人将尝试解决 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);