Trong project google sheet này, mình chia sẻ đến các bạn cách chúng ta sử dụng apps script để viết hàm thực thi gửi email. Nội dung email là trang html và định dạng bằng css. Bảng dữ liệu được lấy từ google sheet. Bạn có thể gửi bảng dữ liệu của bất cứ sheetactive nào. Dưới đây là code và video demo.
Các project excell khác:
- Web App Script CSS JS | Tạo hiệu ứng Click button Nổ tung các mảnh giấy và chuyển link
- Web App Script CSS | Tạo button Liên hệ gồm 3 option đẹp mắt cho trang web
- Web App Script Webapp | Hiệu ứng hoa rơi – Form nhập liệu Gửi nội dung đến email
- Web App Script | Thanh trạng thái Status bar – Giá trị thể hiện theo điểm và label.
- Google sheet Apps script | Data Entry Form – Tự động đọc số tiền thành chữ ở trường input
- Google sheet Apps script | Cập nhật điểm lớp học – Theo danh sách lớp và Theo từng học sinh
- Google sheet, apps script Định dạng dấu phân cách hàng ngàn cho input
- Google sheet apps script | Chọn năm và kiểu biểu đồ để Load dữ liệu lên website
- Google sheet apps script | Scan QR code – Filter and get data table display on webapp
- Google sheet apps script Filter to get data to display on webapp, fill background color for data row
Code trang “code.gs”
function onOpen(){
let ui=SpreadsheetApp.getUi()
ui.createMenu("TÁC VỤ")
.addItem("Gửi email","myFunction")
.addToUi();
}
function myFunction() {
// connect to html template
var html = HtmlService.createTemplateFromFile('email');
// make data collection available to html files
html.dataCollect = dataCollector(0);
// connect to // connect to the template so that we can send it in email
var emailConnect = html.evaluate().getContent();
// send email
GmailApp.sendEmail(
["giaoanlinkdeverlop@gmail.com"],
"Ví dụ gửi bảng dữ liệu bằng HTML qua Mail",
"Bảng dữ liệu được gửi kèm!",
{htmlBody: emailConnect}
)
}
Code trang “email.html”
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= HtmlService.createHtmlOutputFromFile('css').getContent(); ?>
</head>
<body>
<p><a href="https://giaoan.link" target="_blank"><img src="https://giaoan.link/wp-content/uploads/2021/10/logo-giaoan-2021v2.gif"/></a></p>
<p style="font-weight: bold;">BẢNG TỔNG HỢP DOANH THU</p>
<table>
<thead>
<tr>
<th><?= dataCollect[0][0]?></th >
<th><?= dataCollect[0][1]?></th >
<th><?= dataCollect[0][2]?></th >
<th><?= dataCollect[0][3]?></th >
<th><?= dataCollect[0][4]?></th >
<th><?= dataCollect[0][5]?></th >
<th><?= dataCollect[0][6]?></th >
</tr>
</thead>
<tbody>
<? for (var i = 1; i < dataCollect.length; i++){ ?>
<tr>
<th><?= dataCollect[i][0]?></th >
<th><?= dataCollect[i][1]?></th >
<th><?= dataCollect[i][2]?></th >
<th><?= dataCollect[i][3]?></th >
<th><?= dataCollect[i][4]?></th >
<th><?= dataCollect[i][5]?></th >
<th><?= dataCollect[i][6]?></th >
</tr>
<?}?>
</tbody>
</table>
<p style="color:blue; font-style: italic">Cám ơn về sự nổ lực của các bạn!!</p>
</body>
</html>
Code trang “css.html”
<style>
table{
border-collapse: collapse;
border-radius: 15px;
margin: 25px 0;
font-family: sans-serif;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
width: 100%;
text-align: center;
table-layout: fixed;
}
thead tr{
background-color: #dd1818;
color: #ffffff;
padding: 12px 15px;
}
tbody tr{
background-color: #fffebe;
color: #000000;
padding: 12px 15px;
}
</style>
Code trang “function: dataCollector”
function dataCollector(){
//access the workbook
var wb = SpreadsheetApp.getActiveSpreadsheet();
//access the active sheet
var dataSheet= wb.getActiveSheet();
// access the data range
var dataRange = dataSheet.getRange(1, 1, dataSheet.getLastRow(), dataSheet.getLastColumn()).getDisplayValues();
// return statement
return dataRange;
}