2.2.4.9.1 DataRange::Add

Description

Add a new subrange to the DataRange.

Syntax

int Add( LPCSTR lpcszName, LPCSTR lpcszRange )


int Add( LPCSTR lpcszName, Datasheet & ds, int nR1, int nC1, int nR2, int nC2 )


int Add( Datasheet & ds = NULL, int nC1 = 0, LPCSTR lpcszName = NULL, int nC2 = 0, int nR1 = 0, int nR2 = -1 )

Parameters

lpcszName
[input]The name of the subrange like "X", "Y", "Z", or "ED" for Y error. "X" is a general usage, if no need to specify data is XY, XYZ etc.
Use NULL to indicate a separator ("S"). When NULL, then ds can also be NULL.
lpcszRange
[input]range string like "[Book1]Data1!A:A"


lpcszName
[input]The name of the subrange like "X", "Y", "Z", or "ED" for Y error. "X" is a general usage, if no need to specify data is XY, XYZ etc.
Use NULL to indicate a separator ("S"). When NULL, then ds can also be NULL.
ds
[input]Worksheet or MatrixLayer in which range reside
nR1
[input]Row range begin
nC1
[input]Column range begin
nR2
[input]Row range end, use -1 to indicate end of column
nC2
[input]Column range end, if only one column then put in same value as nC1, if to end of worksheet use -1


ds
[input]Worksheet or MatrixLayer in which range reside
nC1
[input]Column range begin
lpcszName
[input]The name of the subrange like "X", "Y", "Z", or "ED" for Y error. "X" is a general usage, if no need to specify data is XY, XYZ etc.
Use NULL to indicate a separator ("S"). When NULL, then ds can also be NULL.
nC2
[input]Column range end
nR1
[input]Row range begin
nR2
[input]Row range end, use -1 to indicate end of column

Return

Returns the number of subranges in the DataRange after execution.


Returns the number of subranges in the DataRange after execution.


Returns the number of subranges in the DataRange after execution.

Examples

EX1

// construt data range for one column and then get data from range object to one vector
void DataRange_Add_Ex1(int nColIndex = 0)
{
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
    {
        out_str("Please keep a worksheet active with data");
        return;
    }  
    
    Column col(wks, nColIndex);
    if( !col )
    {
        out_str("The specified column does not exist.");
        return;
    }  
    
    string strRange;
    if( !col.GetRangeString(strRange) )
    {
        out_str("Fail to get range string.");
        return;    	
    }
    
    DataRange dr;
    dr.Add("X", strRange);
    
    vector vData;
    dr.GetData(&vData, 0); 
    
    if( vData.GetSize() > 0 )
    	printf("The first value of %dth column is %g\n", nColIndex+1, vData[0]);
    else
    	printf("No data in %dth column\n", nColIndex+1);
}

EX2

// construct XY data range from worksheet columns
void DataRange_Add_Ex2()
{
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
    {
        out_str("Please keep a worksheet active with data");
        return;
    }  
    
    Column colX(wks, 0);
    Column colY(wks, 1);    
    if( colX && colY )
    {
    	string strRangeX, strRangeY;
    	if( colX.GetRangeString(strRangeX) && colY.GetRangeString(strRangeY) )
    	{
    		XYRange dr;
    		dr.Add("X", strRangeX); // the range name should be X
    		dr.Add("Y", strRangeY); // the range name should be Y
    		
    		vector vx, vy;
    		dr.GetData(vy, vx);
    	}
    }    
}

EX3

// construct data range from worksheet selected range
void DataRange_Add_Ex3()
{
    Worksheet wks = Project.ActiveLayer();
    if( !wks )
        return;
 
    int c1, c2, r1, r2;
    int nRet = wks.GetSelection(c1, c2, r1, r2); // get the selected range
    if( WKS_SEL_NONE == nRet )
    {
        out_str("No selection on active worksheet!");
        return;
    }   
 
    DataRange dr;
    dr.Add("X", wks, r1, c1, r2, c2); //construct a data range object by selection
 
    vector vData; 
    dr.GetData(vData, 0); // get data from range object    
}

EX4

// get data from Origin matrix window
void DataRange_Add_Ex4()
{
    MatrixLayer matLayer = Project.ActiveLayer();
    if( matLayer )
    { 
    	DataRange     dr;
	    dr.Add("X", matLayer, 0, 0, -1, -1);
	    
	    matrix matData;
	    dr.GetData(matData);
	    printf("The number of row and col is %dx%d.\n", matData.GetNumRows(), matData.GetNumCols());
    }
}

EX5

// put data from matrix object to Origin matrix window
void DataRange_Add_Ex5()
{
    MatrixPage     matPage;
    matPage.Create("Origin");    
    MatrixLayer matLayer = matPage.Layers();
 
    DataRange     dr;
    dr.Add("X", matLayer, 0, 0, -1, -1);
    matrix matData = {{1,2,3}, {4,5,6}, {7,8,9}};
    dr.SetData(matData);
}

EX6

// construct multiple subranges from different workbook
// Before running, please make sure there are Book1 and Book2 in current project.
void DataRange_Add_Ex6()
{
	WorksheetPage wksPage1("Book1");
	WorksheetPage wksPage2("Book2");
	if( wksPage1 && wksPage2 )
	{
		Worksheet wks1 = wksPage1.Layers(-1); // get active sheet
		Worksheet wks2 = wksPage2.Layers(-1);
		
		DataRange dr;	
		// subrange 1
		dr.Add(wks1, 0, "X");
		dr.Add(wks1, 1, "Y");

		// subrange 2
		dr.Add(NULL); // add separator 		
		dr.Add(wks2, 0, "X");
		dr.Add(wks2, 1, "Y");
		
		DWORD dwDataRules = DRR_GET_DEPENDENT;
		vector vx1, vy1, vx2, vy2;
		dr.GetData(dwDataRules, 0, NULL, NULL, &vy1, &vx1); // get xy data from the 1st subrange
		dr.GetData(dwDataRules, 1, NULL, NULL, &vy2, &vx2); // get xy data from the 2nd subrange		
	}
}

Remark

Add a new subrange to the DataRange. The subrange is specified using a Worksheet or MatrixLayer object and beginning and ending row and a column index.

See Also

DataRange::AddInput,DataRange::SetRange

Header to Include

origin.h