RemcoE33
RemcoE33

Reputation: 1620

Sheet sidebar input to google apps script won't work

I made a simple sidebar, with a simple script to append the input to the sheet. But the input never 'leaves' the html file.. My logs are empty. In my console log the getInput() doesn't show. I am really confusion right now..

HTML

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

    <div>
    <input id="field" type="text"> 
    <button onclick="getInput()" id="run" type="submit">Run</button>
    </div>

    <script>
    function getInput(){
    var input = document.getElementById("field").value;
    console.log(input);
    google.script.run.fetchInput(input);
    }

    </script>
  </body>
</html>

Script

function sidebar(){
  var html = HtmlService.createHtmlOutputFromFile('index').setTitle('Test');
  SpreadsheetApp.getUi().showSidebar(html);

}

function fetchInput(input){
 var ss = SpreadsheetApp.getActive().getActiveSheet();
 ss.appendRow(input);
 Logger.log(fetchInput(input));
 console.log(input);

}

What am i doing wrong?

Upvotes: 0

Views: 39

Answers (1)

Cooper
Cooper

Reputation: 64100

This works:

function sidebar(){
  var html = HtmlService.createHtmlOutputFromFile('ah3').setTitle('Test');
  SpreadsheetApp.getUi().showSidebar(html);

}

function fetchInput(input){
 var ss = SpreadsheetApp.getActive().getActiveSheet();
 ss.appendRow([input]);//you need to make it an array here
}

html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>

    <div>
    <input id="field" type="text" /> 
    <input type="button" value="Run" onClick="getInput();" />
    </div>

    <script>
    function getInput(){
    var input = document.getElementById("field").value;
    console.log(input);//this works
    google.script.run.fetchInput(input);
    }

    </script>
  </body>
</html>

Upvotes: 1

Related Questions