skip to Main Content

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 GoogleClient();
$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);

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search