{"id":2174,"date":"2016-02-04T06:47:36","date_gmt":"2016-02-04T06:47:36","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=2174"},"modified":"2016-02-04T06:47:36","modified_gmt":"2016-02-04T06:47:36","slug":"accessing-parts-using-convenience-functions","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/accessing-parts-using-convenience-functions\/","title":{"rendered":"Accessing Parts using Convenience Functions"},"content":{"rendered":"<p class=\"apiPara\"><span class=\"Back\"><a class=\"Back\" href=\"https:\/\/www.ericwhite.com\/blog\/open-xml-sdk-for-javascript\/\">Return to the<br \/>Open XML SDK for JavaScript<br \/>Developer Center<\/a><\/span>The normal way to navigate from the package to the main document part, or to navigate from one part to another part is through relationships.&nbsp; From one point of view, there are two varieties of relationships&ndash; implicit relationships and explicit relationships:<\/p>\n<ul>\n<li class=\"apiLi\"><b>Implicit relationships<\/b> &ndash; these relationships are used in<br \/>\nOpen XML when there is only one target part of a given relationship type.&nbsp;<br \/>\nWhile the relationship has a relationship ID, you will not find a reference to<br \/>\nthis relationship ID in the markup for the source part.&nbsp; Instead, if you want<br \/>\nto get that related part, you look for a relationship with a given relationship<br \/>\ntype.&nbsp; If it exists, then you can open the related part.&nbsp; An example of this<br \/>\ntype of relationship is the relationship in WordprocessingML from the main<br \/>\ndocument part to the comments part.&nbsp; A document may or may not have comments,<br \/>\nso it may or may not have a comments part.&nbsp; To navigate to the comments part,<br \/>\nyou look for a relationship with the relationship type of &#8220;http:\/\/schemas.openxmlformats.org\/officeDocument\/2006\/relationships\/comments&#8221;.<\/li>\n<li class=\"apiLi\"><b>Explicit relationships<\/b> &ndash; these relationships are used in<br \/>\nOpen XML when there may be more than one target part of a given relationship<br \/>\ntype.&nbsp; You will always find markup in the source part that contains the<br \/>\nrelationship ID.&nbsp; If you want to retrieve the related parts, you typically find<br \/>\nthe markup in the source part that contains the reference to the related part.&nbsp;<br \/>\nYou get the relationship ID from the markup, and then look up the relationship<br \/>\nby its relationship ID.&nbsp; An example of this type of relationship is the<br \/>\nrelationship in WordprocessingML from the main document part to a header part.&nbsp;<br \/>\nThere may be multiple header parts, so to navigate to the header part, you<br \/>\nfirst find the markup for the section properties in the main document part.&nbsp;<br \/>\nYou then find the markup for the relationship to the header part and retrieve<br \/>\nthe relationship ID.&nbsp; You then retrieve the relationship with the given ID, and<br \/>\ncan retrieve the related part from the relationship that you found.<\/li>\n<\/ul>\n<p class=\"apiPara\">Note that most of the commonly used relationship types and<br \/>\ncontent types are pre-defined in <b>openxml.js<\/b>.&nbsp; The code that defines<br \/>\nthese relationship and content types looks like this (sorry for the small font, but it looks better when it does not wrap):<\/p>\n<pre style=\"font-size: 10px;\" class=\"prettyprint\">\/\/ *********** relationship types ***********\r\nopenXml.relationshipTypes = {\r\n    alternativeFormatImport: \"http:\/\/schemas.openxmlformats.org\/officeDocument\/2006\/relationships\/aFChunk\",\r\n    calculationChain: \"http:\/\/schemas.openxmlformats.org\/officeDocument\/2006\/relationships\/calcChain\",\r\n    cellMetadata: \"http:\/\/schemas.openxmlformats.org\/officeDocument\/2006\/relationships\/sheetMetadata\",\r\n    chart: \"http:\/\/schemas.openxmlformats.org\/officeDocument\/2006\/relationships\/chart\",\r\n    chartColorStyle: \"http:\/\/schemas.microsoft.com\/office\/2011\/relationships\/chartColorStyle\",\r\n    ...\r\n    worksheet: \"http:\/\/schemas.openxmlformats.org\/officeDocument\/2006\/relationships\/worksheet\",\r\n    worksheetComments: \"http:\/\/schemas.openxmlformats.org\/officeDocument\/2006\/relationships\/comments\",\r\n    worksheetSortMap: \"http:\/\/schemas.microsoft.com\/office\/2006\/relationships\/wsSortMap\",\r\n    xmlSignature: \"http:\/\/schemas.openxmlformats.org\/package\/2006\/relationships\/digital-signature\/signature\",\r\n};\r\n    \r\n\/\/ ********************* content types ***********************\r\nopenXml.contentTypes = {\r\n    calculationChain: \"application\/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml\",\r\n    cellMetadata: \"application\/vnd.openxmlformats-officedocument.spreadsheetml.sheetMetadata+xml\",\r\n    chart: \"application\/vnd.openxmlformats-officedocument.drawingml.chart+xml\",\r\n    chartColorStyle: \"application\/vnd.ms-office.chartcolorstyle+xml\",\r\n    chartDrawing: \"application\/vnd.openxmlformats-officedocument.drawingml.chartshapes+xml\",\r\n    ...\r\n    worksheetComments: \"application\/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml\",\r\n    worksheetSortMap: \"application\/vnd.ms-excel.wsSortMap+xml\",\r\n    xmlSignature: \"application\/vnd.openxmlformats-package.digital-signature-xmlsignature+xml\",\r\n};<\/pre>\n<p class=\"apiPara\">For many of the explicit relationships, there are<br \/>\nconvenience functions that make it easier to retrieve the related part.&nbsp; To<br \/>\ndemonstrate the use of the convenience functions, first let us take a look at<br \/>\nhow to retrieve a part when not using the convenience functions.<\/p>\n<p class=\"apiPara\">You can find the main document part like this:<\/p>\n<pre class=\"prettyprint\">\/\/ Open a blank document that is stored as a base64 string.\r\nvar doc = new openXml.OpenXmlPackage(blankDocument_base64);\r\n\r\n\/\/ Get the main document part.\r\nvar mainPart = doc.getPartByRelationshipType(openXml.relationshipTypes.mainDocument);<\/pre>\n<p class=\"apiPara\">When using the convenience function, the code looks like<br \/>\nthis:<\/p>\n<pre class=\"prettyprint\">\/\/ Open a document that is stored as a base64 string.\r\nvar doc = new openXml.OpenXmlPackage(document_base64);\r\n\r\n\/\/ Get the main document part.\r\nvar mainPart = doc.mainDocumentPart();<\/pre>\n<p class=\"apiPara\">This pattern should look familiar &ndash; it is the same pattern<br \/>\nas followed by the Open XML SDK for the .NET Framework.<\/p>\n<p class=\"apiPara\">The difference in coding is more pronounced when finding the<br \/>\ncomments part:<\/p>\n<pre class=\"prettyprint\">\/\/ Open a document that is stored as a base64 string.\r\nvar doc = new openXml.OpenXmlPackage(document_base64);\r\n\r\n\/\/ Get the main document part.\r\nvar mainPart = doc.mainDocumentPart().wordprocessingCommentsPart();<\/pre>\n<p class=\"apiPara\">If we examine the <b>wordprocessingCommentsPart<\/b><br \/>\nfunction, we see that it is defined like this:<\/p>\n<pre class=\"prettyprint\">openXml.OpenXmlPart.prototype.wordprocessingCommentsPart = function () {\r\n    return this.getPartByRelationshipType(\r\n        openXml.relationshipTypes.wordprocessingComments);\r\n};<\/pre>\n<p class=\"apiPara\">You can see that these convenience methods are pretty<br \/>\nsimple.&nbsp; However, these navigation operations are pretty comment.&nbsp; We often<br \/>\nneed to write code to navigate from the package to the main document part, or<br \/>\nnavigate from the main document part to the comments part or styles part, so these<br \/>\nconvenience methods make it quite a bit easier.<\/p>\n<p class=\"apiPara\">You may find that you need to perform some navigation where<br \/>\nthe convenience functions have not yet been written.&nbsp; When you encounter this<br \/>\nsituation, you can either write a convenience function locally that follows the<br \/>\nsame pattern, or you can simply use the <b>getPartByRelationshipType<\/b> or <b>getPartsByRelationshipType<\/b><br \/>\nmethods.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Return to theOpen XML SDK for JavaScriptDeveloper CenterThe normal way to navigate from the package to the main document part, or to navigate from one part to another part is through relationships.&nbsp; From one point of view, there are two varieties of relationships&ndash; implicit relationships and explicit relationships: Implicit relationships &ndash; these relationships are used [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_s2mail":"","footnotes":""},"class_list":["post-2174","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=2174"}],"version-history":[{"count":1,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2174\/revisions"}],"predecessor-version":[{"id":2175,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2174\/revisions\/2175"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=2174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}