'download pdf from google sheet and

I found this code where I can download google sheet to pdf but I want to change it in order to chose another sheet then the active one and download the pdf once the code run without the box.

function PDF() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetId = ss.getActiveSheet().getSheetId();
  var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + ss.getId() + "/export?exportFormat=pdf&gid=" + sheetId + "&access_token=" + ScriptApp.getOAuthToken();
  var str = '<input type="button" value="Download" onClick="location.href=\'' + url + '\'" >';
  var html = HtmlService.createHtmlOutput(str);
  SpreadsheetApp.getUi().showModalDialog(html, "Download_Report");
}


Solution 1:[1]

From What I meant is I want to run this code on a sheet other then the one I am currently using, for example, if you want to use sheetId of other sheet except for the active sheet, how about the following modification?

From:

var sheetId = ss.getActiveSheet().getSheetId();

To:

When you want to use the sheet name, please modify as follows.

var sheetId = ss.getSheetByName("Sheet2").getSheetId(); // Please set the sheet name like "Sheet2".

When you want to use the index of the sheet, please modify as follows. In this case, index = 1 is the 2nd sheet. So, the 1st sheet index is 0.

var index = 1; // Please set the index of the sheet you want to use.
var sheetId = ss.getSheets()[index].getSheetId();
  • Of cource, you can directly put the specific sheet ID you want to use.

References:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tanaike