Standalone script - open a google sheet file

Viewed 157

I wrote a standalone script deployed as a web application. The point of the script is that by clicking on a button a Google Sheet file is copied to a specific directory, then I want the new file to open. My code is blocking to open the new file. Parts 1 and 2 in codeTest.gs work perfectly (roughly from the value associated with the clicked button, the script will look in a Google Sheet file for the name to give to the form as well as the location where it must be copied).

In my code you will notice that I am talking about a form, but it is indeed a Google Sheet file (in practice the Google Sheet file is a document to be filled in which is similar to a form).

codeTest.gs

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('page');
}
 
 
function fctOpenForm(idForm){
 
  // fonction qui va faire une copie du formulaire pour ensuite l'ouvrir (on préserve ainsi l'original)
 
  // 1 - on va chercher en fonction de l'id du formulaire le répertoire où il doit être copié
    // 1.1 - on "ouvre" le fichier de configuration
    var ssConfigId="1AegVIBq_pOLxhCGb7qcUr-wmCFIk7KRqQ5sB3HBDU04";
    var ssConfig=SpreadsheetApp.openById(ssConfigId);
    var shtListeForms=ssConfig.getSheetByName("Liste des formulaires");
    // 1.2 - on identifie les lignes et colonnes importantes du tableau
    var derLig=ssConfig.getLastRow();
    var derCol=ssConfig.getLastColumn();
    var ligEntete=fctFindLigne(shtListeForms,"Nom du formulaire",1,derLig,1);
    var colNomForm=fctFindColonne(shtListeForms,"Nom du formulaire",1,derCol,ligEntete);
    var colIdForm=fctFindColonne(shtListeForms,"Id du formulaire",1,derCol,ligEntete);
    var colIdRepDest=fctFindColonne(shtListeForms,"Id du répertoire de destination de la copie",1,derCol,ligEntete);
    // 1.3 - on cherche l'id du répertoire de destination de la copie
    var idRepDest=shtListeForms.getRange(fctFindLigne(shtListeForms,idForm,ligEntete,derLig,colIdForm),colIdRepDest).getValue();
    // 1.4 - on cherche le nom du formulaire
    var nomForm=shtListeForms.getRange(fctFindLigne(shtListeForms,idForm,ligEntete,derLig,colIdForm),colNomForm).getValue();
 
  // 2 - on copie le formulaire
  var folderDest=DriveApp.getFolderById(idRepDest);
  var newForm=DriveApp.getFileById(idForm).makeCopy("*"+nomForm,folderDest);
  var newFormId=newForm.getId();
 
  // 3 - on ouvre le nouveau fichier créé
  var url=newForm.getUrl;
  var htmlString = "<script>window.open('" + url +"');</script>";
  return HtmlService.createHtmlOutput(htmlString);
 
}

page.html

<!DOCTYPE html>
<html>
  <h1>Choix du formulaire</h1>
  <p>Cliquer sur le formulaire souhaité</p>
 
 
 
  <button value="1uZwENM_Ilmo7hXLHNYaQP6GdJbuq8QlXkoiY0sIX8KQ" onclick="fctGetValue(this)">Prise de rendez-vous</button>
  <button value="rembEnLigne" onclick="fctGetValue(this)">Remboursement - paiement initial en ligne</button>
  <button value="rembComptoir" onclick="fctGetValue(this)">Remboursement - paiement initial au comptoir</button>
 
 
  <script>
    function fctGetValue(element){
      var idForm=element.value;
      google.script.run.fctOpenForm(idForm);
    }
 
  </script>
 
</html>

I'm not joining the called functions because they work fine. Indeed, if I do a logger.log (url) to check the value of the variable defined on line 34 of codeTest.gs, it does contain the url of the new file created.

I wonder why the last part doesn't work:

  // 3 - on ouvre le nouveau fichier créé
  var url=newForm.getUrl;
  var htmlString = "<script>window.open('" + url +"');</script>";
  return HtmlService.createHtmlOutput(htmlString);

This piece of code works fine if I use it on my own elsewhere, but not here. Is it because in the same script you can't use HtmlService twice? I couldn't find anything about this limitation, but I don't see any other reasons.

Unfortunately I cannot share the documents mentioned in my code with you, I am in a company with a domain not shareable with accounts other than ours.

I am sorry for my english...

2 Answers

If you simply want to open the mentioned URL, you can use UrlFetchApp instead, like this:

UrlFetchApp.fetch(url);

Reference

With Diego's suggestion, I got to this, and it works :

page.html

<!DOCTYPE html>
<html>
  <body onload='fctOpenFile()'>
    <h1>Choix du formulaire</h1>
    <p>Cliquer sur le formulaire souhaité</p>
    
    
    
    <button value="1uZwENM_Ilmo7hXLHNYaQP6GdJbuq8QlXkoiY0sIX8KQ" onclick="fctGo(this)">Prise de rendez-vous</button>
    <button value="rembEnLigne" onclick="fctGetValue(this)">Remboursement - paiement initial en ligne</button>
    <button value="rembComptoir" onclick="fctGetValue(this)">Remboursement - paiement initial au comptoir</button>
  </body>
    
    <script>
      
      function fctGo(element){
        var idForm=element.value;
        google.script.run.withSuccessHandler(fctOpenByUrl).fctOpenForm(idForm);
      }
      
      function fctOpenByUrl(url){
        window.open(url);
      }
      
    </script>

</html>

codeTest.gs

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('page');
}

function fctOpenForm(idForm){
 
  // fonction qui va faire une copie du formulaire pour ensuite l'ouvrir (on préserve ainsi l'original)
  
  // 1 - on va chercher en fonction de l'id du formulaire le répertoire où il doit être copié
    // 1.1 - on "ouvre" le fichier de configuration
    var ssConfigId="1AegVIBq_pOLxhCGb7qcUr-wmCFIk7KRqQ5sB3HBDU04";
    var ssConfig=SpreadsheetApp.openById(ssConfigId);
    var shtListeForms=ssConfig.getSheetByName("Liste des formulaires");
    // 1.2 - on identifie les lignes et colonnes importantes du tableau
    var derLig=ssConfig.getLastRow();
    var derCol=ssConfig.getLastColumn();
    var ligEntete=fctFindLigne(shtListeForms,"Nom du formulaire",1,derLig,1);
    var colNomForm=fctFindColonne(shtListeForms,"Nom du formulaire",1,derCol,ligEntete);
    var colIdForm=fctFindColonne(shtListeForms,"Id du formulaire",1,derCol,ligEntete);
    var colIdRepDest=fctFindColonne(shtListeForms,"Id du répertoire de destination de la copie",1,derCol,ligEntete);
    // 1.3 - on cherche l'id du répertoire de destination de la copie
    var idRepDest=shtListeForms.getRange(fctFindLigne(shtListeForms,idForm,ligEntete,derLig,colIdForm),colIdRepDest).getValue();
    // 1.4 - on cherche le nom du formulaire
    var nomForm=shtListeForms.getRange(fctFindLigne(shtListeForms,idForm,ligEntete,derLig,colIdForm),colNomForm).getValue();
    
  // 2 - on copie le formulaire
  var folderDest=DriveApp.getFolderById(idRepDest);
  var newForm=DriveApp.getFileById(idForm).makeCopy("*"+nomForm,folderDest);
  var newFormId=newForm.getId();
  return newForm.getUrl();

}

Ale13, UrlFetchApp class sounds interesting. I will inquire as well.

Thanks!

Related