javascript — получить таблицу html из функции .gs и вызвать ее в теле .html.

Захватите таблицу html из функции .gs и вызовите ее в теле .html

Может ли кто-нибудь помочь мне понять, как я могу вызвать таблицу HTML, сгенерированную функцией .gs, в теле .html, выделенном для GmailApp.sendemail ()? Вот пример:

1. Прежде всего, мы создаем HTML-таблицу с данными из электронной таблицы:

  function createTableDHL(){ var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("B2C DE"); var row_count = sheet.getLastRow(); var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); //You can use the date format you prefer var data = sheet.getRange("A2:D"  (row_count   1)).getDisplayValues(); //We get all the values from A to D and from row 2 to the last one. var cells = []; var th = [sheet.getRange("A1").getDisplayValues(), sheet.getRange("B1").getDisplayValues(), sheet.getRange("C1").getDisplayValues(), sheet.getRange("D1").getDisplayValues()]; var table = "{amp}lt;table border-collapse=collapse{amp}gt;{amp}lt;tr height=50 style=background-color:rgb(207,226,243){amp}gt;{amp}lt;th{amp}gt;"   th[0]   "{amp}lt;/th{amp}gt;{amp}lt;th width=50{amp}gt;"   th[1]   "{amp}lt;/th{amp}gt;{amp}lt;th width=200{amp}gt;"   th[2]   "{amp}lt;/th{amp}gt;{amp}lt;th{amp}gt;"   th[3]   "{amp}lt;/th{amp}gt;{amp}lt;/tr{amp}gt;"; var col_A = sheet.getRange('A2:A'  row_count).getDisplayValues(); for (var i = row_count; i {amp}gt;= 0 ; i--){ if (col_A[i] != today){ data.splice(i, 1); } } for (var i = 0; i {amp}lt; data.length; i  ){ if (!isNaN(parseFloat(data[i][3]))) { cells = data[i].toString().split(","); table  = "{amp}lt;tr{amp}gt;"; for (var u = 0; u {amp}lt; cells.length; u  ){ table  = "{amp}lt;td align=center{amp}gt;"  cells[u]  "{amp}lt;/td{amp}gt;"; } table  = "{amp}lt;/tr{amp}gt;"; } } table  = "{amp}lt;/table{amp}gt;"; return table; } 

2. Второй шаг — передача созданной html-таблицы в тело html-сообщения: вот проблема. Таблица отображается в отправленном сообщении не как обычная таблица, а просто как код.

 {amp}lt;!DOCTYPE html{amp}gt; {amp}lt;html{amp}gt; {amp}lt;head{amp}gt; {amp}lt;base target="_top"{amp}gt; {amp}lt;/head{amp}gt; {amp}lt;body{amp}gt; {amp}lt;p style=font-family:verdana,sans-serif {amp}gt; Dear All,{amp}lt;br{amp}gt;{amp}lt;br{amp}gt; {amp}lt;span style=background-color:rgb(217,234,211){amp}gt;{amp}lt;b{amp}gt;Delivery for today:{amp}lt;/b{amp}gt;{amp}lt;/span{amp}gt;{amp}lt;/p{amp}gt; {amp}lt;?= tableDHL ?{amp}gt; {amp}lt;p style=font-family:verdana,sans-serif {amp}gt; Trucks should arrive till {amp}lt;b{amp}gt;4:30 PM{amp}lt;/b{amp}gt;.{amp}lt;br{amp}gt; More details can be sent to you by our suppliers.{amp}lt;br{amp}gt;{amp}lt;br{amp}gt; {amp}lt;span style=background-color:rgb(207,226,243){amp}gt;{amp}lt;b{amp}gt;Forecast:{amp}lt;/b{amp}gt;{amp}lt;/span{amp}gt;{amp}lt;br{amp}gt; {amp}lt;?= dayDelivery ?{amp}gt; we are planning to deliver about {amp}lt;b{amp}gt; {amp}lt;?= plansLow ?{amp}gt; - {amp}lt;?= plansHigh ?{amp}gt; parcels.{amp}lt;/b{amp}gt;{amp}lt;br{amp}gt;{amp}lt;br{amp}gt; {amp}lt;span style=background-color:rgb(255,242,204){amp}gt;{amp}lt;b{amp}gt;Returns:{amp}lt;/b{amp}gt;{amp}lt;/span{amp}gt;{amp}lt;br{amp}gt; Tomorrow we are planning to pick up our returned packages by listed trucks:{amp}lt;/p{amp}gt; {amp}lt;?= returns ?{amp}gt; {amp}lt;?= monReturns ?{amp}gt; {amp}lt;/body{amp}gt; {amp}lt;/html{amp}gt; 

3. Вот код, который отправляет электронное письмо:

 function sendEmailsDHL(){ var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("B2C DE"); var today = Utilities.formatDate(new Date(), "UTC", "dd/MM/yyyy"); var data = sheet.getDataRange().getDisplayValues(); //We get all the values from A to D and from row 2 to the last one. var add1day = 1000 * 60 * 60 * 24; var add3days = 1000 * 60 * 60 * 24*3; var today2 = new Date(); var tomorrow = Utilities.formatDate(new Date(today2.getTime()   add1day), "UTC", "dd/MM/yyyy"); var Mon = Utilities.formatDate(new Date(today2.getTime()   add3days), "UTC", "dd/MM/yyyy"); var plansLow = 0 for (i = 0; i {amp}lt; data.length; i  ){ if (Utilities.formatDate(new Date(), "UTC", "EEE") == "Fri"){ if (data[i][0] == Mon {amp}amp;{amp}amp; data[i][3] == "returns" {amp}amp;{amp}amp; data[i][4] == "NL"){ var NLmonRet = data[i][2]; } if (data[i][0] == Mon {amp}amp;{amp}amp; data[i][3] ==""){ plansLow  = 2000; } }else{ if (data[i][0] == tomorrow {amp}amp;{amp}amp; data[i][3] ==""){ plansLow  = 2000; } } } var plansHigh = Number(plansLow)   1500; if (Utilities.formatDate(new Date(), "UTC", "EEE") == "Fri"){ var dayDelivery = "On Monday"; var MonReturns = "{amp}lt;br{amp}gt;On Monday we are planning to pick up our returned packages by listed trucks:{amp}lt;br{amp}gt;"   NLmonRet; }else{ var dayDelivery = "Tomorrow"; var MonReturns = ""; } var htmlTemplate = HtmlService.createTemplateFromFile("message"); htmlTemplate.tableDHL = createTableDHL(); htmlTemplate.dayDelivery = dayDelivery; htmlTemplate.plansLow = plansLow; htmlTemplate.plansHigh = plansHigh; htmlTemplate.returns = truckReturns(); htmlTemplate.monReturns = MonReturns; var mBody = htmlTemplate.evaluate().getContent(); Logger.log(mBody) GmailApp.sendEmail( 'xxxxx@gmail.com', //You can put as many emails you want 'prealert '   today, '', { htmlBody: mBody }); } 

На данный момент переменная {amp}lt;?= tableDHL ?{amp}gt; В HTML-коде выглядит не как таблица, а только как код внутри сообщения, когда я ее отправляю. Как я могу это исправить, чтобы он отображался в виде таблицы? Заранее спасибо за вашу помощь :)

Я добавил журналы выполненного кода. Вот что Logger.log(mBody) возвращает мне в сообщении mBody :

 {amp}lt;!DOCTYPE html{amp}gt; {amp}lt;html{amp}gt; {amp}lt;head{amp}gt; {amp}lt;base target="_top"{amp}gt; {amp}lt;/head{amp}gt; {amp}lt;body{amp}gt; {amp}lt;p style="font-family:verdana,sans-serif" {amp}gt; Dear All,{amp}lt;br{amp}gt;{amp}lt;br{amp}gt; {amp}lt;span style="background-color:rgb(217,234,211)"{amp}gt;{amp}lt;b{amp}gt;Delivery for today:{amp}lt;/b{amp}gt;{amp}lt;/span{amp}gt;{amp}lt;/p{amp}gt; {amp}lt;div onLoad="this.innerHTML = 'x3ctable border-collapse=collapsex3ex3ctr height=50 style=background-color:rgb(207,226,243)x3ex3cthx3eDatex3c/thx3ex3cth width=50x3eDayx3c/thx3ex3cth width=200x3ePlate numbersx3c/thx3ex3cthx3eQ-ty of parcelsx3c/thx3ex3c/trx3ex3ctrx3ex3ctd align=centerx3e14/10/2019x3c/tdx3ex3ctd align=centerx3eMonx3c/tdx3ex3ctd align=centerx3eEKURP85/ETMWC66x3c/tdx3ex3ctd align=centerx3e2 675x3c/tdx3ex3c/trx3ex3ctrx3ex3ctd align=centerx3e14/10/2019x3c/tdx3ex3ctd align=centerx3eMonx3c/tdx3ex3ctd align=centerx3eDDZ FP20/ DL 5334Px3c/tdx3ex3ctd align=centerx3e2 298x3c/tdx3ex3c/trx3ex3c/tablex3e'"{amp}gt;{amp}lt;/div{amp}gt; {amp}lt;p style="font-family:verdana,sans-serif" {amp}gt; Trucks should arrive till {amp}lt;b{amp}gt;4:30 PM{amp}lt;/b{amp}gt;.{amp}lt;br{amp}gt; More details can be sent to you by our suppliers.{amp}lt;br{amp}gt;{amp}lt;br{amp}gt; {amp}lt;span style="background-color:rgb(207,226,243)"{amp}gt;{amp}lt;b{amp}gt;Forecast:{amp}lt;/b{amp}gt;{amp}lt;/span{amp}gt;{amp}lt;br{amp}gt; Tomorrow we are planning to deliver about {amp}lt;b{amp}gt; 6000 - 7500 parcels.{amp}lt;/b{amp}gt;{amp}lt;br{amp}gt;{amp}lt;br{amp}gt; {amp}lt;span style="background-color:rgb(255,242,204)"{amp}gt;{amp}lt;b{amp}gt;Returns:{amp}lt;/b{amp}gt;{amp}lt;/span{amp}gt;{amp}lt;br{amp}gt; Tomorrow we are planning to pick up our returned packages by listed trucks:{amp}lt;/p{amp}gt; {amp}amp;lt;table border=0{amp}amp;gt;{amp}amp;lt;tr{amp}amp;gt;{amp}amp;lt;td{amp}amp;gt;EKURP85/ETMWC66{amp}amp;lt;/td{amp}amp;gt;{amp}amp;lt;/tr{amp}amp;gt;{amp}amp;lt;/table{amp}amp;gt; {amp}lt;/body{amp}gt; {amp}lt;/html{amp}gt; 

Что за странные символы td{amp}amp;gt;{amp}amp;lt x3c;x3e вставленные из {amp}lt;/{amp}gt; ?

Понравилась статья? Поделиться с друзьями:
JavaScript & TypeScript
Adblock
detector