Giaoan.link chia sẻ tiếp đến các bạn project trên Google sheet – Tìm dữ liệu và in trên Google sheet bằng Apps script và web app. Dữ liệu được lưu trữ trên Spreadsheet và chúng ta sẽ viết giao diện người dùng trên web app (html) để tìm dữ liệu theo từ khóa, kết quả tìm kiếm có thể in (print) ra. Dưới đây là video hướng dẫn và code của project.
Bạn xem thêm project excel ứng dụng khác:
- Google webapp | Form tìm, hiển thị kết quả học tập nhiều môn và in phiếu kết quả
- Danh mục các Bài học Google sheet Apps script Cơ bản
- Bản Nâng cấp Hệ thống hóa đơn Phân quyền Quản lý khách Quản lý sản phẩm Quản lý hóa đơn
- Tạo webapp import và update dữ liệu từ Excel lên Google sheet có Load bảng Table lên nền tảng web
- Google sheet, appscript và webapp – Giải pháp thu hoạc phí toàn diện
- Quản Lý Học Phí Bằng Google Apps Script: Form CRUD, Tìm Kiếm, In Phiếu Thu và Tự Động Tính Toán
- 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
Code.gs
function doGet() {
return HtmlService.createTemplateFromFile('index')
.evaluate()
.addMetaTag('viewport', 'width=device-width, initial-scale=1')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
}
function submitData(obj){
var ss = SpreadsheetApp.openById("17Vf7HVlGmyh6-0O1VkzYCR1v6zOGQ5JENR326EObs9U");
var sheet = ss.getSheetByName("data");
var flag = 1 ;
var lr = sheet.getLastRow();
for(var i = 1;i <= lr;i++){
var id = sheet.getRange(i, 1).getValue();
if(id == obj){
flag = 0;
var phonenumber = sheet.getRange(i, 2).getValue();
var namesv = sheet.getRange(i, 3).getValue();
var object1 = sheet.getRange(i, 4).getValue();
var object2 = sheet.getRange(i, 5).getValue();
var object3 = sheet.getRange(i, 6).getValue();
var data ="<table><tr><th colspan=2><center><img src='https://giaoan.link/wp-content/uploads/2021/10/logo-giaoan-2021v2.gif'style='width:80px;margin-top:8px'><br>BẠN ĐẶT LOGO TRƯỜNG Ở ĐÂY</center></th></tr><tr><td style='font-weight: bold;'>Mã sinh viên: </td><td>"
+obj+"</td></tr><tr><td style='font-weight: bold;'>Số điện thoại:</td><td>"+phonenumber+"</td></tr><tr><td style='font-weight: bold;'>Tên sinh viên:</td><td>"
+namesv+"</td></tr><tr><td style='font-weight: bold;'>Điểm Môn 1:</td> <td>"+object1+"</td></tr> <tr><td style='font-weight: bold;'>Điểm môn 2:</td><td>"+object2+"</td></tr><tr><td style='font-weight: bold;'>Điểm Môn 3:</td><td>"+object3+"</td></tr></table>";
return data;
}
}
if(flag==1){
var data ="<p style='font-style: italic; color: red;'>Bạn chưa điền MSV hoặc MSV chưa chính xác!</p>";
return data;
}
};
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top" />
<h2>TIỆN ÍCH TÌM THÔNG TIN SINH VIÊN</h2>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
<style>
body{
background-color:#f0f7f8;
}
input{
width: 90%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #000;
border-radius: 8px;
box-sizing: border-box;
}
table {
font-family: times;
border-collapse: collapse;
width:90%;
color:#e3edf0;
}
td, th {
border: 1px solid #000;
text-align: left;
padding: 8px;
}
tr,th:nth-child(even) {
background-color: #faf7f7;
color:#000;
}
h2{
color:#000;
margin-top:10px;
font-size:25px;
font-weight:bold;
}
</style>
</head>
<body align="center">
<input type="text-align-center"id="id" placeholder="Bạn nhập Mã số sinh viên"/>
<br>
<input type="submit" class="btn btn-primary" value="Tìm kết quả" onclick="info()"/>
<center>
<div id='PrintDiv'>
<div id="result"></div>
</center>
<script>
function info () {
let obj = document.querySelector('#id').value;
let updateLocation = document.querySelector('#result');
updateLocation.innerHTML = "Đang lấy dữ liệu từ Server...";
function onFailure(error){
let warning = "<span style='color:red'>"+error+"</span>";
updateLocation.innerHTML = warning;
};
function onSuccess(response){
let result ="<span style='color:#000;'>"+response+"</span>";
updateLocation.innerHTML = result;
};
google.script.run.withFailureHandler(onFailure)
.withSuccessHandler(onSuccess)
.submitData(obj);
};
</script>
<script>
function Print(DivID) {
/*var disp_setting = "toolbar=yes,location=no,directories=yes,menubar=yes,";
disp_setting += "scrollbars=yes,width=650, height=600, left=100, top=25";*/
var iPrint = document.getElementById(DivID).innerHTML;
var docprint=window.open("","");
docprint.document.open();
docprint.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"');
docprint.document.write('<head><meta name="viewport" content="width=device-width,initial-scale=1.0">');
docprint.document.write('<title>Print</title>');
docprint.document.write('<style type="text/css">@page {size: auto;margin:2em;}');
docprint.document.write('table,td{ border:1px solid black; padding: 5px; border-collapse: collapse;}</style>');
docprint.document.write('</head><center><body onLoad="self.print()">');
docprint.document.write(iPrint);
docprint.document.write('</body></center></html>');
docprint.document.close();
docprint.focus();
}
</script>
<input type="button" class="btn btn-secondary" name="btnprint" value="PRINT" onclick="Print('PrintDiv')"style="width:20%">
</div>
</body>
</html>
