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:
- Giảm dung lượng PDF dễ dàng với PDF24 Creator – Miễn phí và hiệu quả
- Chuyển đổi Pdf dạng bản scan sang MS Excel chuẩn không bị lỗi font và giữ nguyên định dạng
- Chuyển định dạng file pdf dạng scan – dạng chụp hình ảnh sang MS Word định dạng chuẩn 99.9%
- Phím tắt vào BIOS và Boot Options của các hãng máy tính phổ biến
- Google sheet webapp | Form đăng ký Tự động gửi ID và QR code Check in qua địa chỉ mail
- Cách tắt Windows update và Windows Defender cực đơn giản
- Apsscript Radio Button và Input – Sự phụ thuộc của input vào Radio button
- Bạn sử dụng zalo và bị báo đầy ổ đĩa,phải làm sao
- Google sheet Webapp Login form Tocken quản lý phiên đăng nhập, gửi mail lấy lại thông tin đăng nhập
- Chuyển bảng dữ liệu từ file PDF vào Excel
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;
}