保持从表单提交到电子邮件的文本格式。

huangapple go评论74阅读模式
英文:

Maintain Text Formatting from Form Submission to Email

问题

I am trying to maintain formatting/styling of text that is submitted from a form. The text styling is done using TinyMCE. What happens when the form is submitted is, it's first saved to a google sheet. Within the same function, after everything from the form is saved to the google sheet, it then takes all of the data that it just submitted, prepares it in an email using an email template html file, and then sends the email out.

Here is the issue: The formatting of the text is not maintained from the form submission to the email. What I mean by that is, if in the TinyMCE text editor, I bold something, for example, test, it would appear as test in the text editor. However, on the email side of things, it would look like this: <p><em><strong>test</strong></em></p>, which is how it is saved in the database (google sheet). I've tried multiple ways of getting it to work, and each have come up short. To test and see if it was possible to maintain formatting from the google sheet to the email, I tried this in my code:

let alertDetails = data[0]['Alert Details']
let alertDetailsHtml = HtmlService.createHtmlOutput(alertDetails).getContent()

That seemed to work at making sure the text kept its styling if I set html: alertDetailsHtml, however if I try and use the email template, like I want to do, it either errors out, or does not work. I also tried:

<div><?= HtmlService.createHtmlOutput(data[0]['Alert Details']).getContent() ?></div>

in the html email template, and that also didn't work. I will include all of the relevant code so far:

saving the alerts function:

function saveAlerts(data) {
  //Gets all the data in the alerts sheet
  let sheetData = alerts.getDataRange().getDisplayValues()
  //Get keys from the first row of the sheet
  let keys = sheetData[0]
  //For each alert in the array, create a temporary array containing the values for each column in the sheet
  data.forEach(el => {
    let temp = []
    keys.forEach(key => {
      temp.push(el[key])
    })
    //Append the temporary array as a new row in the sheet
    alerts.appendRow(temp)
  })

  let template = HtmlService.createTemplateFromFile('htmlEmail');
  template.data = data;
  template.alertDetails= HtmlService.createHtmlOutput(data[0]['Alert Details']
).getContent()
  let message = template.evaluate().getContent();

  //Takes the data variable and pulls out the the 'Alert Subject' and 'Alert Details', and appends them to an email that is sent out when the form is submitted
  MailApp.sendEmail({
    to: 'lofton.gentry@one-line.com',
    subject: `${data[0]['Alert Subject']}`,
    htmlBody: message,
    replyTo: `${data[0]['User Email']}`
  })
}

The relevant portion of the HTML email template:

<body>
  <div class="alert-container">
    <div class="alert-header" id="alertHeader">
      <p class="alert-title">New Alert</p>
    </div>
    <div class="alert-body">
      <p id="alert-type"><strong>Type:</strong>
        <?= data[0]['Alert Type'] ?>
      </p>
      <p id="alert-date"><strong>Alert Date:</strong>
        <?= data[0]['Alert Date'] ?>
      </p>
      <p id="alert-loc"><strong>Location:</strong>
        <?= data[0]['Alert Location'] ?>
      </p>
      <p id="alert-loc"><strong>Code:</strong>
        <?= data[0]['Alert Code'] ?>
      </p>
      <p id="alert-loc"><strong>Affected:</strong>
        <?= data[0]['Services Affected'] ?>
      </p>
      <p id="alert-add"><strong>Alert Address:</strong>
        <?= data[0]['Alert Address'] ?>
      </p>
      <p id="alert-details"><strong>Alert Details:</strong></p>
        <div><?= alertDetails ?></div>
    </div>
  </div>
</body>

I can provide further details and clarification as necessary

An example, as requested. This is the entire form input. I will include the name of the input and the expected result on the email. NOTE: Some of the inputs are hidden, but are automatically filled based on selection on the form.

In the form:
(Select) Alert Type: Port/Vessel Operations
(Select) Alert Location: Packer Avenue Marine Terminal
(Text) Alert Subject: test
(TextArea) Alert Details: **test** (this is actually bold in the text editor)
(Date) Alert Date: 5/11/2023
(Select) Email: An email

