How To Use Google Sheets API v4 To Create New Sheet or Tab in Spreadsheet with PHP

Volomike

With PHP, it is unclear from the Google Sheets API v4 documentation on how to create a new sheet (aka "tab") in an existing spreadsheet.

I can do it with batchUpdate, oddly, via the API Explorer, but they don't explain from that how to do this in PHP.

Volomike

It looks like the documentation is saying that we must use batchUpdate from $service->spreadsheets_values collection. But that's incorrect. It should be the $service->spreadsheets collection. The proper answer after a lot of trial and error is:

try {
    $body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
        'requests' => array(
            'addSheet' => array(
                'properties' => array(
                    'title' => 'New Sheet'
                )
            )
        )
    ));
    $result1 = $service->spreadsheets->batchUpdate($sSpreadsheetID,$body);
} catch(Exception $ignore) {}

Given that you have already authenticated your API to get a $client object, and then used that to get a $service object of class Google_Service_Sheets, and given that you have assigned $sSpreadsheetID to the ID of your spreadsheet, then the above routine will attempt to add a new tab to your spreadsheet named 'New Sheet' without destroying any existing one, and will not show errors if this tab already exists. At that point, you can do something new with that new sheet by addressing it with the A1 Notation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to call a specific sheet within a spreadsheet via the Google Sheets API v4 in Python

Create spreadsheet with Google Sheets API V4 API Key

How can I get single sheet from a spreadsheet collection from Google Sheets with Google Sheets API v4?

Google sheets API v4 Create sheet and invite users

How to update google spreadsheet via sheets API v4

Get list of sheets and latest sheet in google spreadsheet api v4 in Python

Unable to create a new spreadsheet with the Google Sheets API

Create a new sheet within an existing spreadsheet google sheets

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

How to add a new Sheet into existing SpreadSheet? Google Sheets API with Node.js

How to upload a CSV file to a new tab(sheet) in the google spreadsheet

How to delete rows in google spreadsheet using SHEET API v4 by Python?

Reading whole Google Spreadsheet with Sheets API v4 Java

How to remove a blank row in google sheet using google sheets api v4 javascript?

Sheets API v4 - JS Client: How to add/append a new row to an existing Sheet

How to use sheet ID in Google Sheets API?

How to insert the current date time into a google sheet, respecting the sheet's timezone, using sheets api v4?

Google spreadsheet API v4 with PHP - how to insert empty row to beginning

How to specify the gid (tabs) in Google Spreadsheet API v4?

How to authenticate in Google Spreadsheet Api V4 with Server Key

How to use Google Java Sheet API V4 to format cells as NUMBER and DATE

How to merge cells using the PHP version of the Google Sheet API v4

Google Sheets API: Create or move Spreadsheet into Folder

How can I use the Google Sheets (v4) API getByDataFilter to return a specific row of data?

PDI "Google Spreadsheet Input/Output" plugin compatible with Google Sheets API v4?

How to delete row from Google Sheets using api v4 in PHP

GoogleSheets API v4 Python : Unable to re-order sheets in google spreadsheet

How to append rows with google sheet api v4 in nodejs

How to format a cell with Google Sheets API v4 and Go?