FineReport Formula in JavaScript

  • Last update:April 25, 2025
  • Overview

    When designing reports, you may need to write JavaScript code to handle complex calculations or conversions, such as date formatting, string extraction, and string replacement. If you want to display the current date in the format 2017-02-24 using native JavaScript code, the following code is required:

    var da = new Date(); 
    var year = da.getFullYear(); 
    var month = da.getMonth() + 1; 
    var day = da.getDate();
    if (month < 10) month = '0' + month; 
    if (day < 10) day = '0' + day;  
    var rq = year + '-' + month + '-' + day; 
    alert(rq);

    Although the above code achieves the desired result, the code is lengthy and lacks reusability. If a different format is needed, the code must be modified again. Therefore, is there a simpler way to write JavaScript code in FineReport? FineReport provides built-in date formatting functions, which can be directly called in JavaScript code.

    This document introduces how to call FineReport formulas in JavaScript code.

    Method

    Method One

    In JavaScript, you can use FineReport's built-in formulas directly by using the syntax ${=Formula}. For example, to get the current date, you can use the built-in formula today().

    iconNote:
    Not all formulas can be used with this method.
    var rq='${=today()}'; 
    alert(rq);

    If you want to click a button to display the current date, you can enter the above code in the Click event of the button.

    image.png

    You can preview the report to obtain the current date.

    Method Two

    You can execute functions by using the remote formula FR.remoteEvaluate('=Formula') provided by FineReport. To meet the same needs described in section "Overview", the JavaScript code is as follows:

    iconNote:
    FR.remoteEvaluate('=Formula') is no longer supported in DataAnalyst V9.0 and later versions.
    var rq = FR.remoteEvaluate('=format(today(), "yyyy-MM-dd")'); 
    alert(rq);

    If you want to click a button to display the current date, you can enter the above code in the Click event of the button.

    image.png

    In FineReport, you only need to write one line of code to achieve the same functionality as multiple lines of native JavaScript code.

    When the formula execution requires parameters, you need to concatenate the formula string accordingly. For example, if the parameter name is sale and the parameter value is Mike, the code is as follows:

    var region=FR.remoteEvaluate('=sql("FRDemo","SELECT Region FROM Sales_Volume WHERE Salesperson=\''+sale+'\'",1,1)');
    alert(region);

    image.png

    iconNote:
    Only SELECT statements are supported in the sql() function.

    By using these two methods, you can easily mix JavaScript code with FineReport formulas, significantly improving development efficiency.

    If the use of certain formulas causes the error message "This formula is forbiddenSQL", it is because the platform has enabled Forbid Script to Call Formula. You can disable this option if needed, but doing so may introduce security risks. For details, see Security Protection.

    image.png

    Attachment List


    Theme: Secondary Development
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy