PHP Google Sheets API,在尝试批量更新时由于致命错误而无法将数据写入工作表

问题描述 投票:0回答:0

代码是关于设置 Google Sheets API 并发出批量更新请求。我有

$data
变量和初始化它并具有正确数据的部分。

require_once __DIR__ . '/vendor/autoload.php';

use Google\Client;
use Google\Service\Drive;
use Google\Service\Sheets;

// Create a new Google client object
$client = new Google\Client();
$client->setAuthConfig('E:\work\Aweri Foods\credentials.json');
// Set the scope of the API request to include the Google Sheets API
// $client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->addScope(Google\Service\Drive::DRIVE);
// Create a new Google Sheets service object
$service = new Google_Service_Sheets($client);


// Set the ID of the spreadsheet that you want to write to
$spreadsheetId = '1FIwGZS-VJQcUBA5zTBI41J8DR3sGsOZJL5k9tJFWBmU';

// Set the name of the sheet that you want to write to
$sheetName = 'Sheet1';
if (count((array)$data) > 0) {
        echo "HELLLLLLLLLLLLLO!";
        // Clear the existing data in the sheet
        $clearRequest = new Google_Service_Sheets_ClearValuesRequest();
        $service->spreadsheets_values->clear($spreadsheetId, $sheetName, $clearRequest);
        $range = $sheetName;        
        $requestBody[] = new Google_Service_Sheets_ValueRange([
            'range' => $range,
            'values' => $data
        ]);

        // Write the data to the Google Sheet
        $response = $service->spreadsheets_values->batchUpdate(
            $spreadsheetId,
            $range,
            $requestBody,
            ['valueInputOption' => 'USER_ENTERED']
        );
        // Print the number of cells updated
        echo "Cells updated: ". $response->getUpdatedCells();
        
    }

错误:

PHP 致命错误: 未捕获的类型错误:count():参数 #1 ($value) 必须是 Countable|array 类型,在 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\Handler\CurlFactory 中给定为 null .php:67

堆栈跟踪: #0 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(67): 计数(NULL)

#1 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\Handler\CurlFactory.php(107): GuzzleHttp\Handler\CurlFactory->release(对象(GuzzleHttp\Handler\EasyHandle))

#2 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\Handler\CurlHandler.php(43): GuzzleHttp\Handler\CurlFactory::finish(对象(GuzzleHttp\Handler\CurlHandler), 对象(GuzzleHttp\Handler \EasyHandle), 对象(GuzzleHttp\Handler\CurlFactory))

#3 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\Handler\Proxy.php(28): GuzzleHttp\Handler\CurlHandler->__invoke(对象(GuzzleHttp\Psr7\Request), 数组)

#4 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\Handler\Proxy.php(51): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler{closure}(对象(GuzzleHttp\Psr7\Request),阵列)

#5 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\PrepareBodyMiddleware.php(72): GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler{closure}(对象(GuzzleHttp\Psr7\Request), 数组)

#6 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\Middleware.php(30): GuzzleHttp\PrepareBodyMiddleware->__invoke(对象(GuzzleHttp\Psr7\Request), 数组)

#7 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\RedirectMiddleware.php(68): GuzzleHttp\Middleware::GuzzleHttp{closure}(对象(GuzzleHttp\Psr7\Request), 数组)

#8 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\Middleware.php(59): GuzzleHttp\RedirectMiddleware->__invoke(对象(GuzzleHttp\Psr7\Request), 数组)

#9 E:\work\Aweri Foods endor\guzzlehttp\guzzle\src\HandlerStack.php(67): GuzzleHttp\Middleware::GuzzleHttp{closure}(Object(GuzzleHttp\Psr7\Request), Array) 抛入 E: \work\Aweri Foods endor\guzzlehttp\guzzle\src\Handler\CurlFactory.php 第 67 行

我无法在表格中写入数据!

请帮我解决这个问题:(

php google-sheets guzzle google-api-php-client
© www.soinside.com 2019 - 2024. All rights reserved.