user6016913
user6016913

Reputation: 191

How to get list of sheets from google spread sheet in php

I am using php-google-spreadsheet-client library. I don't know how to get list of sheets available in google sheets. Thanks in advance.

Upvotes: 0

Views: 4305

Answers (2)

Nick Weavers
Nick Weavers

Reputation: 564

<?php

function sheets($spreadsheetID) {
    $sheets = array();    
    // Load Google API library and set up client
    // You need to know $spreadsheetID (can be seen in the URL)
    $sheetService = new Google_Service_Sheets($client);   
    $spreadSheet = $sheetService->spreadsheets->get($spreadsheetID);
    $sheets = $spreadSheet->getSheets();
    foreach($sheets as $sheet) {
        $sheets[] = $sheet->properties->sheetId;
    }   
    return $sheets;
}
?>

Upvotes: 5

abielita
abielita

Reputation: 13469

You can get a list of sheets by using the get method on spreadsheets:

sheet_metadata = service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()
sheets = sheet_metadata.get('sheets', '')
title = sheets[0].get("properties", {}).get("title", "Sheet1")
sheet_id = sheets[0].get("properties", {}).get("sheetId", 0)

You can also check the PHP Quickstart for a PHP command-line application that makes requests to the Google Sheets API.

References:

Upvotes: 2

Related Questions