Variables in Excel template

This method will replicate the variable definition process used in Word templates. Once the document template is processed, these variables are removed from the final output. Aspose.Words also allows for variable redefinition (overriding values) and using one variable to define another, as demonstrated in the following example:

<<var [name = node.Name]>>
<<var [name = node.child.Name]>>
<<var [name2 = node.child.Name2]>>
<<var [name3 = name2 + "-xxx" ]>>

Variables are defined and accessible from the point of their declaration onwards within a page.

Considering these features, let’s explore how closely we can emulate Word’s variable definition capabilities within Cells by potentially introducing a dedicated worksheet for variables.
PFA updated version of proposed variable definition.
smartmarker-var-updated.zip (8.6 KB)

Furthermore, we can consider incorporating name defined at both the worksheet and workbook levels using Name Manager in Excel.

@sagaofsilence.dev,

Thank you for providing the Excel template with the proposed variable definitions. We will review it thoroughly and get back to you soon.

@sagaofsilence.dev
There are some questions:
1). Scope
Do you mean there will be same Variable Name in different scope?

2). message Hello World
We think all data are from WorkbookDesigner.SetDataSource(), so we should not set value of variable in the template. It’s better that there is only definition in the template file, otherwise it will confuse.

3). message2 message + “-xxx”
All data such as message-1, message-2, message-3 and so on in the data source should be processed as message2.

4). Could you post a data source file and Expected result file?
And, you can add more expected variable definition in the template files, then we can better understand your needs.

5). It seems that we cannot support “if … else…”.

6). Does <<var [employees = DataRoot.company.departs.employees]>> mean exporting all employees of company?

7). We will not support variable definitions within the recycling body, such as:

<<foreach [depart in departs]>>
<<var [employees = DataRoot.company.employees]>>
  
<</foreach>>

@sagaofsilence.dev
Q8. in the Row 6: &=employees.FirstName &=employees.LastName &=employeeDept
Do you still want to process them as a table together if smart marker does not contain same prefix?

@sagaofsilence.dev
Q8.
&=Company.Name
&=Company.department.Name
&=employees.FirstName &=employees.LastName
Is such nested structure expected?

@sagaofsilence.dev,

In the meantime, we will evaluate your original requirements and try to support variables in Excel template for smart marker. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-58593

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@sagaofsilence.dev

Q9.

I think the first “name” should be covered by the second “name”, so the first “name” is useless.

@sagaofsilence.dev
20250618.zip (9.0 KB)
We made some changes in variables worksheet.
1,Type column represents the type of Variable.
2,{} represents a Variable is referred in Expression Variable because the name of columns will contains specific characters such as space , quotation mark, and so on.

1 Like

Thanks. Will get back to you soon.

@sagaofsilence.dev
You are welcome. Please take your time to evaluate it. If you have any questions, please feel free to contact us at any time.

@sagaofsilence.dev
Please check files in vmarker.zip (17.6 KB)
which is generated with the following codes and inner hot fix :
Workbook workbook = new Workbook(dir + “vmarker.xlsx”);
WorkbookDesigner workbookDesigner = new WorkbookDesigner(workbook);
workbookDesigner.ContainsVariables = true; // Indicates whether containing variables.
workbookDesigner.SetJsonDataSource(“Company”, File.ReadAllText(dir + “test.json”));
workbookDesigner.Process();
workbook.Save(dir + “dest.xlsx”);

Note: the Expression must follow the rule of formulas of MS Excel. For Example : In MS Excel , “+” only works for number value, if you want to join the string , please use “&” or CONCAT function .

1 Like

