xlconnect
xlconnect copied to clipboard
R1C1 reference method not supported
Trying to use R1C1 references in formula for createName() result in an error. Steps to reproduce the problem:
demoExcelFile <- system.file("demoFiles/multiregion.xlsx", package = "XLConnect")
createName(demoExcelFile,"DemoRange",formula="FirstSheet!R2C2:R3C9")
Expected Result: A name ("DemoRange") is created referring to the range "B2:C9" in the sheet "FirstSheet". Result Instead: rror: IllegalArgumentException (Java): org.apache.poi.ss.formula.FormulaParseException: Cell reference expected after sheet name
Suggested Solution: If you detect that the formula contains R1C1 references pass those to the following formula to convert them to acceptable parameters.
ConvertFromR1C1ToA1<- function(AddressString){
if (class(AddressString)!="character"){
return("")
}
Result<-rep(NA,length(AddressString))
for (i in 1:length(AddressString)){
if (regexpr("[Rr]\\d+[Cc]\\d+",AddressString[i])<0){
return("")
}
Coordinates<-regmatches(AddressString[i],regexec("[Rr](\\d+)[Cc](\\d+)",AddressString[i]))[[1]]
RowNumber<-as.numeric(Coordinates[2])
ColNumber<-as.numeric(Coordinates[3])
if (RowNumber<=0 || RowNumber>65536 || ColNumber<=0 || ColNumber>256){
return("")
}
SecondLetterIndex=0
while(ColNumber>26){
SecondLetterIndex=SecondLetterIndex+1
ColNumber= ColNumber- 26
}
ColumnIndication<- rawToChar(as.raw(64+ColNumber))
if (SecondLetterIndex>0){
ColumnIndication<- paste(rawToChar(as.raw(64+SecondLetterIndex)),ColumnIndication,sep="")
}
Result[i]<-paste(ColumnIndication,RowNumber,sep="")
}
return(Result)
}
I'll check the createName() code and submit a pull request with that solution this weekend if you don't fix it even earlier