【问题标题】:jsreport handlebars engine and xlsx recipe: how to fill cell with colorjsreport 车把引擎和 xlsx 配方:如何用颜色填充单元格
【发布时间】:2020-06-10 21:02:04
【问题描述】:

我正在尝试用 "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,如果有人可以帮助我找出我做错了什么?!

【问题讨论】:

    标签: handlebars.js xlsx jsreport


    【解决方案1】:

    它的(zero-based index) 所以s="1" 将是第二个元素&lt;cellXfs&gt;

    只需记住从零开始的索引,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>
    

    【讨论】:

    • 即使它声明为零索引,如果您传递 s="0" 它将不起作用,请先在 中添加一个 并像它以 '1 开头一样使用它'不是'0'
    • 链接中没有使用填充颜色渲染任何内容?
    • {{ this.value }} 你必须通过"s" number = xf index(from zero) 为了安全起见,在 cellXfs 中添加超过 5
    • 我建议复制这些样式/代码并从那里开始工作,因为 xml 可能非常棘手playground.jsreport.net/w/anon/SyupOZ2Dz-5
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多