40
Office Open XML Developer Workshop SpreadsheetML Advanced

Office Open XML Developer Workshop SpreadsheetML Advanced

Embed Size (px)

Citation preview

Office Open XML Developer Workshop

SpreadsheetML AdvancedSpreadsheetML Advanced

Office Open XML Developer Workshop

DisclaimerDisclaimerThe information contained in this slide deck represents the current view of Microsoft Corporation on the issues discussed as of the date of

publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication.

This slide deck is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.

Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this slide deck may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this slide deck. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this slide deck does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred.

© 2006 Microsoft Corporation. All rights reserved.Microsoft, 2007 Microsoft Office System, .NET Framework 3.0, Visual Studio, and Windows Vista are either registered trademarks or

trademarks of Microsoft Corporation in the United States and/or other countries.The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

Office Open XML Developer Workshop

TABLESTABLES

Office Open XML Developer Workshop

SpreadsheetML TablesSpreadsheetML Tables

SpreadsheetML tables provide structure and formatting for worksheet information

Design goals for SpreadsheetML tables:1. Separate presentation and data

Data stays in the worksheetTable definition is in a separate part (referenced via a relationship)

2. Cell definition lightweight but extensibleComplex type with future storage capabilitiesNamed ranges written in their own collection instead of on each cell

Open XML has different types of tables for each document type, optimized for different scenarios:

WordprocessingML has its tbl elementSpreadsheetML has its table elementPresentationML uses DrawingML tables (tbl inside graphicData)

Office Open XML Developer Workshop

SpreadsheetML Table: simple exampleSpreadsheetML Table: simple example

DEMO

<sheetData> <row r="1" spans="1:2"> <c r="A1" t="s"><v>0</v></c> <c r="B1" t="s"><v>1</v></c> </row> <row r="2" spans="1:2"> <c r="A2"><v>1</v></c> <c r="B2"><v>4</v></c> </row> <row r="3" spans="1:2"> <c r="A3"><v>2</v></c> <c r="B3"><v>5</v></c> </row> <row r="4" spans="1:2"> <c r="A4"><v>3</v></c> <c r="B4"><v>6</v></c> </row></sheetData>...<tableParts count="1"> <tablePart r:id="rId2"/></tableParts>

Headings = shared strings

Worksheet (sheet1.xml)

Table definition (table1.xml)<table … ref="A1:B4” …> <autoFilter ref="A1:B4”/> <tableColumns count="2"> <tableColumn id="1" name="Column1" /> <tableColumn id="2" name="Column2" /> </tableColumns> <tableStyleInfo …/> </table>

Office Open XML Developer Workshop

Header Row

No Totals Row

Header Row

No Totals Row

No Header Row

(merged cells)

Totals Row

Typical TablesTypical TablesTable #1

Table #3

Table #2

Office Open XML Developer Workshop

Table #1Table #1

Office Open XML Developer Workshop

Table #2Table #2

Office Open XML Developer Workshop

Table #3Table #3

Office Open XML Developer Workshop

autoFilter definitionautoFilter definition

In worksheet part (sheet1.xml):

Office Open XML Developer Workshop

AutoFilter ResultAutoFilter Result

Office Open XML Developer Workshop

conditionalFormattingconditionalFormatting

Office Open XML Developer Workshop

Cells with Conditional FormattingCells with Conditional Formatting

> .5

Data Bars

Office Open XML Developer Workshop

COMMENTSCOMMENTS

Office Open XML Developer Workshop

Comment ExampleComment Example

Typical comment

Key concepts:• Comments are not stored in the worksheet• Each worksheet may have a comment part

• Contains all comments for that worksheet

• Each comment is attached to a cell (A1 notation)• Drawing/rendering information is stored separately

• VML-based legacy format

• Implicit worksheet relationships point to comment part and drawing part

Office Open XML Developer Workshop

