Search code examples
javascriptundefinedhighlightsurveyjsdocxtemplater

Docxtemplater - Javascript - hightlight undefined values


I'm using docxtemplater to create a word document based on JSON values (using surveyJS). In case some questions are not answered, the variable shows "undefined" in the final document.

How can I make sure that any "undefined" output is highlighted? I found some answers on how to use nullGetter to replace "undefined" with a custom string but nothing on text background.

The JSON looks something like this:

{
    "nameIndividual": "John Smith",
    "mgmt": "Management Name",
    "mgmtName": "Tim Test",
    "address": "854 Test Avenue, Example City",
    "email": "[email protected]",
    "socialMediaOptions": [
        "Twitter",
        "YouTube",
        "Instagram",
        "Facebook"
    ]
}

Is there a way to do this in the word template itself without having to write {#nameIndividual == undefined}Undefined{/} for every single variable...

If not, how can this be done with Javascript?

Any help is highly appreciated!

Thanks!


Solution

  • You would have to use the docxtemplater HTML module for this. It supports setting a background-color style to html elements. Look here:

    https://docxtemplater.com/modules/html/

    Instead of undefined you could add <span style="background-color: red">Undefined<span> for the missing value.