javascripthtmlgoogle-apps-scriptmake.com

Google Apps Script converts "&" in my URL to "& "


My google apps script gets a webhook address, calendar ID, event ID, and a string (EventPrefix) to combine them all into an URL so I can use to pass data to Integromat for an automation.

The URL should comply with the following syntax (unless I'm misunderstanding something): https://SOMEWEBHOOKADDRESS?EventPrefix=STRING&CalendarID=CALENDARID&EventID=EVENTID

However, the resulting URL is https://SOMEWEBHOOKADDRESS?EventPrefix=STRING&CalendarID=CALENDARID&EventID=EVENTID

As a result, Integromat is getting:

amp;CalendarID instead of CalendarID and
amp;EventID instead of EventID

What do I do to get to the URL as per syntax above? Thank you

Code to get the bits of the URL:

    var items = Calendar.Events.list('abc123@group.calendar.google.com', { timeMin: CurrentDate.toISOString(), timeMax: RangeEnd.toISOString(), maxResults: 2500 }).items;
  items.forEach(e => {
    var EventTitle = e.summary;
    var EventCreatedDate = new Date(e.created);
    var EventStartDate = new Date(e.start.dateTime || e.start.date);
    var EventEndDate = new Date(e.end.dateTime || e.end.date);
    var EventCreator = e.creator.email; //Gets the creator of the event to email notificaiton to
    var EventID = e.id;
    var CalendarID = FamilyCalendar.getId();
    var EventURL = e.htmlLink; 


    //Check if an event was created today AND does not have our names or "FYI" in its title
    if(EventCreatedDate.valueOf() <= CurrentDate.valueOf() && EventTitle.indexOf('Name1')<0 && EventTitle.indexOf('Name2')<0 && EventTitle.indexOf('Name3')<0 && EventTitle.indexOf('Name4')<0 && EventTitle.indexOf('Name5')<0 && EventTitle.indexOf('FYI')<0) 
      {
        //Creates variables for the HTML body of the email notification. The same variables are referenced in the body of the HTML template.
        var EmailMessage = HtmlService.createTemplateFromFile("EmailMessage"); //"EmailMessage" is the name of the HTML file in this script.
            EmailMessage.recepient = EventCreator;
            EmailMessage.eventTitle = EventTitle;
            EmailMessage.eventStartDate = EventStartDate;
            EmailMessage.eventEndDate = EventEndDate;
            EmailMessage.calendarID = CalendarID;
            EmailMessage.eventID = EventID;

Building HTML of the email message to be sent with the calendar details and hyperlinks to trigger Integromat automation:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p>Hi <?= recepient ?>,</p>

    <p>You just created an event in the Family Calendar but did not specify who this event is for:</p>

    <p><b>Event</b>: <?= eventTitle ?><br>
    <b>Starts</b>: <?= eventStartDate ?><br>
    <b>End</b>: <?= eventEndDate ?></p>

    <p>Please click on a link below to add it to the title of the event:</p>

    <p><b><a href="https://hook.integromat.com/p3uunmm7co9k11c5bdpni85k239a7e8x?EventPrefix=FYI&CalendarID=<?= calendarID ?>&EventID=<?= eventID ?>">FYI</a><br></p>

    <p>Thank you</p>
  </body>
</html>


Solution

  • When the value including & is put in the tag, when the HTML data is retrieved by EmailMessage.evaluate().getContent(), & is converted to &amp;. I thought that when &amp; is sent as an email of HTML body, the URL can be correctly used as &.

    But if you want to see &amp; as & when you retrieve the HTML data on your script, how about the following modification?

    Modified script:

    var htmlBody = EmailMessage.evaluate().getContent().replace(/&amp;/g, "&");