使用Google PHP SpreadSheets客户端隐藏表格

时间:2018-03-19 22:37:53

标签: google-sheets google-spreadsheet-api google-api-php-client batch-updates

我使用以下代码隐藏电子表格的某些表格。我用的是php客户端库。我的目标是只将所需的工作表导出为PDF文件。由于没有方法可以实现这一点,我试图隐藏不需要的工作表,然后使用我需要通过电子邮件发送的工作表导出spreadhseet。但所有那些请求都失败了。谁知道原因?谢谢!

function hideSheets($spreadsheetId, $sheetIds)    {

    // Get the sheet service.
    global $sheet_service;

    $requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();

    $requests = array();
    foreach ($sheetIds as $key => $sheetId)  {

        $request= new Google_Service_Sheets_UpdateSheetPropertiesRequest();
        $request_prop = new Google_Service_Sheets_SheetProperties();
        $request_prop->setSheetId($sheetId);
        $request_prop->setHidden(true);
        $request->setProperties($request_prop);
        $request->setFields("hidden");
        $requests[] = $request;
    }

    // print_r($requests[0]);

    $requestBody->setRequests($requests);

    $requestBody->setIncludeSpreadsheetInResponse(false);

    try { $response = $sheet_service->spreadsheets->batchUpdate($spreadsheetId, $requestBody);
    } catch (Exception $e) { print "An error occurred: " . $e->getMessage(); die(); }

    echo '<br><br>';
    print_r($response);
    return;
}

此代码抛出以下错误:

An error occurred: 
{ "error": 
    { "code": 400, "message": "Invalid JSON payload received. Unknown name \"fields\" at 'requests[0]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[0]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[1]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[1]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[2]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[2]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[3]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[3]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[4]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[4]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[5]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[5]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[6]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[6]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[7]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[7]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[8]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[8]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[9]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[9]': Cannot find field.", 

"errors": [ { "message": "Invalid JSON payload received. Unknown name \"fields\" at 'requests[0]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[0]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[1]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[1]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[2]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[2]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[3]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[3]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[4]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[4]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[5]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[5]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[6]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[6]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[7]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[7]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[8]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[8]': Cannot find field
    Invalid JSON payload received. Unknown name \"fields\" at 'requests[9]': Cannot find field
    Invalid JSON payload received. Unknown name \"properties\" at 'requests[9]': Cannot find field.", "domain": "global", "reason": "badRequest" } ],
"status": "INVALID_ARGUMENT" } } 

以下请求正文正常运行:

{  "requests": [
    {   "updateSheetProperties": {
            "properties": {
                 "sheetId": 848799001,
                 "hidden": true
            },
            "fields": "hidden"}}]}

在google上试试这个API应用:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate

但我没有实现将其翻译为谷歌php客户端...... :(

0 个答案:

没有答案