PrintSpirit JS Script Function Operation Manual

PrintSpirit, from its inception, has upheld the core philosophy of "separation of printing and program logic." Through its accompanying label editor and printing API, it greatly simplifies the development of printing functions for application software. There's no need to change the program; you can modify the visual effects of labels independently.

However, reality is complex, a typical example being the printing format of monetary amounts: for the same order, the amount on an invoice needs to be displayed in English Uppercase as ONE THOUSAND FIVE HUNDRED EIGHTY YUAN FIVE JIAO, while the internal retention copy only needs to display the number '1580.50'.

To achieve this, a very common approach is for the backend program to prepare two different fields for the same amount (e.g., an Amount and an Uppercase Amount). While simple, this undoubtedly increases the coupling of the backend program, conflicting with our principle of "separation."

For ultimate flexibility and to meet future challenges, after careful consideration, we ultimately decided to introduce JavaScript scripting as the core solution.

JS Script

With the powerful capabilities of JavaScript, you can perform conditional judgments, loops, string formatting, date operations, function calls, and almost all JS-supported functions within the label. The only requirement is that the final result of the script must be returned via a return statement.

Regarding the problem at the beginning, the backend only needs to provide one field (amount), and the uppercase amount can be easily obtained with toEnglishUppercase(Amount).

In the new version of PrintSpirit's label editor, whether it's the editing interface for single-line text, multi-line text, barcodes, or QR codes, a new "JS Script" tab has been added for you to maintain and write JavaScript code. The script supports the ECMAScript standard, ensuring wide compatibility and stability.

Edit JavaScript basics

Basic Example: Concatenating Fields

1return `${Order Date} ${Order Amount}`

This simple script concatenates the values of the "Order Date" and "Order Amount" fields and returns them.

Advanced Example: Generating GS1 Barcodes Using Built-in Library Functions

1let code = new GS1()
2code.add(GS1.AI.GTIN, "1234567890123")
3code.add(GS1.AI.SSCC, "123456789012345675")
4
5// You can add other barcode AI fields as needed
6
7return code.fullcode()

This example demonstrates how to use the built-in GS1 encoding library function. It helps you conveniently generate compliant GS-1 standard barcode data, automatically calculate checksums, and insert separators, greatly simplifying complex barcode generation logic.

Advanced Example: Array Variables and Summary Calculation

1//set Order Details=[{name:'Product A', price:100}, {name:'Product B', price:200}]
2
3let total=0
4for (cur of Order Details) {
5	total += cur.price
6}
7
8return total

This script shows how to process array-type variables and perform summary calculations. Here, it calculates the sum of prices for all items in Order Details.

Variable Usage Tips: In JS scripts, you can use any variable passed through an API call. However, these variables might not exist during label design, so they need to be defined beforehand, otherwise a "variable not defined" error might be reported.

Two recommended ways to define variables:

  1. Define via the "Variable Management" interface: This method is intuitive and suitable for simple variable declarations.
  2. Define using //set var=xxxx at the beginning of the script: This method is particularly suitable for defining complex variables that have the exact same structure as runtime data, making it easy to test and debug during design. Please note that there should be no space between // and set.

JS Script Library Functions

To further improve development efficiency, PrintSpirit provides script library functions, allowing commonly used programs to be written as functions for easy use, including two categories: standard library functions and user library functions.

Standard Library Functions

Standard library functions, which can be called directly in the JS scripts of all fields like text and barcodes without any setup, include the following categories:

User Library Functions

Custom library functions are effective throughout the entire label range and are managed and maintained by clicking the Edit JavaScript basics button. Once defined, they are used in the same way as standard libraries.

Maintain and manage JS script library functions

Actual Results: A Leap in Efficiency and Flexibility

After implementing this JS script solution, PrintSpirit has brought significant improvements in practical applications:

For Third-Party Application Development Teams:

  • 90% of printing-related change requests no longer require development team intervention and can be adjusted by business or operations personnel themselves.
  • Reduced a large amount of repetitive data formatting code, making the backend program more focused on business logic.
  • Higher system stability, as core business logic is no longer affected by changes in front-end print formats.

For Business Departments:

  • Response time for print format adjustments has been shortened from "days" to "minutes," greatly improving business agility.
  • Can independently experiment and iterate different printing styles without relying on development resources.
  • Can quickly respond to and deploy special printing requirements, such as dedicated formats for promotional activities.

Leave Your Message

login