0
votes

I have spreadsheet https://docs.google.com/spreadsheets/d/somehash/edit#gid=61824872 which has multiple worksheets (sheets) listed at the bottom. How can I get list of all sheets URLs of this spreadsheet?

1

1 Answers

1
votes

You can get the data of specific sheet by its title using the code below:

$service = new Google_Service_Sheets($client);
$range ="sheet_title";
$spreadsheetId = 'someHash';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (empty($values)) {
    print "No data found.\n";
} else {
    echo "<pre>";
    var_dump($values);
    echo "</pre>";
}

If you want to access the data by gid, then you can first get a list of all the worksheets in your google sheet, compare the id with the id you want, get its title, and then, use the following code in conjunction with the above code.

$range ="";
try{
    $spreadSheet = $service->spreadsheets->get($spreadsheetId);
    $sheets = $spreadSheet->getSheets();
    foreach($sheets as $sheet) {
        if($sheet->properties->sheetId == $gid){
            $range = $sheet->properties->title;
            break;
        }
    }   
}
catch(exception $e){
    var_dump($e);
}