Skip to content

Instantly share code, notes, and snippets.

@labnol
Created May 2, 2024 06:55
Show Gist options
  • Save labnol/84a4e8b9cd2ead01c0d2002b8201204d to your computer and use it in GitHub Desktop.
Save labnol/84a4e8b9cd2ead01c0d2002b8201204d to your computer and use it in GitHub Desktop.
Google Sheets function for generating pre-filled Google Form links

This Google Sheets function will help you generate pre-filled Google Form links without nested SUBSTITUE functions.

Watch the video tutorial to learn more.

/**
 * Replaces multiple occurrences of specific values in a string with new values.
 * @returns {string} The modified string with replacements made.
 * 
 * @customfunction
 */
function MULTI_SUBSTITUTE(text,...opts) {
  for (let i = 0; i < opts.length; i += 2) {
    const searchValue = opts[i];
    const replaceValue = opts[i + 1] || ""; // Default to empty string if no replacement provided

    // Create a global regular expression for case-insensitive search (flags 'gi')
    const regex = new RegExp(searchValue, 'gi');

    // Replace all occurrences of the search value with the replacement value
    text = text.replace(regex, replaceValue);
  }
  return text.replace(/ /g, "+");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment