简体   繁体   中英

google apps script push client variable to server

Revised code to be correct and functional per utphx's answer. In google apps script, I have the following basic code:

.gs:

function doGet(e) {
  var template = HtmlService.createTemplateFromFile('Index');
  return template.evaluate()
  .setTitle('Web App Window Title')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function getCellContents() {
  var contents = {};
  var ns = SpreadsheetApp.openById('1A7HaSpjPyJjUcDuQZXc8TqJ-h0Sb0qoUzoypjF5ePOo');
  var nt = ns.getSheetByName('Sheet1');
  contents.value = nt.getRange(1, 1).getValue();
  return contents;
}
function save(v){
  var ns = SpreadsheetApp.openById('%sheetid%');
  var nt = ns.getSheetByName('Sheet1');
  nt.getRange(1, 1).setValue(v);
}

And .html:

<html>
<body>
<div class='block result-display' id='results'>
</div>
<br />
<button id=add>Save</button>
</body>
</html>
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>

<script>
$(function() {
google.script.run
.withSuccessHandler(function(contents) {
$('#results').append('<input id="test" value="'+contents.value+'"></input>');
})
.getCellContents();
});



$('#add').click(function(){
  var i=document.getElementById('test').value;
  google.script.run
  .withSuccessHandler(function() {
  })
  .save(i);
});
</script>

.getCellContents correctly pulls and displays the value I want in a changeable input element. My issue is with .save which I want to be able to change the number, click the "Save" button and have the input value save to the spreadsheet. .setValue edits the data, I just can't figure out how to access the input's value from the server for it to correctly change the data - just erases the data currently because it doesn't recognize var i.

*note, "%sheetid%" is a place-holder for the question, I'm using the correct id in my file, and "var i=$('#test').value;" was left only for show to help explain what I'm looking for - can't declare client-side and use server-side.

这应该工作:

var i=document.getElementById('test').value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM