如何使用Ruby在Rauds上使用AXLSX以自定义颜色格式化货币



我正在使用 axlsx gem生成excel表。

我使用以下代码在Excel中格式化货币单元格:

wb.styles.add_style(:format_code => '[Gray][A6A6A6]$#,##0_;[Red]($#,##0_)')

上面的代码可行,除了一个缺点-negative currencies are rendered in redpositive currencies are NOT rendered in Gray。为什么是?

另外,我转介了此链接-https://support.office.com/en-us/article/number-format-codes-5026bbd6-04bbd6-04bc-48cd-48cd-bf33-80f180f18b4eae68

上面的链接具有此段:

To specify the color for a section in the format code, type the name of one of the following eight colors in the code and enclose the name in square brackets as shown. The color code must be the first item in the code section.
[Black] [Blue] [Cyan] [Green] [Magenta] [Red] [White] [Yellow] 
  1. 这是否意味着我应该仅使用上述颜色之一?
  2. 我可以使用十六进制代码呈现自定义颜色 - 用十六进制代码A6A6A6?
  3. 说出浅灰色

请帮助!

  1. 这是否意味着我应该仅使用上述颜色之一?

如果您打算通过格式代码完全执行此操作,那么是的,因为这些是唯一受支持的颜色翻译,而其他任何内容都会导致无效的格式。(Excel通常称为"不可读的内容")

2.我可以使用十六进制代码呈现自定义颜色 - 用十六进制代码A6A6A6?

说出浅灰色

是的,您可以是因为您不需要在format_code中指定正值的颜色,而只需要在fg_color选项中指定默认颜色,例如So

  wb.styles.add_style(fg_color: 'A6A6A6', format_code: '$#,##0.00_);[Red]($#,##0.00)')

现在,默认颜色将为" A6A6A6",如果数字为负,则[RED]将覆盖默认颜色。

[暗示] 3.如果我也想要零颜色的颜色(链接页面中的第3部分)

如果您需要其他功能,您也可以查看这样的条件格式:

  zero_style = wb.styles.add(fg_color: 'FF69B4')  
  ws.add_conditional_formatting("A:A", # Indicates the cell range
      { type: :cellIs, 
        operator: :equal, 
        formula: "0", 
        dxfId:  zero_style, 
        priority: 1 
      })

现在:(假设上述所有)

  • 正值将为灰色(#a6a6a6)[默认]
  • 负值将为红色([red]),并格式为($#,## 0.00)[格式代码]
  • 零值将为粉红色(#ff69b4)[条件格式]