I have a webpage where I am fetching the name of files in a Folder into an array using VBScript, then I am passing that array to JavaScript variable, so that I can display the names on the screen.
VBScript Code:
Function allFiles()
Dim arr, arr2, oTargetFolder
arr = array()
set oFSO = CreateObject("Scripting.FileSystemObject")
oTargetFolder = "C:\Users\msiddiq1\Documents\WSDLs"
set objFolder = oFSO.GetFolder(oTargetFolder)
set oFiles = objFolder.Files
For Each files in oFiles
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = files.Name
Next
allFiles = arr
End Function
JS:
var folderFiles = allFiles();
alert(folderFiles.length); // alerts `undefined`
I can pass hardcoded values from vbscript to javascript, but not this array.
Please suggest.
You have to wrap the resulting array in a VBArray object and call toArray:
var folderFiles = new VBArray(allFiles());
var ff = folderFiles.toArray();
alert(ff.length);
or in one line:
var folderFiles = (new VBArray(allFiles())).toArray();
Note that VBScript is deprecated in IE11 edge mode, so it will be disappearing at some point.
Related
I'd like to use a variable that contains the key value for a hash table in Google Apps Script.
In place of:
var folderRoots = {
"EYFS English":"sdfg0987897sdfga3",
"EYFS Italian":"sdf8f9g7897sfdfg7",
}
var b = folderRoots["EYFS English"]; // gets sdfg0987897sdfga3
I would like to use:
var depEYFSEn = "EYFS English";
var depEYFSIt = "EYFS Italian";
var folderRoots = {
[depEYFSEn]:"sdfg0987897sdfga3",
[depEYFSIt]:"sdf8f9g7897sfdfg7",
}
var b = folderRoots[depEYFSEn]; // gets sdfg0987897sdfga3
However, when attempting to use square brackets for the key variable as suggested here, the editor will throw an error
Invalid property ID
Maybe Google Apps Script Javascript implementation does not support this?
Unfortunately, computed property names are a feature in ES6, and Google Apps Script is still at ES5.
Instead, you can create your keys from your variables by setting the key on the object using bracket notation []:
var depEYFSEn = "EYFS English";
var depEYFSIt = "EYFS Italian";
var folderRoots = {};
folderRoots[depEYFSEn] = "sdfg0987897sdfga3";
folderRoots[depEYFSIt] = "sdf8f9g7897sfdfg7";
var b = folderRoots[depEYFSEn]; // gets sdfg0987897sdfga3
console.log(b);
I have an array containing four element located in local storage.
function rm(t)
{
// var l = localStorage.getItem("subje");
// hide element from html page
document.getElementById(t).style.display = 'none';
var c = localStorage.getItem("class");
var item = c+""+t; // c is int and t is string
var subj = localStorage.getItem("std-sub");
var x = subj.indexOf(item);
/*
subj.splice(x,1);
localStorage.setItem("final", subj);
var k = localStorage.getItem("final");*/
document.getElementById('show').innerHTML = x;
}
when I executing function the value of x is showing
29,17,7,0. but my given array have only four element.
why?????
getItem always returns a string (or null). Therefore subj.indexOf(item); will get the position of the substring item in the string subj. You should encode and decode the array as JSON and store that in local storage instead.
See Storing Objects in HTML5 localStorage
i am trying to pass non numeric index values through JSON but am not getting the data.
var ConditionArray = new Array();
ConditionArray[0] = "1";
ConditionArray[1] = "2";
ConditionArray[2] = "3";
ConditionArray['module'] = "Test";
ConditionArray['table'] = "tab_test";
var Data = JSON.stringify(ConditionArray);
When i alert the Data Variable it has the Values 1,2 and 3 but module and table are not included. How can this be added so that the whole string is passed.
EDIT : And what if i have some multidimensional elements also included like
ConditionArray[0] = new Array();
ConditionArray[0] = "11";
JSON structure only recognizes numeric properties of an Array. Anything else is ignored.
You need an Object structure if you want to mix them.
var ConditionArray = new Object();
This would be an better approach:
var values = {
array : ["1", "2", "3"],
module : "Test",
table : "tab_test"
};
var data = JSON.stringify(values);
Since javascript array accepts numeric index only. If you want non numeric index,use Object instead.
var ConditionArray = {};
ConditionArray[0] = "1";
ConditionArray[1] = "2";
ConditionArray[2] = "3";
ConditionArray['module'] = "Test";
ConditionArray['table'] = "tab_test";
var Data = JSON.stringify(ConditionArray);
Here is the working DEMO : http://jsfiddle.net/cUhha/
According to the algorithm for JSON.stringfy (step 4b), only the (numeric) indices of arrays are stringified.
This is because Array does not contain your elements.
When you do this:
ConditionArray['module'] = "Test";
You actually add a property to the ConditionArray, not elements. While JSON.stringify converts to string only elements of the ConditionArray. For example:
var arr = new Array;
arr['str'] = 'string';
console.log(arr.length) //outputs 0
You need to use an Object instead of Array
If you change the first line to
var ConditionArray = new Object();
you will achieve the desired outcome.
If for some reason you cannot convert your array into object, for instance you are working on a big framework or legacy code that you dont want to touch and your job is only to add som feature which requires JSON API use, you should consider using JSON.stringify(json,function(k,v){}) version of the API.
In the function you can now decide what to do with value of key is of a specific type.
this is the way how I solved this problem
Where tblItemsTypeform is array and arrange is de index of the array
:
let itemsData = [];
for(var i = 0; i <= this.tblItemsTypeform.length -1;i++){
let itemsForms = {
arrange: i,
values: this.tblItemsTypeform[i]
}
itemsData.push(itemsForms)
}
And finally use this in a variable to send to api:
var data = JSON.stringify(itemsData)
I have a problem delivering assiciative arrays to an injected script.
Global.html
var settings = new Array();
settings["accountID"] = safari.extension.settings.getItem("accountID");
settings["accountName"] = safari.extension.settings.getItem("accountName");
settings["accountEmail"] = safari.extension.settings.getItem("accountEmail");
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("settingsArray", settings);
script.js
switch (msgEvent.name) {
case "settingsArray":
var settings = new Array();
settings = msgEvent.message;
console.log("accountID: " + settings["accountID"]);
break;
When I do it with "normal" arrays, it works fine!
But when delivering associative arrays, I always get "undefined" when calling eg. settings["accountID"]
Does anyone have an idea what's wrong?
You're using arrays when you should be using objects.
var settings = new Array(); // Wrong
var settings = {}; // Right (and better than "new Object()")
You are unnecessarily using the string form of property access.
settings["accountID"] = …; // Works, but too much typing
settings.accountID = …; // Exact same functionality
You only need to use the bracket notation when getting/setting property values if the property name is not a valid JavaScript identifier (e.g. foo["holy!*#$! it works"] = true) or if you need to construct the property name from a variable (e.g. foo["account"+n] = "active";).
You are creating new objects and then throwing them away.
var settings = new Array(); // Makes a new array referenced by a variable
settings = msgEvent.message; // Discards the array and changes the variable
// to reference a new object
Within a javascript app, we're calling a sproc on SQL Server 2005 that's returning a resultset with 3 columns of type: bigint, varchar(20), and varchar(MAX).
When calling the sproc and reading the results, there's no exception being thrown. When enumerating the resultset:
var dbConn = DatabaseConnectionFactory.createDatabaseConnection("1","2","3","4");
var myResultSet = dbConn.executeCachedQuery("EXEC MySproc");
dbConn.close();
var myString = myResultSet.getString("MyVarcharMaxField");
When examining the value of myString, the value is not what I expect:
javax.sql.rowset.serial.SerialClob#15b4206
Any idea how to get the real value of the field?
Calling another method other than getString() maybe?
Varchar(MAX) is a CLOB (Character Large Object) data type. Here's some code you can use in order read its contents.
var clobField = myResultSet.getClob("MyVarcharMaxField");
var strOut = new java.lang.StringBuffer();
var aux = new java.lang.String();
// RTF data conversion
var kit = new Packages.javax.swing.text.rtf.RTFEditorKit();
var doc = new Packages.javax.swing.text.DefaultStyledDocument();
kit.read(clobField.getAsciiStream(), doc, 0);
var text = doc.getText(0, doc.getLength());
var myString = text.toString();