Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When exporting Google sheets to PDF how do I access the parameters

What I want to do is export a Google sheet as a PDF without it displaying the gridlines. I see code samples online of how to access that parameter when using a url like this:

  var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                        "?format=pdf&"+
                                                        "size=0&"+
                                                        "fzr=true&"+
                                                        "portrait=false&"+
                                                        "fitw=true&"+
                                                        "gridlines=false&"+
                                                        "printtitle=true&"+
                                                        "sheetnames=true&"+
                                                        "pagenum=CENTER&"+
                                                        "attachment=true";

But I am using the following code which does not utilitize a url:

       private static void DownloadfileFromGDrive(DriveService service, string fileId, string filePath)
       {
           var request = service.Files.Export(fileId, "application/pdf");

           using (var memoryStream = new MemoryStream())
           {
               request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) =>
               {
                   switch (progress.Status)
                   {
                       case DownloadStatus.Downloading:
                           Debug.WriteLine(progress.BytesDownloaded);
                           break;
                       case DownloadStatus.Completed:
                           Debug.WriteLine("Download Complete");
                           break;
                       case DownloadStatus.Failed:
                           Debug.WriteLine("Download Failed");
                           break;
                   }
               };

               request.Download(memoryStream);

               using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
               {
                   fileStream.Write(memoryStream.GetBuffer(), 0, memoryStream.GetBuffer().Length);
               };
           }
       }

How can I access those parameters from my code?

like image 464
Xiolin Avatar asked Jan 28 '26 17:01

Xiolin


1 Answers

How about this answer?

When the query parameter for exporting as the PDF format is used, unfortunately, service.Files.Export(fileId, "application/pdf") cannot be used for this situation. In this case, it is required to download the PDF file using the access token. The endpoint for this situation is as follows.

Endpoint:

var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                      "?format=pdf&"+
                                                      "size=0&"+
                                                      "fzr=true&"+
                                                      "portrait=false&"+
                                                      "fitw=true&"+
                                                      "gridlines=false&"+
                                                      "printtitle=true&"+
                                                      "sheetnames=true&"+
                                                      "pagenum=CENTER&"+
                                                      "attachment=true&"+
                                                      "access_token=###"; // Added
  • When you request this endpoint as GET method, the PDF data, which reflected the query parameter, can be retrieved.
  • Of course, you can also add the access token to the request header instead of the query parameter.

Reference:

  • Standard Query Parameters

If this was not the direction you want, I apologize.

like image 53
Tanaike Avatar answered Jan 30 '26 08:01

Tanaike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!