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

时间:2017-07-05 08:45:09

标签: php google-sheets google-api 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);

我收到此错误:

  

请求包含无效的身份验证凭据错误

1 个答案:

答案 0 :(得分:4)

$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);