Web App Script | Thanh trạng thái Status bar – Giá trị thể hiện theo điểm và label.
Ở project này, giaoan.link sẽ chia sẻ đến bạn một project status bar ở trên web. Ở đây, dữ liệu sẽ được lấy từ dòng dữ liệu trên google sheet – khi giá trị thay đổi thì thanh trạng thái này tự động cập nhật thay đổi.
Có hai dạng hiển thị giá trị trên thanh trạng thái này: 1 – Bạn chỉ hiển thị một giá trị cuối của dòng dữ liệu; 2- Hiển thị toàn bộ giá trị trên dòng dữ liệu.
Bạn có thể lựa chọn một trong hai để phú hợp với nhu cầu công việc của mình. Phía dưới là video deo demo và code của project.
Các project khác, bạn có thể tìm kiếm ở đây.
Các project excel, google sheet ngẫu nhiên:
- 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
- Google sheet App Script | Tạo custom menu – Xóa tất cả dòng Rỗng trên vùng dữ liệu Nhanh chống
Mã code trang “code.gs”
function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}
function getPointsData() {
const sheetId = '1DDlLcZpsJynmctbxyTaFE9pRJPuxGyQaB2bBAGIsgZc';
const sheet = SpreadsheetApp.openById(sheetId).getSheetByName('Data');
if (!sheet) {
throw new Error('Sheet Data không tồn tại');
}
const range = sheet.getRange('A1:G2'); // Lấy hàng đầu tiên cho header và hàng thứ hai cho dữ liệu điểm
const data = range.getValues();
return data;
}
Mã code trang “index.html”
<!DOCTYPE html>
<html>
<head>
<!--Hiển thị tất cả điểm giá trị-->
<base target="_top">
<style>
.div-container{
width: 900px;
tex-align: center;
padding: 30px;
border: 1px solid grey;
border-radius: 10px;
box-shadow: 2px 2px 3px grey;
margin-left: auto;
margin-right: auto;
}
.progress-container {
width: 800px; /* Đảm bảo đủ khoảng cách cho hình tròn nằm cuối đường line */
height: 20px; /* Tăng chiều cao để đảm bảo không chèn lẫn nhau */
position: relative;
margin-top: 50px;
margin-bottom: 30px; /* Thêm khoảng cách để label không đè lên các thành phần khác */
margin-left: auto;
margin-right: auto;
}
.progress-bar {
width: 100%;
height: 5px;
background-color: #76c7c0;
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
z-index: 1; /* Đảm bảo rằng đường line nằm phía sau các hình tròn */
}
.progress-point {
width: 20px;
height: 20px;
background-color: #ffc107;
box-shadow: 1px 1px 2px grey;
border-radius: 50%;
position: absolute;
top: 50%; /* Đảm bảo hình tròn nằm trùng với đường line */
transform: translateY(-50%);
z-index: 2; /* Đảm bảo hình tròn nằm phía trên đường line */
}
.label {
position: absolute;
top: -30px; /* Đặt label ở trên hình tròn */
background-color: #565454;
box-shadow: 1px 1px 2px grey;
color: white;
padding: 2px 7px;
border-radius: 3px;
font-size: 1em;
white-space: nowrap;
z-index: 3; /* Đảm bảo label nằm trên hình tròn */
}
.title{
font-size: 2em;
font-weight: bold;
color: red;
text-align: center;
}
</style>
<script>
function loadPoints() {
google.script.run.withSuccessHandler(function(data) {
console.log("Data retrieved:", data); // Ghi nhật ký dữ liệu để kiểm tra
displayPoints(data);
}).getPointsData();
}
function displayPoints(data) {
const maxPoints = 100;
const lineWidth = 100;
// const container = document.createElement('div');
const container = document.getElementById('progressContainer');
container.innerHTML = '';
container.className = 'progress-container';
const line = document.createElement('div');
line.className = 'progress-bar';
container.appendChild(line);
for (let i = 1; i < data[0].length; i++) {
const value = data[1][i];
if (value !== "") {
const percentage = (value / maxPoints) * 100;
const progress = (lineWidth / 100) * percentage;
const point = document.createElement('div');
point.className = 'progress-point';
point.style.left = progress + 'px';
const label = document.createElement('div');
label.className = 'label';
label.style.left = progress + 'px';
label.textContent = value;
container.appendChild(point);
container.appendChild(label);
}
}
document.getElementById('progressContainer').appendChild(container);
}
function startAutoUpdate() {
loadPoints();
setInterval(loadPoints, 5000); // Cập nhật mỗi 5 giây
}
window.onload = startAutoUpdate;
</script>
</head>
<body>
<div class="div-container">
<div class="title"> Thanh trạng thái </div>
<div id="progressContainer"></div>
</div>
</body>
</html>