Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 98 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
98
Dung lượng
1,11 MB
Nội dung
Chapter 5. Spreadsheets
Surprisingly, we have already covered a great deal of the information about
spreadsheets. Spreadsheets share a great deal of their markup with tables that you
find in text documents. This shouldn’t come as a surprise—a spreadsheet is just a
two-dimensional table. It can have many blank rows and columns and can do
calculations on the cell entries, but a spreadsheet is still just a table at heart.
However, there are things that make a spreadsheet, well, spreadsheetish. Most
important, the <office:body> has an <office:spreadsheet> element as
its child (rather than <office:text> for a word processing document). Other
elements and attributes specific to spreadsheets are in the styles.xml file, but
most are in content.xml.
Spreadsheet Information in styles.xml
The styles.xml file stores information that OpenOffice.org sets from the sheet
tab of the Format Page dialog, shown in Figure 5.1, “Spreadsheet Page Options”.
Specifically, this information is in the <style:page-layout-properties>
element that is inside the first <style:page-layout> element within the
<office:automatic-styles>.
Figure 5.1. Spreadsheet Page Options
Using OASISOpenDocument XML 93
Chapter 5. Spreadsheets
1. The style:print-page-order attribute has a value of ttb for top
to bottom, and ltr for left to right. If the first page number is not one (the
default), then the style:first-page-number attribute will give the
number that you specify.
2. The value of the style:print attribute summarizes all the marked
checkboxes as a whitespace-separated list. If you turn on all the
checkboxes, the value will be these words (separated by whitespace):
annotations, charts, drawings, formulas, grid, headers,
objects, and zero-values.
3. If you are scaling to a percentage, then the style:scale-to attribute
will have the scaling percentage (with a percent sign) as its value. If you are
fitting to a number of pages, then the style:scale-to-pages
attribute will provide that value. If you are scaling to width and height, then
the style:scale-to-X and style:scale-to-Y attributes will
give the number of pages in each direction.
Example 5.1, “Page Options” shows this markup.
Example 5.1. Page Options
<office:automatic-styles>
<style:page-layout style:name="pm1">
<style:page-layout-properties
style:print-page-order="ttb" style:first-page-number="2"
style:scale-to-pages="1"
style:writing-mode="lr-tb"
style:print="formulas grid headers zero-values"/>
<! etc. >
</style:page-master>
<! more styles >
</office:automatic-styles>
Spreadsheet Information in content.xml
The <office:automatic-styles> element contains
• Column styles
• Row styles
• Sheet styles
• Number styles
• Cell Styles
Column and Row Styles
Each differently styled column in the spreadsheet gets a <style:style> whose
style:family is table-column. Its child <style:table-column-
properties> element specifies the width of the column (style:column-
width) in the form of a length, such as 1.1in.
94 OASISOpenDocument Essentials
Spreadsheet Information in content.xml
The column styles are followed by <style:style> elements whose
style:family is table-row. Their child <style:table-row-
properties> element specifies the style:row-height. If you have chosen
“optimal height” then this element will also set style:use-optimal-row-
height to true.
Styles for the Sheet as a Whole
A <style:style> element with a style:family="table" primarily serves
to connect a table with a master page and to determine whether the sheet is hidden
or not. Example 5.2, “Style Information for a Sheet” shows just such an element.
Example 5.2. Style Information for a Sheet
<style:style style:name="ta1" style:family="table"
style:master-page-name="Default">
<style:table-properties table:display="true"/>
</style:style>
Number Styles
The other major style information in a spreadsheet deals with formatting numbers so
that they show up as percentages, scientific notation, etc. A number style must
convey two pieces of information: what kind of a number it is (number, currency,
percent, date, etc.) and how the number is to be displayed. This is mirrored in the
XML as a <number:entity-style> element, where entity can be number,
currency, percent, date, etc.
This element has a required style:name attribute that gives the style a unique
identifier, and a style:family attribute with a value of data-style. The
contents of this element will tell how to display the number, percent, currency, date,
etc.
Number, Percent, Scientific, and Fraction Styles
Let’s start with the “pure numeric” styles: numbers, percents, scientific notation, and
fractions.
Plain Numbers
A plain number is contained in a <number:number-style> element with a
style:name attribute. Contained within this element is the description of how to
display the number. In this case, we need only a simple <number:number>
element that has these attributes
• number:decimal-places tells how many digits are to the right of the
decimal symbol
• number:min-integer-digits tells how many leading zeros are
present.
Using OASISOpenDocument XML 95
Chapter 5. Spreadsheets
• number:grouping. If you have checked the“thousands separator”
dialog item, then this attribute will be present and will have a value of
true.
Figure 5.2. Number Styles Dialog
Example 5.3, “Number Style for format #,##0.00” shows a number style for
displaying two places to the right of the decimal, one leading zero, and a grouping
separator.
Example 5.3. Number Style for format #,##0.00
<number:number-style style:name="N2"">
<number:number number:decimal-places="2"
number:min-integer-digits="1"
number:grouping="true"/>
</number:number-style>
Note
The decimal symbol and grouping symbol are not specified in the
style; they are set in the application.
If you want negative numbers to be red, then things become radically different.
Rather than having one style, OpenDocument requires two styles, with the negative
being the default and a “conditional style” for positive values. Here is the XML for a
number with two digits to the right of the decimal, one leading zero, a thousands
separator, and negative numbers in red:
96 OASISOpenDocument Essentials
Spreadsheet Information in content.xml
Example 5.4. Number Style for format -#,##0.00 with Negative Values in Red
<number:number-style style:name="N112P0"
style:volatile="true">
<number:number number:decimal-places="2"
number:min-integer-digits="1" number:grouping="true"/>
</number:number-style>
<number:number-style style:name="N112">
<style:text-properties fo:color="#ff0000"/>
<number:text>-</number:text>
<number:number number:decimal-places="2"
number:min-integer-digits="1" number:grouping="true"/>
<style:map style:condition="value()>=0"
style:apply-style-name="N112P0"/>
</number:number-style>
This is the format to be used for positive numbers. The
style:volatile="true" tells the application to retain this style, even if
it is never used.
This is the main style for negative numbers. They should be displayed in red
…
… starting with a minus sign …
… followed by the number with two decimal places, at least one leading zero,
and a thousands separator.
However, in the event that the value of the cell is greater than or equal to
(>=) zero, use the positive number style (N112P0).
Scientific Notation
Scientific notation is a variant on plain numbers; the outer <number:number-
style> contains a <number:scientific-number> element with these
attributes: number:decimal-places and number:min-integer-digits
for the mantissa, and number:min-exponent-digits for the exponent part.
You don’t need to put the E in the specification. Example 5.5, “Scientific Notation
for Format 0.00E+00” shows the style for scientific notation with two digits to the
right of the decimal point, at least one to the left, and at least two digits in the
exponent.
Example 5.5. Scientific Notation for Format 0.00E+00
<number:number-style style:name="N3">
<number:scientific-number
number:decimal-places="2"
number:min-integer-digits="1"
number:min-exponent-digits="2"/>
</number:number-style>
Using OASISOpenDocument XML 97
Chapter 5. Spreadsheets
Fractions
Fractions are also variants of plain numbers. Their <number:number-style>
element contains a <number:fraction> element that has these attributes:
number:min-integer-digits (number of digits in the whole number part),
number:min-numerator-digits, and number:min-denominator-
digits. Example 5.6, “Fraction Style for Format # ??/??” shows a style for a
fraction with an optional whole number part and at least two digits in the numerator
and denominator.
Example 5.6. Fraction Style for Format # ??/??
<number:number-style style:name="N4">
<number:fraction
number:min-integer-digits="0"
number:min-numerator-digits="2"
number:min-denominator-digits="2"/>
</number:number-style>
Percentages
A percentage is represented in much the same way as a plain number; the only
differences are that
• The enclosing element is <number:percentage-style> instead of
<number:number-style>.
• The enclosed <number:number> style is followed by a
<number:text> element with a percent sign as its content.
Example 5.7, “Percent Style for Format #,##0.00%” shows a percentage with two
digits to the right of the decimal, at least one to the left, and a grouping symbol.
Example 5.7. Percent Style for Format #,##0.00%
<number:percentage-style style:name="N5">
<number:number number:decimal-places="2"
number:min-integer-digits="1" number:grouping="true"/>
<number:text>%</number:text>
</number:percentage-style>
Currency Styles
Currency styles are similar to number styles. Specifying a currency always creates
two styles: one for negative values, and one for positive values. Example 5.8,
“Currency in Format -$#,##0.00” shows the XML for a currency format of US
Dollars with two digits after the decimal point, a minimum of one digit before the
decimal, and a thousands separator.
98 OASISOpenDocument Essentials
Spreadsheet Information in content.xml
Example 5.8. Currency in Format -$#,##0.00
<number:currency-style style:name="N102P0"
style:volatile="true">
<number:currency-symbol
number:language="en"
number:country="US">$</number:currency-symbol>
<number:number number:decimal-places="2"
number:min-integer-digits="1" number:grouping="true"/>
</number:currency-style>
<number:currency-style style:name="N102">
<number:text>-</number:text>
<number:currency-symbol number:language="en"
number:country="US">$</number:currency-symbol>
<number:number number:decimal-places="2"
number:min-integer-digits="1" number:grouping="true"/>
<style:map style:condition="value()>=0"
style:apply-style-name="N102P0"/>
</number:currency-style>
The formatting for positive values appears first, contained in a
<number:currency-style> element.
The <number:currency-symbol> ’s content is the dollar sign. The
number:language and number:country allow a program to
distinguish the US dollar from the New Zealand dollar or Mexican new peso
symbol, which look the same but have different countries and/or languages.
The number portion of the display uses the same <number:number>
element that we have already described
[6]
.
For negative values, the minus sign precedes the currency symbol.
As in Example 5.4, “Number Style for format -#,##0.00 with Negative Values
in Red”, a <style:map> is used to choose whether to use the negative
number format or the positive number format.
The appearance of <number:text> elements mirrors the order in which the text
appears. Example 5.9, “Currency Format for Greek Drachma” shows the negative
number portion of the XML for the Greek drachma. In this format, the value is
shown in red, the minus sign appears first, then the number, then a blank and the
letters “Δρχ.” (We are showing only the negative number specification.)
[6]
If you want to have a replacement for the decimal part of the number (as in $15 ),
you add number:decimal-replacement=" " to the <number:number>
element.
Using OASISOpenDocument XML 99
Chapter 5. Spreadsheets
Example 5.9. Currency Format for Greek Drachma
<number:currency-style style:name="N111">
<style:properties fo:color="#ff0000"/>
<number:text>-</number:text>
<number:number number:decimal-places="2"
number:min-integer-digits="1" number:grouping="true"/>
<number:text> </number:text>
<number:currency-symbol number:language="el"
number:country="GR">Δρχ</number:currency-symbol>
<style:map style:condition="value()>=0"
style:apply-style-name="N111P0"/>
</number:currency-style>
Date and Time Styles
OpenDocument applications support a large number of different formats for dates
and times. Rather than explain each one in detail, it’s easier to simply compose the
style you want out of parts.
For dates, the enclosing element is a <number:date-style> element, with the
usual style:name attribute. The number:automatic-order attribute is
used to automatically order data to match the default order for the language and
country of the data. You may also set the number:format-source to fixed,
to let the application determine the value of “short” and “long” representations of
months, days, etc. If the value is language, then those values are taken from the
language and country set in the style.
Within the <number:date-style> element are the following elements, with
their significant attributes:
<number:year>
Gives the year in two-digit form; the year 2003 appears as 03. If
number:style="long" then the year appears as four digits.
<number:month>
If number:textual="true" then the month appears as an abbreviated
name; otherwise a number without a leading zero. To get the full name of the
month or the month number with a leading zero, set
number:style="long".
<number:day-of-week>
The day of the week as an abbreviated name; to see the full name, use
number:style="long".
<number:day>
The day of the month as a number without a leading zero; to see leading
zeros, use number:style="long".
100 OASISOpenDocument Essentials
Spreadsheet Information in content.xml
<number:quarter>
Which quarter of the year; in U.S. English, a date in October appears as Q4.
If number:style="long", then it appears as 4th quarter.
<number:week-of-year>
Displays which week of the year this date occurs in; thus January 1st displays
as 1 and December 31st displays as 52 (or, in OpenOffice.org’s case, as 1 if
there are 53 weeks in the year, as there are in 2003!)
Example 5.10, “Date Styles” shows three date styles. The first will display the fourth
day of the seventh month of 2005 as Monday, July 4, 2005; the second will
display it as 07/04/05, and the third as 3rd Quarter 05.
Example 5.10. Date Styles
<number:date-style style:name="N79" number:automatic-order="true">
<number:day-of-week number:style="long"/>
<number:text>, </number:text>
<number:month number:style="long"
number:textual="true"/>
<number:text> </number:text>
<number:day/>
<number:text>, </number:text>
<number:year number:style="long"/>
</number:date-style>
<number:date-style style:name="N37" number:automatic-order="true">
<number:month number:style="long"/>
<number:text>/</number:text>
<number:day number:style="long"/>
<number:text>/</number:text>
<number:year/>
</number:date-style>
<number:date-style style:name="N20106">
<number:quarter number:style="long"/>
<number:text> </number:text>
<number:year/>
</number:date-style>
Time values are represented by the <number:time-style> element. Its sub-
elements are:
<number:hours>
Shows the number of hours; if you want leading zeros on hours less than ten,
set number:style="long". If a duration is more than 24 hours, it will
be displayed mod 24. If you do not want this to happen, then set
number:truncate-on-overflow="false" on the
<number:time-style> element.
<number:minutes>
Displays the number of minutes without a leading zero; if you want two
digits, set number:style="long".
Using OASISOpenDocument XML 101
Chapter 5. Spreadsheets
<number:seconds>
Displays the number of seconds without a leading zero; if you want two
digits, set number:style="long". If you wish to see decimal fractions
of a second, then add a number:decimal-places attribute whose value
is the number of decimal places you want.
<number:am-pm>
This empty element inserts the appropriate am or pm (in the selected locale).
Example 5.11, “Time Style” shows the style required to display a time in the format
09:02:34 AM
Example 5.11. Time Style
<number:time-style style:name="N43">
<number:hours number:style="long"/>
<number:text>:</number:text>
<number:minutes number:style="long"/>
<number:text>:</number:text>
<number:seconds number:style="long"/>
<number:text> </number:text>
<number:am-pm/>
</number:time-style>
Note
A <number:date-style> element may also specify hours,
minutes, and seconds.
Internationalizing Number Styles
An OpenDocument-compatible application gets its cues for displaying numbers
from the current language setting. You may set the display of a number to a specific
language and country by adding the number:language and number:country
attributes to a <number:entity-style> element. Thus, to make a date display
in Korean format, you would start the specification as follows:
<number:date-style style:name="N5076"
number:language="ko" number:country="KR">
<number:year number:style="long"/>
<number:text> 년 </number:text>
<number:month/>
<number:text> 월 </number:text>
<number:day/>
<number:text>일</number:text>
</number:date-style>
102 OASISOpenDocument Essentials
[...]... xmlns:office="urn :oasis: names:tc :opendocument: xmlns:office:1.0" xmlns:meta="urn :oasis: names:tc :opendocument: xmlns:meta:1.0" xmlns:config="urn :oasis: names:tc :opendocument: xmlns:config:1.0" xmlns:text="urn :oasis: names:tc :opendocument: xmlns:text:1.0" xmlns:table="urn :oasis: names:tc :opendocument: xmlns:table:1.0" xmlns:draw="urn :oasis: names:tc :opendocument: xmlns:drawing:1.0" xmlns:presentation="urn :oasis: names:tc :opendocument: xmlns:►... xmlns:dr3d="urn :oasis: names:tc :opendocument: xmlns:dr3d:1.0" xmlns:chart="urn :oasis: names:tc :opendocument: xmlns:chart:1.0" xmlns:form="urn :oasis: names:tc :opendocument: xmlns:form:1.0" xmlns:script="urn :oasis: names:tc :opendocument: xmlns:script:1.0" xmlns:style="urn :oasis: names:tc :opendocument: xmlns:style:1.0" xmlns:number="urn :oasis: names:tc :opendocument: xmlns:datastyle:1.0" xmlns:anim="urn :oasis: names:tc :opendocument: xmlns:animation:1.0"... xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:fo="urn :oasis: names:tc :opendocument: xmlns:► xsl-fo-compatible:1.0" xmlns:svg="urn :oasis: names:tc :opendocument: xmlns:► svg-compatible:1.0" xmlns:smil="urn :oasis: names:tc :opendocument: xmlns:► smil-compatible:1.0" > xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer"... subtract one from the column number before dividing (so that the math works out right), and must add one to the result, because characters are numbered starting at one UsingOASISOpenDocument XML 127 Chapter 5 Spreadsheets 128 OASISOpenDocumentEssentials ... formatString UsingOASISOpenDocument XML 113 Chapter 5 Spreadsheets def endStr( self ): if (self.textStr != ""): textElement = self.document.createElement( "number:text" ) textNode = self.document.createTextNode( self.textStr ) textElement.appendChild( textNode ) self.fragment.appendChild( textElement ) self.textStr = "" def addCurrency( self ): self.endStr() node = self.fragment.appendChild( self.document.createElement(... nodes in the document, indexed by the value of their id attribute We will use this index, which has the name student-index, to access a student’s information from another node’s context UsingOASISOpenDocument XML 119 Chapter 5 Spreadsheets Here’s the first template: what to output when we encounter the root node of the document It will... In this case, we are using to modify the order in which the templates are applied to the selected nodes The first specifies the primary sort key; the second specifies the secondary key You may have as many keys as you need UsingOASISOpenDocument XML 123 Chapter 5 Spreadsheets When... zipfile import * from StringIO import * if (len(sys.argv) == 4): # Open an existing OpenDocument file # inFile = ZipFile( sys.argv[1] ) # and a brand new output file # outFile = ZipFile( sys.argv[2], "w", ZIP_DEFLATED ); getParameters( sys.argv[3] ) # # 108 modify all appropriate currency styles OASISOpenDocumentEssentials Case Study: Modifying a Spreadsheet # fixCurrency( "styles.xml" ) fixCurrency(... only, ignoring the content of the cell’s 104 OASISOpenDocumentEssentials Table Content Putting it all Together Figure 5.3, “Spreadsheet Showing Various Data Types” shows a simple spreadsheet with the default language set to Dutch (Netherlands) Figure 5.3 Spreadsheet Showing Various Data Types Showing you the actual XML would be more confusing than illuminating Instead, we’ve boiled down the... UsingOASISOpenDocument XML 109 Chapter 5 Spreadsheets Converting the XML Take a deep breath and hold on tight; this is the largest function in the program def fixCurrency( filename ): # # Read the styles.xml file as a string file # and create a disk file for output # dataSource = StringIO (inFile.read( filename )) tempFileName = "/tmp/workfile" dataSink = open(tempFileName, "w") # # Parse the document . table:formula="oooc:=TIME([.E1];[.E2];[.E3])"/>
106 OASIS OpenDocument Essentials
Table Content
If you are using an array formula which would be represented. appropriate for your system.)
Using OASIS OpenDocument XML 111
Chapter 5. Spreadsheets
We will convert the format strings to document fragments so that