【问题标题】:How to set validation method in Google spreadsheets API如何在 Google 电子表格 API 中设置验证方法
【发布时间】:2017-11-02 16:17:05
【问题描述】:

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

【问题讨论】:

    标签: php google-api google-spreadsheet-api google-api-php-client


    【解决方案1】:

    感谢@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);
    

    【讨论】:

    • 你能解释一下每个类的作用吗?
    • * 新的 Google_Service_Sheets_BooleanCondition(); - 从值中设置电子表格单元格的验证,这些值被收集到上面的数组中 * new Google_Service_Sheets_DataValidationRule(); - 为用户单击表格中的数组之前可见的单元格设置“提示” * new Google_Service_Sheets_GridRange(); - 设置新数据验证的范围(列、行) * new Google_Service_Sheets_SetDataValidationRequest(); - 一起收集验证和范围 * new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(); - 在一个请求中上传多个更改到 Google API 的类
    • 请在 Google API SDK 中找到更多关于类的信息和包含的示例developers.google.com/sheets/api/quickstart/…
    • 你知道设置后如何清除数据验证吗?
    【解决方案2】:

    DataValidationRule 对象如下所示:

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

    您想使用rule.condition.typeONE_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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 2023-03-29
      相关资源
      最近更新 更多