adding element to end of array in sharedObject - actionscript-3

I want to add an element to an array with every click button.
I used shared Object. because i'm going to call my data after i reopen my app.
how can i define an array in shared object?
i cant use this:
myData.data.list = [];
since when i open my application again, my last data will be deleted.
and how can i add elements to my variable in shared object?
i tried this:
Array(myData.data.list).push(number);
but when i trace that, every index of that is still undefined and nothing has changed.
what's the solution?

Try this steps:
First ActionScript code to create an Array, and push the element in the Array. and finally stored Array in ShraedObject. SharedObject is stored disk. so it will probably be in the form of ByteArray. so read and the data type must be converted to match.
If you want write to a Array in SharedObject
var myArray:Array = [obj1, obj2...];
var so:SharedObject = SharedObject.getLocal("myArray");
so.data.myArrayData = myArray;
so.flush();
To retrieve it back anywhere and adding element to end of array. and resaved.
var so:SharedObject = SharedObject.getLocal("myArray");
var myArray:Array = so.data.myArrayData;
myArray.push(number);
so.data.myArrayData = myArray;
so.flush();

I found something helpful that I'm going to add into the above code answer:
To retrieve it back anywhere and adding element to end of array. and resaved.
var so:SharedObject = SharedObject.getLocal("myArray","/");
var myArray:Array = so.data.myArrayData;
myArray.push(number);
so.data.myArrayData = myArray;
so.flush();
To avoid inadvertently restricting access to a shared object, use the localpath parameter. - which is the "/"

Related

How do you POST files in chunks, to server, using input id as param, in HTML/jQuery?

Description
I have a function that takes HTTP_POST path for server side hardcoded .js handler and file id from input[type=file]. I'm trying to POST that binary file to server but in chunks with no success.
What have you tried?
Using my uncle wisdom, I have noted that there is a way to slice the file using :
var File1 = document.querySelector('input').files[0].slice(0,7);
But passing this variable to the function is not an option since it takes id ="file_1" as a parameter from <input type="file">. Also, printing this variable doesn't work with console.log (half of you are already thinking to send me back to uni) hint: I've never studied Javascript. So I poked my uncle a little harder and found it. FileReader gives you the ability to actually read the inside, which gave me hope since my file was printed out to console.log
var reader = new FileReader;
reader.readAsBinaryString(File1);
reader.onload = function(e) {
var rawLog = reader.result;
console.log(rawLog);
};
At this point console.log gave me a print of first seven bytes, of my file. And by all of this we got to the question I wish to ask
Question ?¿
How to past this print as with input to the function so I can have this file sliced and saved?
You can slice off a piece of the file, use that piece to create a File object add it to a FileList and then overwrite the FileList from the input with the one that has your new file
var input = document.querySelector('input');
var originalfile = input.files[0];
var slice = originalfile.slice(0,7); // slice the file
var newfile = new File([slice] , 'slicedfile.dat'); // create new file
// Need to use a data transfer object to get a new FileList object
var datTran = new ClipboardEvent('').clipboardData || new DataTransfer();
datTran.items.add(newfile); // Add the file to the DT object
input.files = datTran.files; // overwrite the input file list with ours
// call function that takes path and id as parameters

Getting undefined instance tree for DWFX file

We are trying to get all node elements of DWFX file but we are getting undefined instance tree for DWFX file. We have used below code to get each element id.
// Try to get instance tree for DWFX file
var model = this.viewer.model;
var modelData = model.getData();
var it = modelData.instanceTree; // get instance tree
We have used another way to get element node id for DWFX file. (In that case, we are getting only panel label id for DWFX file) But that logic is not working for all DWFX files.
// Try to get all ids for DWFX file
var model = this.viewer.model;
var modelData = model.getData();
var allIds = modelData.stringDbIds; // get all ids
Please us know If I am using wrong approach to get all elements for DWFX file.
You need to wait for Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT event to make sure the instanceTree is available in your loaded model:
viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, function () {
var model = this.viewer.model;
var modelData = model.getData();
var it = modelData.instanceTree;
console.log(it)
})
In some cases you may have to wait also for Autodesk.Viewing.GEOMETRY_LOADED_EVENT event if you intend to access geometry of the components. Here is an article that may be relevant: Asynchronous viewer events notification

getCalendarById where the ID is a String

