| From JScript Arrays To VB Arrays |
While working writing JScript code to interact with COM objects, it may be necessary to pass an array of strings or other data types. Unfortunately, due to the design of the method, JavaScript or JScript arrays may not be accepted. Therefore it is necessary to find an easy way to convert a JavaScript array into a VBArray.
You may download or use the following code in your JScript to provide a function to translate a JScript array into a VBArray:
// Returns the array as a VBArray.
Array.prototype.toVBArray = function()
{
var dict = new ActiveXObject("Scripting.Dictionary");
for(var i = 0, len = this.length; i < len; i++)
dict.add(i, this[i]);
return dict.Items();
};
The following example will show you how to use the method defined above:
/*
.
.
.
*/
// Create the PivotTable.
var pt = wb.PivotCaches().Add(/*SourceType:=*/xlDatabase,
/*SourceData:=*/ws.UsedRange)
.CreatePivotTable(/*TableDestination:=*/wsPT.Range("A3"));
pt.MergeLabels = true;
pt.AddFields(["Manufacturer", "Cost Center"].toVBArray());
/*
.
.
.
*/
Powered by jPaq.