【问题标题】:How to remove DataValidationRule via Google Sheets API如何通过 Google Sheets API 删除 DataValidationRule
【发布时间】:2019-07-15 22:59:27
【问题描述】:

目前我正在通过他们的 PHP 库使用 Google Sheets API 来构建动态电子表格。我在电子表格上设置了验证规则,专门用于创建要选择的状态下拉列表。

我已经更新了电子表格,将状态下拉列表放在不同的列中。但是,执行此操作后,为上一列设置的 DataValidationRule 似乎仍然存在。

在重新应用我想要的任何验证之前,我尝试创建一种方法来删除工作表中的所有验证,但它似乎不起作用。

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#conditiontype

在设置条件类型时,我想将验证恢复为CONDITION_TYPE_UNSPECIFIED,但如果我这样做,API 只会返回一个错误。我尝试使用其他人,例如ONE_OF_LIST,但随后每个单元格都错误提示:

Invalid: Input must be an item on specified list

这是有道理的,考虑到没有生成列表(我也不想要一个)。

其余列可以是数字/日期/文本的任意组合,因此我想在再次应用验证之前简单地删除所有验证。

这是我当前的 clearValidation 代码:

public function clearSpreadsheetValidations($spreadsheetId) {
    $client = $this->getClient();
    $service = new Google_Service_Sheets($client);

    $conditions = new Google_Service_Sheets_BooleanCondition();
    $conditions->setType('CONDITION_TYPE_UNSPECIFIED');
    $conditions->setValues(null);

    $setRule= new Google_Service_Sheets_DataValidationRule();
    $setRule->setCondition($conditions);
    $setRule->setInputMessage(null);
    $setRule->setShowCustomUi(false);

    $valReq = new Google_Service_Sheets_SetDataValidationRequest();
    $valReq->setRule($setRule);

    $sheetReq = new Google_Service_Sheets_Request();
    $sheetReq->setSetDataValidation($valReq);

    $requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
    $requestBody->setRequests($sheetReq);

    $service->spreadsheets->batchUpdate($spreadsheetId, $requestBody);
}

如何调用工作表 API 以删除电子表格中所有以前设置的 DataValidationRules?

谢谢!

【问题讨论】:

    标签: php google-sheets google-sheets-api


    【解决方案1】:

    对于 java,此代码适用于我的情况。

    public BatchUpdateSpreadsheetResponse removeAllDataValidation(String spreadsheetId, Sheets service)
            throws IOException {
        // [START sheets_conditional_formatting]
        List<Request> requests = Arrays.asList(new Request().setSetDataValidation(
                new SetDataValidationRequest()));
    
        BatchUpdateSpreadsheetRequest body = new BatchUpdateSpreadsheetRequest().setRequests(requests);
        BatchUpdateSpreadsheetResponse result = service.spreadsheets().batchUpdate(spreadsheetId, body).execute();
        System.out.printf("%d cells updated.", result.getReplies().size());
        // [END sheets_conditional_formatting]
        return result;
    }
    

    【讨论】:

      【解决方案2】:

      好的,就像这里所说的那样

      https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request

      SetDataValidationRequest

      为范围内的每个单元格设置数据验证规则。清除 范围内的验证,在没有指定规则的情况下调用它。

      所以我所要做的就是不声明范围或设置规则,而是在现有电子表格上运行此方法以清除所有现有验证

      public function clearSpreadsheetValidations($spreadsheetId) {
          $client = $this->getSheetsClient();
          $service = new Google_Service_Sheets($client);
          $valReq = new Google_Service_Sheets_SetDataValidationRequest();
          $sheetReq = new Google_Service_Sheets_Request();
          $sheetReq->setSetDataValidation($valReq);
      
          $requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
          $requestBody->setRequests($sheetReq);
      
          $service->spreadsheets->batchUpdate($spreadsheetId, $requestBody);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多