Comment part (comment1.xml)Comment part (comment1.xml)<comments xmlns="...spreadsheetml..."> <authors> <author>John Smith</author> </authors> <commentList> <comment ref="D4" authorId="0"> <text> <r> <rPr> <b /> <sz val="8" /> <color indexed="81" /> <rFont val="Tahoma" /> <family val="2" /> </rPr> <t>John:</t> </r> <r> <rPr> <sz val="8" /> <color indexed="81" /> <rFont val="Tahoma" /> <family val="2" /> </rPr> <t xml:space="preserve">Why is shipping so high on this item?</t> </r> </text> </comment> </commentList></comments>

Comment for cell D4

Author 0 (from the list)

Rich text formatting

Office Open XML Developer Workshop

Comments: vmlDrawing1.vmlComments: vmlDrawing1.vml

Defines how to render the commentUses VML (legacy schema being replaced by DrawingML)

<xml xmlns:v="urn:schemas-microsoft-com:vml“xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:x="urn:schemas-microsoft-com:office:excel"> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1"/> </o:shapelayout> <v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe"> <v:stroke joinstyle="miter"/> <v:path gradientshapeok="t" o:connecttype="rect"/> </v:shapetype> <v:shape id="_x0000_s1025" type="#_x0000_t202" style='position:absolute; margin-left:203.25pt;margin-top:37.5pt;width:96pt;height:55.5pt;z-index:1; visibility:hidden' fillcolor="#ffffe1" o:insetmode="auto"> <v:fill color2="#ffffe1"/> <v:shadow on="t" color="black" obscured="t"/> <v:path o:connecttype="none"/> <v:textbox style='mso-direction-alt:auto'> <div style='text-align:left'></div> </v:textbox> <x:ClientData ObjectType="Note"> <x:MoveWithCells/> <x:SizeWithCells/> <x:Anchor>4, 15, 2, 10, 6, 15, 6, 4</x:Anchor> <x:AutoFill>False</x:AutoFill> <x:Row>3</x:Row> <x:Column>3</x:Column> </x:ClientData> </v:shape></xml>

Office Open XML Developer Workshop

STYLESSTYLES

Office Open XML Developer Workshop

Styles & ThemesStyles & Themes

We have seen examples of direct formattingRich text settings in the <rPr> run properties elementApplied directly to cell contents or comment text

Formatting can also be specified by a styleNamed collection of formatting elementsStyle types: cell style, table style, pivotTable style

A style may refer to a theme definitionThemes define a set of colors, font information, and shape effectsDirect formatting may also refer to a themePicking a different theme changes the styles/formatting that refer to it

Office Open XML Developer Workshop

Style part (styles.xml)Style part (styles.xml)

Contents of the <styleSheet> element:

Description Tag

Number Format Expressions <numFmts>

Font Definitions <fonts>

Fill Definitions <fills>

Border Definitions <borders>

Master Records – Cell Styles <cellStyleXfs>

Master Records – Formatting <cellXfs>

Cell Styles <cellStyles>

Differential Formatting Records <dxfs>

Custom Table Styles <tableStyles>

Office Open XML Developer Workshop

Number Format Expressions <numFmts>Number Format Expressions <numFmts>

Cell number format definitionsThe numFmtId attribute uniquely identifies each entry

Number formats don’t use 0-based index like other style definitions

Example (for currency formatting):

May not reference theme definitions

<numFmts count="1"> <numFmt numFmtId="164" formatCode=""$"#,##0.00" /> </numFmts>

# of formats defined Formatting expression

ID for this formatting style (predefined in the spec)

Office Open XML Developer Workshop

Font Definitions <fonts>Font Definitions <fonts>

Referenced by index (fontId) from master recordsProperties may reference theme definitions

<fonts count="3"> <font>   <sz val="11" />   <color theme="1" />   <name val="Calibri" />   <family val="2" />   <scheme val="minor" />   </font> <font>   <sz val="8" />   <color indexed="81" />   <name val="Tahoma" />   <family val="2" />   </font> <font>   <b />   <sz val="8" />   <color indexed="81" />   <name val="Tahoma" />   <family val="2" />   </font></fonts>

Color 1 from theme definition

Indexed color reference

Bold version of above font

fontId=0

fontId=1

fontId=2

Office Open XML Developer Workshop

Fill Definitions <fills>Fill Definitions <fills>

Referenced by index (fillId) from master recordsST_PatternType fill patterns documented in §3.18.57

