Accessing Excel's Object Model from Javascript - javascript

I have excel as an activeX object in javascript. I seem to be missing something with reards to how to interact with the object model from there. My watch window shows the value of the "Value" property of the range I am trying to pull data from as "undefined" when I try to assign "range.Value" to an array.
Unfortunately I am unable to update the outdated browsers on my machine at work so I cannot upload pictures.
My script:
function open_files(A, B, C)
{
var excel = new ActiveXObject("Excel.Application");
excel.Visible=true;
excel.DisplayAlerts = false;
var wbA = excel.Workbooks.Open(document.getElementById(A).value);
var wbB = excel.Workbooks.Open(document.getElementById(B).value);
var wbC = excel.Workbooks.Open(document.getElementById(C).value);
excel.EnableEvents = false;
excel.ScreenUpdating = false;
excel.Calculation = -4135 //xlCalculationManual enumeration;
var wb_collection = [wbA, wbB, wbC];
excel.Application.Run("'" + wbA.name + "'" + '!update_links');
var CLIN_list = [wbA.Sheets("Control Form").Range("B62:B141").value(1)]
for (i = 0; i = CLIN_list.length; i++)
{
if (CLIN_list(i) > 0)
{
var CLIN_list_count = i
}
}
var decrement_range_start = wbA.Sheets("Fee & Decrement Table").Range("AJ14")
//for (i = 0; i < 80; i++){
//Sheets("Fee & Decrement Table").Cells(decrement_range_start.column+i
// Model Setup for VBA
wbA.Sheets("CONTROL FORM").Activate
wbA.Sheets("CONTROL FORM").OLEObjects("TextBox21").Object.Text = wbB.fullname
wbA.Sheets("CONTROL FORM").OLEObjects("TextBox22").Object.Text = wbC.fullname
excel.Application.Run("'" + wbA.name + "'" + '!Run_JPO');

I found an answer on another forum. A Range cannot be assigned directly to a js array, it has to be converted. The line below works to fill my CLIN_list variable.
var CLIN_list = new VBArray(wbA.Sheets("Control Form").Range("B62:B141").value).toArray();

Related

Writing from a custom type to an array in Javascript

I've been stuck for a few days now on this. I can get my albums to write to the console but I can't figure out how to get them to write into an array. The code works so when I click "Show Albums" button, it returns nothing other than [Object, object]. Can anyone help me figure out how to write the new albums to the array so it displays properly? Thanks in advance.
(function() {
var name = document.getElementById("name");
var year = document.getElementById("year");
var showbutton = document.getElementById("showlist");
var submitbutton = document.getElementById("submit");
var validate = document.getElementById("validate");
var albumlist = [];
var albums = function album(name, year) {
this.name = name;
this.year = year;
this.album = function() {
return ((this.name + ", " + this.year));
};
}
albums.prototype.genre = "rock";
var album1 = new albums("album1", "1999");
var album2 = new albums("album2", "2000");
var album3 = new albums("album3", "2001");
var album4 = new albums("album4", "2002");
var album5 = new albums("album5", "2003");
albumlist.push(album1, album2, album3, album4, album5);
showbutton.onclick = function() {
for (var i = 0; i < albumlist.length; i++) {
console.log(albumlist[i]);
}
document.getElementById("myalbums").innerHTML = albumlist.sort() + "<br/>";
}
document.getElementById("myalbums").innerHTML = albumlist.sort() + "<br/>";
Take a look at what this line does. This takes an array, which is an object with many functions, keys, etc, and tries to plot in on the page.
It will not be possible, since javascript doesn't know how to turn that array into a string.
For you to do that, you have to first create the string you want and than plot it on the page.
You need to iterate through the items and generate a string, for example:
var names = "";
for(var i = 0; i < albumlist.length; i++){
names += albumlist[i].name + ' ';
}
and then replace it on the html:
document.getElementById("myalbums").innerHTML = names + "<br/>";

Accessing Stored Object

I have an object "Driver" defined at the beginning of my script as such:
function Driver(draw, name) {
this.draw = draw;
this.name = name;
}
I'm using this bit of JQuery to create new drivers:
var main = function () {
// add driver to table
$('#button').click(function ( ) {
var name = $('input[name=name]').val();
var draw = $('input[name=draw]').val();
var draw2 = "#"+draw;
var name2 = "driver"+draw
console.log(draw2);
console.log(name2);
if($(name2).text().length > 0){
alert("That number has already been selected");}
else{$(name2).text(name);
var name2 = new Driver(draw, name);}
});
That part is working great. However, when I try later on to access those drivers, the console returns that it is undefined:
$('.print').click(function ( ) {
for(var i=1; i<60; i++){
var driverList = "driver"+i;
if($(driverList.draw>0)){
console.log(driverList);
console.log(driverList.name);
}
If you're interested, I've uploaded the entire project I'm working on to this site:
http://precisioncomputerservices.com/slideways/index.html
Basically, the bottom bit of code is just to try to see if I'm accessing the drivers in the correct manner (which, I'm obviously not). Once I know how to access them, I'm going to save them to a file to be used on a different page.
Also a problem is the If Statement in the last bit of code. I'm trying to get it to print only drivers that have actually been inputed into the form. I have a space for 60 drivers, but not all of them will be used, and the ones that are used won't be consecutive.
Thanks for helping out the new guy.
You can't use a variable to refer to a variable as you have done.
In your case one option is to use an key/value based object like
var drivers = {};
var main = function () {
// add driver to table
$('#button').click(function () {
var name = $('input[name=name]').val();
var draw = $('input[name=draw]').val();
var draw2 = "#" + draw;
var name2 = "driver" + draw
console.log(draw2);
console.log(name2);
if ($(name2).text().length > 0) {
alert("That number has already been selected");
} else {
$(name2).text(name);
drivers[name2] = new Driver(draw, name);
}
});
$('.print').click(function () {
for (var i = 1; i < 60; i++) {
var name2 = "driver" + i;
var driver = drivers[name2];
if (driver.draw > 0) {
console.log(driver);
console.log(driver.name);
}

Access XML Attribute Names in Extendscript

I'm trying to parse an xml Object in extendscript and especially deal with the Attributes. I know i can access xml attributes by
xmlObj.#attributename
and
xmlObj.attributes()
returns a list of all attributes, but I also need the attribute names not just the values. Is there anyway to get something like and associative array/object with names and values?
(I use extendscript for illustrator CS6)
thank you,
arno
The code below should get you going. Take also a look into the XMLElement Object.
var main = function() {
// create some xml and write it to file
var root = new XML("<root/>");
var child = new XML("<child/>");
child.#string = "Hello Attribute"; // jshint ignore:line
child.#num = 23; // jshint ignore:line
root.appendChild(child);
var file = new File("~/Desktop/test.xml");
var xml = root.toXMLString();
file.open("W");
file.write(xml);
file.close();
// get the current doc
var doc = app.activeDocument;
// import the xml
doc.importXML(file);
// get the elements
var xmlroot = doc.xmlElements[0];
var xmlchild = xmlroot.xmlElements[0];
// loop all attributes of element "child"
// and write them into the console
for (var i = 0; i < xmlchild.xmlAttributes.length; i++) {
var attr = xmlchild.xmlAttributes[i];
$.writeln(attr.name);
}
};
main();
i've found a way solve it with regular Expressions
function getAttributes(xml_node_str) {
// select the start tag <elem >
var reg_exp = /<[^>]*>/;
var start_tag_str = reg_exp.exec(xml_node_str);
// extract the attributes
reg_exp = /[^"\s]*="[^"]*"/g;
var result;
var attributes = [];
while ((result = reg_exp.exec(start_tag_str)) !== null) {
// the attribute (name="value")
var attr = result[0];
// array containing name and "value"
var attr_arr = attr.split('=');
// delete the "'s
attr_arr[1] = attr_arr[1].substr(1, attr_arr[1].length - 2);
attributes.push(attr_arr);
}
return attributes;
}
I still parse the xml with Extendscripts/Illustrators xml-class and then extract the attributes manually
var xml = <root><obj a1="01" a2="02" ></obj></root > ;
var attributes = getAttributes(xml.obj.toXMLString());
for (var i = 0; i < attributes.length; i++) {
alert(attributes[i][0] + ' -> ' + attributes[i][1]);
}

ActiveXObject Number of rows counter

I have this piece of code:
function GetData(evt){
var adresses = new Array();
var j = 0;
var excel = new ActiveXObject("Excel.Application");
var fil = document.getElementById("file");
var excel_file = excel.Workbooks.Open(fil.value);
var excel_sheet = excel.Worksheets(1);
for(var i=2;i<500;i++){
var morada = excel_sheet.Range("E"+ i );
var localidade = excel_sheet.Range("C"+ i );
var pais = excel_sheet.Range("A"+i);
adresses[j] = (morada + ", " + localidade + ", " + pais);
j++;
}
for(var k = 0; k<j; k++) {
codeAddress(adresses[k]);
}
}
It receives an excel file and processes the data like I want. The thing is, it is very hard coded.
For instant, in this for:
for(var i=2;i<500;i++)
I am using 500, but I would like to use the number of rows in the sheet. I have already tried a few things like rows.count and whatever and I gave some alerts to see the results, but I just can't find the one who tells me the number of rows.
Anyone know how to do it?
Try that :
excel_sheet.UsedRange.Rows.Count
Does it works ?

sharepoint javascript on click shape

I need to add an onclick event to shapes from Visio in SharePoint, with JavaScript, like the vwaControl handler shapeselectionchanged but on click, is there any way I could do that?
I'm sorry about my English is not my native language.
I hope you can understand me.
I just did something similar.
You can use the shapeSelectionChangedHandler to handle clicks to. As far as I know there is no onClick functionality, but shapeSelectionChangedHandler works fine for me.
See:
Programming with Visio in SharePoint, create new Outlook meeting in ?Javascript?
See: http://msdn.microsoft.com/en-us/library/gg243427.aspx for guide to set it up with Content WebPart and so on.
Code I use, just add what you want in shapeSelectionChangedHandler = function(source, args) {}
<script language="javascript">
var app = Sys.Application;
app.add_load(onApplicationLoad);
// hold an instance of the Visio VWA control
var vwaControl;
var shapeSelectionChangedHandler = null;
function onApplicationLoad()
{
vwaControl= new Vwa.VwaControl("WebPartWPQ4");
vwaControl.addHandler("diagramcomplete", onDiagramComplete);
vwaControl.addHandler("shapeselectionchanged", shapeSelectionChangedHandler);
}
function onDiagramComplete()
{
var vwaPage = vwaControl.getActivePage();
vwaPage.setZoom(35); // force the initial zoom level
}
shapeSelectionChangedHandler = function(source, args)
{
// get the selected shape from the shapes on the page
var vwaPage = vwaControl.getActivePage();
var vwaShapes = vwaPage.getShapes();
var shape = vwaShapes.getItemById(args);
// get the data to display for the selected shape
var data = shape.getShapeData();
var strRoomName = "";
var strFloorNumber = "";
var strCapacity = "";
var strStatus = "";
for (var j = 0; j < data.length; j++)
{
if (data[j].label == "RoomName")
{
strRoomName = data[j].value;
continue;
}
if (data[j].label == "FloorNumber")
{
strFloorNumber = data[j].value;
continue;
}
if (data[j].label == "Capacity")
{
strCapacity = data[j].value;
continue;
}
if (data[j].label == "RoomStatus")
{
strStatus = data[j].value;
continue;
}
}
// get the selected state input and set its value
var inputRoomName = document.getElementById('strRoomName');
inputRoomName.value = strRoomName;
var inputFloorNumber = document.getElementById('strFloorNumber');
inputFloorNumber.value = strFloorNumber;
var inputCapacity = document.getElementById('strCapacity');
inputCapacity.value = strCapacity;
var inputStatus = document.getElementById('strStatus');
inputStatus.value = strStatus;
}

Categories

Resources