1

I am trying to run a script that when a if() statement returns false it closes the current tab window of the google sheet or it redirects to another URL (as long as one of these work, thats ok for me).

I understand that this may be possible through using HTML but i don't know HTML well enough to do this.

I have found the below code which is meant to redirect to a different page which I tried but this does not work:

      return HtmlService.createHtmlOutput(
        "<form action='http://www.google.com' method='get' id='foo'></form>" + 
        "<script>document.getElementById('foo').submit();</script>");

Does anyone have any ideas?

See code that this is in below below:

  if (name == password)
  {
    Browser.msgBox('You have succesfully logged in');
    sortOutSheets();
  }
  else (name != password)
  {
    var response = Browser.msgBox('Login Failed - do you want to login again?', Browser.Buttons.YES_NO);
    if(response == "yes")
      enterPassword();
    else(response == "no")
    HtmlService.createHtmlOutputFromFile('Close Window');
  }
}

I have now turned to writing the HTML in another HTML file but still no luck

Gav
  • 328
  • 5
  • 17
  • Does this answer your question? [Can't stop Google Apps Script from masking redirected URL](https://stackoverflow.com/questions/56685553/cant-stop-google-apps-script-from-masking-redirected-url) – TheMaster Feb 16 '20 at 16:24
  • Sorry @TheMaster - no that doesn't..... – Gav Feb 16 '20 at 16:33
  • You say `I am trying to run a script that when a if() statement returns false it closes the current tab...` Can you show us that script? Also what's the point of the script you did display? Is your script running client side or server? – Cooper Feb 16 '20 at 19:35
  • hello @cooper - the script is only on client side and is as below. I have now turned to trying to create the code in a html file but not exactly sure what to put! ` if (name == password) { Browser.msgBox('You have succesfully logged in'); sortOutSheets(); } else (name != password) { var response = Browser.msgBox('Login Failed - do you want to login again?', Browser.Buttons.YES_NO); if(response == "yes") enterPassword(); else(response == "no") HtmlService.createHtmlOutputFromFile('Close Window'); }` } – Gav Feb 16 '20 at 20:17
  • Please post it in the question. – Cooper Feb 16 '20 at 20:19
  • You cannot use HtmlService client side? – Cooper Feb 16 '20 at 20:23
  • It's not clear to me what you want to do. Do you want to close a dialog, or webapp or spreadsheet? Maybe some images would help. – Cooper Feb 16 '20 at 20:28
  • looks like your're right.... just having a look at google development site: https://developers.google.com/apps-script/guides/html/communication :( - any other ideas how i could run that? – Gav Feb 16 '20 at 20:29
  • I would really like to close the spreadsheet if at all possible. – Gav Feb 16 '20 at 20:29
  • You can't close the spreadsheet from a webapp. As a matter of fact you can't open a spreadsheet from a webapp in the same way that a user can. – Cooper Feb 16 '20 at 21:38

1 Answers1

0

Here are the steps that I would program.

  • Show Dialog box in spreadsheet asking for a password
  • User enters password
  • User clicks Submit button
  • Client side code calls the server and sends the password to code in GS file
  • Server code tests the password
  • If password is good, build new HTML and open another dialog box
  • The opening of a new dialog box will automatically close the first dialog box
  • The new dialog box will have a <a> tag link in it, built with a new url on the server
  • When the new dialog box opens, automatically run code with window.onload = function() {var aTagElement; aTagElement = document.getElementById('idOfLinkTag');aTagElement.click() }
  • The client code will automatically run when the dialog box opens, and programmatically click the link, which will open a new file in the same browser tab
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • Thanks @Alan Wells - will have a look and see if I can create something based on that. May have to do quite a bit of research first as I'm not all that literate with html! – Gav Feb 17 '20 at 13:01