<fills count="2"> <fill>  <patternFill patternType="none" />   </fill> <fill>  <patternFill patternType="gray125" />   </fill> <fill> <patternFill patternType=“solid”> <fgColor theme=“4”/> <bgColor theme=“4”/> </patternFill></fills>

Color 4 from theme definition

Office Open XML Developer Workshop

Master Record – Cell Styles <cellStyleXfs>Master Record – Cell Styles <cellStyleXfs>

Groups together a specific combination of style settings for use in a cellStyle definition

Master records are referenced by index (xfId)

<cellStyleXfs count="1"> <xf numFmtId="0" fontId="0" fillId="0" borderId="0" /> </cellStyleXfs>

Border definition (0-based index) Fill definition (0-based index) Font definition (0-based index) Number format expression (unique ID)

Office Open XML Developer Workshop

Master Record – Formatting <cellXfs>Master Record – Formatting <cellXfs>

Groups together a specific combination of style settings for use in directing formatting from a cell

NOTE: master records are referenced by index and not xfId (§3.7.3.6). For example, to reference the 2nd master record above (i.e., index=1):

<cellXfs count="2">  <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" />   <xf numFmtId="164" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1" /> </cellXfs>

<c r=“A1” s=“1” t=“s”> <v>0</v></c>

Office Open XML Developer Workshop

Master Record Direct FormattingMaster Record Direct Formatting

Office Open XML Developer Workshop

Cell Styles <cellStyles>Cell Styles <cellStyles>

Collection of the cell styles used in the workbook

<cellStyles count="1">  <cellStyle name="Normal" xfId="0" builtinId="0" />   <cellStyle name=“Accent1" xfId=“1" builtinId=“29" /> </cellStyles>

Office Open XML Developer Workshop

Differential Formatting Records <dxfs>Differential Formatting Records <dxfs>

Groups of formatting information that can be applied as a differential/additive styleReferenced by index (0-based) from a tableStyle elementWhen applied, only the specified properties are changed and all other formatting properties retain their values

<dxfs count=“1”> <dxf> <font> <b/> <color theme=“0”/> </font> <fill> <patternFill patternType=“solid”> <fgColor theme=“5”/> <bgColor theme=“5”/> </patternFill> </fill> </dxf></dxfs>

Change font to bold and change color, leave other font properties unchanged

Office Open XML Developer Workshop

Custom Table Styles <tableStyles>Custom Table Styles <tableStyles>

Only custom-defined styles are saved – predefined Table/PivotTable styles are not saved

Predefined Table/PivtoTable styles – see §3.8.4013 table regions can be formatted15 additional PivotTables regionsAll predefined styles reference themesCustom styles may reference themes

<tableStyles count=“1" defaultTableStyle="TableStyleMedium9" defaultPivotStyle="PivotStyleLight16" /> <tableStyle name=“TableStyleMedium10 – Custom” pivot=“0” count=“1”> <tableStyleElement type=“WholeTable” dxfId=“6”/> </tableStyle></tableStyles>

Apply the 7th differential formatting record to the “whole table” region

Office Open XML Developer Workshop

Custom Table StyleCustom Table Style

Styles part:

Table definition:

Office Open XML Developer Workshop

Style & Formatting ExampleStyle & Formatting Example

Left set of numbers have direct & themed formattingTable has a table style appliedPivotTable has a table style applied

Office Open XML Developer Workshop

Vary The ThemeVary The Theme

Since all colors are specified as theme colors, they change when a different theme is selectedCells with non-theme colors specified wouldn’t change color with theme changes

Office Open XML Developer Workshop

NAMED RANGESNAMED RANGES

Office Open XML Developer Workshop

Named Ranges <defineNames>Named Ranges <defineNames>

This workbook-level element defines names for:CellsNamed rangesFormulasConstants

Names can’t be in the range A1 – XFD1048576Names can’t begin with _xlnm

Predefined names: _xlnm.Print_Area, _xlnm.Sheet_Title, etc.

Best practice: use named ranges

Office Open XML Developer Workshop

Named Range AttributesNamed Range Attributes

