How to generate files in batches

Do you want to: Suggest a feature / Report a bug / Ask a how-to question
For feature suggestions, describe the result you would like to achieve in detail:
For bug reports, provide the steps to reproduce and if possible a minimal demo of the problem:
DocumentBuilder version: 7.x
Installation method: exe
OS: windows

I want to batch generate a word file based on a template file and some data, similar to mail merge.

Hello @huzedong2022
I believe we need your exact usage scenario. What difficulties have you faced?
For example, you can use GlobalVariable method to exchange the data among files in your scenario. Please provide us with details on your usage scenario.

I simplified the requirements

Template file content is:

hello {{userName}}

There are 100 user information in the database, and I want to use this template to generate 100 Word files

result:
file 1 content: hello apple
file 2 content: hello samsung

file 100 content: hello sony

Probably you can use GlobalVariable +SearchandReplace methods. I’ve recorded my test where users have been added to xlsx file: Monosnap

My script:

builder.OpenFile("\\path to file\\.xlsx");
var oWorksheet = Api.GetActiveSheet();
var oRange = oWorksheet.GetRange("A1");
var text = oRange.GetText();
GlobalVariable["1"] = text;
oRange = oWorksheet.GetRange("A2"); 
var text_two = oRange.GetText();    
GlobalVariable["2"] = text_two;      
oRange = oWorksheet.GetRange("A3"); 
var text_three = oRange.GetText();   
GlobalVariable["3"] = text_three;   
builder.CloseFile();

builder.OpenFile("\\path to file\\.docx");
var text = GlobalVariable["1"];     
var oDocument = Api.GetDocument();
var oParagraph = oDocument.GetElement(0);
oDocument.SearchAndReplace({"searchString": "{{userName}}", "replaceString": text});
builder.SaveFile("docx", "\\path to file\\.docx");
builder.CloseFile();

builder.OpenFile("\\path to file\\.docx");
var text_two = GlobalVariable["2"];  
var oDocument = Api.GetDocument();
var oParagraph = oDocument.GetElement(0);
oDocument.SearchAndReplace({"searchString": "{{userName}}", "replaceString": text_two});
builder.SaveFile("docx", "\\path to file\\.docx");
builder.CloseFile();

builder.OpenFile("\\path to file\\.docx");
var text_three = GlobalVariable["3"];  
var oDocument = Api.GetDocument();
var oParagraph = oDocument.GetElement(0);
oDocument.SearchAndReplace({"searchString": "{{userName}}", "replaceString": text_three});
builder.SaveFile("docx", "\\path to file\\.docx");
builder.CloseFile();