2.8 Using LabTalk to Get Worksheet Information


LabTalk is a high-level, full-featured programming language which has access to most of Origin's operation. So you can take the advantage of LabTalk to use it in LabVIEW to facilitate your work. This tutorial will show you how to run LabTalk scripts in LabVIEW to return the maximum numbers of the column and row that have data.

1. Run Origin. If Origin is already running, start a new project. Make sure that there is a Workbook with the name Book1 and a worksheet with the name Sheet1.

2. Add several columns in the worksheet, for example add 3 columns. Select a range of cells and right-click on it to select Fill Range With: Row Number.

3. Run LabVIEW and create a blank VI.

4. Turn to Block Diagram window, and choose OA_ConnectToOrigin VI OA ConnectToOrigin App.jpg and place it to the window.

5. Select and place OA_FindWorksheet VI OA FindWorksheet.jpg, and then wire the Origin.IOApplication and Error IO connectors to the OA_ConnectToOrigin VI.

6. Add a string constant and assign to it the string [Book1]Sheet1, and then wire it to the Name connector of OA_FindWorksheet VI.

7. Move the mouse to the Origin.Worksheet connector of the OA_FindWorksheet.vi. When the shape of the mouse cursor looks like Connector.png, right click on and choose Create/ Method/ Execute to create a Property Node. The Property Node will look like InvokeNodeExcute.png. Then wire reference connector to Origin.Worksheet connector. And wire Error IO connector to the corresponding connector too. Then add a string constant and input the following scripts into it. And then wire it to the LabTalkStr connector.

maxRows =wks.maxrows;
j=wks.ncols;
for(i=wks.ncols;i>0;i--) 
{
  get wcol(i) -e v1;
  if(v1>0) break;
  j=i-1;
};
maxCols = j;

8. Then we will try to get the values of two variables: maxRows and maxCols. Select All Functions/ Application Control from Functions palette and place Invoke Node VI InvodeNode.png on the diagram. Then wire reference connector to Origin.IOApplication connector of OA_FindWorksheet VI. And wire Error IO connector to the corresponding connector of the previous Invoke Node VI too. Right click on Method and choose Methods/LTVar(get) from the fly-out menu. Then add a string constant and set it to maxRows, and then wire it to the LabTalkStr connector. This VI should look like LTVar.png.

9. Select All Function/Numeric/ Conversion/ To Long Integer to add the To Long Integer vi ConvertToInte.png into the diagram. Wire its Number connector to the LVar connector of the InvodeNode. And remember to create an Indicate to its another connector.

10. Then repeat the steps 8~9 to get the maximum number of columns (Please remember to change the string value to maxCols.). The Block Diagram should look like:

LabTalk Return WKS Info.png

11. Click Run button to run this VI and then you can turn to Front Panel to see the results.