Skip to content

Commit

Permalink
Create tokensInTemplate.js
Browse files Browse the repository at this point in the history
  • Loading branch information
meksone authored Apr 20, 2024
1 parent 60d5dcf commit b6b7952
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tokensInTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Replace token defined in args in HTML template
* The tokens must be provided in form { token1name: 'token1Value', token2Name: 'token2Value' ...}
*
* @param {*} filename
* @param {...{}} args
* @returns {*}
*/
function tokensInTemplate(filename, ...args) {
// Ensure the first argument is a string
if (typeof filename !== 'string') {
throw new Error('First argument must be a string; be sure to write the template name correctly, without ending .html');
}

// Merge all objects into one
let replacements = Object.assign({}, ...args);

// Create a template from a file
let template = HtmlService.createTemplateFromFile(filename);

// Replace tokens in the template with replacements
for (let key in replacements) {
template[key] = replacements[key];
}

// Evaluate the template and return the result
return template.evaluate().getContent();
}

0 comments on commit b6b7952

Please sign in to comment.