I'm trying to export the current active Google Sheet to a PDF. The below nearly does what I want but I would like a few changes.
Changes.
Save the PDF in the same folder as the main spreadsheet rather than the root drive folder.
Use the currently active sheet rather than a defined sheet.
function generatePdf() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var mainSheetName = 'Barney'; //Change 'Main Sheet' to the name of your main sheet var mainSheet = ss.getSheetByName(mainSheetName); var name = mainSheet.getRange('C2').getValue(); //Gets the pdf name from cell B6 var sheets = ss.getSheets(); //Pulls all sheets as an array for (var i = 0; i < sheets.length; i++) { //Iterates through all sheets if (sheets[i].getSheetName() !== mainSheetName) { //Hides all sheets that are not the main one sheets[i].hideSheet() } } DriveApp.createFile(ss.getBlob().getAs('application/pdf').setName(name)); //Exports the main sheet for (var i = 0; i < sheets.length; i++) { //Brings back all sheets that were hidden sheets[i].showSheet() } }