Lab Exercise 8.1 - Creating A Javascript Program: In-Sight Spreadsheets Advanced In-Sight Spreadsheets Advanced
Lab Exercise 8.1 - Creating A Javascript Program: In-Sight Spreadsheets Advanced In-Sight Spreadsheets Advanced
NOTE: This lab requires version 5.1 or later of In-Sight Explorer, with the Emulator running
as an In-Sight 5705. Only one image is used with the Emulator.
NOTE: A JavaScript Quick Reference Guide is provided at the end of this lab.
In this exercise, Participants will create a JavaScript program, starting with the JavaScript
template that will compute the average area of 234 squares and return it to the
spreadsheet using a Script function.
An ExtractBlobs tool will determine the areas of the squares, and the Script will reference
the ExtractBlobs structure in order to pass the results to the JavaScript program. The
program will calculate the average area and return it to the Script function in the 3. Insert an ExtractBlobs function. Set Number to Sort = 0 and make sure the
spreadsheet. Region is big enough to encompass all 234 white squares. Set other parameters
appropriately.
Spreadsheet
4. Insert a Script function into the spreadsheet. The Script Editor should pop up,
showing the JavaScript template.
Page 1 Page 2
In-Sight Spreadsheets Advanced Section 8 | Lab Exercise In-Sight Spreadsheets Advanced Section 8 | Lab Exercise
NOTE: Anything sandwiched between /* and */ is an optional comment for 6. The default name used in the template is “Script.” We could leave it at that, but let’s
readability, and not part of the code that will be run. Also, anything on a line after change the name to a more descriptive name: AvgBlobArea. Note that this name
“//” is a comment. is case sensitive.
Make this change on six lines of the template: lines 18, 26, 39, 62, 76, and 91.
Comment
Comment
Page 3 Page 4
In-Sight Spreadsheets Advanced Section 8 | Lab Exercise In-Sight Spreadsheets Advanced Section 8 | Lab Exercise
NOTE: If you were to exit the Editor at this point and reenter it, you would see an Now we have the result we want (avg). Finally, add a return so that the JavaScript
error message at the bottom of the Editor: returns the average value back to the spreadsheet.
#!ERR: TypeError: arg0.getNFound is not a function.
This would be because we have not yet referenced the ExtractBlobs tool in the
Script function in the spreadsheet.
8. Next, insert a repeating loop that will cycle through the 234 blob results, adding the
area of each to sum.
10. Click on the Test Script button at the bottom of the program window to see if there
are any errors in the program. If so, determine why and correct them.
11. When the Test Script runs without error, click on OK at the bottom of the JavaScript
Editor to return to the spreadsheet. It should now display the result, i.e., the
average blob area.
This tells the program to start with a loop index variable named idx set to 0.
The syntax
++idx
is shorthand for
idx = idx +1
Each time the loop executes, it will increment idx by 1, as long as idx is less than N.
(This means N times, since the loop starts at 0.) Each time the loop is repeated,
arg0.getArea(idx) will get the value of the area of the indexed blob. getArea is the Script (B4)
required name, and is case-sensitive.
Each iteration of the loop with add the blob area to the running sum; this is done by If you used an automatic threshold in your ExtractBlobs tool, your result may be slightly
sum += area different than shown. Why would that be?
this is shorthand for sum = sum+ area. __________________________________________________
When all iterations of the loop have competed, sum will be the sum of all the blob
areas. 12. Double click on the Script cell to reenter the JavaScript program.
9. Next, compute the average by dividing the sum by the total. You will see the same result displayed at the bottom of the JavaScript editor:
Page 5 Page 6
In-Sight Spreadsheets Advanced Section 8 | Lab Exercise In-Sight Spreadsheets Advanced Section 8 | Lab Exercise
13. We have used the JavaScript template, which has many things not essential to our The corresponding spreadsheet is:
program. An equivalent JavaScript program with just the essentials (no error
checking, no comments, etc.) might look like this:
NOTE: With multiple returned values, the Script cell now displays instead of
displaying the single returned value in the preceding example.
14. Our JavaScript program (as well as the stripped-down version) returns a single
value (avg), which also appears in the spreadsheet in the Script cell.
Suppose we wanted to return more than one value from the JavaScript program.
We can do this by using Get functions, one per value to be returned. We also need
to change the JavaScript program accordingly.
Suppose we want to return two values to the spreadsheet: the sum and average.
We can do this with a slightly different return statement:
Page 7 Page 8
In-Sight Spreadsheets Advanced Section 8 | Lab Exercise In-Sight Spreadsheets Advanced Section 8 | Lab Exercise
The Participant will utilize the following In-Sight Function to successfully complete this
exercise:
• Script
The Participant will work with the ArgumentsAndResults snippet that comes with In-Sight
Explorer. It uses a Script, one of whose inputs is a reference to a SortBlobs tool.
NOTE: This lab requires version 5.1 or later of In-Sight Explorer, with the Emulator running
as an In-Sight 5705.
This lab demonstrates the use of a Script with one result, which is returned into the cell
containing the Script function. No Get functions are used.
Page 9 Page 10
In-Sight Spreadsheets Advanced Section 8 | Lab Exercise In-Sight Spreadsheets Advanced Section 8 | Lab Exercise
4. Navigate to the folder containing the images and create a filmstrip. Play the first
image.
5. Adjust the Region in ExtractBlobs to include all the blobs. Observe what happens
Description of Snippet: on the image.
A4: ExtractBlobs (contains blob results sorted largest area first)
A9: SortBlobs (sorts blobs according to x,y pixel coordinates) in order to plot red
points where blobs are missing.
E13: Script(A9,B12,B13,G12)
NOTE: Read through the JavaScript program to get a sense of how it works. Even if
you are not familiar with JavaScript, you can read the comments. The number of
blobs found by the blob tool, called blobs.getNFound in the JavaScript program, is
used in lines 51 and 108.
NOTE: For this part of the lab there are two images containing grids of blobs, one
with all squares present, one with missing squares.
Page 11 Page 12
In-Sight Spreadsheets Advanced Section 8 | Lab Exercise In-Sight Spreadsheets Advanced Section 8 | Lab Exercise
7. Look at the JavaScript program. Can you identify the part that detects the missing
squares? The part that plots red points where blobs are missing? Lab Exercise 8.3 – Scripting with Multiple Gets for Results
8. Change the size of the grid of blobs by changing B12 to 11 and B13 to 14. Adjust Optional Lab, only if time permits
the Region in ExtractBlobs to cover the upper left part of the grid: 11 squares down
and 14 squares across. Observe that the tool still works. At the end of this lab exercise, Participants will be able to:
NOTE: If you tried to do the same thing using multiple blob tools (no Script or
• Change a spreadsheet to pass 12 input values to a Script instead of 10 values
looping), it would take hundreds of cells, and be much more difficult to change the
• Change a JavaScript program to calculate an additional value and return it to the
size of the grid.
spreadsheet
The Participant will utilize the following In-Sight Functions to successfully complete this
exercise:
• Script
• Get
The Participant will work with the ArgumentsAndResults snippet that comes with In-Sight
Explorer. It uses a Script to return four statistics calculated from a set of input values.
NOTE: This lab requires version 5.1 or later of In-Sight Explorer, with the Emulator running
as an In-Sight 5705. No images are used.
This lab demonstrates the use of a Script with a variable number of inputs, and four results
that are inserted in spreadsheet cells by four Get functions.
Page 13 Page 14
In-Sight Spreadsheets Advanced Section 8 | Lab Exercise In-Sight Spreadsheets Advanced Section 8 | Lab Exercise
Description of Snippet:
A4:A15 contain 10 numbers, generated by Rand (random number) functions.
D3: Script(A4:A15) – This executes JavaScript code written to calculate the
3. Open the Scripting category and drag the ArgumentsAndResults snippet into cell average, standard deviation, minimum, and maximum of the 10 numbers.
A2 of the spreadsheet. D4: Get(D3, “avg”) retrieves the average calculated by the Script
D5: Get(D3, “stddev”) retrieves the standard deviation calculated by the Script
D6: Get(D3, “min”) retrieves the minimum calculated by the Script
D7: Get(D3, “max”) retrieves the maximum calculated by the Script
NOTE: Each time the spreadsheet is executed, new random numbers are
generated by the Rand functions and sent to the Script.
4. Insert a Button function into cell B0 and change its trigger parameter to Manual.
NOTE: When running the Emulator, F5 or the toolbar trigger icon does not trigger
the spreadsheet.
5. Each time you click the Button, the spreadsheet will execute, the 10 input values
will change, and the average, standard deviation, minimum, and maximum will
update.
6. Add 2 more Rand functions in cells A16 and A17.
NOTE: This increases the number of values from 10 to 12. Each time you click the
button the spreadsheet will now process 12 values.
7. Double click on cell D3, which will display the JavaScript code.
NOTE: Read through the code to get a sense of how it works. Even if you are not
familiar with JavaScript, you can read the comments.
Page 15 Page 16
In-Sight Spreadsheets Advanced Section 8 | Lab Exercise In-Sight Spreadsheets Advanced Section 8 | Lab Exercise
8. Find the part of the JavaScript program that looks like this:
return { Lab Exercise 8.4 – Look at Additional Examples of Scripting
avg: avg,
stddev: stddev, Optional Lab, only if time permits
min: minAndMax.min,
max: minAndMax.max At the end of this lab exercise, Participants will be able to:
} • Analyze sample Scripts that:
} − Add two numbers
NOTE: The ‘return’ tells the Script to return the four values calculated elsewhere in − Compute the sum and mean of four numbers and return two outputs
the Script, naming the returned values avg, stddev, min, and max. The four Gets − Compute the mean and maximum of the height and width of blobs detected in an
in the spreadsheet use these same names. image
9. Modify the Script so that it also returns the ratio min/max. − From two blob results, return the blob results set that has 1) highest average
The return section will look like this: blob area if today's date is an even number, or 2) lowest average perimeter if
return { today’s date is an odd number
avg: avg, − Compare two dates and determine which occurs sooner
stddev: stddev, − Plot a polygon using randomly picked centroids from blobs in an image.
min: minAndMax.min,
max: minAndMax.max, The Participant will utilize the following In-Sight Functions to successfully complete this
ratio: minAndMax.min/minAndMax.max exercise:
} • Script
} • Get
10. Add another Get to the spreadsheet to display the ratio.
The Participant will use a job that is provided, named Scripting_GettingStarted.
The updated spreadsheet displays.
NOTE: This lab requires version 5.1 or later of In-Sight Explorer, with the Emulator running
as an In-Sight 5705. The only Script that requires an image is the last.
Page 17 Page 18
In-Sight Spreadsheets Advanced Section 8 | Lab Exercise In-Sight Spreadsheets Advanced Section 8 | Lab Exercise
2. This job contains a number of examples of Scripts, each described by comments in The Script will plot a polygon on the image similar to the image below.
the spreadsheet and each controlled by a CheckBox. NOTE: The crosshairs will be in green, the connecting polygon in red.
Page 19 Page 20