The issues you have found earlier (filed as CELLSNET-58593) have been fixed in this update. This message was posted using Bugs notification tool by leoluo

  1. Scope
    Do you mean there will be same Variable Name in different scope?
    Answer: Yes, the user can declare the same variable name for the different worksheets. This could be intentional and a well-considered decision.

  2. message Hello World
    We think all data are from WorkbookDesigner.SetDataSource(), so we should not set value of variable in the template. It’s better that there is only definition in the template file, otherwise it will confuse.
    Answer: You can use hand-coded data in the template as you wish.,The data may vary from one template to another, and different customers might use different data for the same type of template.

  3. message2 message + “-xxx”
    All data such as message-1, message-2, message-3 and so on in the data source should be processed as message2.
    Answer: The calculated data may not be included in the system-generated JSON payload, so users can create the calculated data from the basic/atomic data in the JSON payload.

  4. Could you post a data source file and Expected result file?
    And, you can add more expected variable definitions in the template files, then we can better understand your needs.
    Answer: I have attached the sample data company.zip (380 Bytes), which can be referred to for the variables declared in the examples. Please note that the examples provided are hypothetical. When writing complex expressions, declaring and reusing variables becomes an important feature.

  5. It seems that we cannot support “if … else…”.
    Answer: Our users need this feature. When users compare the Word template to the Excel template to achieve the same output, the Excel template does not support the conditional inclusion of sections or changing the cell value conditionally. In Excel, it’s not possible to add expressions into the IF formula; the formula only accepts cell references and values recognized by Excel formulae.

<<foreach [product in node.Products]>>
Product: <<product.Name]>>, Tax rate: <<if [product.Code == "IMPORTED"]>> 30% <<else>> 10% <</if>>
<</foreach>>
<<if [product.Code == "IMPORTED"]>>
Imported Products
<<foreach [product in node.Products]>>
<<product.Name]>>, 
<</foreach>>
<</if>>
  1. Does <<var [employees = DataRoot.company.departs.employees]>> mean exporting all employees of company?

Answer: The variable is simply a shorthand in this case, allowing us to avoid repeating the expression multiple times.

Instead of writing the lengthy expression:

&=DataRoot.company.departs.employees.FirstName
&=DataRoot.company.departs.employees.LastName

The user can declare the variable:
employees = DataRoot.company.departs.employees

And then use the compact smart markers:
&=employees.FirstName and &=employees.LastName

All the ideas related to variables are influenced by those supported by Aspose Word template, variables, and tags.

  1. We will not support variable definitions within the recycling body, such as:
<<foreach [depart in departs]>>
<<var [employees = DataRoot.company.employees]>>
<</foreach>>

Answer: This is fine. This will unnecessarily complicate the Excel template.

These are indeed great ideas.

Thank you for incorporating it and providing working examples.

I note that CELLSNET-58593 is mentioned in the release notes for Aspose Cells .NET version 25.7 but not for Aspose Cells Java. We need variable support in Java.

@sagaofsilence.dev,

Yes, the ticket was originally logged in .NET. Please note that the fixes and enhancements have also been incorporated into the Java version simultaneously, as the Java version is ported from the .NET codebase. We recommend trying the latest available version of Aspose.Cells for Java v23.7.

Feel free to reach out if you encounter any issues.

@sagaofsilence.dev
1, message2 message + “-xxx”
Please check the attached file in Variables in Excel template - #18 by simon.zhao.
Is it your need?

We have to support if… else with two ways:

Now you can define Tax rate as an Expression in variables worksheet as “Message2”.
And we are thinking about whether we can directly process it as an Expression .
The smart marker like : &=&=if({product.Code}== “IMPORTED”,0.3,0.1)~(Expression)

We have to add an “IF” or “Condition” smart marker to support this feature.
&=product.Name(IF:product.Code == "IMPORTED“)
As parentheses represent starting and ending parameters, so will you use complex condition in the “IF”?

Given our multi-tenant SaaS environment, where template designs and user needs vary significantly across different domains, the complexity of conditional expressions will likewise differ. We aim to implement a conditional expressions feature that closely mimics the functionality of Word’s <<if>> tag. This feature should ideally control either a single cell smart marker or a specified range of cells containing smart markers. I will prepare and share a sample template for further illustration shortly.

@sagaofsilence.dev,

Thank you for providing additional details regarding your requirements and outlining your desired conditional expressions. We will carefully review the information and respond with our evaluation soon.