Author: miguel
Date: 2008-02-11 20:52:51 -0500 (Mon, 11 Feb 2008)
New Revision: 95477
Added:
trunk/wahid/Formula.cs
Modified:
trunk/wahid/Cell.cs
trunk/wahid/Makefile
trunk/wahid/Value.cs
trunk/wahid/main.cs
trunk/wahid/xlsx.cs
Log:
Do more parsing
Modified: trunk/wahid/Cell.cs
===================================================================
--- trunk/wahid/Cell.cs 2008-02-12 01:29:28 UTC (rev 95476)
+++ trunk/wahid/Cell.cs 2008-02-12 01:52:51 UTC (rev 95477)
@@ -30,11 +30,14 @@
namespace Wahid {
public class Cell {
+ public static Value DefaultValue = new StringValue ("");
public Sheet Sheet { get; private set; }
public int Col { get; set; }
public int Row { get; set; }
+ Value val;
+
/// <summary>
/// Internal constructor, must only be called by
Sheet.CreateCell
/// </summary>
@@ -43,8 +46,29 @@
Sheet = sheet;
Col = col;
Row = row;
+ val = DefaultValue;
+ }
- Console.WriteLine ("Creating cell {0} {1}", row, col);
+ public Value Value {
+ get {
+ return val;
+ }
+
+ set {
+ val = value;
+ }
}
+
+ Formula formula;
+
+ public Formula Formula {
+ get {
+ return formula;
+ }
+
+ set {
+ formula = value;
+ }
+ }
}
}
\ No newline at end of file
Added: trunk/wahid/Formula.cs
===================================================================
--- trunk/wahid/Formula.cs 2008-02-12 01:29:28 UTC (rev 95476)
+++ trunk/wahid/Formula.cs 2008-02-12 01:52:51 UTC (rev 95477)
@@ -0,0 +1,39 @@
+//
+// Formula.cs
+//
+// Authors:
+// Miguel de Icaza ([EMAIL PROTECTED])
+//
+// Copyright 2008 Novell, Inc (http://www.novell.com).
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+using System;
+
+namespace Wahid {
+
+ public class Formula {
+
+ public Formula (string s)
+ {
+ // Parse the beast.
+ }
+ }
+}
\ No newline at end of file
Property changes on: trunk/wahid/Formula.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/wahid/Makefile
===================================================================
--- trunk/wahid/Makefile 2008-02-12 01:29:28 UTC (rev 95476)
+++ trunk/wahid/Makefile 2008-02-12 01:52:51 UTC (rev 95477)
@@ -1,4 +1,4 @@
-SOURCES = Cell.cs Locale.cs Sheet.cs Workbook.cs ZipAccess.cs Value.cs xlsx.cs
+SOURCES = Cell.cs Formula.cs Locale.cs Sheet.cs Workbook.cs ZipAccess.cs
Value.cs xlsx.cs
DESKTOP_SOURCES = DirAccess.cs
Modified: trunk/wahid/Value.cs
===================================================================
--- trunk/wahid/Value.cs 2008-02-12 01:29:28 UTC (rev 95476)
+++ trunk/wahid/Value.cs 2008-02-12 01:52:51 UTC (rev 95477)
@@ -25,6 +25,7 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
+using System.Xml.Linq;
namespace Wahid {
@@ -38,13 +39,20 @@
{
this.Value = v;
}
+
+ public static implicit operator BoolValue (bool v)
+ {
+ return new BoolValue (v);
+ }
}
public class ErrorValue : Value {
- public ErrorValue ()
+ public string Error;
+
+ public ErrorValue (string v)
{
+ Error = v;
}
-
}
public class StringValue : Value {
@@ -54,8 +62,27 @@
{
this.Value = v;
}
+
+ public static implicit operator StringValue (string s)
+ {
+ return new StringValue (s);
+ }
}
+ public class RichStringValue : Value {
+ public XElement Value;
+
+ public RichStringValue (XElement x)
+ {
+ this.Value = x;
+ }
+
+ public static implicit operator RichStringValue (XElement d)
+ {
+ return new RichStringValue (d);
+ }
+ }
+
public class NumberValue : Value {
public double Value;
@@ -63,5 +90,10 @@
{
Value = v;
}
+
+ public static implicit operator NumberValue (double d)
+ {
+ return new NumberValue (d);
+ }
}
}
\ No newline at end of file
Modified: trunk/wahid/main.cs
===================================================================
--- trunk/wahid/main.cs 2008-02-12 01:29:28 UTC (rev 95476)
+++ trunk/wahid/main.cs 2008-02-12 01:52:51 UTC (rev 95477)
@@ -1,6 +1,6 @@
using System;
using Wahid;
-using Wahid.OOXML;
+using Wahid.SpreadsheetML;
class Test {
static void Main ()
Modified: trunk/wahid/xlsx.cs
===================================================================
--- trunk/wahid/xlsx.cs 2008-02-12 01:29:28 UTC (rev 95476)
+++ trunk/wahid/xlsx.cs 2008-02-12 01:52:51 UTC (rev 95477)
@@ -37,8 +37,17 @@
using System.Reflection;
-namespace Wahid.OOXML {
+namespace Wahid.SpreadsheetML {
+ public enum CellType {
+ Boolean,
+ Error,
+ InlineString, // value inside <is> not inside <v> in the cell.
+ Number,
+ SharedString,
+ FormulaString
+ }
+
public class LoadException : Exception {
public LoadException (string msg) : base (msg) {}
}
@@ -83,23 +92,65 @@
}
- public class OOXML {
-
+ static public class OOXML {
+ public static XNamespace ns_workbook =
"http://schemas.openxmlformats.org/spreadsheetml/2006/main";
+ public static XNamespace ns_rels =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships";
+
+ public static XName c = "c";
+ public static XName f = ns_workbook + "f";
+ public static XName r = "r";
+ public static XName s = "s";
+ public static XName t = "t";
+ public static XName v = ns_workbook + "v";
+ public static XName is_ = "is";
+
+ public static void ParseReference (string s, out int col, out
int row)
+ {
+ col = 0;
+ row = 0;
+
+ if (s == null)
+ return;
+
+ foreach (char c in s){
+ if (c >= 'A' && c <= 'Z'){
+ col = col * 26 + ((int) c) - 'A';
+ } else {
+ row = row * 10 + ((int) c - '0');
+ }
+ }
+ }
+
+ public static CellType GetCellType (XElement xcell, CellType
defvalue)
+ {
+ string type = (string) xcell.Attribute (t);
+
+ switch (type){
+ case "b":
+ return CellType.Boolean;
+ case "e":
+ return CellType.Error;
+ case "inlineStr":
+ return CellType.InlineString;
+ case "n":
+ return CellType.Number;
+ case "s":
+ return CellType.SharedString;
+ case "str":
+ return CellType.FormulaString;
+ case null:
+ return defvalue;
+ }
+ throw new LoadException ("Unknown type for cell: " +
type);
+ }
}
public class Xlsx {
- static XNamespace ns_workbook =
"http://schemas.openxmlformats.org/spreadsheetml/2006/main";
- static XNamespace ns_rels =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships";
-
- static XName xnc = "c";
- static XName xnr = "r";
- static XName xns = "s";
- static XName xnt = "t";
-
public Workbook Workbook { get; private set; }
public string Error { get; private set; }
Dictionary<string,Relationship> relationships;
+ List<Value> sharedstrings;
ZipAccess loader;
@@ -162,20 +213,29 @@
relationships = x.ToDictionary (f => f.Id);
}
- void ParseReference (string s, out int col, out int row)
+ void LoadSharedStrings ()
{
- col = 0;
- row = 0;
+ sharedstrings = new List<Value> ();
- foreach (char c in s){
- if (c >= 'A' && c <= 'Z'){
- col = col * 26 + ((int) c) - 'A';
- } else {
- row = row * 10 + ((int) c - '0');
- }
+ XElement xsharedstrings = null;
+
+ try {
+ xsharedstrings = XElement.Load (Get
("xl/sharedStrings.xml"));
+ } catch {
+ // not an error to not have sharedStrings
+ return;
}
+
+ foreach (var ssi in xsharedstrings.Elements ()){
+ XElement first = ssi.FirstNode as XElement;
+
+ if (first.Name == OOXML.t)
+ sharedstrings.Add (new StringValue
(first.Value));
+ else
+ sharedstrings.Add (new RichStringValue
(first));
+ }
}
-
+
void LoadSheetData (Sheet sheet, XElement xsheetData)
{
// <row>
@@ -185,9 +245,10 @@
// <c>
foreach (XElement xcell in xrow.Elements ()){
int cell_col, cell_row;
-
- ParseReference ((string)
xcell.Attribute (xnr), out cell_col, out cell_row);
+ // r=
+ OOXML.ParseReference ((string)
xcell.Attribute (OOXML.r), out cell_col, out cell_row);
+
if (row != cell_row){
// this should not happen
@@ -197,22 +258,61 @@
Cell cell = sheet.CreateCell (cell_col,
cell_row);
- string type = (string) xcell.Attribute
(xnt);
+ //CellType type = OOXML.GetCellType
(xcell, CellType.Number);
+ string type = (string) xcell.Attribute
(OOXML.t);
- //
- // HERE:
- //
- // Parse type of cell, get data from
the cell, put that inside
- // the cell
- //
+ foreach (XElement cval in
xcell.Elements ()){
+ if (type == "inlineStr"){
+ if (cval.Name ==
OOXML.is_)
+ cell.Value =
new RichStringValue (cval);
+ else
+ throw new
LoadException ("Cell type is InlineString, but found other values");
+ }
+
+ if (cval.Name == OOXML.f){
+ cell.Formula = new
Formula (cval.Value);
+ continue;
+ }
+
+ if (cval.Name == OOXML.v){
+ string s = cval.Value;
+ bool v;
+
+ switch (type){
+ case "b":
+ if
(bool.TryParse (s, out v))
+
cell.Value = new BoolValue (v);
+ else
+
cell.Value = new BoolValue (int.Parse (s) == 1);
+ break;
+
+ case "e":
+ cell.Value =
new ErrorValue (s);
+ break;
+
+ case "n":
+ cell.Value =
new NumberValue (double.Parse (s));
+ break;
+
+ case "s":
+ // Lookup the
string in the share string table
+ cell.Value =
sharedstrings [Int32.Parse (s)];
+ break;
+
+ case "str":
+ cell.Value =
new StringValue (cval.Value);
+ break;
+ }
+ }
+ }
}
}
}
void LoadSheet (Sheet sheet, XElement xsheet)
{
- XName xcols = ns_workbook + "cols";
- XName xsheetData = ns_workbook + "sheetData";
+ XName xcols = OOXML.ns_workbook + "cols";
+ XName xsheetData = OOXML.ns_workbook + "sheetData";
foreach (XElement se in xsheet.Elements ()){
if (se.Name == xsheetData)
@@ -226,8 +326,8 @@
// Since Linq.XPath is not working, work around it
var sheets_and_sources = from l in xwork.Elements ()
- let xsheets = ns_workbook + "sheets"
- let xrid = ns_rels + "id"
+ let xsheets = OOXML.ns_workbook + "sheets"
+ let xrid = OOXML.ns_rels + "id"
where l.Name == xsheets
from s in l.Elements ()
let rtarget = relationships [(string)
s.Attribute (xrid)].Target
@@ -238,11 +338,12 @@
foreach (var ss in sheets_and_sources)
LoadSheet (ss.sheet, XElement.Load (Get ("xl/"
+ ss.rtarget)));
}
-
+
void Load ()
{
LoadRelationships ();
// LoadStyle ();
+ LoadSharedStrings ();
LoadWorkbook ();
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches