Google sheet Apps script | Data Entry Form – Tự động đọc số tiền thành chữ ở trường input
Giaoan.link chia sẻ đến các bạn projetc về Google sheet Apps script | Data Entry Form – Tự động đọc số tiền thành chữ ở trường input. Với project này, bạn sẽ điền số tiền ở trường 1 và trường 2 sẽ tự động đọc thành chữ. Bạn hoàn toàn có thể submit dữ liệu này.
Các project excel, apps script ngẫu nhiên:
- Web App Script CSS | Tạo button Liên hệ gồm 3 option đẹp mắt cho trang web
- Web App Script Webapp | Hiệu ứng hoa rơi – Form nhập liệu Gửi nội dung đến email
- Web App Script | Thanh trạng thái Status bar – Giá trị thể hiện theo điểm và label.
- Google sheet Apps script | Data Entry Form – Tự động đọc số tiền thành chữ ở trường input
- Google sheet Apps script | Cập nhật điểm lớp học – Theo danh sách lớp và Theo từng học sinh
- Google sheet, apps script Định dạng dấu phân cách hàng ngàn cho input
- Google sheet apps script | Chọn năm và kiểu biểu đồ để Load dữ liệu lên website
- Google sheet apps script | Scan QR code – Filter and get data table display on webapp
- Google sheet apps script Filter to get data to display on webapp, fill background color for data row
- Google sheet Apps script Webapp | Project Quản lý đơn hàng – Cập nhật sản phẩm – In phiếu kiểm soát
Mã trang “code.gs”
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function convertNumberToText(number) {
var units = ['', 'một', 'hai', 'ba', 'bốn', 'năm', 'sáu', 'bảy', 'tám', 'chín'];
var teens = ['mười', 'mười một', 'mười hai', 'mười ba', 'mười bốn', 'mười lăm', 'mười sáu', 'mười bảy', 'mười tám', 'mười chín'];
var tens = ['', '', 'hai mươi', 'ba mươi', 'bốn mươi', 'năm mươi', 'sáu mươi', 'bảy mươi', 'tám mươi', 'chín mươi'];
function convertToText(n) {
if (n < 10) return units[n];
else if (n < 20) return teens[n - 10];
else {
var unit = n % 10;
var ten = Math.floor(n / 10);
return tens[ten] + (unit ? ' ' + units[unit] : '');
}
}
function convertGroupOfThree(number, isFirstGroup) {
var hundred = Math.floor(number / 100);
var rest = number % 100;
var text = '';
if (hundred) {
text += units[hundred] + ' trăm';
if (rest) {
if (rest < 10) {
text += ' lẻ ' + convertToText(rest);
} else {
text += ' ' + convertToText(rest);
}
}
} else if (rest) {
if (rest < 10) {
text += (!isFirstGroup ? 'không trăm lẻ ' : '') + convertToText(rest);
} else {
text += (!isFirstGroup ? 'không trăm ' : '') + convertToText(rest);
}
} else if (!isFirstGroup) {
text += 'không trăm';
}
return text;
}
var groups = [];
while (number > 0) {
groups.push(number % 1000);
number = Math.floor(number / 1000);
}
groups.reverse();
var text = '';
var groupText = ['', 'nghìn', 'triệu', 'tỷ', 'nghìn tỷ', 'triệu tỷ'];
for (var i = 0; i < groups.length; i++) {
if (groups[i] !== 0 || i === groups.length - 1) {
text += (i > 0 ? ' ' : '') + convertGroupOfThree(groups[i], i === 0) + (groupText[groups.length - 1 - i] ? ' ' + groupText[groups.length - 1 - i] : '');
}
}
return text.trim() + ' đồng';
}
function saveData(amount) {
var sheet = SpreadsheetApp.openById('1JCCDbaqR1juCKX33WEu20PElBWwRgZB-CCrSjO5bFWI').getSheetByName('Sheet1');
var text = convertNumberToText(amount);
sheet.appendRow([amount, text]);
return text;
}
Mã trang “Index.html”
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
body {
font-family: Arial, sans-serif;
width: 800px;
border: 1px solid #ff9600;
border-radius: 15px;
box-shadow: 2px 2px 3px grey;
padding: 10px;
margin-left: auto;
margin-right: auto;
}
.input-container {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="number"], input[type="text"] {
width: 100%;
padding: 8px;
box-sizing: border-box;
font-size: 1.2em;
color: blue;
border: 1px solid #ff9600;
border-radius: 8px;
}
input[type="text"] {
white-space: pre-wrap;
word-wrap: break-word;
}
.title{
color: blue;
text-align: center;
font-size: 1.5em;
font-weight: bold;
}
.btn{
boder: 1px solid grey;
border-radius: 8px;
padding: 10px;
margin-top: 10px;
background-color: #ff9600;
cursor: pointer;
}
@media (max-width: 800px) {
input[type="text"] {
font-size: 14px;
}
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function updateText() {
var amount = document.getElementById('amount').value;
if (amount) {
google.script.run.withSuccessHandler(function(text) {
document.getElementById('amountText').value = text;
}).convertNumberToText(amount);
} else {
document.getElementById('amountText').value = '';
}
}
function submitData() {
var amount = document.getElementById('amount').value;
if (amount) {
google.script.run.withSuccessHandler(function(text) {
alert('Dữ liệu được lưu thành công!');
}).saveData(amount);
} else {
alert('Vui lòng điền số tiền.');
}
}
</script>
</head>
<body>
<div class="title">CHUYỂN ĐỔI SỐ THÀNH CHỮ Ở TRƯỜNG INPUT</div>
<div class="input-container">
<label for="amount">Số tiền:</label>
<input type="number" id="amount" onchange="updateText()">
</div>
<div class="input-container">
<label for="amountText">Văn bản:</label>
<input type="text" id="amountText" readonly>
</div>
<button class="btn" onclick="submitData()">Gửi đi</button>
</body>
</html>