Module: Rspreadsheet::Tools

Defined in:
lib/rspreadsheet/tools.rb

Overview

this module contains methods used bz several objects

Class Method Summary collapse

Class Method Details

.convert_cell_address(*coords) ⇒ Object

converts cell adress like ‘F12’ to pair od integers [row,col]



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rspreadsheet/tools.rb', line 6

def self.convert_cell_address(*coords)
  if coords.length == 1
    coords[0].match(/^([A-Z]{1,3})(\d{1,8})$/)
    colname = $~[1]
    rowname = $~[2]
  elsif coords.length == 2
    colname = coords[0]
    rowname = coords[1]
  else
    raise 'Wrong number of arguments'
  end
    
  colname=colname.rjust(3,'@')
  col = (colname[-1].ord-64)+(colname[-2].ord-64)*26+(colname[-3].ord-64)*26*26
  row = rowname.to_i
  return [row,col]
end

.get_namespace(prefix) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rspreadsheet/tools.rb', line 23

def self.get_namespace(prefix)
  ns_array = {
    'office'=>"urn:oasis:names:tc:opendocument:xmlns:office:1.0",
    'style'=>"urn:oasis:names:tc:opendocument:xmlns:style:1.0",
    'text'=>"urn:oasis:names:tc:opendocument:xmlns:text:1.0",
    'table'=>"urn:oasis:names:tc:opendocument:xmlns:table:1.0",
    'draw'=>"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",
    'fo'=>"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0",
    'xlink'=>"http://www.w3.org/1999/xlink",
    'dc'=>"http://purl.org/dc/elements/1.1/",
    'meta'=>"urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
    'number'=>"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
    'presentation'=>"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",
    'svg'=>"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0",
    'chart'=>"urn:oasis:names:tc:opendocument:xmlns:chart:1.0",
    'dr3d'=>"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0",
    'math'=>"http://www.w3.org/1998/Math/MathML",
    'form'=>"urn:oasis:names:tc:opendocument:xmlns:form:1.0",
    'script'=>"urn:oasis:names:tc:opendocument:xmlns:script:1.0",
    'ooo'=>"http://openoffice.org/2004/office",
    'ooow'=>"http://openoffice.org/2004/writer",
    'oooc'=>"http://openoffice.org/2004/calc",
    'dom'=>"http://www.w3.org/2001/xml-events",
    'xforms'=>"http://www.w3.org/2002/xforms",
    'xsd'=>"http://www.w3.org/2001/XMLSchema",
    'xsi'=>"http://www.w3.org/2001/XMLSchema-instance",
    'rpt'=>"http://openoffice.org/2005/report",
    'of'=>"urn:oasis:names:tc:opendocument:xmlns:of:1.2",
    'xhtml'=>"http://www.w3.org/1999/xhtml",
    'grddl'=>"http://www.w3.org/2003/g/data-view#",
    'tableooo'=>"http://openoffice.org/2009/table",
    'drawooo'=>"http://openoffice.org/2010/draw",
    'calcext'=>"urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0",
    'loext'=>"urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0",
    'field'=>"urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0",
    'formx'=>"urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0",
    'css3t'=>"http://www.w3.org/TR/css3-text/"
  }
  if @pomnode.nil?
    @pomnode = LibXML::XML::Node.new('xxx')
  end
  if @ns.nil? then @ns={} end
  if @ns[prefix].nil?
    @ns[prefix] = LibXML::XML::Namespace.new(@pomnode, prefix, ns_array[prefix])
  end
  return @ns[prefix]
end

.get_ns_attribute(node, ns_prefix, key) ⇒ Object



87
88
89
# File 'lib/rspreadsheet/tools.rb', line 87

def self.get_ns_attribute(node,ns_prefix,key)
  node.attributes.get_attribute_ns(Tools.get_namespace(ns_prefix).href,key)
end

.get_ns_attribute_value(node, ns_prefix, key) ⇒ Object



90
91
92
# File 'lib/rspreadsheet/tools.rb', line 90

def self.get_ns_attribute_value(node,ns_prefix,key)
  Tools.get_ns_attribute(node,ns_prefix,key).andand.value
end

.remove_ns_attribute(node, ns_prefix, key) ⇒ Object



93
94
95
96
# File 'lib/rspreadsheet/tools.rb', line 93

def self.remove_ns_attribute(node,ns_prefix,key)
  node.attributes.get_attribute_ns(Tools.get_namespace(ns_prefix).href,key)
  attr.remove! unless attr.nil? 
end

.set_ns_attribute(node, ns_prefix, key, value, delete_value = nil) ⇒ Object

sets namespaced attribute “ns_prefix:key” in node to value. if value == delete_value then remove the attribute



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rspreadsheet/tools.rb', line 71

def self.set_ns_attribute(node,ns_prefix,key,value,delete_value=nil)
  ns = Tools.get_namespace(ns_prefix)
  attr = node.attributes.get_attribute_ns(ns.href, key)
  
  unless value==delete_value # set attribute
    if attr.nil? # create attribute if needed
      attr = LibXML::XML::Attr.new(node, key,'temporarilyempty')
      attr.namespaces.namespace = ns
    end
    attr.value = value.to_s
    attr
  else # remove attribute
    attr.remove! unless attr.nil? 
    nil
  end
end