0

I'm using this script to download a pdf from active sheet

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, "scarica il documento in pdf");
}

i'd like to close download dialog once you've clicked "download" and also hide sheet once download dialog is closed

how can i get this?

Rubén
  • 34,714
  • 9
  • 70
  • 166

1 Answers1

0

To automatically close a dialog created with the HtmlService from Google Apps Script use google.script.host.close() and to hide a sheet you need to use google.script.run to call a server side function that hide the sheet.

In order to be able to use both, you modify the value of the str variable or create the HtmlOutput in a different way.

Please read the official guide regaring dialogs in Google apps: Serve HTML as a Google Docs, Sheets, Slides, or Forms user interface

Also checkout the following Stack Overflow questions:

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • I'm just learning some basis of scripting.. I read the articles you kindly link to me but.. at the moment my knowledge of scripting tecniques is too poor!Can you please explain me in a more practical way? – Luca Telleschi Oct 30 '22 at 07:17
  • @LucaTelleschi The first thing that you should do is to write the HTML and JavaScript code for the dialog. I suggest you to create a HTML file instead of writing as a string in a .gs file. If you need help with that, please read again the first link in my answer, if after that you still need help, post a new question asking how to create a dialog using a HTML file. – Rubén Oct 30 '22 at 07:37
  • 1
    I think I’ve got the concept of your suggestion.. I’ll check what I can translate in practice! thanknyou! – Luca Telleschi Oct 30 '22 at 09:34