Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with Excel ranges 1

Status
Not open for further replies.

stevesmith69

Technical User
Mar 13, 2009
14
GB
Hello, I'm fairly new to Javascript and excel and have struggled with this for days now. No amount of Googling has revealed the answer to me.

How do I access the values in a named range in Excel using Javascript?

I can see how to access a single value in a known location e.g.

var1 = Excel.Workbooks.Open("C:\\TEST.xls").ActiveSheet.Cells(1,1).Value;

how do I retrieve the values in a named range e.g. "MyRange" and assign them to variables?

I can retrieve the address of the range but cant see how that helps me to loop through the values in the range and assign them. I have played with for in statements to try a for each approach to the range but can't get that to work.

Any help appreciated.

Thank you.
 
Glad to give you the challenge. :)

Many thanks for all your help Dan.
 
I'm really sorry Dan, but I can't get that to work. jsRangeArray is still null or not an object. Perhaps I'm not using it correctly - could you throw up your code please?
 
[0] I reference to your post of 13 Mar 09 8:34 only (the rest I ignore).

[1] It is not clear what you meant by prop, blah[prop]. If you mean the cell's address and its value, it is this.
[tt]
var s = '';
[red]//[/red]for (props in blah) {
[red]//[/red]s += props + ': ' + blah[props] + '\n';
[red]//[/red]}
[blue]for (var i=0;i<blah.cells.count;i++) {
s+=blah.cells(i+1).address(true) + " : " + blah.cells(i+1).value + "\n";
}[/blue]
alert(s);
[/tt]
 
Odd that it's not working for you. Here's my cut-down code, which shows '4':

Code:
<html>
<head>
	<script type="text/javascript">
		var Worksheet = 'C:\\test.xls';
		var Excel = new ActiveXObject('Excel.Application');

		Excel.Visible = false;
		var Excel_file = Excel.Workbooks.Open(Worksheet);

		var blah = Excel_file.ActiveSheet.Range('MyRange');
		var jsRangeArray = new VBArray(blah.Value).toArray();
		alert(jsRangeArray.length); 	// Should show 4

		Excel.ActiveWorkbook.Close(true);
		Excel.Application.Quit();
		Excel = null;
	</script>
</head>

<body>
</body>
</html>

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Yes, I too got a length of 4 for the array but still get null or not an object error when referencing the contents of the array.
 
Then I'd say you're doing something strange to read the data from the array. With the following, I see 10, 20, 30, 40:

Code:
<html>
<head>
	<script type="text/javascript">
		var Worksheet = 'C:\\test.xls';
		var Excel = new ActiveXObject('Excel.Application');

		Excel.Visible = false;
		var Excel_file = Excel.Workbooks.Open(Worksheet);

		var blah = Excel_file.ActiveSheet.Range('MyRange');
		var jsRangeArray = new VBArray(blah.Value).toArray();
		alert(jsRangeArray.length); 	// Shows 4
		alert(jsRangeArray[0]);			// Shows 10
		alert(jsRangeArray[1]);			// Shows 20
		alert(jsRangeArray[2]);			// Shows 30
		alert(jsRangeArray[3]);			// Shows 40

		Excel.ActiveWorkbook.Close(true);
		Excel.Application.Quit();
		Excel = null;
	</script>
</head>

<body>
</body>
</html>

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Me too now!

Really pleased to get this working. Many thanks for all your help Dan. I would never in a million years of cracked this. Much appreciated.

Steve.

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top