简体   繁体   English

如何使用Google表格脚本将Google表格单元格数据添加到html电子邮件中?

[英]How do you add google sheet cell data into an html-email using Google Scripts for Sheets?

I'm working on a script add-on for Sheets to send an invoice. 我正在为Sheets发送发票发送脚本。 I'd like to take data from a cell and add it directly into an html chart and then email this to clients. 我想从单元格中获取数据并将其直接添加到html图表中,然后通过电子邮件将其发送给客户端。 I'm having difficulty getting the data from the cell into the html chart. 我很难将数据从单元格获取到html图表中。

I've built [with the help of this community] a script that can send an html chart from Sheets into an email. 我已经建立了一个[可以在这个社区的帮助下]脚本,可以将表格中的html图表发送到电子邮件中。 However, the cell values never come out properly in the email [after I add them into the HTML format]. 但是,单元格值永远不会在电子邮件中正确显示出来(在将它们添加为HTML格式之后)。

 function composeChart() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[1] var data = sheet.getRange(4,2); var date = data.getValues(); //var chartComplete = titleChart+date+TD+date+TD+TR+endChart; var html2 = '<body>' + '<h2> Test </h2><br />' + '<p> Greetings Earthling </p>' + '<TABLE>'+ '<TR>'+ data +'</TD>'+ '<TD>' + data + '</TD>'+ '</TR>'+ '<TR>'+ '<TD>Data 3</TD>'+ '<TD>Data 4</TD>'+ '</TR>'+ '<TR>'+ '<TD>Data 5</TD>'+ '<TD>Data 6</TD>'+ '</TR>'+ '</TABLE>'+ '</body>' testMailApp3(html2, data); } function testMailApp3(html2, data) { MailApp.sendEmail( 'bakukai@gmail.com', // recipient 'test MailApp DATA', // subject 'TEST'+data, { // body htmlBody: html2 // advanced options } ); } 

I'd like the chart to look like this [ideally with simple borders, not shown below]: 我希望图表看起来像这样[理想情况下带有简单的边框,下面未显示]:

DATE    4/5 4/12 4/19 4/26
AMOUNT  231 213  424  213

Some remarks 一些评论

  • Arrays start with the index 0, rather than 1. So if you want to get the first sheet of your spreadsheet -it should be var sheet = ss.getSheets()[0] . 数组以索引0而不是1开头。因此,如果要获取电子表格的第一张纸,则应为var sheet = ss.getSheets()[0]
  • Data in your case is just the dat containing range, not its contents. 您的数据只是包含范围的数据,而不是其内容。 The contents are assigned to the variable date - in a 2-dimensional array. 将内容分配给可变date -二维数组。
  • Is all your data located in one cell? 您所有的数据都位于一个单元格中吗? The code implementation would be easier if every element you want to insert in were located in a separate cell. 如果要插入的每个元素都位于单独的单元格中,则代码实现会更容易。
  • From your range definition getRange(4,2) I undestand that all your data is contained in the single cell B4 . 从您的范围定义getRange(4,2)我无法理解您的所有数据都包含在单个单元格B4 In this case you need to convert the content of the 2-dimensional array date to a string and split your string into substrings, eg var stringArray=date[0][0].toString().split(" "); 在这种情况下,您需要将二维数组date的内容转换为字符串并将字符串拆分为子字符串,例如var stringArray=date[0][0].toString().split(" "); - provided the entries are separated by spaces -如果条目之间用空格隔开
  • The above step will assign your values to array elements of the 1-dimensional array stringArray 上面的步骤会将您的值分配给一维数组stringArray数组元素
  • in the next step you can embed the array elements within the HTML code: 在下一步中,您可以将数组元素嵌入HTML代码中:
'<TR>'+ stringArray[0] +'</TD>'+ '<TD>' + stringArray[1] + '</TD>'...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用脚本从 Google 表格中“工作表 B”上的数据生成“工作表 A”上的列表 - How Do You Generate List On "Sheet A" from Data on "Sheet B" in Google Sheets Using a Script 将Google工作表转换为html并使用Google App脚本作为电子邮件发送 - Convert google sheet to html and send as email using google app scripts 如何使用脚本在 Google 表格中更新单元格时添加时间戳? - How to time stamp when a cell was updated in Google Sheets using scripts? 在Google表格中,您如何更改用户看到的表格? - In Google Sheets, how do you change the sheet that the user sees? Google表格脚本onOpen(e)向表格添加新行 - Google Sheets Scripts onOpen(e) Add New Row to Sheet 如何使用谷歌应用程序脚本和谷歌工作表数据逐行发送 html 电子邮件 - How to send html email line by line using google apps script and google sheet data Google Scripts / Sheets 一旦数据被输入到单元格中,就为数据添加前缀 - Google Scripts / Sheets Add prefix to data once it has been entered into the cell 你如何将 HTML 变量传递给谷歌应用脚本 - How do you pass HTML variables to google app scripts 在Google表格中使用fontcolor()返回单元格中的HTML - Using fontcolor() in Google Sheets Returns HTML in Cell HTML 使用 Apps 脚本登陆“谢谢”页面向 Google 表格提交表单 - HTML Form Submission to Google Sheets using Apps Scripts Landing "Thank You" Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM