Merge and Download Documents in Zoho Writer
Table of Contents
- Description
- Syntax
- Example
- Supply CSV content to the merge fields and download the new merged documents in .pdf format
- Supply CSV data from a URL to the merge fields and download the new merged documents in .docx format
- Supply JSON content in TEXT format to the merge fields and protect the downloaded .docx file with a password
- Supply JSON content in KEY-VALUE format to the merge fields and download the new merged documents in .pdf format
- Upload the merged and downloaded file to Zoho Workdrive
- Response Format
- Related Links
Description
The zoho.writer.mergeAndDownload task supplies values to the merge fields inserted to a document, and returns the new merged documents converted into a single file of specified format. The values of the merge fields are provided from a data source of type CSV or JSON.
This integration task is based on the Zoho Writer API - Merge document.
- The merge fields inserted into the document should be the column headers of a CSV data source, or the keys in a JSON data source.
- On average, it takes about a minute to index a file/folder after it is created or updated. This indexing time can increase if the queue size increases. You will not get the expected result if you execute zoho.writer.mergeAndDownload task before the document you need to merge is indexed.
Syntax
<response> = zoho.writer.mergeAndDownload(<document_id>, <output_format>, <values_map>, <connection>);
Params | Data type | Description |
<response> | FILE | The output file object that contains the new merged documents. |
<document_id> | TEXT | The ID of the source document that will be merged with values. The document ID can be obtained from the URL of the source document. The URL is in the following format: |
<output_format> | TEXT | The format of the output file. Allowed values:
|
<values_map> | KEY-VALUE | The values that will be supplied to the merge fields. The values can be fetched only from CSV file or JSON file/text. Note: The keys of this parameter should be specified as given in the Zoho Writer API. Applicable keys and values for CSV and JSON data as specified in Zoho Writer - Merge document API: CSV:
JSON:
|
<connection> | TEXT | The link name of the connection. Note:
|
Example 1: Supply CSV content to the merge fields and download the new merged documents in .pdf format
The following script supplies the CSV data stored in the FILE variable - csv_content to the merge fields present in the specified documents, and stores the file to the variable - response in .pdf format.
// Fetch the CSV data from the web and store it in the variable - csv_content csv_content = invokeurl [ url : "http://insight.dev.schoolwires.com/HelpAssets/C2Assets/C2Files/C2ImportCalEventSample.csv" type : GET ]; // Construct a KEY-VALUE variable to hold the merge data merge_values = Map(); merge_values.put("merge_data_csv_content", csv_content); // Perform the Merge and Download task response = zoho.writer.mergeAndDownload("al296XXXXXXXXXXXXXXXXXXXXXXXXXXXa516c", "pdf", merge_values, "writer_oauth_connection");
where:
response
"al296XXXXXXXXXXXXXXXXXXXXXXXXXXXa516c"
"pdf"
merge_value
"writer_oauth_connection"
"merge_data_csv_content"
csv_content
Example 2: Supply CSV data from a URL to the merge fields and download the new merged documents in .docx format
The following script supplies CSV values fetched from the specified URL to the merge fields present in the specified documents, and stores the file in the variable - response.
// Construct a KEY-VALUE variable to hold the merge data and subject of the email merge_values = Map(); merge_values.put("merge_data_csv_url", "http://insight.dev.schoolwires.com/HelpAssets/C2Assets/C2Files/C2ImportCalEventSample.csv"); // Perform the Merge and Download task response = zoho.writer.mergeAndDownload("al296XXXXXXXXXXXXXXXXXXXXXXXXXXXa516c", "docx", merge_values, "writer_oauth_connection");
where:
"docx"
"merge_data_csv_url"
"http://insight.dev.schoolwires.com/HelpAssets/
C2Assets/C2Files/C2ImportCalEventSample.csv"
Example 3: Supply JSON content in TEXT format to the merge fields and protect the downloaded .docx file with a password
The following script supplies JSON values held by the variable - json_text to their respective merge fields, and protects the downloaded .docx file with the password - mergedFile2019.
// Assign the JSON data in TEXT format to the variable - json_text json_text = "{\"data\":[{\"Name\":\"Shawn\",\"Age\":\"20\"},{\"Name\":\"Kate\",\"Age\":\"29\"}]}"; // Construct a KEY-VALUE variable to hold the merge data merge_values = Map(); merge_values.put("password", "mergedFile2019"); merge_values.put("merge_data", json_text); // Perform the Merge and Download task response = zoho.writer.mergeAndDownload("al296XXXXXXXXXXXXXXXXXXXXXXXXXXXa516c", "docx", merge_values, "writer_oauth_connection");
where:
"merge_data"
json_text
password
Example 4: Supply JSON content in KEY-VALUE format to the merge fields and download the new merged documents in .docx format
The following script will take the JSON values in Key-Value format from the variable - Content to merge them with their respective merge fields and download the new merged documents in .docx format.
// Assign the JSON data in Key-Value format to the variable - Content Content = Map(); Content.put("Name","Shawn"); Content.put("Age","20"); // Construct a KEY-VALUE variable to hold the merge data merge_values = Map(); merge_values.put("merge_data", {"data":Content}); // Perform the Merge and Download task response = zoho.writer.mergeAndDownload("bc296XXXXXXXXXXXXXXXXXXXXXXXXXXXa563a", "pdf", merge_values, "writer_oauth_connection");
where:
"merge_data"
Content
Example 5: Upload the merged and downloaded file to Zoho Workdrive
The following example uploads the merged file to workdrive:
csv_content = invokeurl [ url : "http://insight.dev.schoolwires.com/HelpAssets/C2Assets/C2Files/C2ImportCalEventSample.csv" type : GET ]; merge_values = Map(); merge_values.put("merge_data_csv_content", csv_content); response = zoho.writer.mergeAndDownload("al296XXXXXXXXXXXXXXXXXXXXXXXXXXXa516c", "pdf", merge_values, "writer_oauth_connection"); // Upload the merged pdf file to Zoho Workdrive using integration task upload_response = zoho.workdrive.uploadFile(response, "690xuXXXXXXXXXXXXXXXXXXXXXXXXXXX5b399", "fileName", false, "workdrive_oauth_connection");
where:
upload_response
response
Response Format
Success Response
The success response will be returned in the following format:
fileName.pdf
Failure Response
The failure response for empty merge data will be returned in the following format:
{
"error": {
"documentation_url": "https://www.zoho.com/writer/help/api/v1/general-errors.html",
"message": "Merge data not found. Please try again with proper merge data.",
"errorcode": "1837"
}
}The failure response for improper document ID will be returned in the following format:
{
"error": {
"message": "Request URL not configured properly. Please check and try again",
"documentation_url": "https://www.zoho.com/writer/help/api/v1/general-errors.html",
"response_generated_time": 1563961624177,
"request_url": "https://writer.zoho.com/api/v1/documents//collaboration",
"errorcode": "R2008"
}
}The failure response when you try to merge values to a document that you do not have permission will be returned in the following format:
{
"error": { "message": "Permission denied. You are not authorized to perform this operation.", "documentation_url": "https://www.zoho.com/writer/help/api/v1/general-errors.html", "response_generated_time": 1563961602526, "request_url": "https://writer.zoho.com/api/v1/documents/al296XXXXXXXXXXXXXXXXXXXXXXXX6964add/collaboration", "errorcode": "R5010" } }