File: gnumericHandler.R

package info (click to toggle)
r-cran-xml 3.99-0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: ansic: 6,659; xml: 2,890; asm: 486; sh: 12; makefile: 2
file content (35 lines) | stat: -rw-r--r-- 947 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#
# Should turn this into a data frame rather than a matrix.
# This would allow us to preserve different data types across
# columns/variables. Of course, there isn't an exact one-to-one
# correspondence between spreadsheets and data frames.


gnumericHandler <- 
function(fileName)
{

    # read the XML tree from the file.
  d <- xmlTreeParse(fileName)
    #  Get the Sheet
  sh <- d$doc$children[["Workbook"]]$children[["Sheets"]]$children[["Sheet"]]$children

  mat <- matrix(0, as.integer(sh$MaxRow$children[[1]]$value)+1,  as.integer(sh$MaxCol$children[[1]]$value)+1)
  vals <- sh$Cells$children


  gnumericCellEntry <- function(x)
   {
     atts <- sapply(x$attributes, as.integer)
     val <- x$children$Content$children$text$value

     tmp <-  switch(atts[["Style"]], "1"= as.numeric(val), "2"=as.numeric(val), "3"=val)
     mat[atts[["Row"]]+1, atts[["Col"]]+1] <<- tmp
     tmp
  }

  sapply(vals, gnumericCellEntry)


 return(mat)
}