Skip to content

Commit

Permalink
v1.3 fixed repeater bug
Browse files Browse the repository at this point in the history
  • Loading branch information
phonixor committed Jul 3, 2014
1 parent b34d93a commit 6ae43ce
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
14 changes: 5 additions & 9 deletions R/readODS.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ library(XML)
#
#TODO: test gnummeric and koffice generated ODS files
#TODO: check if emtpy rows are the only ones with "number-rows-repeated"...
# ALSO number-columns-repeated
# ALSO number-columns-repeated -- FIXED
#
# the thing i should have probably read (AKA the ODS standard :P):
# http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#__RefHeading__1420324_253892949
Expand Down Expand Up @@ -49,14 +49,13 @@ read.ods=function(file=NULL, sheet=NULL, formulaAsFormula=F){
for(sheeti in sheets[names(sheets)=="table"]){
sheetIndex=sheetIndex+1
#sheeti=sheets[[3]]

d=list()
d[1]="" # avoid bug later on if no rows
# fill it
rowIndex=0
for(row in sheeti[names(sheeti)=="table-row"]){
rowIndex=rowIndex+1
# print(rowIndex)
if(!is.na(xmlAttrs(row)["number-rows-repeated"])){
# only on empty rows???
rowIndex=rowIndex+as.integer(xmlAttrs(row)[["number-rows-repeated"]])-1
Expand All @@ -69,17 +68,12 @@ read.ods=function(file=NULL, sheet=NULL, formulaAsFormula=F){
if(is.null(xmlAttrs(cell))){ # silly liblre office has: <table:table-cell/>
next
}
# print(colIndex)
print(paste("row:",rowIndex," col:",colIndex,sep=""))
# print(paste("row:",rowIndex," col:",colIndex,sep=""))
#<table:table-cell table:number-columns-repeated="3"/>
# colsRepeated=1
# print(names(cell))
# print(length((names(cell))))
if(!is.na(xmlAttrs(cell)["number-columns-repeated"]) && length((names(cell)))==0 ){
# print(as.integer(xmlAttrs(cell)[["number-columns-repeated"]]))
# repeat empty columns
colIndex=colIndex+as.integer(xmlAttrs(cell)[["number-columns-repeated"]])-1
# colsRepeated=as.integer(xmlAttrs(cell)[["number-columns-repeated"]]
next
}
# display the formula instead of its result
Expand Down Expand Up @@ -265,6 +259,8 @@ lettersToNumber=function( listOfStrings=NULL){
#' libre office can do crap like this:
#' <text:p>></text:p>
#' which is not valid xml... and the XML package doesn't like that..
#' OK NM NOW IT DOES! leaving the code here in case of changes...
#'
#' also to not make the code fulgy as all hell, the content.xml file is first scanned for these XML violations, and then fixed!
#' <text:p>></text:p> --> <text:p>&gt</text:p>
#' @param file - the xml file to be parsed...
Expand Down
1 change: 1 addition & 0 deletions notes.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ read.ods(file,formulaAsFormula = T)
read.ods(file ,sheet=1)



read.ods(file,usePreParser = F)

odsPreParser(file)
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test_readODS.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,33 @@ test_that("read.ods", {
stringsAsFactors = F)
expect_equal(read.ods(file, sheet=1),df)

file=paste("../testdata/layout_test.ods",sep="")
sheet1=read.ods(file, sheet=1)
expect_equal(sheet1[8,"F"],"empty") # this is a repeated element

sheet2=read.ods(file, sheet=2)
expect_equal(dim(sheet2),c(22,13))
expect_true(all(sheet1[21,]==sheet2[22,]))

file=paste("../testdata/multisheet.ods",sep="")
df=data.frame(matrix("",14,7),stringsAsFactors = F)
df[1,1]="COOKIES"
df[4,2]="1"
df[6,3]="2"
df[8,3]="3"
df[14,4]="3"
df[7,5]="3"
df[9,5]="1"
df[10,7]="1"
sheet2=read.ods(file, sheet=2)
expect_true(all(sheet2==df))



# this test will fail on windows!!!
# file=paste("../testdata/test.ods",sep="")
# sheet1=read.ods(file, sheet=1)
# expect_equal(sheet1[3,"E"],'lalala lalala,,””,,')

})

Expand Down

0 comments on commit 6ae43ce

Please sign in to comment.