Jscript Tutorial Wolfgang Unger and Tobias Sommer



Yüklə 0,62 Mb.
Pdf görüntüsü
səhifə17/21
tarix07.11.2018
ölçüsü0,62 Mb.
#78679
1   ...   13   14   15   16   17   18   19   20   21

Chapter 12

Advanced JScripts

The JScripts provided in this chapter are longer and therefore not commented out in great

detail anymore. It needs some experience to understand them. Nevertheless, with the gained

knowledge of the preceding chapters it shoud be possible to understand and use them.

12.1


Write spectrum data to an Excel-Sheet

Before running the script, open a random spectrum in DOASIS. While the script is executed,

Excel will automatically launch and a new worksheet will be created. In the first column

of the worksheet, the channels of the spectrum will be written and in the second one the

intensity.

65



import System;

import DoasCore.Spectra;

// Start Excel and get Application object.

var oXL = new ActiveXObject("Excel.Application");

// Make the Excel program visible.

oXL.Visible = true;

// Get a new workbook and add a worksheet

var oWB = oXL.Workbooks.Add();

// Make the opened worksheet ready for modifying

var oSheet = oWB.ActiveSheet;

// Add table headers going cell by cell.

oSheet.Cells(1, 1).Value = "Pixel";

oSheet.Cells(1, 2).Value = "Current";

// put the intensity values of the current spectrum into

// the Excel sheet

var i;


for(i = 0; i < Specbar.CurrentSpectrum.NChannel; i++)

{

oSheet.Cells(2 + i, 1).Value = i;



oSheet.Cells(2 + i, 2).Value = Specbar.CurrentSpectrum.Intensity[i];

Console.WriteLine("Put pixel " + i + " to Excel sheet");

}

Console.WriteLine("Ready.");



12.1.1

A word on ActiveXObjects

In the example above the command var oXL = new ActiveXObject("Excel.Application")

is used. It creates an “Application Object” which in this case is of the ”type” Excel. This

object is put into the variable oXL. We have to imagine, that this variable contains some-

how the whole Excel program. By using it’s properties like e.g. “ActiveSheet” or “Visible”

and methods you can work in Excel ”through the channel” of the variable oXL. Under

http://msdn2.microsoft.com/en-us/library/bb149137.aspx

you can find a summary

of all members of such an Application Object. In a similar way you could connect e.g. to

Outlook.

ActiveXObjects are often used to get an interface to other programs. Here are some more

examples for “ActiveXObjects”, which can be used instead of “Excel.Application“

• Scripting.FileSystemObject

• WinDoasMotor.DoasMotor

• WinDoasMath.DoasMath

66



12.2

Exception handling

ßindextry..catch..finally JScript provides the possibility to handle exceptions by putting

commands which might cause errors in the “try”-block of a so called try/catch/finally en-

vironment. If any command of the code enclosed in the “try”-block causes an error, the

entire


block will be skipped and instead the content of the “catch”-block is executed. If

no error occurs in the “try”-block, the “catch” block will not be executed. The content of

the “finally” block is executed no matter whether an error occurred in the “try”-block or

even in the “catch”-block or not. In the following example, the type of the error is displayed

in a pop up window. By pressing “yes” or “no”, the user can either accept the error and

continue the script or abort it.

67



import System;

import DoasCore.Spectra;

// This namespace provides the class "OldUI"

import DoasCore.HMI;

// Open an empty spectrum in the "Specbar"

var Spec = Specbar.GetSpectrum("MeasSpectrum");

// Create an instance of "OldUI"-class which contains the method "MessageBoxYesNo"

// to create a checkbox

var checkbox = new OldUI();

var answer;

function main()

{

// In the "try"-block, a spectrum should be loaded into the empty spectrum



// "Spec".

If the file "C:

\Spectrum.sp2" exists, the spectrum will be

// opened and the message "Spectrum opened!" is displayed.

If the file

// does not exist, the entire "try"-block is skipped and the "catch"-block

// is executed.

try


{

Spec.Open("C:

\\Spectrum.sp2");

Console.WriteLine("Spectrum opened!");

}

catch(e)


{

// Show a pop up window including a "yes/no" button with the title

// "An error occurred!" and displaying the reason for

// the error (e).

If "yes" is pressed the method returns "true", if "no"

// is pressed it returns "false"

answer = checkbox.MessageBoxYesNo(e+" Continue?","An error occurred!");

// If "answer = true", then continue, else exit the "main()"-function

// and thus the entire script

if(answer)

{

Console.WriteLine("Script is continued!");



}

else


{

Console.WriteLine("Script is aborted!");

// Exit the "main()"-function

return;


}

}

finally



{

Console.WriteLine("Always execute this part!");

}

Console.WriteLine("End of script!");



}

// Run the main function

main();

68



Remarks

• For the first time in this manual a function is declared here. Making use of functions is

a very common and convenient style of programming. The program itself only starts

in the last line:

main();

calling the function “main()” which has been declared in the beginning. In this script



the function “main()” was used in order to be able to quit the script by leaving the

“main()”-function, if the user clicks on “no” when the checkbox appears.

• The “finally”-block is executed even though “no” is pressed in the checkbox and thus

the “return” command is called. However the command

Console.WriteLine("End of script!")

is not executed in that case.

• It is advisable to be very careful with the usage of the “try/catch” routine. If the error

message is not displayed, the program might pretend to work well even though it only

jumped over parts of the code.

• More information about checkboxes can be found in the Programming Documentation

of DOASIS by searching for “OldUI”.

12.3


Evaluate data with a fit scenario file

This is a more elaborate example of how to evaluate your data using a fit scenario file. It

was suggested by Stefan Kraus, the programmer of DOASIS.

/* EvaluateExample.js\\

This example script gives an example about how to evaluate existing

spectra using a already given fit scenario.

Note: To run the example its necessary to have already some spectra

saved as


C:\scanTest

and that you have an existing fit scenario file

C:\scanTest\EvaluateExample.fs

Author: Stefan Kraus

Version: 1.0 @ 10/28/2003 */

import System;

// use the System libary of the .NET framework

import DoasCore;

// use the DoasCore library

import DoasCore.Device;

// use the Device namespace

import DoasCore.IO;

// use the Input-/Output namespace

import DoasCore.Script;

// use the Script namespace

import DoasCore.Spectra;

// use the Spectra namespace

// create the spectrograph device object

var spectrograph = new Spectrograph();

// create the AutoFileName object to store the files

69



Yüklə 0,62 Mb.

Dostları ilə paylaş:
1   ...   13   14   15   16   17   18   19   20   21




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə