使用Google脚本将变量推送到HTML中 [英] Push Variables Into HTML Using Google Script

查看:123
本文介绍了使用Google脚本将变量推送到HTML中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Google脚本变量用于mail_templateHTML文档。我不明白为什么我的变量不能用于这个HTML文档,而我可以将它们用于google脚本本身。



我有3个文档:

p>


  1. 链接到Google工作表的Google脚本(.gs)(我们称之为X)

  2. 一个链接到上述XGoogle表格的html文档

  3. 和X* Google表格本身

这项工作的目的是使用Google工作表数据发送设计的电子邮件。



我是初学者,文档似乎适用于更高级的用户。
https://developers.google.com/apps-script/指南/ html /模板



有些插件正在做同样的工作,但它们未被授权到我的工作区中。这是非常困难的,我试图解决这个问题,因为3天,但我仍然不能。

 <!DOCTYPE html>< html> < HEAD> < base target =_ top> < /头> <身体GT; < ;? solution1?> < /体> < / html>  

-lang =jsdata-hide =falsedata-console =truedata-babel =false>

  //这个常量写在列O中,用于已成功发送电子邮件的行.var EMAIL_SENT =EMAIL_SENT; function testSchemas(){{var sheet = SpreadsheetApp.getActiveSheet(); var startRow = sheet.getLastRow(); var numRows = 1; var dataRange = sheet.getRange(startRow,1,numRows,15)var data = dataRange.getValues(); for(var i = 0; i< data.length; ++ i){var row = data [i]; var name = row [2]; var surname = row [3]; var salesRepEmail = row [4]; var qualityAnalystEmail =xxx@yahoo.frvar customerEmail = row [5]; var websiteURL = row [6]; var solution1 = row [7];函数doGet(){return HtmlService .createTemplateFromFile('mail_template').evaluate();} var htmlBody = HtmlService.createHtmlOutputFromFile('mail_template')。getContent(); var emailSent = row [14]; //第三栏if(emailSent!=EMAIL_SENT){//防止发送重复邮件/*MailApp.sendEmail(customerEmail,主题,消息,{cc:,bcc:qualityAnalystEmail +,+ salesRepEmail}); * / MailApp.sendEmail({to:customerEmail,bcc:qualityAnalystEmail +,+ salesRepEmail,subject:'Derere Consultation Du Site Mobile'+ websiteURL,htmlBody:htmlBody,}); } Logger.log(name); sheet.getRange(startRow + i,15).setValue(EMAIL_SENT); //确保在脚本中断的情况下马上更新单元格SpreadsheetApp.flush(); 

解决方案

如果您希望将.gs文件中的变量传递给您的HTML模板,请在调用模板对象上的evaluate()之前尝试以下代码:

  var template = HtmlService.createTemplateFromFile('mail_template'); 
template.myVar = myVar;
return template.evaluate();

调用evaluate()执行JS脚本(包装在<?>中的内联代码)。要使模板可以访问该变量,只需将其作为对象属性进行附加。最后,将变量打印到页面的正确符号是:

 <?= myVar?> 


I would like to use Google Script variables into a "mail_template" HTML document. I do not understand why my variables can't be used into this HTML document, while I can use them into the google script itself.

I have 3 documents:

  1. A google script ( .gs ) linked to a Google sheet ( Let's call it "X" )
  2. An html document linked to the above "X" Google sheet
  3. And the "X" *Google sheet itself

The purpose of this work is to send designed email using Google sheet data.

I am beginner, and the documentation seems to be for more advanced users. https://developers.google.com/apps-script/guides/html/templates

Some plugins are doing the same work, but they are not authorized into my workspace. It's very difficult, I am trying to solve that since 3 days, but I still can't.

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <? solution1 ?>
  
  </body>
 </html>

// This constant is written in column O for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";

function testSchemas() { {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = sheet.getLastRow();  
  var numRows = 1;  
  var dataRange = sheet.getRange(startRow, 1, numRows, 15)
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var name = row[2]; 
    var surname = row[3]; 
    var salesRepEmail = row[4]; 
    var qualityAnalystEmail = "xxx@yahoo.fr"
    var customerEmail = row[5]; 
    var websiteURL = row[6]; 
    var solution1 = row[7];
    function doGet() {
  return HtmlService
      .createTemplateFromFile('mail_template')
      .evaluate();
}

    
    
    var htmlBody = HtmlService.createHtmlOutputFromFile('mail_template').getContent(); 
    var emailSent = row[14];     // Third column
    if (emailSent != "EMAIL_SENT") {  // Prevents sending duplicates
     /*MailApp.sendEmail(customerEmail, subject, message, {
        cc: "",
        bcc: qualityAnalystEmail + ", " + salesRepEmail
      }); */
    MailApp.sendEmail({
    to: customerEmail,
    bcc: qualityAnalystEmail + ", " + salesRepEmail,
    subject: 'Résumé De Notre Consultation Du Site Mobile ' + websiteURL,
    htmlBody: htmlBody,
  }); 
}
      Logger.log(name);
      sheet.getRange(startRow + i, 15).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}

解决方案

If you'd like to pass variables from .gs file to your HTML template, try this before calling evaluate() on the template object:

var template = HtmlService.createTemplateFromFile('mail_template');
template.myVar = myVar;
return template.evaluate();

Calling evaluate() executes the JS scriptlets (inline code wrapped in <? ?>). To make the variable accessible to the template, simply attach it as object property. Finally, the correct notation for printing your variable to the page is

<?= myVar ?>

这篇关于使用Google脚本将变量推送到HTML中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