Advertisement
RemcoE33

Text Replace with {{placeholders}}

Mar 2nd, 2023
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Replaces {{placeholders}} in sentance(s).
  3. *
  4. * @param {A1:C1} headers The headers.
  5. * @param {A2:C10} header_values The values below the headers.
  6. * @param {D2:D10} sentence The text with the headernames placeholders
  7. * @return {array} Replaces sentence(s)
  8. * @customfunction
  9. */
  10. function TEXT_REPLACE(headers, header_values, sentence) {
  11.   if (!Array.isArray(headers)) {
  12.     headers = [headers]
  13.   }
  14.   headers = headers.flat()
  15.  
  16.   if (!Array.isArray(header_values)) {
  17.     header_values = [[header_values]]
  18.   }
  19.  
  20.   if (!Array.isArray(sentence)) {
  21.     sentence = [sentence]
  22.   }
  23.   sentence = sentence.flat()
  24.  
  25.   const results = [];
  26.   sentence.forEach((s, i) => {
  27.     if (s == "") {
  28.       results.push([null])
  29.       return;
  30.     }
  31.  
  32.     headers.forEach((h, j) => {
  33.       const key = `{{${h}}}`
  34.       s = s.replace(key, header_values[i][j])
  35.     })
  36.  
  37.     results.push([s])
  38.    
  39.   })
  40.   return results
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement