使用Google Sheet API在电子表格中添加(创建)新工作表

Mr.*_* HK 2 php google-api google-sheets google-sheets-api

我尝试了下面的代码在电子表格中创建新工作表,但无法正常工作。

    $service = new Google_Service_Sheets($client);

    //pr($service);
    $spreadsheetId = ‘You spredsheet id’;
    $json_request_string = ‘{“requests”:[{“addSheet”: {“properties”: {“title”: “Project tasks”}}}]}’;
    //$requests = json_decode($json_request_string);

    // TODO: Assign values to desired properties of `requestBody`:
    $requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
    $requestBody->setRequests($json_request_string);

    $response = $service->spreadsheets->batchUpdate($spreadsheetId, $requestBody);
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

请求的验证凭证无效

小智 5

$title = "my new sheet";

//Create New Sheet

$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
   'requests' => array('addSheet' => array('properties' => array('title' => $title )))));

$result = $service->spreadsheets->batchUpdate(SHEET_ID,$body);
Run Code Online (Sandbox Code Playgroud)