【问题标题】:Add (Create) new sheet in spreadsheet using Google Sheet API使用 Google Sheet API 在电子表格中添加(创建)新工作表
【发布时间】:2017-07-05 08:45:08
【问题描述】:

我已尝试使用以下代码在电子表格中创建新工作表,但它不起作用。

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

我收到此错误:

请求具有无效的身份验证凭据错误

【问题讨论】:

  • 它会给你任何错误吗?
  • 是的,我收到“请求具有无效的身份验证凭据错误。但我认为这是无效请求错误。
  • 我会从你的身份验证开始。查看 $client 并确认所有设置都正确。

标签: php google-sheets google-api google-sheets-api


【解决方案1】:
$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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2013-10-22
    相关资源
    最近更新 更多