1.4.7 Saving Meta Data into Origin Books

Passing XML to Origin Workbook

VBA

    'First add Reference to Microsoft XML v2.6 or later
    'Before running, first run Origin with default workbook which you can open Workbook Organizer
    'to examine the result
    Dim app As Origin.ApplicationSI
    Dim WkBk As Origin.WorksheetPage
    Dim Wks As Origin.Worksheet
    Dim strXML As String
    Dim tree As MSXML2.DOMDocument
    Dim treeNodeTop As MSXML2.IXMLDOMElement
    Dim treeNode As MSXML2.IXMLDOMElement
    Set tree = New MSXML2.DOMDocument
 
    'Set XML String, which has two branch and some nodes.
    Set treeNodeTop = tree.appendChild(tree.createElement("OriginStorage"))
    Set treeNode = treeNodeTop.appendChild(tree.createElement("data"))
    'make the data branch Read Only with blue text, 0=disable, 1 = enable, 2 = read-only blue text, 3=read-only normal text
    treeNode.setAttribute "Enable", 2
    Set treeNode = treeNode.appendChild(tree.createElement("StartTime"))
    treeNode.Text = "2007-03-05 09:15:06.345"
    Set treeNode = treeNode.parentNode
    Set treeNode = treeNode.appendChild(tree.createElement("StopTime"))
    treeNode.Text = "2007-03-05 11:37:43.123"
    Set treeNode = treeNode.parentNode
    Set treeNode = treeNode.appendChild(tree.createElement("Operator"))
    treeNode.Text = "John Smith"
    Set treeNode = treeNodeTop.appendChild(tree.createElement("Instrument"))
    'make the Instrument branch Read Only with normal text
    treeNode.setAttribute "Enable", 3
    Set treeNode = treeNode.appendChild(tree.createElement("SerialNumber"))
    treeNode.Text = "EQR-23456"
    Set treeNode = treeNode.parentNode
    Set treeNode = treeNode.appendChild(tree.createElement("UsageCount"))
    treeNode.Text = "2345"
 
    strXML = treeNodeTop.XML
 
    Set app = New Origin.ApplicationSI
    Set Wks = app.FindWorksheet("")   'Set Wks as the active Worksheet
    Set WkBk = Wks.Parent   'Set WkBk as the active Workbook
    
    Dim bRet As Boolean
    
    bRet = Wks.Columns(0).SetMetaData(strXML, "UserColInfo", True)  'Set XML to Column
    bRet = Wks.SetMetaData(strXML, "UserSheetInfo", True)   'Set XML to Worksheet
    bRet = WkBk.SetMetaData(strXML, "UserBookInfo", True)   'Set XML to Workbook
    bRet = WkBk.SetMetaData(strXML, "CustomInfo", False)   'hide this from user, should not see this from Workbook Organizer, but can see by page.info listing

C#

using Origin;
using System.Xml;
static void SetMetaData()
{
    //Set XML String, which has two branch and some nodes.
    XmlDocument xmlDoc = new XmlDocument();
    XmlNode Node;
    XmlElement topXML;
    XmlElement Element;

    topXML = xmlDoc.CreateElement("DAQ");
    //make the data branch Read Only with blue text, 0=disable, 1 = enable, 2 = read-only blue text, 3=read-only normal text
    topXML.SetAttribute("Enable", "2");
    Node = xmlDoc.AppendChild(topXML);
    Node = Node.AppendChild(xmlDoc.CreateElement("Data"));
    Element = xmlDoc.CreateElement("StartTime");
    Element.InnerText = "2007-03-05 09:15:06.345";
    Node = Node.AppendChild(Element);
    Element = xmlDoc.CreateElement("StopTime");
    Element.InnerText = "2007-03-05 09:15:06.345";
    Node = Node.ParentNode.AppendChild(Element);
    Element = xmlDoc.CreateElement("Operator");
    Element.InnerText = "John Smith";
    Node = Node.ParentNode.AppendChild(Element);
    Node = Node.ParentNode.ParentNode.AppendChild(xmlDoc.CreateElement("Instrument"));
    //make the Instrument branch Read Only with normal text
    topXML.SetAttribute("Enable", "3");
    Element = xmlDoc.CreateElement("SerialNumber");
    Element.InnerText = "EQR-23456";
    Node = Node.AppendChild(Element);
    Element = xmlDoc.CreateElement("UsageCount");
    Element.InnerText = "2345";
    Node = Node.ParentNode.AppendChild(Element);

    string strXML = topXML.OuterXml;

    Origin.ApplicationSI app = new Origin.ApplicationSI();     //Connect to the running Origin
    Worksheet wks = app.FindWorksheet("");     //Set wks as the active Worksheet
    WorksheetPage wkbk = (WorksheetPage)wks.Parent;     //Set wkbk as the active Workbook

    wks.Columns[0].SetMetaData(strXML, "UserColInfo", true);     //Set XML to Column
    wks.SetMetaData(strXML, "UserSheetInfo", true);     //Set XML to Worksheet
    wkbk.SetMetaData(strXML, "UserBookInfo", true);     //Set XML to Workbook
    wkbk.SetMetaData(strXML, "CustomTree", false);     //Set XML to Workbook. This will no longer be visible from book organizer, but can be accessed from Origin C program

    //Save the project
    app.Save(app.path(Origin.APPPATH_TYPES.APPPATH_USER) + "SetMetaData.opj");
}

