0

I made working PHP script to download PDF file from Google Sheet using Google API. However I would like to download only specific sheet (specific gid). Is it possible? I can't find anything about additional parameters in the export. I tried "array('gid' => '123456')" but it's not working (still download whole spreadsheet). Does anyone know how to do this?

$client = new \Google\Client();
$client->setAuthConfig('credentials.json');
$client->addScope(\Google_Service_Drive::DRIVE);

$driveService = new \Google_Service_Drive($client);
$spreadsheetId = '123456789123456789';
     
$response = $driveService->files->export(
  $spreadsheetId,
  'application/pdf',
  array('alt' => 'media')
);
$content = $response->getBody()->getContents();
file_put_contents('file.pdf',$content);
Stickman
  • 31
  • 7
  • not working how, exactly? Is there an error? Unexpected result? You'll need to be more specific before anyone can reasonably help you. See also [ask]. Thanks. – ADyson Jun 08 '23 at 17:03
  • Not working I mean it still download whole spreadsheet (I reedited the post). – Stickman Jun 08 '23 at 19:27
  • Drive can just work with whole files I think. I expect you'd need to work with the Sheets API perhaps, to achieve that – ADyson Jun 08 '23 at 20:34
  • P.S. You said `I tried "array('gid' => '123456')"`, but it's not clear how/where you did that. That's what you need to be showing in your edited code. – ADyson Jun 08 '23 at 20:36
  • P.P.S. I'd imagine one of the answers in https://stackoverflow.com/questions/11619805/using-the-google-drive-api-to-download-a-spreadsheet-in-csv-format would probably help you. The code is in python but it shows you how to use the API. – ADyson Jun 08 '23 at 20:41
  • @ADyson There is also comment "'gid' query parameter doesn't work - it redirects to 404 page" but thanks – Stickman Jun 09 '23 at 08:19
  • The author updated the code after that comment though, if you notice. It's worth a try, I would have thought. There are also potentially useful links in some of the other answers. – ADyson Jun 09 '23 at 08:24

2 Answers2

0

Google drive does not have access to file data. Export will only export the full file there is no way to set it to only export a single tab / or sheet with in a google sheet.

A work around would be to use the Google Sheets api to split out the single tab/ sheet you are looking for into a single file itself. Then use google drive to convert that into a pdf.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

I finally created new spreadsheet and used IMPORTRANGE() from base spreadsheet with only needed data, so now I can use it to download specific sheet.

Stickman
  • 31
  • 7