Converting Spreadsheet to PDF

Converting Spreadsheet to PDF

This sample script converts from a spreadsheet to a PDF file which has all sheets in the spreadsheet. When you use this, please enable Drive API at Google API console.

var spreadsheetId = "#####";
var folderId = "#####";
var outputFilename = "#####";

DriveApp.getFolderById(folderId)
    .createFile(UrlFetchApp.fetch(
      "https://www.googleapis.com/drive/v3/files/" +
        spreadsheetId +
        "/export?mimeType=application/pdf",
      {
        method: "GET",
        headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
        muteHttpExceptions: true
      })
    .getBlob())
    .setName(outputFilename);