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:
- VBA Excel Xây dựng hàm chuyển chuỗi có dấu thành không dấu, khoản trắng thành dấu –
- Google sheet Webapp | Lấy dữ liệu trên Sheet hiển thị dạng Bảng có phân trang trùy chọn trên website
- Web App Script | Trang trắc nghiệm online hỗ trợ hình ảnh, lưu file kết quả – Form nhập bộ câu hỏi
- Google sheet Webapp Quản lý Lịch dạy học giáo viên trong trung tâm-Lọc theo Tên-Lớp-Khung giờ -Môn
- Google sheet webapp | CRUD FORM – ĐIỀN THÔNG TIN TỪ FORM VÀO TEMPLATE GOOGLE DOC, KÈM HÌNH ẢNH 1
- Google sheet webapp | Dropdown phụ thuộc – Truy xuất Spreadsheet và các Sheet phụ thuộc từ Folder
- Tạo hóa đơn trên Google sheet – Gửi email đính kèm hóa đơn pdf – Quản lý url hóa đơn trên danh sách
- Google sheet Ứng dụng theo dõi đơn hàng – Danh sách sản phẩm mua, trạng thái đơn hàng
- Tip on Ms Word Hướng dẫn tạo ngắt trang, xóa ngắt trang đơn lẽ hoặc toàn bộ ngắt trang trên file doc
- Google sheet | Generate QR Codes – Mã hóa nội dung Cell – Thêm hình ở giữa mã QR code
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>