Google sheet App Script Web app – Tìm kiếm có gợi ý – AutoComplete field
Giáo án link chia sẻ đến các bạn thêm một project về Google sheet App Script Web app – Tìm kiếm có gợi ý – AutoComplete field. Với chức năng này, khi bạn gõ một vài từ thì sẽ được hiển thị thêm các cụm từ gần giống để bạn có thể lựa chọn nhanh hơn. Các bạn xem video hướng dẫn bên dưới và tham khảo code app script trong video.
Các bài học excel ứng dụng:
- Google sheet Webapp Script | Tìm kiếm, lọc và In chi tiết thông tin sản phẩm của mã Khách hàng
- Google sheet Apps script | Data Entry Popup From – Thêm mới – Chỉnh sửa – Xóa
- Google sheet Webapp Script | Quản lý đặt xe cho chuyến đi – Tài xế cập nhật hoàn thành thông tin
- Apps script Webapp | Lấy giá trị Input hiển thị lên – Web Get value input field display on web
- Google sheet Apps script | Todo List and Send mail – Tạo danh sách nhắc việc và gửi mail
- Web App Script | Bộ Icon CSS Rất dễ lập trình cho giao diện webapp
- Google sheet Webapp | Project Quản lý khách hàng – Cập nhật tiến độ sửa chữa
- Google sheet Apps script Webapp | Login form OTP xác minh qua email, Chuyển link cho từng User
- 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
Code trong file “Code.gs”
function doGet(e) {
var html = HtmlService.createTemplateFromFile("index").evaluate().setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
return html;
}
function getZibcode()
{
var ss= SpreadsheetApp.getActiveSpreadsheet();
var zibcodeSheet = ss.getSheetByName("Data");
var zibcodeRange = zibcodeSheet.getRange("B2:B");
var zibcodeValues = zibcodeRange.getValues();
return zibcodeValues;
}
function submitData(obj){
var ws=SpreadsheetApp.openById("PASTE ID SPREADSHEET HERE");
var ss=ws.getSheetByName("Data");
var flag = 1;
var lr=ss.getLastRow();
var obj=obj.toLowerCase();
for(var i=2;i<=lr;i++){
var vId=ss.getRange(i,2).getValue().toLowerCase();
if(vId==obj){
flag=0;
let b1= ss.getRange(i,1).getValue();
let b2= ss.getRange(i,2).getValue();
let b3= ss.getRange(i,3).getValue();
let b4= ss.getRange(i,4).getValue();
var data="<table>"
+"<tr>"
+"<th> STT </th><th> TỈNH/THÀNH </th><th> MÃ BƯU CHÍNH </th> <th>MÃ VÙNG </th>"
+"</tr> "
+"<tr>"
+"<td>"+b1+"</td><td>"+b2+"</td><td>"+b3+"</td><td>"+b4+"</td>"
+"</tr>"
+"<tr>"
+"<td colspan='4'><p>Mã bưu điện quốc gia bao gồm tập hợp 05 (năm) ký tự số, cụ thể như sau:<br>* 2 ký tự đầu tiên dùng để xác định tỉnh, thành phố trực thuộc Trung ương.<br> * 3 hoặc 4 ký tự đầu tiên được sử dụng để xác định quận, huyện và đơn vị hành chính tương đương. <br>* Toàn bộ 5 ký tự xác định đối tượng gán Mã bưu chính quốc gia.</p><p style='font-style:italic; color:red'>Lưu ý: một số trang web nước ngoài khi điền thông tin nếu vẫn yêu cầu điền 6 chữ số thì bạn chỉ cần thêm 1 số không (0) vào cuối là được.</p></td>"
+"</tr>"
+"</table>";
return data;
}
}
if (flag==1){
if (obj==""){
var data = "<span style = 'font-size:1.5em; color:red'>Bạn chưa điền tên tỉnh thành!</span>";
}else if(vId!=obj){
var data = "<span style = 'font-size:1.5em; color:red'>Không tồn tại mã bưu chính của tỉnh/thành: <font color='blue'>"+obj+" </font>!</span>";
}
return data;
}
};
Code trong file “index.html”
<html>
<head>
<?!=HtmlService.createHtmlOutputFromFile('javascript').getContent();?>
</head>
<body>
<div class="col text-center">
<br>
<form align="center">
<div class="mb-1">
<label style="font-weight: bold; color: blue" >TIỆN ÍCH TÌM MÃ BƯU CHÍNH VÀ MÃ VÙNG 63 TỈNH THÀNH VIỆT NAM<br>
<font color="red">NĂM 2023</font>
</label>
</div>
<div class="mb-1">
<div class="form-check form-check-inline">
<label style="font-weight: bold;" for="id" class="form-label">GÕ TÊN TỈNH THÀNH:</label>
</div>
<div class="form-check form-check-inline">
<input type="text" id="id" name="id" class="form-control form-control-sm" placeholder="Tên tỉnh thành" required>
</div>
</div>
<button type="button" class="btn btn-primary" onclick="info()">Lấy thông tin</button>
<hr>
<div align="center" id="result"></div>
</form>
<script>
//Auto complete field
$( document ).ready(function() {
getZibcode();
});
function getZibcode()
{
google.script.run.withSuccessHandler(function(ar)
{
console.log(ar);
statesArray = [];
ar.forEach(function(item, index)
{
statesArray.push(item[0]);
});
$("#id").autocomplete({
source: statesArray
});
}).getZibcode();
}
//Hien thi thong tin zibcode
function info(){
let msv= document.querySelector('#id').value;
let updateLocation = document.querySelector('#result');
updateLocation.innerHTML = "Đang lấy dữ liệu...";
function onFailure(error){
let warning = "<span style='color:red'>"+error+"</span>";
updateLocation.innerHTML=warning;
};
function onSuccess(response){
let result = "<span style='color: black'>"+response+"</span>";
updateLocation.innerHTML=result;
};
google.script.run.withFailureHandler(onFailure)
.withSuccessHandler(onSuccess)
.submitData(msv);
};
</script>
</body>
</html>
Code file “javascript.html”
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" ></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" ></script>
<style>
.hideform {
display: none;
}
body {
background: #ffffff;
}
table{
align: middle;
font-family: times;
border-collapse: collapse;
width: 100%;
}
td{
border: 1px solid #000000;
text-align: center;
padding: 8px;
}
th{
border: 1px solid #000000;
background-color: yellow;
text-align: center;
padding: 8px;
}
tr:nth-child(even){
background-color: #dddddd;
}
.frame{
box-shadow: 10px 10px 15px rgb(193,182,182), -6px 6px 15px #eee;
padding: 20px;
max-width: 60%;
margin: 0px auto;
margin-top: 50px;
border-radius: 10px;
background: white;
}
.btn.focus, .btn:focus {
outline: 0;
box-shadow: none!important;
color: white;
}
.btn-info: hover {
background-color: gold;
color: white;
border-color: gold;
}
.btn-info {color: white;}
.ui-autocomplete {
position: absolute;
top: 100%;
left: 0;
z-index: 1000;
display: none;
float: left;
min-width: 160px;
padding: 5px 0;
margin: 2px 0 0;
list-style: none;
font-size: 14px;
text-align: left;
background-color: #ffffff;
border: 1px solid #cccccc;
border: 1px solid rgba(0, 0, 0, 0.15);
border-radius: 4px;
-webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
background-clip: padding-box;
}
.ui-autocomplete > li > div {
display: block;
padding: 3px 20px;
clear: both;
font-weight: normal;
line-height: 1.42857143;
color: #333333;
white-space: nowrap;
}
.ui-state-hover,
.ui-state-active,
.ui-state-focus {
text-decoration: none;
color: #262626;
background-color: #f5f5f5;
cursor: pointer;
}
.ui-helper-hidden-accessible {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
</style>