Tutorial Example: How to Access Microsoft Excel (Chart) using the Windows Scripting Host (JScript)


WSH (Window Scripting Host) is a powerful and handy scripting environment. It is installed by default and can be found on almost every windows versions since Win98. There are two programming languages you can use easily, which is JScript and VBScript. The scripts will be interpreted by cscript.exe (console) or wscript.exe (graphical, prefer dialogs).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
C:\Windows\system32>cscript/?
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
 
Usage: CScript scriptname.extension [option...] [arguments...]
 
Options:
 //B         Batch mode: Suppresses script errors and prompts from displaying
 //D         Enable Active Debugging
 //E:engine  Use engine for executing script
 //H:CScript Changes the default script host to CScript.exe
 //H:WScript Changes the default script host to WScript.exe (default)
 //I         Interactive mode (default, opposite of //B)
 //Job:xxxx  Execute a WSF job
 //Logo      Display logo (default)
 //Nologo    Prevent logo display: No banner will be shown at execution time
 //S         Save current command line options for this user
 //T:nn      Time out in seconds:  Maximum time a script is permitted to run
 //X         Execute script in debugger
 //U         Use Unicode for redirected I/O from the console
C:\Windows\system32>cscript/?
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Usage: CScript scriptname.extension [option...] [arguments...]

Options:
 //B         Batch mode: Suppresses script errors and prompts from displaying
 //D         Enable Active Debugging
 //E:engine  Use engine for executing script
 //H:CScript Changes the default script host to CScript.exe
 //H:WScript Changes the default script host to WScript.exe (default)
 //I         Interactive mode (default, opposite of //B)
 //Job:xxxx  Execute a WSF job
 //Logo      Display logo (default)
 //Nologo    Prevent logo display: No banner will be shown at execution time
 //S         Save current command line options for this user
 //T:nn      Time out in seconds:  Maximum time a script is permitted to run
 //X         Execute script in debugger
 //U         Use Unicode for redirected I/O from the console

The Microsoft Office products are very successful and among those, the excel spreadsheet is the most widely office software. You could interact with the core functions of the Office products by creating corresponding COM object extractions. For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//////////////////////////////////////////////////////////////////////////////////
//
// Excel Sample
//
 
var objEXL;
 
objEXL = WScript.CreateObject("Excel.Application"); // create Excel object
objEXL.Workbooks.Add;
objEXL.Cells(1,1).Value = 5;
objEXL.Cells(1,2).Value = 10;
objEXL.Cells(1,3).Value = 15
objEXL.Range("A1:C1").Select;
 
var chart = objEXL.Charts.Add();
objEXL.Visible = true;
chart.Type = -4100;
 
var rotate;
for(rotate = 5; rotate <= 180; rotate += 5) {     
    chart.Rotation = rotate; 
} 
for (rotate = 175; rotate >= 0; rotate -= 5) {
    chart.Rotation = rotate;
}
//////////////////////////////////////////////////////////////////////////////////
//
// Excel Sample
//

var objEXL;

objEXL = WScript.CreateObject("Excel.Application"); // create Excel object
objEXL.Workbooks.Add;
objEXL.Cells(1,1).Value = 5;
objEXL.Cells(1,2).Value = 10;
objEXL.Cells(1,3).Value = 15
objEXL.Range("A1:C1").Select;

var chart = objEXL.Charts.Add();
objEXL.Visible = true;
chart.Type = -4100;

var rotate;
for(rotate = 5; rotate <= 180; rotate += 5) {     
    chart.Rotation = rotate; 
} 
for (rotate = 175; rotate >= 0; rotate -= 5) {
    chart.Rotation = rotate;
}

The above will invoke excel application, create a chart and finally rotate it.

wsh-excel Tutorial Example: How to Access Microsoft Excel (Chart) using the Windows Scripting Host (JScript) beginner code code library implementation javascript Office Processing and ProcessingJS VBA vbscript windows windows command shell windows scripting host

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
396 words
Last Post: How To Use Compound Commands in Windows Command Shell
Next Post: How to Show System Environment Variables using VBScript/JScript under Window Scripting Host

The Permanent URL is: Tutorial Example: How to Access Microsoft Excel (Chart) using the Windows Scripting Host (JScript)

Leave a Reply