0

I would like to pass the variable 'MyLink' from the Google Scripts Code to HTML code.

This is the GS Code.

 function FirstView(){  
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var MyNewBook = ss.copy("A Copy of " + ss.getName());
 var MyLink = MyNewBook.getUrl()

 var html = HtmlService.createHtmlOutputFromFile('Index');
 SpreadsheetApp.getUi().showModalDialog(html, "hello");
 }

 function DOUBLE() {
 var linkButton = document.getElementById("link-button");
linkbutton.setAttribute('href', MyLink);
}

     

This is the HTML Code.

<html>

<head>
<base target="_top">
</head>
<body 
onload="DOUBLE();">
Click <a id="link-button">here</a>
<input type="button" value="Close" onclick="google.script.host.close()" />
</body>

</html

The Code Will show a dialog box with the 'MyLink' URL.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Tacitus
  • 87
  • 1
  • 3
  • 10
  • Does this answer your question? [How to pass a parameter to html?](https://stackoverflow.com/questions/30033459/how-to-pass-a-parameter-to-html) – Alex Tullenhoff Oct 06 '22 at 16:58

2 Answers2

0

I would update the href with the link like:

var ss = SpreadsheetApp.getActiveSpreadsheet();
     var MyNewBook = ss.copy("A Copy of " + ss.getName());
     var MyLink = MyNewBook.getUrl()

var linkButton = document.getElementById("link-button");
linkButton.setAttribute('href', MyLink);

and then add the id to the anchor:

Click <a id="link-button">here</a>

Update:

Create function to set the href in the link:

initFunction() {
    var linkButton = document.getElementById("link-button");
    linkButton.setAttribute('href', MyLink);
}

Call the function onload of the body tag:

<body onload="initFunction();">
lky
  • 1,081
  • 3
  • 15
  • 31
  • Thank You for your help, heady12 - I get an error "document is not defined". I have updated my initial code with the full code I am using. Any thoughts, please? – Tacitus Aug 21 '17 at 16:36
  • Its looks like google scripts doesn't allow you to execute javascript functions to run in the browser, unfortunately I haven't worked with GS before so I am unsure whether there is a different approach you would need to take to be able to set the href on the anchor tag. I would advise you read over the documentation and see if there is anything mentioned in there that would help. – lky Aug 22 '17 at 08:19
  • Thank You, heady12. But I see it used here . . .https://developers.google.com/apps-script/guides/html/communication – Tacitus Aug 22 '17 at 14:11
  • Hi Tacitus, I can see in those examples that they are all included within custom functions? maybe you need to add the code within a function that adds the href and then call this function on the page? – lky Aug 22 '17 at 15:15
  • Thank You heady12 - I thought that as well . .but I am not sure how to construct a function in HTML . . . would you have an idea? – Tacitus Aug 22 '17 at 15:34
  • Hi Tacitus, I've updated my answer for you :) I would include the function within your GS so that you have access to the MyLink variable, and then call this function onload of the body tag, although I am not sure how the GS is included within the HTML so unsure whether this may return 'undefined function' error. – lky Aug 23 '17 at 08:16
  • Thank You, heady12 - I am so grateful for your help! I think we are close. So I don't receive an error anymore!, but the words "click here" appear as normal words, not a link . . . I have updated my initial response to what I have in the script . .do you think my syntax is wrong, please? – Tacitus Aug 23 '17 at 14:41
  • Hi Tacitus, Try to add an alert in the double function and see whether this is appearing? It seems like the function doesn't seem to be running :/ Also try removing the var html section for the time being, i think there may be a syntax error within here where you have the comment "// Or DocumentApp or FormApp." I believe the . showModalDialog should be on the same line as SpreadsheetApp.getUi(). – lky Aug 23 '17 at 15:29
  • Also there is a } underneath the var html section which I don't believe you need? Where is it opening? – lky Aug 23 '17 at 15:44
  • Hi heady12. I have updated my initial post with the changes you recommended . . .I get no error, but the link does not appear in the pop-up box that shows in the Sheet . . . it syntax seems correct . . I am not sure what I am doing wrong . . . – Tacitus Aug 23 '17 at 17:01
  • Im not too sure, it looks like it doesn't have any reference to the functions as I cannot see anything wrong with the code. How is the GS added to the html document and have you got the FirstView() function running as this holds the variable with the link.. Maybe try merging the two functions together and running that one function onload of the body? – lky Aug 24 '17 at 08:10
  • Well . .the GS and HTML are in separate modules, but one calls the other. The code works as a copy of the sheet is made, but for some reason the mylink variable is just no being passed to the HTML code. I tried merging the function, but then we get the "document" not defined error. Google Sheets is free - if you like, you can test as well . . . – Tacitus Aug 24 '17 at 17:17
  • 1
    Hi Tacitus, I would take a look at https://stackoverflow.com/questions/37059639/passing-variable-from-google-script-to-html-dialog ? Does that help? It mentions templated HTML. Also see: https://developers.google.com/apps-script/guides/html/templates – lky Aug 25 '17 at 08:16
  • Thank You, heady12. I am continuing to try - no luck yet. Thank you for you persistent help . . I will keep trying. I never thought it would be so difficult to pass a variable :) I am used to programming with VBA and global and local variables are not so difficult . . . – Tacitus Aug 28 '17 at 13:44
  • Hi Tacitus, I'm sorry I wasn't able to help to get this resolved for you. I have seen the following which I am unsure whether may help you? https://ctrlq.org/code/19954-html-service-google-scripts – lky Aug 30 '17 at 09:25
  • Heady12 - no need to apologize at all :) You have been more than generous with your help! I will continue to research . . .and thank you for the link . . .I am going to review it now! – Tacitus Aug 31 '17 at 04:58
0

You can just select the element by id for example and modify its href

Reference

var ss = SpreadsheetApp.getActiveSpreadsheet();
var MyNewBook = ss.copy("A Copy of " + ss.getName());
document.getElementById("link").href = MyNewBook.getUrl()
<html>

<head>
  <base target="_top">
</head>

<body>
  Click <a id="link" href="**MyLink**">here</a>
  <input type="button" value="Close" onclick="google.script.host.close()" />
</body>

</html>
Rubén
  • 34,714
  • 9
  • 70
  • 166
solimanware
  • 2,952
  • 4
  • 20
  • 40