Expected Result in Email:

Type: Port/Vessel Operations
Alert Date: 2023/05/11
Location: Packer Avenue Marine Terminal
(Hidden Input) Code: USPHL02
(Hidden Input) Affected: AL2
(Hidden Input) Alert Address: Packer Avenue Marine Terminal 3301 South Columbus Blvd Philadelphia PA 19148
Alert Details: **test** (this is actually bold text in the email)

Actual Result in Email:

Type: Port/Vessel Operations
Alert Date: 2023/05/11
Location: Packer Avenue Marine Terminal
(Hidden Input) Code: USPHL02
(Hidden Input) Affected: AL2
(Hidden Input) Alert Address: Packer Avenue Marine Terminal 3301 South Columbus Blvd Philadelphia PA 19148
Alert Details: &lt;p&gt;&lt;strong&gt;test&lt;/strong&gt;&lt;/p&gt;
英文:

I am trying to maintain formatting/styling of text that is submitted from a form. The text styling is done using TinyMCE. What happens when the form is submitted is, it's first saved to a google sheet. Within the same function, after everything from the form is saved to the google sheet, it then takes all of the data that it just submitted, prepares it in an email using an email template html file, and then sends the email out.

Here is the issue: The formatting of the text is not maintained from the form submission to the email. What I mean by that is, if in the TinyMCE text editor, I bold something, for example, test, it would appear as test in the text editor. However, on the email side of things, it would look like this: &lt;p&gt;&lt;em&gt;&lt;strong&gt;test&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;, which is how it is saved in the database (google sheet). I've tried multiple ways of getting it to work, and each have come up short. To test and see if it was possible to maintain formatting from the google sheet to the email, I tried this in my code:

