简体   繁体   English

Google Apps脚本将客户端变量推送​​到服务器

[英]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.

相关问题 如何将服务器端数据存储到Google Apps脚本中的客户端变量? - How to store server side data to client side variable in Google Apps Script? 在 Google Apps 脚本中从服务器到客户端进行通信 - Communicate from server to client side in Google Apps script 具有 Google Apps 脚本值的服务器端和客户端通信函数 - Server Side and Client Side communications Functions with values at Google Apps Script 在Google Apps脚本中从服务器端(.gs)传递到客户端(html)时,已知类型的变量变为“未定义” - Variable of known type becomes “undefined” when passed from server side (.gs) to client-side(html) in Google Apps Script Google Apps 脚本服务器端的全局变量错误 - Global Variable Bug in Google Apps Script Server Side Google Apps脚本变量传递 - Google Apps Script Variable Passing Google Apps脚本变量重新分配 - Google Apps Script Variable Reassignment 将 Google Apps 脚本变量传递给 HTML 脚本 - Pass Google Apps Script variable to HTML script 我可以使用一个ID作为变量,并在调用客户端函数时遇到问题吗?google-apps-script - Can I have an id that is a variable and having trouble with calling client side function… google-apps-script 从客户端发送到Google Apps脚本(服务器)base64字符串 - send from client side to google apps script (server) base64 string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM