本文介绍了使用MailApp在htmlBody中从电子表格发送格式化的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Google电子表格中自动发送一些数据。
它工作正常,但布局是纯文本格式,没有格式化,就像在标准的CSV文件中一样。



我发现,我希望添加Google电子表格中的范围作为邮件内容中的HTML
。但我无法弄清楚如何做到这一点。



我尝试过使用函数 MailApp.sendEmail({recipient,subject,htmlBody:body} ); ,但是当定义我的var body时,我显然有问题。



范围可能类似于 A1:E15 ,其中A行包含标题。



结果在邮件中我得到了这个结果:

我需要的只是数据在行和列中与电子表格中一样
明智的解决方案是定义一个起始位置,如单元格A1,然后显示/使用数据直到下一个空行。

解决方案

因为您已经有了从电子表格发送数据的代码,所以我们只会看看如何生成格式良好的HTML输出。



我发布了,并带有一个 convertRange2html()函数,该函数可以给定一个Range对象,并将内容作为格式化的HTML表格返回。 (图书馆代码可以的形式提供。)



如果图书馆对您而言是新手,请阅读Google的开发人员网站。


$ b 要获取发送给您自己的电子表格的所有数据作为HTML表格:

  ... 
var recipient = Session.getActiveUser()。getEmail();
var subject =电子表格内容;
var range = SpreadsheetApp.getActiveSheet()。getDataRange();
var htmlTable = SheetConverter.convertRange2html(range);
var body =这里是表格:< br />< br />
+ htmlTable
+< br />< br />结束。
MailApp.sendEmail({recipient,subject,htmlBody:body});
...


I am trying to send some data automatically from a Google Spreadsheet.It works OK, but the layout is plain text with no formatting, as in a standard CSV file.

I found the Sending emails from a Spreadsheet tutorial in the Google Developer site, and I want a range from my Google Spreadsheet to be added as HTMLin the mail content. But I cannot figure out how to do this.

I have tried with the function MailApp.sendEmail ({recipient, subject, htmlBody: body});, but when defining my "var body" I obviously have the problem.

The range would be something like A1:E15, where row A contains the headline.

As a result in the mail I get this:

All I need is basically the data in rows and columns as in the spreadsheetThe brilliant solution would be to define a start position like cell A1 and then show/use data until next empty row.

解决方案

Since you already have code that sends data from the spreadsheet, we'll only look at how to generate the HTML body for nicely formatted output.

I've published the SheetConverter Library, with a convertRange2html() function that can be given a Range object and return the contents as a formatted HTML table. (The library code is available as a gist.)

If libraries are new to you, read the Libraries overview from the Google Developers site.

To get all the data of a spreadsheet sent to yourself as an HTML table:

...
var recipient = Session.getActiveUser().getEmail();
var subject = "Spreadsheet contents";
var range = SpreadsheetApp.getActiveSheet().getDataRange();
var htmlTable = SheetConverter.convertRange2html(range);
var body = "Here is the table:<br/><br/>"
         + htmlTable
         + "<br/><br/>The end."
MailApp.sendEmail({recipient, subject, htmlBody: body});
...

这篇关于使用MailApp在htmlBody中从电子表格发送格式化的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:29