title: Get Variable Value from inside code.gs Google App Script to Javascript
body: <p>I have a simple webapp that show a button and when a user clicked, it will open a new window that shows a website. What I want to do is when a user clicked the button, it will get the value from my google sheet and open a new window based on that value. Then, the webapp will update the value in the google sheet with a new value.</p>
  <p>For example:
  if the value in my google sheet is <strong>&quot;Google&quot;</strong>, it will open a window to <strong>&quot;www.google.com&quot;</strong> and then update the google sheet value to <strong>&quot;other website&quot;</strong>.</p>
  <p><a href="https://i.stack.imgur.com/DdmGR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DdmGR.png" alt="enter image description here" /></a></p>
  <p>I have succesfully made the function for updating the value on google sheet whenever a user clicked the button but I fail in getting the value from the code.gs/google sheet to my Javascript.
  Please help.</p>
  <p>here is my code.gs:</p>
  <pre><code>var url = &quot;url&quot;; //mygooglesheet url
  var web = &quot;&quot;;
  
  function doGet(e) {
    let tmp = HtmlService.createTemplateFromFile(&quot;index&quot;);
    return tmp.evaluate();
  
  }
  
  function setWebsite () {
  
    var ss = SpreadsheetApp.openByUrl(url);
    var ws = ss.getSheetByName(&quot;website&quot;);
    web = ws.getRange(1,1,1,1).getValue();
    
    if (web === &quot;Google&quot;) {
      ws.getRange(1,1,1,1).setValue(&quot;Youtube&quot;);
    } 
      else if (web === &quot;Youtube&quot;) {
      ws.getRange(1,1,1,1).setValue(&quot;Facebook&quot;);
    }
      else {
      ws.getRange(1,1,1,1).setValue(&quot;Google&quot;);
    } 
  }
  
  function getWebsite() {  
    
    var ss = SpreadsheetApp.openByUrl(url);
    var ws = ss.getSheetByName(&quot;website&quot;);
    var web = ws.getRange(1,1,1,1).getValue();
      return web;
      }
  </code></pre>
  <p>my index.html:</p>
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;base target=&quot;_top&quot;&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;h1&gt;Please Click Below&lt;/h1&gt;
      &lt;!-- &lt;h2&gt;&lt;?=web?&gt;:&lt;/h2&gt; --&gt;
      &lt;button id = &quot;btn&quot; type=&quot;submit&quot; &gt;Open Window&lt;/button&gt;
                
    &lt;script&gt;
    document.getElementById(&quot;btn&quot;).addEventListener(&quot;click&quot;, doStuff);
    var web = &quot;&quot;;
    
    function doStuff() {
  
    google.script.run.getWebsite(); //dont know for sure if this is needed or not
    google.script.run.setWebsite();
    web = &lt;?=web?&gt;; //dont know for sure if this is needed or not
    if (web === &quot;Google&quot;) {
        window.open(&quot;https://www.google.com/&quot;);
      } 
        else if (web === &quot;Youtube&quot;) {
        window.open(&quot;https://www.youtube.com/&quot;);
      }
       else {
        window.open(&quot;https://www.facebook.com/&quot;);
      } 
    }
  &lt;/script&gt;
  
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Right now, my webapp only open a new window to <strong>&quot;facebook.com&quot;</strong> and update to the next value in the code. I tried <em>&quot;google.script.run.withSuccessHandler(onSuccess).getWebsite()&quot;</em> but not successful to get the variable value from the <strong>code.gs</strong>, please help.</p>
  <p>Thank you.</p>