[英]google apps script push client variable to server
Revised code to be correct and functional per utphx's answer. 根据utphx的答案,修改后的代码正确无误且功能正常。 In google apps script, I have the following basic code:
在google apps脚本中,我有以下基本代码:
.gs: .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:
<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. .getCellContents正确提取并在可变输入元素中显示我想要的值。 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.
我的问题是.save,我希望它能够更改数字,单击“保存”按钮,然后将输入值保存到电子表格中。 .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.
.setValue编辑数据,我只是想不出如何从服务器访问输入的值以使其正确更改数据-仅清除当前数据,因为它无法识别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;" *请注意,“%sheetid%”是该问题的占位符,我在文件中使用了正确的ID,并且“ 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.