2.42.20 SetData


Description

Put two dimensional data into a specified Origin worksheet starting at a specified column and row.

This method is the same as Application.PutWorksheet.

Syntax

VB: Function SetData(data As ByVal Object, [ nRowOffset As ByVal Object ], [ nColOffset As ByVal Object ] ) As Boolean
C++: bool SetData(_variant_t data, _variant_t nRowOffset, _variant_t nColOffset )
C#: bool SetData(var data, var nRowOffset, var nColOffset )

Parameters

data
A two-dimensional safearray containing the data that will be put into the worksheet.
nRowOffset
Zero based index of the first row, in the worksheet, to receive the data. Default is zero. Use -1 to append the data.
nColOffset
Zero based index of the first column, in the worksheet, to receive the data. Default is zero. Use -1 to append the data.

Return

Returns true if successful else false.

Remark

Examples

The following examples will connect to the currently running Origin and put data into the active worksheet.

See examples at Column.SetData for more details about buffering data of different types.

VBA

Set numeric data into an Origin worksheet.

Public Sub SetNumericData()
    Dim app As Origin.ApplicationSI
    Dim Wks As Origin.Worksheet
    Dim Data(1 To 100, 1 To 3) As Double
    Dim ii As Integer
    'Set the 2D numeric array, which has 100 rows and 3 cols
    For ii = 1 To 100
        Data(ii, 1) = ii * 0.01 - 1.23
        Data(ii, 2) = ii / 12.8 - 0.75
        Data(ii, 3) = ii + 0.123
    Next
 
    Set app = New Origin.ApplicationSI     'Connect to the running Origin
    Set Wks = app.FindWorksheet("")        'Select the active worksheet
 
    'Send Numeric values to Origin
    Wks.SetData (Data)

    'Create a new graph page using Origin template
    Dim glay As Origin.GraphLayer
    Dim dr As Origin.DataRange
    Dim dp As Origin.DataPlot
    Set glay = Wks.Application.GraphPages.Add("Origin").Layers(0)
    Set dr = Wks.NewDataRange(0, 0, -1, 2) 'create range to be plotted, all rows,A,B,C
    Set dp = glay.DataPlots.Add(dr, IDM_PLOT_LINE)
    
    'Save the project to Origin User folder
    app.Save (app.Path(APPPATH_USER) + "COMServerExamples_WN.opj")
 
End Sub

Set text data into an Origin worksheet.

Public Sub SetTextData()
    Dim app As Origin.ApplicationSI
    Dim Wks As Origin.Worksheet
    Dim tData(1 To 100, 1 To 2) As String
    Dim ii As Integer
    'Set the 2D string array, which has 100 rows and 2 cols
    For ii = 1 To 100
        tData(ii, 1) = "abc" + Str(ii)
        tData(ii, 2) = "def" + Str(ii + 3)
    Next
 
    Set app = New Origin.ApplicationSI
    Set Wks = app.FindWorksheet("")
 
    'Send Text to Origin
    Wks.SetData (tData)
    
    'Save the project to Origin User folder
    app.Save (app.Path(APPPATH_USER) + "COMServerExamples_WT.opj")
 
End Sub

The next example simulates real-time data being sent to the active worksheet.

Private Sub SimulateRealTimeData()
   
    Const NUM_BLOCKS = 10000
    Const NUM_PTS_PER_BLOCK = 100

    Dim app As Origin.ApplicationSI
    Dim wks As Origin.Worksheet
    Dim block As Integer
    Dim data(1 To NUM_PTS_PER_BLOCK, 1) As Double
    Dim point As Integer
    Dim startSecs As Single
    Dim row As Integer
    Dim bRet As Integer
    
    Set app = New Origin.ApplicationSI
    Set wks = app.FindWorksheet("") ' active worksheet
    If wks Is Nothing Then
        MsgBox ("Specified Worksheet not found")
    Else
    
    row = 0
    For block = 1 To NUM_BLOCKS
        ' generate some random data
        For point = 1 To NUM_PTS_PER_BLOCK
            data(point, 0) = Rnd(1)
            data(point, 1) = Rnd(1)
        Next point
        
        ' send data to worksheet
        bRet = wks.SetData(data, row)
        
        ' calc target row for next block
        row = row + NUM_PTS_PER_BLOCK

        ' pause before sending next block of data
        startSecs = Timer
        Do While Timer < startSecs + 2
            DoEvents
        Loop
    Next block
    
    End If
    
