1.6.1.1 Basic Workbook Operation

Create New Workbook

The Create method is used for creating new workbooks.

// create a hidden workbook using the STAT template
WorksheetPage wksPg;
wksPg.Create("STAT", CREATE_HIDDEN);

Open Workbook

If the workbook with data is saved (as extension of ogw), it can be opened by the Open method.

Worksheet wks;  // The Open method belongs to Worksheet
string strOGW = "D:\\Book1.ogw";  // Path of the workbook
wks.Open(strOGW);  // Open the workbook

Access Workbook

There are multiple ways to access an existing workbook. The Project class contains a collection of all the workbooks in the project. The following example shows how to loop through them.

foreach(WorksheetPage wksPg in Project.WorksheetPages)
    out_str(wksPg.GetName()); // output workbook name

You can also access a workbook by passing its index to the Item method of the Collection class.

WorksheetPage wksPg;
wksPg = Project.WorksheetPages.Item(2);
if(wksPg) // if there is a 3rd workbook
    out_str(wksPg.GetName()); // output workbook name

If the workbook name is known, this workbook can be accessed by passing its name to the class constructor.

WorksheetPage wksPg("Book1");
if(wksPg) // if there is a workbook named "Book1"
    wksPg.SetName("MyBook1"); // rename the workbook

Save Workbook

Origin allows you to save a workbook with data to a file (*.ogw), or as a template without data (*.otw), and for the workbook with analysis, it is able to be saved as an analysis template (*.ogw). And methods SaveToFile and SaveTemplate are used for saving workbook as *.ogw and *.otw files respectively.

WorksheetPage wksPg("Book1");
// Save workbook as OGW file
bool bRet1 = wksPg.SaveToFile("D:\\" + wksPg.GetName() + ".ogw");  
// Save workbook as OTW template
bool bRet2 = wksPg.SaveTemplate("D:\\" + wksPg.GetName() + ".otw");

Show or Hide Workbook

The WorksheetPage class inherits the Show property from OriginObject class to show or hide itself.

WorksheetPage wksPg("Book1");
wksPg.Show = false;  // Hide the workbook. If true, show the workbook

Activate Workbook

To activate a workbook, the method SetShow can be used by passing parameter of value PAGE_ACTIVATE.

WorksheetPage wksPg("Book1");
wksPg.SetShow(PAGE_ACTIVATE);  // Activate the workbook
// More operations can be done by passing different values, such as
// wksPg.SetShow(PAGE_HIDDEN);  // Hide the workbook
// wksPg.SetShow(PAGE_MINIMIZED);  // Minimize the workbook
// wksPg.SetShow(PAGE_MAXIMIZED);  // Maximize the workbook

Delete Workbook

All of Origin C's internal classes are derived from the OriginObject class. This class has a Destroy method that is used to destroy the object. Calling this method on a workbook will destroy it, together with all the sheets in the workbook, and all the columns in each sheet.

WorksheetPage wksPg;
wksPg = Project.WorksheetPages.Item(0); // get first workbook in project
if( wksPg ) // if there is a workbook
    wksPg.Destroy(); // delete the workbook

Clone/Duplicate Workbook

The WorksheetPage class (for a Workbook) is derived from the Page class. This class has a Clone method that is used to clone the source page.

// Duplicate "Book1" window with data and style 
// Before calling make sure these windows exist
WorksheetPage wksPage("Book1");
WorksheetPage wksPage1 = wksPage.Clone();

Name and Label Workbook

For a workbook, there will be short name, Long Name, and Comments. The inherited methods, SetName, SetLongName, SetComments, which are defined in OriginObject class, can be used to control workbook's name (both short name and Long Name) and comments.

WorksheetPage wksPg("Book1");
if(wksPg) 
{
	wksPg.SetName("MyBook");  // Rename workbook
	wksPg.SetLongName("This is Long Name", false);  // Set Long Name
	wksPg.SetComments("Comments");  // Set Comments
}

Also, Label property is provided for changing Long Name. And TitleShow property is for how to show short name and Long Name on the workbook's title.

WorksheetPage wksPg1("Book2");
if(wksPg1) 
{
	wksPg1.Label = "My Label";  // Set Label (also called Long Name)
	// Show only Label on workbook's title
	wksPg1.TitleShow = WIN_TITLE_SHOW_LABEL;  
	// Show only short name on workbook's title
	// wksPg1.TitleShow = WIN_TITLE_SHOW_NAME;  
	// Show both short name and Label on workbook's title
	// wksPg1.TitleShow = WIN_TITLE_SHOW_BOTH;  
}