On a Google Spreadsheet there are several calendar IDs. Each is in Column B, and is in the format of "SuperLongStringGoesHere#group.calendar.google.com".
Things work when I manually enter the calendar ID instead of using a variable (the variable is calRngVal). When I use a variable, it returns null. How do I call getCalendarById using a variable?
var calRng = calids.getRange(a, 2);
var calRngVal = calRng.getDisplayValue();
var cal = CalendarApp.getCalendarById(calRngVal);
var ad = cal.getName();
Thanks in advance!
Here is a picture of the debugger:
You don't need to do anything special to access a calendar using a string value stored in a sheet. Both your approach and that mentioned in Simon's answer should work.
There must be something else going wrong. Are you sure you have permission to access all of the calendars listed in the sheet? If you don't have permission, getCalendarById will return null.
Wrap the code after your getCalendarById call in a try/catch block so that one "bad" calendar won't crash the script.
EG:
var calRng = calids.getRange(a, 2);
var calRngVal = calRng.getDisplayValue();
var cal = CalendarApp.getCalendarById(calRngVal);
try{
var ad = cal.getName();
//other code here
}catch(e){
Logger.log('Exception processing '+calRngVal+', row '+a+'. Message: '+e);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
Also note that looping and fetching the ranges one by one is a slow way to do this, you could use getDataRange() to return all the data in the sheet in a two dimensional array, then just loop over the array. However I would fix the "null" problem before optimizing.
Are you sure the code is selecting a cell with a calendar id value in it? Try this:
var calRng = calids.getRange("A1:B");
var calRngVal = calRng.getValues();
var cal = CalendarApp.getCalendarById(calRngVal[1][1]);
var ad = cal.getName();
If it doesn't work, try using the logger sometime:
Logger.log(calRngVal);

Google html service to sheets

I'm not a big fan of google forms so I made the form for my user input in the html service. I found a way to push the data out of the form and into google sheets using all of my variables in the html file like this:
<textarea type="text" name="Special Instructions" id="instructions"></textarea>
...
var instructions = document.getElementById("instructions").value;
...
google.script.run
.formsubmit (instructions,...)
google.script.host.close()}
in combination with the following in the code file:
function formsubmit(instructions,...)
var ss = SpreadsheetApp.getActive().getSheetByName("Sheet1");
ss.getRange(ss.getLastRow(),7,1,1).setValue(instructions);
...
The problem is, not only is the code very slow to output results, but if I have more than 37 or so variables defined, it glitches out and rather than closing the dialog box and recording the values in a spreadsheet, it opens a blank web page.
I know there has to be better (and more efficient) way, but I'm afraid I don't know it.
On the "client side", put all of your variables into a JSON object or an array, the stringify it, and send that string to the server.
var objectOfData;
variableOne = "one";
variable2 = "two";
objectOfData = {};
objectOfData['varOne'] = variableOne;//Create a new element in the object
objectOfData['var2'] = variable2;//key name is in the brackets
objectOfData = JSON.stringify(objectOfData);//Convert object to string
google.script.run
.formsubmit(objectOfData);
And then convert the object as a string back to a real object:
function formsubmit(o) {
var arrayOfValues,k,myData,outerArray;
myData = JSON.parse(o);//Convert string back to object
var ss = SpreadsheetApp.getActive().getSheetByName("Sheet1");
arrayOfValues = [];
for (k in myData) {//Loop through every property in the object
thisValue = myData[k];//
Logger.log('thisValue: ' + thisValue);//VIEW the LOGS to see print out
arrayOfValues.push(thisValue);
}
outerArray = [];
outerArray.push(arrayOfValues);
ss.getRange(ss.getLastRow() + 1,7,1,arrayOfValues.length).setValue(outerArray);
...
Note that the last parameter of getRange('start row', start column, number of rows, number of columns) uses the length of the inner array named arrayOfValues. This insures that the parameter value will always be correct regardless of how the array is constructed.

How to get a document object from a file object in a Google-Apps Script

My problem is that I have a number of Google documents (docs) and each has a table of contents. I want to grab the TOC's and place them in another document. This should be easy, but I have run into a snag.
I can grab all of files in a folder with:
var folder = DocsList.getFolder('yourfolder');
var contents = folder.getFilesByType(DocsList.FileType.DOCUMENT);
This gives me an array of files as the variable 'contents'
Great.
I also know that
var TOC = **doc**.getAs(DocumentApp.ElementType.TABLE_OF_CONTENTS)
The problem is that I cannot figure out how to get a document object from a file object or alternately how to get an array of documents in a folder rather than an array of files.
I have searched for an answer and not only on this site. If anyone can help, I would appreciate it very much
DocsList and DocumentApp have at least one method in common which is that they have access to the ID of documents so it is quite straightforward to pass this ID parameter from one method to the other.
This code demonstrates :
function myFunction() {
var folder = DocsList.getFolder('yourfolder');
var contents = folder.getFilesByType(DocsList.FileType.DOCUMENT);
var docObject = [];
for(var c in contents){
docObject.push(DocumentApp.openById(contents[c].getId()));
}
Logger.log(docObject);// now you have an array of DocumentApp objects
}