如何从 Google Sheet 获取单元格值以定义为 html 电子邮件中的变量

发布于 2025-01-13 07:26:38 字数 3268 浏览 3 评论 0原文

首先,我要说的是,我不太熟悉应用程序脚本,并且一直在尝试利用几个小时来参考该网站中过去的示例和内容线程,这些都很棒。这对我来说可能是一个愚蠢的错误。

问题陈述: 想要从工作表中获取一个值(以 InvoiceTemplate1 为例),然后将其定义为一个可以包含在我的 html 文件中的变量,我们可以在系统生成的电子邮件中使用该变量。

当前状态 拥有一个现有的应用程序脚本,可将工作表转换为 PDF,然后根据参考电子邮件地址向收件人发送电子邮件。该电子邮件使用 hmtl 文件 (Template1.html)。该电子邮件的格式很好,但我想使用“message1”的值。我可能没有正确使用 getValue 函数。

  function getEmailHtml() {
  var htmlTemplate = HtmlService.createTemplateFromFile("Template1.html");
  htmlTemplate.message1 =  
  SpreadsheetApp.getActive().getSheetByName('InvoiceTemplate1'). 
  getRange('M11').getValue();
  var htmlBody = htmlTemplate.evaluate().getContent();
  return htmlBody;
  }



 function emailSpreadsheetAsPDF1() {
 DocumentApp.getActiveDocument();
 DriveApp.getFiles();
 var htmlBody = getEmailHtml();

// 这是我的电子表格的链接,其中包含表单响应和发票模板表 // 替换“d/”和“/edit”之间链接中的文本 const ss = SpreadsheetApp.openByUrl("我的工作表");

// 从“InvoiceTemplate1”工作表的单元格“Q14”获取电子邮件地址 // 更改单元格的引用或工作表的名称(如果不同) const value = ss.getSheetByName("InvoiceTemplate1").getRange("Q14").getValue(); const email = value.toString();

//Display the hidden desired sheet - Template1 - that will be used to generate the PDF
 var spreadsheet = SpreadsheetApp.getActive();
 spreadsheet.getRange('D13').activate();
 spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Template1'), true);
 spreadsheet.getRange('A3').activate() 
 SpreadsheetApp.getActive().getSheetByName('Template1').showSheet();

// 电子邮件的主题 const subject = '新发票:' + ss.getSheetByName("InvoiceTemplate1").getRange("M11").getValue().toString() + ' ' + ss.getSheetByName("InvoiceTemplate1").getRange("C15").getValue().toString()

// 再次,电子表格的 URL,但现在末尾带有“/export” // 将其更改为电子表格的链接,但保留“/export” const url = '我的工作表/导出?';

const exportOptions =
'exportFormat=pdf&format=pdf' + // export as pdf
'&size=letter' + // paper size letter / You can use A4 or legal
'&portrait=true' + // orientation portal, use false for landscape
'&fitw=false' + // fit to page width false, to get the actual size
'&top_margin=0.10&bottom_margin=0.00&left_margin=0.33&right_margin=0.00' +
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid=ID'; // the sheet's Id. Change it to your sheet ID.
// You can find the sheet ID in the link bar. 
// Select the sheet that you want to print and check the link,
// the gid number of the sheet is on the end of your link.

var params = {method:"GET",headers:{"authorization":"Bearer "+ 
ScriptApp.getOAuthToken()}};
var templ = HtmlService.createTemplateFromFile('Template1');

// Generate the PDF file
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();

// Send the PDF file as an attachement 
GmailApp.sendEmail(email, subject, body, {
  htmlBody: htmlBody,
  attachments: [{
        fileName: "Invoice No.: " + 
    ss.getSheetByName("InvoiceTemplate1").getRange("M11").getValue().toString() + ".pdf",
        content: response.getBytes(),
        mimeType: "application/pdf"
    }]
    });

First, let me say that I'm not well versed with app scripts and have been trying to leverage hours to reference past examples and content threads within this site that has been wonderful. This is probably a silly error on my part.

Problem Statement:
Want to obtain a value from a sheet (InvoiceTemplate1 as an example) and then define it as a variable that can be included in my html file that can we consumed in a system generated email.

Current Status
Have an existing app script that will convert a sheet to PDF and then emails a recipient based on a reference email address. The email uses a hmtl file (Template1.html). The email is well formatted but I would like to consume the value of 'message1'. I'm probably not using the getValue function correctly.

  function getEmailHtml() {
  var htmlTemplate = HtmlService.createTemplateFromFile("Template1.html");
  htmlTemplate.message1 =  
  SpreadsheetApp.getActive().getSheetByName('InvoiceTemplate1'). 
  getRange('M11').getValue();
  var htmlBody = htmlTemplate.evaluate().getContent();
  return htmlBody;
  }



 function emailSpreadsheetAsPDF1() {
 DocumentApp.getActiveDocument();
 DriveApp.getFiles();
 var htmlBody = getEmailHtml();

// This is the link to my spreadsheet with the Form responses and the Invoice Template sheets
// replace the text in the link between "d/" and "/edit"
const ss = SpreadsheetApp.openByUrl("my sheet");

// Obtain the email address from the cell "Q14" from the "InvoiceTemplate1" sheet
// Change the reference of the cell or the name of the sheet if it is different
const value = ss.getSheetByName("InvoiceTemplate1").getRange("Q14").getValue();
const email = value.toString();

//Display the hidden desired sheet - Template1 - that will be used to generate the PDF
 var spreadsheet = SpreadsheetApp.getActive();
 spreadsheet.getRange('D13').activate();
 spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Template1'), true);
 spreadsheet.getRange('A3').activate() 
 SpreadsheetApp.getActive().getSheetByName('Template1').showSheet();

// Subject of the email message
const subject = 'New Invoice: ' +
ss.getSheetByName("InvoiceTemplate1").getRange("M11").getValue().toString() + ' ' +
ss.getSheetByName("InvoiceTemplate1").getRange("C15").getValue().toString()

// Again, the URL to your spreadsheet but now with "/export" at the end
// Change it to the link of your spreadsheet, but leave the "/export"
const url = 'my sheet / export?';

const exportOptions =
'exportFormat=pdf&format=pdf' + // export as pdf
'&size=letter' + // paper size letter / You can use A4 or legal
'&portrait=true' + // orientation portal, use false for landscape
'&fitw=false' + // fit to page width false, to get the actual size
'&top_margin=0.10&bottom_margin=0.00&left_margin=0.33&right_margin=0.00' +
'&sheetnames=false&printtitle=false' + // hide optional headers and footers
'&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
'&fzr=false' + // do not repeat row headers (frozen rows) on each page
'&gid=ID'; // the sheet's Id. Change it to your sheet ID.
// You can find the sheet ID in the link bar. 
// Select the sheet that you want to print and check the link,
// the gid number of the sheet is on the end of your link.

var params = {method:"GET",headers:{"authorization":"Bearer "+ 
ScriptApp.getOAuthToken()}};
var templ = HtmlService.createTemplateFromFile('Template1');

// Generate the PDF file
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();

// Send the PDF file as an attachement 
GmailApp.sendEmail(email, subject, body, {
  htmlBody: htmlBody,
  attachments: [{
        fileName: "Invoice No.: " + 
    ss.getSheetByName("InvoiceTemplate1").getRange("M11").getValue().toString() + ".pdf",
        content: response.getBytes(),
        mimeType: "application/pdf"
    }]
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2025-01-20 07:26:38

我在 google 文档中使用了占位符的标签,例如,然后在脚本中替换

// substitute candidate first name for tag
body = body.replace("<first_name>", FirstName);

FirstName 是之前定义的变量

I have used tags in the google doc for placeholders such as <first_name>, then substitute in the script

// substitute candidate first name for tag
body = body.replace("<first_name>", FirstName);

FirstName is a variable defined previously

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文