var ss = SpreadsheetApp.getActive();
function hideSheets() {
var sheets = ss.getSheets();
for (var i=0;i<sheets.length;i++) {
if (sheets[i].getName() != "Sheet1") {
sheets[i].hideSheet();
}
}
}
function showSheets() {
var sheets = ss.getSheets();
for (var i=0;i<sheets.length;i++) {
if (sheets[i].getName() != "Sheet1") {
sheets[i].showSheet();
}
}
}
function createPDF() {
hideSheets();
var theBlob = ss.getBlob().getAs('application/pdf').setName("Sheet1.pdf");
var newFile = DriveApp.getRootFolder().createFile(theBlob);
showSheets();
}
The script first hides all the sheets except the one you want to export as a pdf. Then it generates the pdf of the sheet in the root folder of your google drive. Finally it unhides all the previously hidden sheets. You may lose some formatting, and you will need to give permissions to run the scripts. Please note that the sheet "Sheet1" is hard coded in the scripts.
function createPDF() {
var folderId = 'YOUR FOLDER ID HERE';
hideSheets();
var theBlob = ss.getBlob().getAs('application/pdf').setName("Sheet1.pdf");
var newFile = DriveApp.getFolderById(folderId).createFile(theBlob);
showSheets();
}
En realidad me lo brindó chatgpt pero lo que buscaba era que pueda colocar como parámetro la hoja que deseaba y el formato de exportación (elegir opción de pdf o xlsx)
Probé tu método y funciona bien
Pero ahora quiero ver cómo lo adapto con los bloques de app inventor2
function createPdfFromUrl(sheetURL, folderId) {
var pdfUrl = SheetURL + "export?format=pdf&size=0&fzr=true&portrait=false&gid=0";
var folder = DriveApp.getFolderById(folder Id);
var blob = UrlFetchApp.fetch(pdfUrl).getBlob().setName("gsdl1.pdf");
var newFile = folder.createFile(blob);
}
As I indicated earlier, you will need a google apps script web app if you want to call these functions from your app. Then the blocks are fairly straight forward.