Andrew Roberts
Andrew Roberts

Reputation: 2798

Using "export" query string in URL to create PDF from GSheet

This worked until recently, but now it only works one time after I've made a code or GSheet change, but subsequent calls give me a server 500 error.

I've tried all combinations of the URL, down to absolute minimum and running the generated URL in the browser address bar when I get the same error.

The code is based on

https://gist.github.com/Spencer-Easton/78f9867a691e549c9c70

var url = SpreadsheetApp.getActive().getUrl().replace(/\/edit.*$/, '')

url += '/export?exportFormat=pdf&format=pdf'   
+ '&gid=' + sheetId
+ '&size=letter'      // paper size
+ '&portrait=true'    // orientation, false for landscape
+ '&fitw=true'        // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false&pagenumbers=false'  //hide optional headers and footers
+ '&gridlines=false'  // hide gridlines

var options = {
  muteHttpExceptions: false,
  headers: {
    'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
  }
}

// Throws 500 error on second call ...    
var response = UrlFetchApp.fetch(url, options)

if (response.getResponseCode() !== 200) {
  throw new Error(response.getContentText())
}

Upvotes: 0

Views: 176

Answers (1)

Andrew Roberts
Andrew Roberts

Reputation: 2798

The problem was that I was hiding the GSheet tab to be exported before the export.

Upvotes: 1

Related Questions