14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

500 Extending JMP Chapter 14<br />

OLE Automation<br />

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Source As Range)<br />

Counter = Counter + 1<br />

‘Save the control chart to a PNG every time 5 elements get updated<br />

If (Counter Mod 5 = 0 Or Counter = 1) Then<br />

‘If the Control Chart has not been created yet, do so<br />

If Not (ChartOpen) Then<br />

Set CChart = JMPDoc.CreateControlChart ‘create chart<br />

CChart.LaunchAddProcess "Column 1" ‘Add column<br />

CChart.LaunchAddSampleUnitSize 5<br />

CChart.LaunchSetChartType jmpControlChartVar<br />

CChart.Launch ‘launch the chart<br />

ChartOpen = True 'set flag to remember that a chart is open<br />

End If<br />

CChart.SaveGraphicOutputAs "C:\ControlChart.png", jmpPNG<br />

End If<br />

End Sub<br />

The Workbook_Open subroutine is called when the Excel table is initially loaded. It initializes some<br />

variables, starts JMP, and tells JMP to open (through ODBC) the same Excel file that is currently loaded<br />

into Excel 2007. Note that JMP opens the Excel file as a database object rather than opening it as a file. This<br />

is necessary because JMP does not open a file that is already open in another application.<br />

The Workbook_Change event is generated every time a user changes the data in any cell in any worksheet<br />

in the workbook. This sample assumes that there is only one active worksheet in the workbook. The first<br />

time the user changes a cell value in the worksheet, the Workbook_Change subroutine creates a Control<br />

Chart in JMP using the current data table.<br />

In this sample, the Workbook_change subroutine also creates a PNG graphic file of the Control Chart<br />

output and updates it on the disk every fifth time a change is made to the workbook. This just gives some<br />

ideas on how Excel events and JMP automation can be used together to create output.<br />

Finally, the Workbook_BeforeClose subroutine is invoked when the Excel workbook is closed, but before<br />

the window goes away. The code within this subroutine instructs JMP to close down as well.<br />

Note that there are some limitations in this method. This example is good if the only activities that occur<br />

with the data are additions or changes. The Excel Worksheet_Change event is very limited in the reporting<br />

that it provides. In particular, cell-by-cell updating of a JMP data table can be difficult in instances where<br />

deletion, drag and drop, or block replication needs support.<br />

If these are problem cases, it is probably better to rely on a brute-force approach. One way is to reload the<br />

data into JMP every time a certain number of changes occur. An example is shown here.<br />

Private Sub Workbook_SheetChange(ByVal Sh as Object, ByVal Source as Range)<br />

Counter = Counter + 1<br />

If (Counter Mod 10 = 0) Then<br />

‘If there is a previous chart of Table opened, close it first<br />

If(DocOpen) Then<br />

JMPDoc.Close False, ""<br />

CChart.CloseWindow<br />

End If

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!