Passing state/data to Google Apps Script ServerHandler - google-apps-script

I am trying to work out how I can pass some arbitrary state to a ServerHandler in Google Apps Script. The following code illustrates the question - can anybody help?
Thanks.
function myFunc(e) {
// want to get my data object back out here..?
}
function setUp()
{
var data = getMyDataArray();
// ... set up UI...
var h = app.createServerHandler('myFunc');
// How do I passs my data object to the myFunc handler?
flow.add(app.createButton().setText("OK").addClickHandler(h));
app.add(flow);
s.show(app);
}

You can use Hidden elements to store arbitrary data and send it along with a server handler invocation. The issue is that the the element can only store a string. But you can solve this using JSON.
function myFunc(e) {
var yourObj = Utilities.jsonParse(e.parameter.yourObject);
//do what you need
}
function setUp()
{
var data = getMyDataArray();
// ... set up UI...
var hidden = app.createHidden("yourObject", Utilities.jsonStringify(data));
var h = app.createServerHandler('myFunc').addCallbackElement(hidden);
flow.add(app.createButton().setText("OK").addClickHandler(h));
app.add(flow);
s.show(app);
}

Related

How can I keep an array filled outside of a function?

This question is part of finding a solution to a larger problem. However, I am wondering if there is a way to use a function to fill an Array. Based on the test code below, it seems arrays automatically empty after the function. I am looking to have a function that fills the array repeatedly, and another one that triggers to paste the resulting array.Note the paintArray=[] defined outside of the functions.
The console logs '1' for fillArray() but when I run logArray() right after it logs [].
const paintArray = []
function logArray(){
console.log(paintArray)
}
function fillArray(){
paintArray.push(1)
console.log(paintArray)
}
You can use Properties Service this way:
var scriptProp = PropertiesService.getScriptProperties();
function logArray(){
Logger.log(scriptProp.getProperty("paintArray"))
}
function fillArray(){
var obj = scriptProp.getProperty("paintArray")
if(obj){ //check if paintArray Object exists
var arr = JSON.parse(obj); //parse the value to array
arr.push(1); //push value 1
scriptProp.setProperty("paintArray", JSON.stringify(arr)) //set new value of arr to properties
}else{
scriptProp.setProperty("paintArray", "[1]"); //set initial value if paintArray object is undefined
}
}
First run:
Second run:
Third run:
Save and Retrieve Array from Properties Service
function saveArray(a) {
PropertiesService.getScriptProperties().setProperty("myarray",JSON.stringify(a));
}
function getArray() {
return JSON.parse(PropertiesService.getScriptProperties().getProperty("myarray"));
}

how to insert an image from base64 string

The code below should insert an image blob made from the base64 string img_3k, but somehow it doesn't. What am I doing wrong?
var img_3k = "iVBORw0KGg..."
function searchAndReplace() {
var body = DocumentApp.getActiveDocument().getBody();
var blobImg = Utilities.newBlob(Utilities.base64Decode(img_3k), MimeType.PNG);
body.insertImage(0, blobImg);
}
Well, I have no problem with your code.
I guess your problem is that you use a global variable, but you can not use global variables in Google Apps Script.
function searchAndReplace() {
var img= getImage();
var body = DocumentApp.getActiveDocument().getBody();
var blobImg = Utilities.newBlob(Utilities.base64Decode(img), MimeType.PNG);
body.insertImage(0, blobImg);
}
function getImage() {
return "iVBORw0KGgoAAA...SUVORK5CYII=";
}
just remove the "var" before your global var declaration:
img_3k = "iVBORw0KGg...";
Variables declared with var in global scope won't be visible in functions.
You could capitalize the variable as well (IMG_3K) so you know immediately that it is a static global one
The code was actually correct, but didn't get executed because I didn't call it from the right place. Put in the onInstall method and then it worked.

Google Apps Script, HTML addClickHandler ServerHandler does NOT work

Can anyone confirm that HTML widgets accept ClickHandlers on the Server side ? I can't get my below code to work.
I create a serverHandler (and for good measure I have even added a useless callback element). Subsequently, I add it to a HTML.addClickHander (for good measure I have even added it to .addMouseUpHandler as well). The function is NOT executed.
var mouseclick = app.createServerHandler("handleTrainingClick_").addCallbackElement(lstFilter);
var params = [ "fromOrg", "trainingTitle", "dueDate", "medical", "status" ];
var resultSet = blSelectActiveTrainings_();
while (resultSet.hasNext()) {
var training = resultSet.next();
var html = TRAINING_ROW;
for (var pI in params) {
html = html.replace("$"+params[pI], training[params[pI]]);
}
pnlList.add(app.createHTML(html).setId(training.id).addClickHandler(mouseclick).addMouseUpHandler(mouseclick)
.addMouseMoveHandler(mousemove).addMouseOutHandler(mouseout).addMouseOverHandler(mouseover));
}
function handleTrainingClick_(e) {
Logger.log(e.source);
var app = UiApp.getActiveApplication();
return app;
}
HTML widgets server side handlers work just fine. It was an incorrect reference in my code. Thanks all.

Binding JSON string to ListView in Metro apps?

I have a metro application(HTML5 & WinJS) in which am trying to display service data . Actually here am retrieving JSON data from my service but am unable to bind this data into listview . Anyone give me some working example.
Thank you.
You can use the WinJS.xhr() for this. You can read more about it on this link https://msdn.microsoft.com/pt-br/library/windows/apps/br229787.aspx and here is an example:
var path = "data/file.json";
function getData(path) {
WinJS.xhr({ url: path }).then(
function (response) {
var json = JSON.parse(response.responseText);
// Since this is an asynchronous function, you can't
// return the data, so you can:
// 1) retrieve the data to a namespace once the app loads.
var list = new WinJS.Binding.List(json);
Somenomespace.data = list;
// 2) or do all the binding inside the function.
var listView = document.getElementById("listViewID");
listView.winControl.itemDataSource = list.dataSource;
});
}
If you use the built in JSON.parse(jsonString) function you can loop through the content using a normal for loop as it then is a normal object and add it as usuall. Just remember to process or render the data.
Her is an example from code i had in a search page using listview:
var response = JSON.parse(data) ;
var originalResults = new WinJS.Binding.List();
for (x in response) {
originalResults.push(response[x]);
}
this.populateFilterBar(element, originalResults);
this.applyFilter(this.filters[0], originalResults);

Is there a work around for an object oriented front-end in Google Script

I'm having some problems with an event handler in the object below. I can't remember the error message but it basically said that it could not find the function. The code below is an example of what I'm trying to do.
var anObject = function () {
var n = 0;
var HandleClick(e) {
n ++;
};
return {
Init: function () {
var app = UiApp.getActiveApplication();
var handler = app.createServerHandler("HandleClick");
var com = UiApp.LoadComponent("MyGui", {prefix: "a"});
com.getElementById("button").addClickHandler(handler);
}
}
}
Would really appreciate a work-around if possible, if that is not possible then please tell me what you would suggest because I'm not sure how best to get around this.
Thanks guys.
All handler functions must be top level functions on your script. It's not possible to have it inside an object like this.