Name – requiredDescription – user-viewableComment – internal documentationFunction/hidden/vbProcedure – booleansScope defined by localSheetIdOther attributes – see §3.2.5

DEMO

<definedNames> <definedName name=“MyFormula” comment=“My comment.">SUM(Sheet3!$B$2:$B$9)</definedName> <definedName name=“MyRange1">Sheet3!$A$1:$C$12</definedName> <definedName name=“MyRange2" localSheetId="2” hidden="1">Sheet5!$A$1:$T$47</definedName></definedNames>

Office Open XML Developer Workshop

PIVOT TABLESPIVOT TABLES

Office Open XML Developer Workshop

Pivot TablesPivot Tables

Pivot tables simplify large data setsAggregate information by rows/columnsTwo components must be defined:1. The pivot cache definition

• Defines the data source• Workbook has a relation to the

pivot cache definition2. The pivot table definition

• Defines the appearance• Worksheet has a relationship

to the pivot table definition

Sheet

Workbook

PivotCacheDefinition

PivotTable

1

1 1

1

Row axis Column axis

Values

n n

n

n

Office Open XML Developer Workshop

Pivot Cache DefinitionPivot Cache Definition

Defines data source (may be internal or external)Defines pivot-table fields and pivot values

<pivotCacheDefinition refreshOnLoad="1" invalid="1"> <cacheSource type="worksheet"> <worksheetSource ref="A1:G1525" sheet="Sheet1" /> </cacheSource> <cacheFields count="2"> <cacheField name="Territory" numFmtId="0"> <sharedItems count="10"> <s v="Canada" /> <s v="Northwest" /> <s v="Southeast" /> ... <s v="Australia" /> </sharedItems> </cacheField> <cacheField name="Sale Total" numFmtId="0"> <sharedItems containsSemiMixedTypes="0“ containsString="0“ containsNumber="1" /> </cacheField> </cacheFields></pivotCacheDefinition>

<pivotCacheDefinition refreshOnLoad="1" invalid="1"> <cacheSource type="worksheet"> <worksheetSource ref="A1:G1525" sheet="Sheet1" /> </cacheSource> <cacheFields count="2"> <cacheField name="Territory" numFmtId="0"> <sharedItems count="10"> <s v="Canada" /> <s v="Northwest" /> <s v="Southeast" /> ... <s v="Australia" /> </sharedItems> </cacheField> <cacheField name="Sale Total" numFmtId="0"> <sharedItems containsSemiMixedTypes="0“ containsString="0“ containsNumber="1" /> </cacheField> </cacheFields></pivotCacheDefinition>

Data source

Cache field

Pivot values

Office Open XML Developer Workshop

Pivot Table DefinitionPivot Table Definition

Defines the look & feel of the pivot table<pivotTableDefinition name="PivotTable1" cacheId="1“ dataCaption="Value">

<location ref="A1:B12" firstHeaderRow="1" firstDataRow="2" firstDataCol="1" />

<pivotFields count="2"> <pivotField axis="axisRow" showAll="0"> <items count="11"> <item x="0" /> <item x="1" /> ... <item t="default" /> </items> </pivotField> <pivotField dataField="1" showAll="0" /> </pivotFields> <rowFields count="1"> <field x="0" /> </rowFields> <dataFields count="1"> <dataField name="Sum of Sales" fld="1" /> </dataFields></pivotTableDefinition>

<pivotTableDefinition name="PivotTable1" cacheId="1“ dataCaption="Value">

<location ref="A1:B12" firstHeaderRow="1" firstDataRow="2" firstDataCol="1" />

<pivotFields count="2"> <pivotField axis="axisRow" showAll="0"> <items count="11"> <item x="0" /> <item x="1" /> ... <item t="default" /> </items> </pivotField> <pivotField dataField="1" showAll="0" /> </pivotFields> <rowFields count="1"> <field x="0" /> </rowFields> <dataFields count="1"> <dataField name="Sum of Sales" fld="1" /> </dataFields></pivotTableDefinition>

Associated cache definition (ID from workbook part)

Location of the pivot table within the worksheet

DEMO

Office Open XML Developer Workshop