Hardik Kalathiya
Hardik Kalathiya

Reputation: 2253

Add (Create) new sheet in spreadsheet using Google Sheet API

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

Upvotes: 3

Views: 4475

Answers (1)

Mthobisi Mlombo
Mthobisi Mlombo

Reputation: 116

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

Upvotes: 9

Related Questions