Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 485
Volomike Avatar asked Feb 19 '17 09:02

Volomike


1 Answers

Take a look this

SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = 'credentials.json'
SPREADSHEET_ID = spreadsheetId

creds = None
creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)

service = discovery.build(
    'sheets', 'v4', credentials=creds, cache_discovery=False)

batch_update_values_request_body = {
        'requests': [
            {
                'addSheet': {
                    'properties': {
                        'title': worksheetName
                    }
                }
            }
        ]
    }
    request = service.spreadsheets().batchUpdate(
        spreadsheetId=SPREADSHEET_ID, body=batch_update_values_request_body)
    response = request.execute()
like image 189
Vanderson Andrade Avatar answered Oct 11 '22 18:10

Vanderson Andrade