0

From my Index.html page, I take parameters to my Trabajo.html page through the following link:

https://script.google.com/macros/s/************/exec?page=Trabajos&item=23

but I don't know how to receive the variable "item" on my page "Trabajo.html"

Codigo.html

function getScriptUrl() {
 var url = ScriptApp.getService().getUrl();
 return url;
}

Index.Html

 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
 <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">

 <html>
    <head>
      <base target="_top">
   </head>
<body>
   <?var url = getScriptUrl();?><a href='<?=url?>?page=Trabajos&item=23' target="_blank"> <input type='button' name='button' value='Trabajos.html'></a>
</body>
 </html>

Trabajos.html

 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
 <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
 <!DOCTYPE html>
  <html>
   <head>
       <base target="_top">
   </head>
   <body>
      <br><br>
      //Here I want to show the value of the item, and also in a <script>
      <input type='text' value='<?=item?>' >
      <br><br>
      <?var url = getScriptUrl();?><a href='<?=url?>?page=Index'> <input type='button' name='button' value='Index.html'></a>
    </body>
  </html>

I don't know how to receive the item variable, how could I do it if my code is like that?

Kevincs7
  • 206
  • 1
  • 5
  • 14

2 Answers2

0

It seems you need to use templates

function doGet() {
  var htmlTemplate = HtmlService.createTemplateFromFile('Trabajos');
  htmlTemplate.item = 'Set the item here';
  return htmlTemplate.evaluate();
}
contributorpw
  • 4,739
  • 5
  • 27
  • 50
  • I'm sorry, I can't ask what you answer ... I'll edit my question so that I understand the code I have – Kevincs7 Oct 12 '19 at 06:49
  • I get this error--> "ReferenceError: item" no está definido. (línea 25, archivo "Código", proyecto "Inci")" – Kevincs7 Oct 12 '19 at 14:41
0

I assume that you creating a Google Web-App.

  • A Google Web App usually consists of a Code.gs (not Code.html) file and one .html file which will be returned when the Web App url is opened.

  • A crucial part of a Web App is a doGet(e) function within which the html file to call will be returned.

  • In your case, you have two .html files that you want to open with the same Web App url.
  • In this case you need to make a case differentiation within the doGet() function to create a different html template in function of item
  • To pass item from the url https://script.google.com/macros/s/************/exec?page=Trabajos&item=23 to the .html file, you need to retrieve it within the doGet() function as a e.parameter and pass it to the htmlTemplate.

Sample:

Code.gs

function doGet(e){
   var item=e.parameter.item;  
  if(item==23){
     var htmlTemplate = HtmlService.createTemplateFromFile('Trabajos');
         htmlTemplate.item = item;
  }else if(item==24){
    ...
  }
  }else if(!item){
     var htmlTemplate = HtmlService.createTemplateFromFile('Index');
  }
  ...
  return htmlTemplate.evaluate();
}


function getScriptUrl() {
 var url = ScriptApp.getService().getUrl();
 return url;
}

Index.html and Trabajos.html can stay for this sample as they are.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33