Basic Workbook Operation

You can manipulate workbooks with the Page object and Window command. You can also use Data Manipulation X-Functions. With these tools, you can create new worksbooks, duplicate workbooks, save workbook as template, etc. Some practical examples are provided below.

Create New Workbook

The newbook X-Function can be used to create new workbook. With the arguments of this X-Function, you can specify the newly created workbook with Long Name, number of sheets, template to use, whether hidden, etc.

//Create a new workbook with the Long Name "MyResultBook"
newbook MyResultBook;

// Create a new workbook with 3 worksheets
// and use "MyData" as Long Name and short name
newbook name:="MyData" sheet:=3 option:=lsname;

// Create a new hidden workbook
// and the workbook name is stored in myBkName$ variable
newbook hidden:=1 result:=myBkName$;
// Output workbook name
myBkName$ = ;

// By default, the built-in template "Origin" is used to 
// create the new workbook, you can also use a specified template
// Create a new workbook with the XYZ template
newbook template:=XYZ;

Also, the command win -ti is capable of creating a minimized new workbook from a template file.

// Create a new wookbook from the FFT template
// and Long Name and short name to be MyFFT, then minimize it
win -ti wks FFT MyFFT;

Open Workbook

If the workbook with data is saved (as extension of ogw), it can be opened by the doc -o command.

// The path of the workbook to open
string strName$ = system.path.program$;
strName$ += "Samples\Graphing\Automobile Data.ogw";
// Open the workbook
doc -o %(strName$);

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).

  1. The command save -i is able to save the active workbook with data to an ogw file.
  2. // Create a new workbook
    // Fill some data to col(1)
    col(1) = uniform(32);
    // Save this workbook with data to MyData.ogw under User Files Folder
    save -i %YMyData.ogw;
  3. The X-Function template_saveas is used to save workbook as a template.
  4. // Create a new workbook with 3 sheets
    newbook sheet:=3;
    // Save this workbook as a template named My3SheetsBook
    // in User Files Folder (default)
    template_saveas template:=My3SheetsBook;
  5. To save a workbook with analysis, the command save -ik can be used.
  6. // Create a project
    string strOpj$ = system.path.program$ + "Samples\Analysis.opj";
    doc -o %(strOpj$);
    // Activate the workbook to be saved as analysis template
    win -a Book1J;
    // Save this workbook as an analysis template
    // name MyAnalysis.ogw under User Files Folder
    save -ik %YMyAnalysis.ogw;

Close Workbook

To close workbook, just click the Close button in the top right corner of the workbook. And this behovior is done by command win -ca, and a dialog pops up to prompt user to delete or hide the workbook.

// Create a workbook, and name is stored in MyBook$ variable
newbook result:=MyBook$;

// Simulate the Close button clicking
win -ca %(MyBook$);

To close the workbook directly without prompting, and delete all the data, you can use command win -cd. And this is the same with Delete Workbook below.

// Create a new workbook for closing

// close this workbook without prompting, and delete all the data
win -cd %H;

Show or Hide Workbook

There are three switches, -ch, -h, and -hc, in win command for showing or hiding workbook.

// Create 3 workbooks for hiding
newbook name:=MyBook1 option:=lsname;  // first workbook, MyBook1
newbook name:=MyBook2 option:=lsname;  // second workbook, MyBook2
newbook name:=MyBook3 option:=lsname;  // third workbook, MyBook3;

// Use -ch to hide the active workbook, MyBook3
// And the View Mode in Project Explorer is Hidden
win -ch 1;

// Use -hc to hide the first workbook (not the active one), MyBook1
// And the View Mode in Project Explorer is Hidden
win -hc 1 MyBook1;

// Use -h to hide the second workbook (active workbook), MyBook2
// The View Mode in Project Explorer is still Normal
win -h 1;

// Actually, MyBook2 is still the active workbook
// It is able to show it by:
win -h 0;

// To show MyBook1 and MyBook3, need to use the -hc switch to specify
// the workbook name
win -hc 0 MyBook1;
win -hc 0 MyBook3;

Name and Label Workbook

For a workbook, there will be short name, Long Name, and Comments. You can rename (short name) a workbook with win -r command, and use the page object to control Long Name and Comments, including how to show the workbook title (short name, Long Name, or both).

// Create a new workbook with name of "Data", 
// and show both in workbook title
// both short name and Long Name are the same
// workbook title only shows short name
newbook name:=Data option:=lsname;

// Rename the workbook to "RawData"
win -r Data RawData;

// Change Long Name to be "FFT Data"
page.longname$ = "FFT Data";
// Add Comments, "1st group data for fft"
page.comments$ = "1st group data for fft";

// Let the workbook title shows Long Name only
page.title = 1;  // 1 = Long Name, 2 = short name, 3 = both

Activate Workbook

To activate a workbook, the command win -a can be used.

// The path of project to be opened
string strOpj$ = system.path.program$;
strOpj$ += "Samples\Curve Fitting\Intro_to_Nonlinear_Curve_Fit_Tool.opj";
// Open the project
doc -o %(strOpj$);

// Activate workbook, Book1, in the second subfolder of the project
win -a Book1;

// It also can put the workbook name into a variable
// Variable for the name of workbook, Gaussian, in the project
string strGau$ = Gaussian;
// Activate the Gaussian workbook in the first subfolder
win -a %(strGau$);

Most Origin commands operate on the active window, so you may be tempted to use win -a to activate a workbook and then run script after it to assume the active workbook. This will work in simple codes but for longer script, there might be timing issues and we recommend that you use window -o winName {script} instead. See A Note about Object that a Script Operates upon for more detail explanation.

Delete Workbook

To delete a workbook, you can use the win -c command, and this command will delete the workbook directly without prompts.

// The path of project to be opened
string strOpj$ = system.path.program$ + "Samples\Curve Fitting\2D Bin and Fit.opj";
// Open the project
doc -o %(strOpj$);
// Delete workbook, Book1, from the project
win -c Book1;

// To delete an active workbook, the workbook name can be omitted
// Or using %H to refer to the workbook name
win -a MatrixFit1;  // Activate the workbook MatrixFit1
win -c;  // Delete the workbook
// Or using this one
// win -c %H;

// It also allows to delete a workbook whose name is stored in a variable
// Create a new workbook using newbook X-Function
// And the name of this workbook is stored in string variable ToDel$
newbook result:=ToDel$;
// delete the workbook created just now
win -c %(ToDel$);