jsreport handlebars 引擎和 xlsx 配方:如何用颜色填充单元格

jsreport handlebars engine and xlsx recipe: how to fill cell with color

我正在尝试根据单元格的值用 "red""green" 填充单元格;这是我到目前为止尝试过的:

{{#xlsxAdd 'xl/worksheets/sheet1.xml' "worksheet.conditionalFormatting"}}
    <conditionalFormatting sqref="G7:M7">
        <cfRule type="cellIs" dxfId="0" priority="1" operator="notBetween">
            <formula>{{productObj.min}}</formula>
            <formula>{{productObj.max}}</formula>
        </cfRule>
    </conditionalFormatting>
{{/xlsxAdd}}
{{#xlsxAdd 'xl/worksheets/sheet1.xml' "worksheet.conditionalFormatting"}}
    <conditionalFormatting sqref="G7:M7">
        <cfRule type="cellIs" dxfId="2" priority="1" operator="between">
            <formula>{{productObj.min}}</formula>
            <formula>{{productObj.max}}</formula>
        </cfRule>
    </conditionalFormatting>
{{/xlsxAdd}}

但条件 notBetween 似乎总是与单元格匹配,但在我下载文件然后更改单元格的值后,条件开始正常工作。

我不知道 reportjs 是否将我的数字视为 strings,因为在下载文件后更改值非常有效。

-- 更新 ---

发现这个问题:https://github.com/jsreport/jsreport-html-to-xlsx/issues/7

jsreport 本身有问题,在我的数字前添加 ' 导致条件渲染失败。 但是,我仍然希望有一种正确的方法来用条件格式填充单元格 "NOT" 所以我会留下问题

--- 结束更新---

我也试过用静态颜色填充这个但是没用:

 {{#xlsxReplace "xl/styles.xml" "styleSheet.fills"}}
    <fills count="5">
        <fill>
            <patternFill patternType="none"/>
        </fill>
        <fill>
            <patternFill patternType="lightGray"/>
        </fill>
        <fill>
            <patternFill patternType="solid">
                <fgColor rgb="FFBFBFBF"/>
                <bgColor rgb="FFBFBFBF"/>
            </patternFill>
        </fill>
        <fill>
            <patternFill patternType="solid">
                <fgColor rgb="FFFF0000"/>
                <bgColor rgb="FFFF0000"/>
            </patternFill>
        </fill>
        <fill>
            <patternFill patternType="solid">
                <fgColor rgb="FF00FF00"/>
                <bgColor rgb="FF00FF00"/>
            </patternFill>
        </fill>
    </fills>
{{/xlsxReplace}}
// then adding the cell like this:
 <c t="inlineStr" fillId="3" ><is><t>{{val}}</t></is></c>

但仍然没有完成这项工作,我什至尝试添加 dxfs 而不是 fills,如果有人能帮我找出我做错了什么?!

它的 (zero-based index) 所以 s="1" 将是第二个元素 <cellXfs>

记住从零开始的索引,counts 属性就是里面元素的计数

在主 jsreport 文件中添加

{{#xlsxReplace "xl/styles.xml"}}
    {#asset style.xml}}
{{/xlsxReplace}} 

创建 style.xml 并在其中添加

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
    <fonts count="2" x14ac:knownFonts="2">
        <font>
            <sz val="12" />
            <name val="Calibri" />
            <b />
        </font>
        <font>
            <sz val="12" />
            <name val="Calibri" />
        </font>        
    </fonts>
    <fills count="5">
        <fill>
            <patternFill patternType="solid">
                <fgColor rgb="bfbfbf" />
            </patternFill>
        </fill>
        <fill>
            <patternFill patternType="solid">
                <fgColor rgb="bfbfbf" />
            </patternFill>
        </fill>            
        <fill>
            <patternFill patternType="solid">
                <fgColor rgb="bfbfbf" />
            </patternFill>
        </fill>    
        <fill>
            <patternFill patternType="solid">
                <fgColor rgb="FFFF00" />
            </patternFill>
        </fill>
        <fill>
            <patternFill patternType="solid">
                <fgColor rgb="FF0000" />
            </patternFill>
        </fill>      
    </fills>
    <borders count="1">
        <border>
            <left/>
            <right/>
            <top/>
            <bottom/>
            <diagonal/>
        </border>
    </borders>
    <cellXfs count="7">
        <xf />
        <xf fontId="0" fillId="2">
            <alignment horizontal="center" vertical="center" />
        </xf>
        <xf fontId="0" fillId="3">
            <alignment horizontal="center" vertical="center" />
        </xf>
        <xf fontId="0" fillId="4">
            <alignment horizontal="center" vertical="center" />
        </xf>
        <xf fontId="1">
            <alignment horizontal="center" vertical="center" />
        </xf>
        <xf fontId="1" fillId="3">
            <alignment horizontal="center" vertical="center" />
        </xf>
        <xf fontId="1" fillId="4">
            <alignment horizontal="center" vertical="center" />
        </xf>
    </cellXfs>   
</styleSheet>