let alertDetails = data[0][&#39;Alert Details&#39;]
let alertDetailsHtml = HtmlService.createHtmlOutput(alertDetails).getContent()

That seemed to work at making sure the text kept it's styling if I set html: alertDetailsHtml, however if I try and use the email template, like I want to do, it either errors out, or does not work. I also tried:

&lt;div&gt;&lt;?= HtmlService.createHtmlOutput(data[0][&#39;Alert Details&#39;]).getContent() ?&gt;&lt;/div&gt;

in the html email template, and that also didn't work. I will include all of the relevant code so far:

saving the alerts function:

function saveAlerts(data) {
  //Gets all the data in the alerts sheet
  let sheetData = alerts.getDataRange().getDisplayValues()
  //Get keys from the first row of the sheet
  let keys = sheetData[0]
  //For each alert in the array, create a temporary array containing the values for each column in the sheet
  data.forEach(el =&gt; {
    let temp = []
    keys.forEach(key =&gt; {
      temp.push(el[key])
    })
    //Append the temporary array as a new row in the sheet
    alerts.appendRow(temp)
  })

  let template = HtmlService.createTemplateFromFile(&#39;htmlEmail&#39;);
  template.data = data;
  template.alertDetails= HtmlService.createHtmlOutput(data[0][&#39;Alert Details&#39;]
).getContent()
  let message = template.evaluate().getContent();

  //Takes the data variable and pulls out the the &#39;Alert Subject&#39; and &#39;Alert Details&#39;, and appends them to an email that is sent out when the form is submitted
  MailApp.sendEmail({
    to: &#39;lofton.gentry@one-line.com&#39;,
    subject: `${data[0][&#39;Alert Subject&#39;]}`,
    htmlBody: message,
    replyTo: `${data[0][&#39;User Email&#39;]}`
  })
}

The relevant portion of the HTML email template:

&lt;body&gt;
  &lt;div class=&quot;alert-container&quot;&gt;
    &lt;div class=&quot;alert-header&quot; id=&quot;alertHeader&quot;&gt;
      &lt;p class=&quot;alert-title&quot;&gt;New Alert&lt;/p&gt;
    &lt;/div&gt;
    &lt;div class=&quot;alert-body&quot;&gt;
      &lt;p id=&quot;alert-type&quot;&gt;&lt;strong&gt;Type:&lt;/strong&gt;
        &lt;?= data[0][&#39;Alert Type&#39;] ?&gt;
      &lt;/p&gt;
      &lt;p id=&quot;alert-date&quot;&gt;&lt;strong&gt;Alert Date:&lt;/strong&gt;
        &lt;?= data[0][&#39;Alert Date&#39;] ?&gt;
      &lt;/p&gt;
      &lt;p id=&quot;alert-loc&quot;&gt;&lt;strong&gt;Location:&lt;/strong&gt;
        &lt;?= data[0][&#39;Alert Location&#39;] ?&gt;
      &lt;/p&gt;
      &lt;p id=&quot;alert-loc&quot;&gt;&lt;strong&gt;Code:&lt;/strong&gt;
        &lt;?= data[0][&#39;Alert Code&#39;] ?&gt;
      &lt;/p&gt;
      &lt;p id=&quot;alert-loc&quot;&gt;&lt;strong&gt;Affected:&lt;/strong&gt;
        &lt;?= data[0][&#39;Services Affected&#39;] ?&gt;
      &lt;/p&gt;
      &lt;p id=&quot;alert-add&quot;&gt;&lt;strong&gt;Alert Address:&lt;/strong&gt;
        &lt;?= data[0][&#39;Alert Address&#39;] ?&gt;
      &lt;/p&gt;
      &lt;p id=&quot;alert-details&quot;&gt;&lt;strong&gt;Alert Details:&lt;/strong&gt;&lt;/p&gt;
        &lt;div&gt;&lt;?= alertDetails ?&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/body&gt;

I can provide further details and clarification as necessary

An example, as requested. This is the entire form input. I will include the name of the input and the expected result on the email. NOTE: Some of the inputs are hidden, but are automatically filled based on selection on the form.

In the form:
(Select) Alert Type: Port/Vessel Operations
(Select) Alert Location: Packer Avenue Marine Terminal
(Text) Alert Subject: test
(TextArea) Alert Details: **test** (this is actually bold in the text editor)
(Date) Alert Date: 5/11/2023
(Select) Email: An email

Expected Result in Email:

Type: Port/Vessel Operations
Alert Date: 2023/05/11
Location: Packer Avenue Marine Terminal
(Hidden Input) Code: USPHL02
(Hidden Input) Affected: AL2
(Hidden Input) Alert Address: Packer Avenue Marine Terminal 3301 South Columbus Bllvd Philadelphia PA 19148
Alert Details: **test** (this is actually bold text in the email)

Actual Result in Email:

Type: Port/Vessel Operations
Alert Date: 2023/05/11
Location: Packer Avenue Marine Terminal
(Hidden Input) Code: USPHL02
(Hidden Input) Affected: AL2
(Hidden Input) Alert Address: Packer Avenue Marine Terminal 3301 South Columbus Bllvd Philadelphia PA 19148
Alert Details: &lt;p&gt;&lt;strong&gt;test&lt;/strong&gt;&lt;/p&gt;

答案1

得分: 0

I needed to change <div><?= alertDetails ?></div> to this: <div><?!= alertDetails ?></div> to include the actual HTML into my script, and not just a string. To quote the answer I linked, the tags <?= ?> basically print out the input from the form as a string, not converting the tags into actual HTML like I wanted.

英文:

Going off of this answer from a very similar previous question here: I needed to change &lt;div&gt;&lt;?= alertDetails ?&gt;&lt;/div&gt; to this: &lt;div&gt;&lt;?!= alertDetails ?&gt;&lt;/div&gt; to include the actual HTML into my script, and not just a string. To quote the answer I linked, the tags &lt;?= ?&gt; basically print out the input from the form as a string, not converting the tags into actual HTML like I wanted.

huangapple
  • 本文由 发表于 2023年5月10日 23:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定