VC++

#import "Origin8.tlb" rename_namespace("Origin")
using namespace Origin;
using namespace System::Xml;

#define	PAGE_TYPE_WKS	2
#define	PAGE_VISIBLE	2 
bool saveMetaDataToWks()
{
	XmlDocument xmlDoc;
	XmlElement^ xmlEleTop = xmlDoc.CreateElement("DAQ");
	//make the data branch Read Only with blue text, 0=disable, 1 = enable, 2 = read-only blue text, 3=read-only normal text
	xmlEleTop->SetAttribute("Enable", "2");

	XmlNode^ xmlNode = xmlDoc.AppendChild(xmlEleTop);
	xmlNode = xmlNode->AppendChild(xmlDoc.CreateElement("Data"));

	XmlNode^ xmlEle = xmlDoc.CreateElement("StartTime");
	xmlEle->InnerText = "2008-12-25 15:35:36.222";
	xmlNode = xmlNode->AppendChild(xmlEle);

	xmlEle = xmlDoc.CreateElement("StopTime");
	xmlEle->InnerText = "2008-12-25 15:40:40.125";
	xmlNode = xmlNode->ParentNode->AppendChild(xmlEle);

	xmlEle = xmlDoc.CreateElement("Operator");
	xmlEle->InnerText = "Sophy Huang";
	xmlNode = xmlNode->ParentNode->AppendChild(xmlEle);
	xmlNode = xmlNode->ParentNode->ParentNode->AppendChild(xmlDoc.CreateElement("Instrument"));
	//make the Instrument branch Read Only with normal text
	xmlEleTop->SetAttribute("Enable", "3");

	xmlEle = xmlDoc.CreateElement("SerialNumber");
	xmlEle->InnerText = "EQR-23456";
	xmlNode = xmlNode->AppendChild(xmlEle);
	xmlEle = xmlDoc.CreateElement("UsageCount");
	xmlEle->InnerText = "2345";
	xmlNode = xmlNode->ParentNode->AppendChild(xmlEle);

 
	CString str = xmlEleTop->OuterXml;
	_bstr_t strXML = str.GetString();
 
	Origin::IOApplicationPtr pOrigin;
	pOrigin.CreateInstance(__uuidof(ApplicationSI)); //connect to running Origin instance

	//Origin::WorksheetPtr pwks = pOrigin->; //get active worksheet
	pOrigin->CreatePage( PAGE_TYPE_WKS, "CustomerBook", "w", PAGE_VISIBLE);
	Origin::WorksheetPagePtr pWksPage = pOrigin->GetActivePage();
	Origin::WorksheetPtr pwks = pWksPage->GetLayers()->Add("UserData");
	if ( pwks )
	{
		pwks->Activate();
		Origin::ColumnPtr pCol = pwks->GetColumns()->Add("MyVC++");

		pCol->SetMetaData(strXML, "UserColInfo", true);//set XML to Column
		pwks->SetMetaData(strXML, "UserSheetInfo", true); //set XML to worksheet
		pWksPage->SetMetaData(strXML, "UserBookInfo", true); //set XML to workbook
		pWksPage->SetMetaData(strXML, "CustomTree", false); //set XML to workbook, this will no longer be visible from book organizer but can be accessed by OC code
		pOrigin->Save(pOrigin->Path(Origin::APPPATH_USER) + "SetMetaData.opj" ); //save project for check whether it is successfully set

	}
	return true;
}