如何将变量放入GAS中

时间:2017-03-15 21:10:35

标签: javascript jquery html google-apps-script

嗨所以我一直在尝试在Jquery javascript html和谷歌应用程序脚本编码,我已经得到了应用程序的一部分,但我不能做另一半是采取jquery变量并在谷歌打印它电子表格。这就是它现在的样子。 GAS:

function doGet() {
    return HtmlService.createTemplateFromFile('index')
      .evaluate()
      .setTitle('The Game')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);

}

function codefunction() {
  Logger.log("In codefunction in codegs");


}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <h1> The Game </h1> 
    <b><em>Output:</em></b>
    <p id="output">
    </p>

    <input type="text" id="txt_box"/>
    <button type="button" id="input_button" class="enjoy-css">Enter</button>

    <?!= HtmlService.createHtmlOutputFromFile('javascript').getContent(); ?>

  </body>
</html>

的Jquery / JavaScript的:          

<script>

$(function() {

    $('#input_button').bind('click',clickButton);
    linkTocodegs();

});

function clickButton() {
  var boxContents = $("#txt_box").val();
  $("#txt_box").val("");
  outputMessage(boxContents);
}


function outputMessage(message) {
   var text = $('#output');
   var item = $('<p>');
   var title = $('<span>').text(message);
   title.append(item);
   text.append(title);
}

function linkTocodegs() {
  console.log("in code gs function link");
  google.script.run.withSuccessHandler(outputMessage)
  .codefunction();
}

</script> 

1 个答案:

答案 0 :(得分:0)

您需要从文本框中取出值并将其传递给服务器:

function linkTocodegs() {
  var userInput;

  userInput = document.getElementById('txt_box').value;

  console.log("in code gs function link");
  google.script.run.withSuccessHandler(outputMessage)
  .codefunction(userInput);
}

Code.gs

function codefunction(receivedValue) {
  Logger.log("In codefunction in codegs: " + receivedValue);
  var ss,sh;

  ss = SpreadsheetApp.getActiveSpreadsheet();
  sh = ss.getSheetByName('sheet1');

  sh.getRange(1,1,1,1).setValue(receivedValue);

}