How can I keep an array filled outside of a function? - google-apps-script

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"));
}

Related

What does this conf do?

So I was looking at a tutorial online and came across this:
function generateRobot(conf:Object = null):Robot {
var conf:Object = conf || {};
var defaults:Object = {
laserColor:red,
personality: "evil"
}
for (var key:String in defaults){
conf[key] = conf[key] || defaults[key];
}
Can someone help explain what line 2 and line 8 mean? Thanks for helping a new coder!
I have added some comments and renamed the param to make it clearer:
//param is a parameter of the type object with a default value of null that is passed
//into the function, if nothing is passed in it will be null
function generateRobot(param:Object = null):Robot {
//declare a local variable called conf and populate
//it with the parameter if it exists, otherwise create a new object {}
var conf:Object = param || {};
//create a default settings object
var defaults:Object = {
laserColor:red,
personality: "evil"
}
//loop through the default settings
for (var key:String in defaults){
//conf setting becomes param if exists otherwise use the defaults value
conf[key] = conf[key] || defaults[key];
}
The questions seems specific to the || construct in the variable assignment. As #Thilo mentioned, it is simply a way to specify a default, should the field be missing in the parameter.
For example:
function read_file(file, delete_after) {
delete_after = delete_after || "False";
//rest of code
}
would be such that, if variable delete_after is not passed when function read_file is called, then it will assume value "False", or anything after the || sign.
Some prefer an explicit check against undefined.
Other pointers to look at:
Set a default parameter value for a JavaScript function
http://www.codereadability.com/javascript-default-parameters-with-or-operator/

How do you get a selection from a DocsListDialogue?

I have a docs List dialogue, here is my code so far. How do I get the actual selection from the DocListDialogue though? I keep tried eventInfo.parameter,but that only returned a generic object and I need a file to be returned. Here is my code:
function init() {
var app = UiApp.createApplication().setTitle("WriteWell");
var selectionHandler = app.createServerHandler("selectHandler");
app.createDocsListDialog().showDocsPicker().setDialogTitle("Select File to Open").addSelectionHandler(selectionHandler);
app.add(app.createVerticalPanel().setId("Panel"));
return app;
}
function doGet(e) {
return init();
}
function selectHandler(eventInfo){
var parameter = eventInfo.parameter;//Selection???
var app = UiApp.getActiveApplication();
var panel = app.getElementById("Panel");
panel.add(app.createLabel(parameter.getId()));//Returns an error
}
When inspecting the content of eventInfo.parameter, we see that returns something like this:
{
source=u01234567890,
items=[
{
id=0Abcd-efgH_ijKLLLmnOPQr0stuvwX,
name=file_name,
url=https://docs.google.com/file/d/0Abcd-efgH_ijKLLLmnOPQr0stuvwX/edit?usp=drive_web
}
],
u01234567890=[
{
id=0Abcd-efgH_ijKLLLmnOPQr0stuvwX,
name=file_name,
url=https://docs.google.com/file/d/0Abcd-efgH_ijKLLLmnOPQr0stuvwX/edit?usp=drive_web
}
],
eventType=selection
}
If you need the ID of the selected file, you'll need something like:
...
eventInfo.parameter.items[0].id;
...
If you want to see what is in the eventInfo you can use
Logger.log(Utilities.jsonStringify(eventInfo));
which in this case would return something like that :
[13-10-13 21:25:21:722 CEST] {"parameter":{"source":"u16052058908","items":[{"id":"0AnZ5_ShBzI6pdHd4SWo0bUJYOEp4VFE4cDI1SUFvZFE","name":"Tracker locaux","url":"https://docs.google.com/a/insas.be/spreadsheet/ccc?key\u003d0AnZ5_ShBzI6pdHd4SWo0bUJYOEp4VFE4cDI1SUFvZFE\u0026usp\u003ddrive_web"}],"eventType":"selection","u16052058908":[{"id":"0AnZ5_ShBzI6pdHd4SWo0bUJYOEp4VFE4cDI1SUFvZFE","name":"Tracker locaux","url":"https://docs.google.com/a/insas.be/spreadsheet/ccc?key\u003d0AnZ5_ShBzI6pdHd4SWo0bUJYOEp4VFE4cDI1SUFvZFE\u0026usp\u003ddrive_web"}]}}
Looking at it you'll see that you can get the object properties you want using (for example) :
var docsInfo = eventInfo.parameter.items;
that will return an array of objects (one for each selected file) that contains file names, IDs and urls
Just iterate this objects array to get what you want from each item.

Return JSON value from a function

I am able to pass a JSON string to my function but can't return a value back.
var json = {"First":"ABC", "Middle":"DEF", "Last":"GHI"};
allFunction6(json);
alert(first); //this does not work
function allFunction6(json) {
var first = json.First;
alert(first); //this alerts "ABC"
return first;
}
Does the variable not retain its value outside of the function, or am I missing something?
Thanks.
Nope, the variable does not retain its value outside of the function, because that is where it is scoped to, the function.
You need to save the returned value.
var storedReturnValue = allFunction6(json);

Passing state/data to Google Apps Script ServerHandler

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);
}

How to Getting the Variable from Function in as3 without using get ,set method?

Hai am Getting trouble to retrive the values from function(addText).i Called from another function onFullScreen().I dont know how Can i do this,Kindly Help me?Here i attach my Code
private function addText()
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, ncOnStatus);
function ncOnStatus(infoObject:NetStatusEvent)
{
trace("nc: "+infoObject.info.code+" ("+infoObject.info.description+")");
if (infoObject.info.code == "NetConnection.Connect.Success")
{
initSharedObject(chatSharedObjectName);
}
}
function formatMessage(chatData:Object)
{
trace("room"+chatData.message);
number = chatData.txtalign;//i want to retrive the value of number
number.toString();
return number;
}
function syncEventHandler(ev:SyncEvent)
{
var infoObj:Object = ev.changeList;
// if first time only show last 4 messages in the list
if (lastChatId == 0)
{
lastChatId = Number(textchat_so.data["lastChatId"]) - 1;
if (lastChatId < 0)
lastChatId = 0;
}
}
function connectSharedObject(soName:String)
{
textchat_so = SharedObject.getRemote(soName, nc.uri)
// add new message to the chat box as they come in
textchat_so.addEventListener(SyncEvent.SYNC, syncEventHandler)
textchat_so.connect(nc)
}
function connectSharedObjectRes(soName:String)
{
connectSharedObject(soName)
trace(soName)
}
function initSharedObject(soName:String)
{
// initialize the shared object server side
nc.call("initSharedObject", new Responder(connectSharedObjectRes), soName)
}
}
i using the variable in another function ,but I cannot retrive the Value.
private function onFullScreen(event:FullScreenEvent):void
{
mediaContainer.addMediaElement(alert);
alert.alert("Error",number);// if i cannot retrive the value hnumber here
}
The addText() method is asynchronous, meaning that you can't simply call it , you need to wait for the event listener to return a value.
I'm not sure why you would feel the need to enclose all these functions, it's not very legible and I doubt it's necessary. You're also missing quite a few semi colons...
In any case , I couldn't see where the formatMessage() method was called, it seems that's the only place where the "number" variable gets defined.
You could create a variable outside the scope of the functions.
private var num:int;
Then in your addText function, assign a value to the variable:
num = infoObject.info.code;
Then in your onFullScreen function, access the num variable:
alert.alert("Error", num);