![](/img/trans.png)
[英]How to store server side data to client side variable in Google Apps Script?
[英]google apps script push client variable to server
根據utphx的答案,修改后的代碼正確無誤且功能正常。 在google apps腳本中,我有以下基本代碼:
.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);
}
和.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正確提取並在可變輸入元素中顯示我想要的值。 我的問題是.save,我希望它能夠更改數字,單擊“保存”按鈕,然后將輸入值保存到電子表格中。 .setValue編輯數據,我只是想不出如何從服務器訪問輸入的值以使其正確更改數據-僅清除當前數據,因為它無法識別var i。
*請注意,“%sheetid%”是該問題的占位符,我在文件中使用了正確的ID,並且“ var i = $('#test')。value;” 僅用於顯示以幫助解釋我在尋找什么-不能聲明客戶端並使用服務器端。
這應該工作:
var i=document.getElementById('test').value
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.