3
votes

I have tried below code to create new sheet inside spreadsheet, but it’s not working.

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

I get this error:

Request had invalid authentication credentials error

1
Does it give you any error? - Antony
Yes I got "Request had invalid authentication credentials error. But I think it's invalid request error . - Mr. HK
I'd start with your authentication then. Look at $client and confirm all the settings are correct. - Antony

1 Answers

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