Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在扩展电子表格的Google脚本上传递按钮事件处理程序中的数据?_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript 如何在扩展电子表格的Google脚本上传递按钮事件处理程序中的数据?

Javascript 如何在扩展电子表格的Google脚本上传递按钮事件处理程序中的数据?,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,扩展Google电子表格,我运行一个脚本,在侧边栏中显示一些数据。在底部,我想添加一个按钮来发送数据 但是,我不知道如何将数据从按钮传递到处理程序: 不可能将数据传递到事件调用中; 无法从事件信息中获取button对象,我只能获取ID,如果无法获取在handler函数之外创建的uiInstance,则ID是无用的。 那么诀窍是什么呢?在将其分配给按钮之前,必须添加一个回调元素(可能是一个面板),将所需的所有内容包装到服务器处理程序中。例如: function myFunction() { v

扩展Google电子表格,我运行一个脚本,在侧边栏中显示一些数据。在底部,我想添加一个按钮来发送数据

但是,我不知道如何将数据从按钮传递到处理程序:

不可能将数据传递到事件调用中; 无法从事件信息中获取button对象,我只能获取ID,如果无法获取在handler函数之外创建的uiInstance,则ID是无用的。
那么诀窍是什么呢?

在将其分配给按钮之前,必须添加一个回调元素(可能是一个面板),将所需的所有内容包装到服务器处理程序中。例如:

function myFunction() {
  var app = UiApp.createApplication();
  var panel = app.createVerticalPanel();
  panel.setId('myPanel').add(
    app.createTextBox().setName('boxExample')).add(
    app.createListBox().setName('listExample').addItem('A').addItem('B'));
  //                                                       ↓↓ this is what you need ↓↓
  var handler = app.createServerHandler('callbackFunction').addCallbackElement(panel);
  var btn = app.createButton(btn, handler);

  app.add(panel.add(btn));
  //show app...
}

function callbackFunction(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById('myPanel').add(
    app.createLabel(e.parameter.boxExample)).add(
    app.createLabel(e.parameter['listExample']));
  return app;
}
使用PropertiesService

我发现我不知道GoogleScript是如何提供一个名为的数据存储服务的

假设在本例中,数据仅供用户使用,我需要首先将数据存储为:

var userProperties = PropertiesService.getUserProperties()
userProperties.setProperty("myKey", "myValue")
// Note myValue will be a string, so to store an array, 
// you'd need JSON.stringify(myArray)
然后,在调用按钮处理程序时,脚本可以轻松检索数据:

var userProperties = PropertiesService.getUserProperties()
var myValue = userProperties.getProperty("myKey")
使用隐藏小部件 另一种选择似乎是使用

来自链接引用的代码

 function doGet() {
   var app = UiApp.createApplication();
   // Note that the name "appState" for callbacks, and the id "hidden" for
   // getting a reference to the widget, are not required to be the same.
   var hidden = app.createHidden("appState", "0").setId("hidden");
   app.add(hidden);
   var handler = app.createServerHandler("click").addCallbackElement(hidden);
   app.add(app.createButton("click me!", handler));
   app.add(app.createLabel("clicked 0 times").setId("label"));
   return app;
 }

 function click(eventInfo) {
   var app = UiApp.createApplication();
   // We have the value of the hidden field because it was a callback element.
   var numClicks = Number(eventInfo.parameter.appState);
   numClicks++;
   // Just store the number as a string. We could actually store arbitrarily complex data
   // here using JSON.stringify() to turn a JavaScript object into a string to store, and
   // JSON.parse() to turn the string back into an object.
   app.getElementById("hidden").setValue(String(numClicks));
   app.getElementById("label").setText("clicked " + numClicks + " times");
   return app;
 }