Google sheet Apps script | Data Entry Popup From – Thêm mới – Chỉnh sửa – Xóa
Hôn nay, giaosanlink dẽ chia sẻ code đến các bạn về Google sheet Apps script | Data Entry Popup From – Thêm mới – Chỉnh sửa – Xóa. Đây là một dạng CRUD From, một đặc điểm khá thú vị là nó ẩn/hiện kiểu popup nên khá thuận tiện trong việc tiết kiệm khoản không. Dưới đây là demo trên video và code. Bạn có thể xem và thực hiện.
Bạn truy cập vào đây để tìm thêm nhiều project khác của app script, excell
Các project ngẫu nhiên:
- Google Script Web | From upload file – Kiểm tra trùng lặp – Ghi đè hoặc Tạo phiên bản mới cho file
- Google Script Web | Trang Tìm kiếm nhiều điều kiên riêng lẽ hoặc hết hợp – Print, Xuất file PDF
- Hướng dẫn kiểm tra Bàn phím, Tháo và thay bàn phím mới cho Dell Inspirion 15 3000 series
- Google sheet webapp | Quản lý bán hàng – Chức nằn trả góp, In thông tin
- Ổ C đầy Di chuyển dữ liệu ZALO sang Ổ D hoặc ổ khác một cách dễ dàng Không lo zalo báo đầy ổ đĩa
- Google sheet webapp|Nhập liệu-Quản lý khách hàng – Click tên hiển thị popup thông tin thêm – Print
- Google sheet webapp | Multi Load table data từ google sheet lên web và frezee header of table.
- HTML WEBAPP CSS -Mô phỏng hệ mặt trời -Trái đất -Mặt trăng đơn giản trên nền vũ trụ đen sao lấp lánh
- 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
Mã code “Code.gs”
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function loadData() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
if (!sheet) {
throw new Error('Sheet "Data" không tồn tại');
}
const data = sheet.getDataRange().getValues();
const headers = data.shift();
return data.map((row, index) => ({
id: index + 2, // Bắt đầu từ hàng 2, vì hàng đầu tiên là tiêu đề
studentCode: row[0],
studentName: row[1],
studentClass: row[2]
}));
}
function saveData(studentCode, studentName, studentClass) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
sheet.appendRow([studentCode, studentName, studentClass]);
}
function updateData(rowId, studentCode, studentName, studentClass) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
const range = sheet.getRange('A' + rowId + ':C' + rowId);
range.setValues([[studentCode, studentName, studentClass]]);
}
function deleteData(rowId) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
sheet.deleteRow(rowId);
}
Mã code “Index.html”
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function loadData() {
google.script.run.withSuccessHandler(renderTable).loadData();
}
function renderTable(data) {
const tableBody = document.getElementById('tableBody');
tableBody.innerHTML = '';
data.forEach(item => {
const row = document.createElement('tr');
const studentCodeCell = document.createElement('td');
studentCodeCell.innerText = item.studentCode;
row.appendChild(studentCodeCell);
const studentNameCell = document.createElement('td');
studentNameCell.innerText = item.studentName;
row.appendChild(studentNameCell);
const studentClassCell = document.createElement('td');
studentClassCell.innerText = item.studentClass;
row.appendChild(studentClassCell);
const editButton = document.createElement('button');
editButton.innerText = 'Sửa';
editButton.onclick = () => openForm('edit', item);
const editCell = document.createElement('td');
editCell.appendChild(editButton);
row.appendChild(editCell);
const deleteButton = document.createElement('button');
deleteButton.innerText = 'Xóa';
deleteButton.onclick = () => deleteData(item.id);
const deleteCell = document.createElement('td');
deleteCell.appendChild(deleteButton);
row.appendChild(deleteCell);
tableBody.appendChild(row);
});
}
function openForm(action, item = {}) {
const formPopup = document.getElementById('formPopup');
formPopup.style.display = 'block';
document.getElementById('formAction').value = action;
document.getElementById('rowId').value = item.id || '';
document.getElementById('studentCode').value = item.studentCode || '';
document.getElementById('studentName').value = item.studentName || '';
document.getElementById('studentClass').value = item.studentClass || '';
}
function closeForm() {
document.getElementById('formPopup').style.display = 'none';
}
function saveForm() {
const action = document.getElementById('formAction').value;
const rowId = document.getElementById('rowId').value;
const studentCode = document.getElementById('studentCode').value;
const studentName = document.getElementById('studentName').value;
const studentClass = document.getElementById('studentClass').value;
if (action === 'add') {
google.script.run.withSuccessHandler(loadData).saveData(studentCode, studentName, studentClass);
} else if (action === 'edit') {
google.script.run.withSuccessHandler(loadData).updateData(rowId, studentCode, studentName, studentClass);
}
closeForm();
}
function deleteData(rowId) {
google.script.run.withSuccessHandler(loadData).deleteData(rowId);
}
document.addEventListener('DOMContentLoaded', loadData);
</script>
<style>
#formPopup {
display: none;
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
padding: 15px;
background-color: white;
border: 1px solid #ff0000;
border-radius: 10px;
box-shadow: 2px 2px 3px grey;
z-index: 1000;
}
.table1 {
width: 100%;
border-collapse: collapse;
border: 1px solid grey;
}
.table1 th {
padding: 10px 0px;
text-align: center;
font-size:1em;
font-weight: bold;
background-color: #f68429;
color: black;
border: 1px solid grey;
}
.table1 td {
text-align: center;
border: 1px solid grey;
padding: 8px;
}
.table1 tr:{
background-color: #fbd2d2;
}
.bnt-them{
border: 1px solid grey;
border-radius: 15px;
padding: 8px;
margin-bottom: 10px;
font-size: 1em;
font-weight: bold;
color: white;
background-color: #1d801b;
cursor:pointer;
}
.bnt-them:hover{
box-shadow: 2px 2px 3px grey;
}
.bnt-luu{
border: 1px solid grey;
border-radius: 8px;
padding: 8px;
margin-bottom: 0px;
margin-top: 15px;
font-size: 1em;
font-weight: bold;
color: white;
background-color: #287804;
cursor:pointer;
}
.bnt-luu:hover{
box-shadow: 1px 1px 3px grey;
}
.bnt-dong{
border: 1px solid grey;
border-radius: 8px;
padding: 8px;
margin-bottom: 0px;
margin-top: 15px;
font-size: 1em;
font-weight: bold;
color: white;
background-color: #ff0000;
cursor:pointer;
}
.bnt-luu:dong{
box-shadow: 1px 1px 3px grey;
}
input{
border: 1px solid #4005b8;
border-radius: 8px;
padding: 8px;
margin-top: 8px;
box-shadow: 1px 1px 2px grey;
}
label{
font-size: 1.2em;
font-weight: bold;
}
@media(max-width: 800px) {
input{
width: 90%;
}
}
</style>
</head>
<body>
<button class="bnt-them" onclick="openForm('add')">Thêm mới</button>
<table class="table1">
<thead>
<tr>
<th>Mã Sinh Viên</th>
<th>Họ và Tên</th>
<th>Lớp Học</th>
<th colspan="2">Hành Động</th>
</tr>
</thead>
<tbody id="tableBody"></tbody>
</table>
<div id="formPopup">
<input type="hidden" id="formAction">
<input type="hidden" id="rowId">
<div>
<label for="studentCode">Mã Sinh Viên:</label>
<input type="text" id="studentCode">
</div>
<div>
<label for="studentName">Họ và Tên:</label>
<input type="text" id="studentName">
</div>
<div>
<label for="studentClass">Lớp Học:</label>
<input type="text" id="studentClass">
</div>
<button class="bnt-luu" onclick="saveForm()">Lưu</button>
<button class="bnt-dong" onclick="closeForm()">Đóng</button>
</div>
</body>
</html>