OOXML - 使用 Ruby 创建的电子表格 (.XLSX) 不会重新计算
我正在构建一个 ruby 类/组件,用于在我的 Rails 项目中基于 Excel .xlsx 文件创建报告/导出。使用该组件,我可以打开“模板”.xlsx 文件,将行数据添加到工作表中,保存文件,然后将其下载给用户。几个月以来,它一直运行良好。
现在,我需要获取一个预先存在的 .xlsx 文件(认为是“表单”),将其作为模板打开,在多个单元格中插入值,然后保存并下载给用户。在大多数情况下,这个过程是有效的。一个问题是,我用数据更新的一个单元格位于应用了 SUM 函数的单元格范围内。问题:SUM 单元格中没有正确的总和。
我在下载时检查了 Excel 中的单元格以及底层 xml - 单元格及其数据是数字 - 而不是文本。当我尝试手动重新计算工作表时 - 虚无。我可以更新正在求和的范围内的其他单元格之一,它神奇地开始工作 - SUM 单元格显示正确的总数。
我今天早些时候读过一篇文章,其中提到从总计字段中删除该元素,以便在打开电子表格时向 Excel 发出信号,表明它应该重新计算 - 不。
一旦我进一步了解这个组件,我真的很想开源这个组件;我认为这将对 Ruby 社区有很大帮助。预先感谢您的任何帮助!
I am building a ruby class/component to use in my Rails projects for creating reports/exports based on Excel .xlsx files. With the component, I can open a "template" .xlsx file, add data in rows to a sheet, save and then download the file to the user. It has been working well for several months now.
Now I need to take a pre-existing .xlsx file (think "form"), open it as a template, insert values in several of the cells, and then save and download to the user. For the most part, the process works. The one hitch is that one of the cells I am updating with data is within a range of cells that gets a SUM function applied to it. The problem: the SUM cell doesn't have the correct sum in it.
I've checked the cell both in Excel upon download, and also the underlying xml - the cell and its data is numeric - not text. When I try to manually recalc the sheet - nada. I can update one of the other cells in the range that is getting SUM'd, and it magically starts working - the SUM cell shows the proper total.
I read a post earlier today that mentioned removing the element from the total field in order to signal to Excel when the spreadsheet is opened that it should recalc - nope.
I'd really like to open source this component once I get this further along; I think it would be a BIG help to the Ruby community. Thanks in advance for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您需要将
calcPr
元素的fullCalcOnLoad
属性设置为 true:这将导致 Excel 文档在打开文件时在所有工作簿中执行计算。
Sounds like you need to set the
fullCalcOnLoad
attribute of thecalcPr
element to true:This will cause the Excel document to perform the caluclations within all the workbooks when the file is opened.