Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7903685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:48:42+00:00 2026-06-03T09:48:42+00:00

I tried to create a script which reads values (name, date, etc) from a

  • 0

I tried to create a script which reads values (name, date, etc) from a spreadsheet in google docs and customizes a specific form’s description (text content) to be sent to users with those values. But the string.replace() method isn’t working like I expect it to. It just replaces the key with nothing. Am I missing something? I’ve spent hours googling with no answers. Any help would be greatly appreciated.

var docId = ScriptProperties.getProperty("QuestionnaireDocId"); 

var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var numRows = 1000000;   // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
// Fetch values for each row in the Range.
var data = dataRange.getValues();

for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var name = row[0]; // this var isn't empty. - tested it to check

    var formSpreadsheet  = SpreadsheetApp.openById(docId);
    var sFormUrl = formSpreadsheet.getFormUrl();
    if(sFormUrl) {
        var response = UrlFetchApp.fetch(sFormUrl);
        var sFormContent = response.getContentText();

        //Replace <keyName> in the form description with the value of var 'name'
        sFormContent = sFormContent.replace("<keyName>", name); 


        //get only body content
        sFormContent = sFormContent.replace(/[\s\S]+<body[^>]*>([\s\S]+)<\/body>[\s\S]+/i, '$1');

        //remove script
        sFormContent = sFormContent.replace(/<script[^>]*>[\s\S]*<\/script>/i, '');
        //add inline style for form elements, since gmail does not allow style tag and      link tag to style it
        sFormContent = sFormContent.replace(/class\=\"errorbox-good\"/gi, 'class="errorbox-good" style="margin-bottom:1.5em;zoom:1"');
        sFormContent = sFormContent.replace(/class\=\"ss-q-title\"/gi, 'class="ss-q-title" style="display:block;font-weight:bold;"');
        sFormContent = sFormContent.replace(/class\=\"ss-q-help\"/gi, 'class="ss-q-help" style="display:block;color:#666;margin:.1em 0 .25em 0"');
        sFormContent = sFormContent.replace(/class\=\"ss-choices\"/gi, 'class="ss-choices" style="list-style:none;margin:.5em 0 0 0;padding:0;"');
    }
    var mailBody2;

    if(sFormUrl) {
        mailBody2 = '<br /><br />If you have trouble viewing or submitting this form, you can fill it out online:<br />';
        mailBody2 += '<a href="' + sFormUrl + '" title="">' + sFormUrl + '</a>';
        mailBody2 += '</body></html>';
        mailBody2 += '<br /><br />';
        mailBody2 += sFormContent;
        mailBody2 += '<br />';
    }       


    // Embed form and send the email - the emails work fine

    var subject = "Test email";

    MailApp.sendEmail(email_address, subject, mailBody2, {htmlBody: mailBody2}); 

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:48:43+00:00Added an answer on June 3, 2026 at 9:48 am

    It looks like there are a couple things going on that cause this not to work as written. First, in the form content that you get back from the UrlFetchApp call, there are two places where the form description shows up. One looks like this. I just put foo in the description field of my form:

    <meta itemprop="description" content="foo &lt;keyName&gt;">
    

    and the other looks like this:

    <div class="ss-form-desc ss-no-ignore-whitespace">foo </div>
    

    The second instance is the one that’s actually displayed in your emails, and as you can see, it seems that the is being stripped out already. My guess is that it’s stripping out anything that looks like it might be HTML.

    So here’s what I tried, which worked for me. In your form description, put [keyName] instead of .

    Then, you also need to search and replace globally, not just the first instance of [keyName], to make sure you get it in both places. So change this line:

    sFormContent = sFormContent.replace("<keyName>", name); 
    

    to this:

    sFormContent = sFormContent.replace(/\[keyName\]/g, name);  
    

    Once I made those changes, I was able to get it working, where the emails I received contained the values from my spreadsheet in the description, after the word ‘foo’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the first Python script I've tried to create. I'm reading a xml
Drupal cannot create the files folder. I tried to upload a php script with
Here is a snippet from a script which I generally execute from cron: if
I want to create script which use find like this: find . -regex $1
Tabs document I would like to create a new tab which from the link
I tried to create this by following the video at http://www.asp.net/learn/videos/video-7026.aspx where Joe Stagner
I tried to create a Style for DataGridTextColumn with the following code <Style TargetType={x:Type
I tried to create a file using Oracle UTL_FILE.FOPEN command but i get this
I tried to create a new Swing GUI desktop application template using NetBean 7.0
Have anybody ever tried to create thumbnails/previews of MS Office files? I do not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.