Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Google电子表格API获取按筛选视图筛选的值_Php_Google Sheets Api_Google Api Php Client - Fatal编程技术网

Php 使用Google电子表格API获取按筛选视图筛选的值

Php 使用Google电子表格API获取按筛选视图筛选的值,php,google-sheets-api,google-api-php-client,Php,Google Sheets Api,Google Api Php Client,我正在尝试使用GoogleSheetsAPI(在PHP中使用Google/apiclient和OAuth2.0)从电子表格中获取数据 此电子表格定义了一些“过滤器视图”(在数据>过滤器视图中定义),例如,其中一个过滤器仅显示价格列大于x的行 我试图找到一种方法,使用现有的一个过滤器视图获取已过滤的数据,但找不到它。有什么办法吗?尽管我不确定您当前的脚本是否符合您的问题,但当您想使用“过滤器”视图从工作表中检索过滤后的值时,不幸的是,在当前阶段,没有直接实现它的方法。因此,在这种情况下,需要使用变

我正在尝试使用GoogleSheetsAPI(在PHP中使用Google/apiclient和OAuth2.0)从电子表格中获取数据

此电子表格定义了一些“过滤器视图”(在数据>过滤器视图中定义),例如,其中一个过滤器仅显示价格列大于x的行


我试图找到一种方法,使用现有的一个过滤器视图获取已过滤的数据,但找不到它。有什么办法吗?

尽管我不确定您当前的脚本是否符合您的问题,但当您想使用“过滤器”视图从工作表中检索过滤后的值时,不幸的是,在当前阶段,没有直接实现它的方法。因此,在这种情况下,需要使用变通方法。解决方案的流程如下所示

此解决方案的流程:
  • 检索要使用的筛选器视图(
    filterViews
    )的设置。
    • 在这种情况下,可以使用“spreadsheets.get”方法
  • 使用“过滤器”视图的检索设置为要使用的图纸创建新的基本过滤器。
    • 在这种情况下,可以使用“spreadsheets.batchUpdate”方法
  • 检索工作表的
    行元数据的值。
    
    • 在这种情况下,可以使用“spreadsheets.get”方法
    • rowmatadata
      的值处,筛选的行具有
      “hiddenByFilter”的属性:true,
      。使用此选项,可以检索隐藏行和/或显示行
  • 删除创建的基本筛选器
  • 通过上述流程,可以检索过滤后的值

    示例脚本: 当您已经能够使用Sheets API和googleapis for PHP获取并输入Google电子表格的值时,作为上述解决方案的示例脚本,您可以使用以下脚本

    $service = new Google_Service_Sheets($client); // Please use $client from your script.
    
    $spreadSheetId = '###'; // Please set the Spreadsheet ID.
    $sheetName = 'Sheet1'; // Please set the sheet name.
    $filterViewName = 'sampleFilter1'; // Please set the filter view name.
    
    // 1. Retrieve the settings of the filter view (`filterViews`) you want to use.
    $sheets = $service->spreadsheets->get($spreadSheetId, ["ranges" => [$sheetName], "fields" => "sheets"])->getSheets();
    $sheetId = $sheets[0]->getProperties()->getSheetId();
    $filterViews = $sheets[0]->getFilterViews();
    $filterView = array();
    foreach ($filterViews as $i => $f) {
        if ($f->getTitle() == $filterViewName) {
            array_push($filterView, $f);
        };
    };
    if (count($filterView) == 0) return;
    
    // 2. Create new basic filter to the sheet you want to use using the retrieved settings of the filter view.
    $obj = $filterView[0];
    $obj['range']['sheetId'] = $sheetId;
    $requests = [
        new Google_Service_Sheets_Request(['clearBasicFilter' => ['sheetId' => $sheetId]]),
        new Google_Service_Sheets_Request([
            'setBasicFilter' => [
                'filter' => [
                    'criteria' => $obj['criteria'],
                    'filterSpecs' => $obj['filterSpecs'],
                    'range' => $obj['range'],
                    'sortSpecs' => $obj['sortSpecs'],
                ]
            ]
        ])
    ];
    $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(['requests' => $requests]);
    $service->spreadsheets->batchUpdate($spreadSheetId, $batchUpdateRequest);
    
    // 3. Retrieve the values of `rowMetadata` of the sheet.
    $sheets = $service->spreadsheets->get($spreadSheetId, ["ranges" => [$sheetName], "fields" => "sheets"])->getSheets();
    $rowMetadata = $sheets[0]->getData()[0]->getRowMetadata();
    $filteredRows = array(
        'hiddenRows' => array(),
        'showingRows' => array()
    );
    foreach ($rowMetadata as $i => $r) {
        if (isset($r['hiddenByFilter']) && $r['hiddenByFilter'] === true) {
            array_push($filteredRows['hiddenRows'], $i + 1);
        } else {
            array_push($filteredRows['showingRows'], $i + 1);
        };
    };
    
    // 4. Delete the created basic filter.
    $requests = [new Google_Service_Sheets_Request(['clearBasicFilter' => ['sheetId' => $sheetId]])];
    $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(['requests' => $requests]);
    $service->spreadsheets->batchUpdate($spreadSheetId, $batchUpdateRequest);
    
    print($filteredRows);
    
    结果: 当上述脚本用于以下示例电子表格时

    未设置前过滤器视图。

    设置过滤器视图后。

    结果值 根据上述电子表格,得出以下结果

    {
      "hiddenRows": [2, 3, 5, 6, 8, 9],
      "showingRows": [1, 4, 7, 10, 11, 12, 13, 14, 15]
    }
    
    • hiddenRows
      是隐藏的行号
    • showingRows
      是showingRows行数
    注:
    • 重要提示:在本示例脚本中,当在工作表中使用基本筛选器时,基本筛选器将被清除。请小心这个。测试此脚本时,请使用示例电子表格
    • 在此示例脚本中,将检索隐藏行和显示行。使用这些值,可以检索过滤后的值
    参考资料:
    • 相关线程。
        • 此线程用于Google应用程序脚本。不幸的是,我找不到用于此的PHP脚本。因此,我添加了示例脚本

    这个答案看起来非常完美,我毫不怀疑它在我的情况下会起作用。我想我理解这种变通方法背后的逻辑。很遗憾,没有简单的API方法来解决这个问题,但我相信您的答案将是最好的解决方案。谢谢大家!@Marcin感谢您的回复和测试。不幸的是,在当前阶段,似乎没有从过滤器视图直接检索过滤值的方法。所以我提出了一个解决办法。也谢谢你。