简体   繁体   English

Rails axlsx gem-公式无法转义

[英]Rails axlsx gem - Formula not escaping

Is there a way to ignore executing a formula while rendering spreadsheet? 有没有一种方法可以在呈现电子表格时忽略执行公式?

Currently, sheet.add_row("=10+10") will evaluate 20, even if I give :formula => :false or :type=> :string 目前,即使我给出:formula => :false:type=> :stringsheet.add_row("=10+10")也会计算为20

The only hacky way is to provide a single quote, but it's not a pretty approach. 唯一的方法是提供单引号,但这不是一个好方法。

I found an answer for this in Stop Excel from automatically converting certain text values to dates 我在Stop Excel中从自动将某些文本值转换为日期中找到了答案

require 'axlsx'
Axlsx::Package.new do |p|
  p.workbook.add_worksheet(:name => 'DATA') do |sheet|
    sheet.add_row(['="10+10"', 'Maybe this is the best solution'])
    sheet.add_row(["'10+10", 'Hack with single quote'])
  end    
  p.serialize('test.xlsx')
end

This results in: 结果是:

在此处输入图片说明

I looked at the source code of the gem in question, the following code is there: 我查看了有问题的gem的源代码,其中有以下代码:

def is_formula?
   @type == :string && @value.to_s.start_with?('=')
end

It means that anything of type string with '=' will be treated like a formula. 这意味着任何类型为'='的字符串都将被视为公式。 And the only accepted types are date, string, integer, float etc. Anything else in place of :type => :string and it does not accept it. 唯一可接受的类型是日期,字符串,整数,浮点型等。其他任何代替:type =>:string的类型都不会接受。

As an alternative, I had to open the class cell_serializer.rb in the gem and reimplement the method in a custom way to get rid of cell.is_formula? 或者,我必须在gem中打开类cell_serializer.rb并以自定义方式重新实现该方法,以摆脱cell.is_formula? check. 校验。

def string_type_serialization(cell, str='')
      if cell.is_formula?
        formula_serialization cell, str
      elsif !cell.ssti.nil?
        value_serialization 's', cell.ssti.to_s, str
      else
        inline_string_serialization cell, str
      end
 end

Reimplemented method: 重新实现的方法:

def string_type_serialization(cell, str='')
        if !cell.ssti.nil?
          value_serialization 's', cell.ssti.to_s, str
        else
          inline_string_serialization cell, str
        end
end

I realize it's a hacky way, but it affects system wide code. 我意识到这是一种骇客的方式,但是会影响系统范围的代码。 If I need anything complex in future, I can always make changes to one central place. 如果将来我需要任何复杂的东西,我总是可以在一个中心位置进行更改。

I found another approach. 我找到了另一种方法。 In the approach mentioned in this answer, although the formula is not executed when the spreadsheet opens, it is evaluated if the user clicks on it and then blurs away. 答案中提到的方法中,尽管在打开电子表格时未执行该公式,但是会评估用户是否单击该公式然后使其模糊。 This might not be the best solution. 这可能不是最佳解决方案。

Better solution is to wrap any excel functions in TEXT function. 更好的解决方案是将所有excel函数包装在TEXT函数中。 This ensures the formulae is not executed. 这样可以确保不执行公式。

eg 例如

= 9 + 9 can be substituted with =TEXT("9+9","#"), and it will be printed as it is, without evaluation. = 9 + 9可以用= TEXT(“ 9 + 9”,“#”)代替,它将按原样打印,而无需评估。

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

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