End Sub

C#

Set numeric data into an Origin worksheet.

static void SetNumericData()
{
	// Create some data to put into the worksheet.
	float[,] data = new float[100, 3];
	for (int i = 0; i < 100; i++)
	{
		data[i, 0] = (float)(i * 0.01 - 1.23);
		data[i, 1] = (float)(i / 12.8 - 0.75);
		data[i, 2] = (float)(i + 0.123);
	}

	// Connect to Origin.
	Origin.ApplicationSI originApp = new Origin.ApplicationSI();

	// Get the active worksheet.
	Origin.Worksheet wks = originApp.FindWorksheet("");

	// Put the data into the worksheet starting at column 1 and row 1.
	wks.SetData(data, 0, 0);
}

Set text data into an Origin worksheet.

static void SetTextData()
{
	// Create some data to put into the worksheet.
	string[,] data = new string[100, 2];
	for (int i = 0; i < 100; i++)
	{
		data[i, 0] = "abc" + i.ToString();
		data[i, 1] = "def" + (i + 3).ToString();
	}

	// Connect to Origin.
	Origin.ApplicationSI originApp = new Origin.ApplicationSI();

	// Get the active worksheet.
	Origin.Worksheet wks = originApp.FindWorksheet("");

	// Put the data into the worksheet starting at column 1 and row 1.
	wks.SetData(data, 0, 0);
}

VC++

The following code will run a new instance of Origin and send data to a specified worksheet

#include "stdafx.h"
#include <afxdisp.h>        // MFC classes, as to use COleSafeArray
//make sure Origin8.tlb in the same folder as this program
#import "Origin8.tlb" rename_namespace("Origin")
using namespace Origin;
 
#define	NUM_COLS 4
#define NUM_ROWS 10
#define	PAGE_TYPE_WKS	2
#define	PAGE_VISIBLE	2 
 
bool sendDataToWks()
{
	Origin::IOApplicationPtr pOrigin;
	pOrigin.CreateInstance(__uuidof(Application)); //use __uuidof(ApplicationSI) will connect to a running Origin instance
	pOrigin->PutVisible(Origin::MAINWND_SHOW_BRING_TO_FRONT);
 
	pOrigin->CreatePage(PAGE_TYPE_WKS, "Data", "w", PAGE_VISIBLE); //"w" is template name while "Data" as worksheet page name
	Origin::WorksheetPagePtr pPage = pOrigin->GetActivePage();
	pPage->Activate();
 
	Origin::WorksheetPtr pwks = pPage->GetLayers()->Add("abc"); //new worksheet name is abc
	pwks->Activate();
 
	COleSafeArray csarr;
	DWORD dwBound[] = {NUM_ROWS, NUM_COLS}; //declare the array as NUM_ROWS*NUM_COLS
	csarr.Create(VT_UI2, 2, dwBound);
 
	long nIndices[2];
	USHORT uData;
	for ( int ii = 0; ii < NUM_ROWS; ii++ )
	{
		for ( int jj = 0; jj < NUM_COLS; jj++) 
		{
			nIndices[0] = ii;
			nIndices[1] = jj;
			uData = (ii + jj)%65536;
			csarr.PutElement(nIndices, &uData);
		}
	}
 
	pwks->SetData(csarr, 0, 0);
 
 	System::Console::ReadKey();//press key will exit Origin and this instance
	pOrigin->Exit();
	return true;
}

Version Information

8.0SR2

See Also

GetData | Application.PutWorksheet | Application.GetWorksheet | Column.SetData | Column.GetData