简体   繁体   English

在R中的read.xlsx之前强制执行Excel工作簿评估?

[英]Force Excel Workbook evaluation before read.xlsx in R?

I am reading Excel Workbooks (.xlsx format) with read.xlsx() from the xlsx package. 我正在从xlsx包中读取带有read.xlsx() Excel工作簿(.xlsx格式)。 My Workbooks have dates stored as functions (eg, =DATE(1900, 1, 1) ) rather than strings or fractional days from 1899-12-31 (eg, 1/1/1900 or 1 ). 我的工作簿中的日期存储为函数(例如=DATE(1900, 1, 1) 1/1/1900 =DATE(1900, 1, 1) ),而不是字符串或1/1/1900日至31日之间的小数天(例如1/1/19001 )。

This is not a problem for read.xlsx if the Workbook has ever been opened/evaluated. 如果工作簿曾经被打开/评估过,对于read.xlsx这不是问题。 But many of these Workbooks have never been opened/evaluated (they're generated by my data provider, not by Excel). 但是许多这些工作簿从未被打开/评估过(它们是由我的数据提供者而不是Excel生成的)。 Because the Workbook has not been opened/evaluated it contains -1 rather than the actual fractional number of days since 1899-12-31 (eg, 37036 for May 25, 2001). 由于尚未打开/评估工作簿,因此它包含-1,而不是自1899-12-31以来的实际小数天数(例如,2001年5月25日为37036)。 I learned this here . 我在这里学到的。 So if I read a date in one of these (never opened) Workbooks, I get a column of -1, which read.xlsx interprets as 1899-12-30 (ie, one day before 1899-12-31). 因此,如果我从其中一本(从未打开过的)工作簿中读取日期,则会得到一列-1,其中read.xlsx解释为read.xlsx (即read.xlsx之前的一天)。

Is there a fix here? 这里有解决办法吗? Can I touch a Workbook to force evaluation? 我可以touch工作簿以进行评估吗? Is there a bit that I can flip in read.xlsx to force evaluation first? 我是否可以read.xlsx来强制执行评估? I see only a colClasses argument, which doesn't seem to be the problem? 我只看到一个colClasses参数,这似乎不是问题吗? Can I avoid using an Excel VBA script to write these Workbooks to text? 我可以避免使用Excel VBA脚本将这些工作簿写成文本吗? Sorry I can't provide any reproducible code, but if you open an Excel Workbook to enter =Date() , the problem won't exist. 抱歉,我无法提供任何可复制的代码,但是如果您打开Excel工作簿以输入=Date() ,则该问题将不存在。

Using XLConnect package , You can use : 使用XLConnect软件包,可以使用:

getForceFormulaRecalculation(object,sheet)

to compute for all sheets : 计算所有工作表:

getForceFormulaRecalculation(wb, sheet = "*")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM