Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

524
Vistas
How do I grab URL in Apps Script (.gs) and hand it to a .html script?

This is my first question here and I am pretty new to js and html so please excuse if I'm missing something obvious.

Goal: I want to create a script, that creates a Folder in your Google Drive but only if it's not already existing. In that folder it should create a .txt that contains certain information. After that, I want a new Tab to automatically open the URL of the newly created txt-file.

If I insert a normal URL in my .html (like "https://www.google.com") it all works perfectly fine. However I'm stuck at the part where the Apps Script hands over the grabbed Url of the newly created file to my html.

Any help is appreciated!

Google Apps Script Code (Code.gs):

function myFunction() {

  var FData = "Very important Data"       
  
  var destFolder = DriveApp.getFoldersByName("Folder");     //create Drive folder if not already created
  var destFolder = destFolder.hasNext() ? 
    destFolder.next() : DriveApp.createFolder("Folder");

  var fileName = "Status.txt";                              //create txt file in that folder and add data to it
  var newFile = destFolder.createFile(fileName,FData);
  var url = newFile.getUrl();                               //?GIVE THIS URL TO openUrl.html?

  var htmlOutput = HtmlService.createHtmlOutputFromFile('openUrl').setHeight(100);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Opening...');
}

HTML (openUrl.html):

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
     var urlToOpen = url;                     //insert grabbed Url here
     var winRef = window.open(urlToOpen);
     google.script.host.close();
    </script>
  </head>
  <body>
    
  </body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In your script, how about the following modification?

Google Apps Script side:

function myFunction() {
  var FData = "Very important Data"
  var destFolder = DriveApp.getFoldersByName("Folder");
  var destFolder = destFolder.hasNext() ? destFolder.next() : DriveApp.createFolder("Folder");
  var fileName = "Status.txt";
  var newFile = destFolder.createFile(fileName, FData);
  var htmlOutput = HtmlService.createTemplateFromFile('openUrl');
  htmlOutput.url = newFile.getUrl();
  SpreadsheetApp.getUi().showModalDialog(htmlOutput.evaluate().setHeight(100), 'Opening...');
}

HTML & Javascript side:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
     var urlToOpen = "<?!= url ?>";
     var winRef = window.open(urlToOpen);
     google.script.host.close();
    </script>
  </head>
  <body>
  </body>
</html>
  • When myFunction() is run, a dialog is opened by HTML including url using HTML template.

Reference:

  • HTML Service: Templated HTML
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda