In Sheet-to-Doc, loop placeholders are a powerful advanced feature that allows you to process repeated data in Excel and generate complex document structures, such as tables, lists, nested content, etc. Today, I will delve into the usage techniques of loop placeholders to help you master this feature and create more complex and professional automated documents.
- Official Website: https://s.wtsolutions.cn/sheet-to-doc.html
- Download Page: https://sheet-to-doc.wtsolutions.cn/zh-cn/latest/Download.html
What are Loop Placeholders?
Loop placeholders are used to define a repeated structure in Word templates, and the Sheet-to-Doc tool will automatically generate multiple instances based on repeated items in Excel data. Loop placeholders are particularly suitable for processing data with parent-child relationships, such as orders and order details, products and product features, students and grades, etc.
Two Types of Loop Placeholders
Sheet-to-Doc supports two types of loop placeholders, which have different syntax and usage scenarios.
1. Loop Placeholder 1: Fixed Syntax, Only Supports Generation Mode 2
Syntax
1 | {#data}...{/data} |
Usage Instructions
- Fixed syntax, cannot customize name
- Only takes effect in generation mode 2 (single document)
- Used to generate all data into one document
- No special processing of Excel data required
2. Loop Placeholder 2: General Version, Supports Generation Modes 1 and 2
Syntax
1 | {#loop name}...{/loop name} |
Usage Instructions
- Can customize loop name (cannot be
data) - Supports generation modes 1 and 2
- Requires special processing of Excel data (adding prefix)
- Suitable for processing complex data structures
Usage Scenarios of Loop Placeholders
Scenario 1: Generate Product List (Using Loop Placeholder 1)
When you need to generate a product list from Excel data into one document, you can use Loop Placeholder 1.
Excel Data
| Product ID | Product Name | Price | Stock |
|---|---|---|---|
| P001 | Product A | 100 | 50 |
| P002 | Product B | 200 | 30 |
| P003 | Product C | 150 | 40 |
Word Template (Generation Mode 2)
1 | Product Catalog |
Generated Document
1 | Product Catalog |
Scenario 1.1: Generate Product List (Using Loop Placeholder 2)
When you need to use a custom loop name, you can use Loop Placeholder 2.
Excel Data (needs prefix)
| product.Product ID | product.Product Name | product.Price | product.Stock |
|---|---|---|---|
| P001 | Product A | 100 | 50 |
| P002 | Product B | 200 | 30 |
| P003 | Product C | 150 | 40 |
Word Template (Generation Mode 1)
1 | Product Catalog |
Word Template (Generation Mode 2)
1 | Product Catalog |
Scenario 2: Generate Tables (Using Loop Placeholder 1)
Loop placeholders are very suitable for generating tables. You can use loop placeholders in tables to generate multiple rows of data.
Excel Data
| Name | Department | Position | Salary |
|---|---|---|---|
| Zhang San | Technical Department | Engineer | 8000 |
| Li Si | Marketing Department | Manager | 12000 |
| Wang Wu | Finance Department | Accountant | 9000 |
Word Template (Generation Mode 2)
1 | Employee Information Table |
Generated Document
1 | Employee Information Table |
Scenario 3: Generate Nested Content
When Excel data has a nested structure, you can use nested loop placeholders to generate complex documents.
Excel Data
| Order Number | Customer Name | Product Name | Quantity | Unit Price |
|---|---|---|---|---|
| OD001 | Customer A | Product A | 2 | 100 |
| OD001 | Customer A | Product B | 1 | 200 |
| OD002 | Customer B | Product C | 3 | 150 |
Word Template (Using Loop Placeholder 1)
1 | Order Details |
Generated Document
1 | Order Details |
Advanced Techniques for Loop Placeholders
1. Use Conditional Placeholders within Loops
You can use conditional placeholders within loop placeholders to display different content based on conditions.
1 | {#product_list} |
2. Use Conditional Placeholders within Loops
You can use conditional placeholders within loop placeholders to display different content based on data conditions.
1 | {#data} |
3. Use Image Placeholders within Loops
You can use image placeholders within loop placeholders to dynamically insert images based on data.
1 | {#data} |
Best Practices for Using Loop Placeholders
1. Reasonably Organize Excel Data
Ensure that Excel data has a clear structure, making it easy for loop placeholders to process. For nested data, it is recommended to use the same primary key (such as order number) to associate data at different levels.
2. Use Clear Loop Names
Choose concise and clear loop names for easy understanding and maintenance.
3. Test Loop Structures
Before generating a large number of documents, test the loop structure with a small amount of data to ensure that loop placeholders can correctly process the data.
4. Avoid Excessive Nesting
Although loop placeholders can be used nested, excessive nesting can make templates complex and difficult to maintain. It is recommended to nest at most 2-3 layers of loops.
5. Combine with Other Placeholders
Flexibly combine loop placeholders, data placeholders, conditional placeholders, and image placeholders to create more powerful document templates.
6. Use Data Comparison Function
Before generating documents, use Sheet-to-Doc’s data comparison function to check whether loop placeholders are correct, avoiding the generation of incorrect documents.
Common Problems and Solutions for Loop Placeholders
Problem 1: Loop Content Not Repeating Correctly
Reason: The name of the loop placeholder does not match the column name in Excel data, or the Excel data structure is incorrect.
Solution:
- Check whether the name of the loop placeholder is consistent with the column name in Excel data
- Ensure that Excel data structure is correct with clear parent-child relationships
- Use data comparison function to check whether placeholders are correct
Problem 2: Nested Loops Not Generating Correctly
Reason: The structure of nested loops is incorrect, or the association relationship in Excel data is not clear.
Solution:
- Check whether the structure of nested loops is correct, ensuring that each loop has corresponding start and end markers
- Ensure that the association relationship in Excel data is clear, using the same primary key to associate data at different levels
- Simplify nested structures and reduce nesting levels
Summary
Loop placeholders are a powerful feature of Sheet-to-Doc that allows you to process complex Excel data structures and generate various types of documents. By mastering the usage techniques of loop placeholders, you can create more complex and professional document templates, realizing more efficient automated document generation.
In the following series of articles, we will delve into Sheet-to-Doc’s image processing functions, including how to insert dynamic images in Word, usage methods and best practices of image placeholders, etc. Stay tuned!