Tiếp tục seri Chia sẻ kiến thức về google sheet, apps script, webapp. Với project “Lấy dữ liệu từ sheet hiển thị dưới dạng biểu đồ trên html Web” sẽ giúp bạn kéo dữ liệu từ spreadsheet lên webapp, hiển thị dưới dạng biểu đồ (charts). Bạn rất dễ dàng trong việc cập nhật dữ liệu, cũng như việc bạn lựa chọn nhiều loại biểu đồ khác nhau từ thư viện của google chart. Để tìm hiểu thêm các loại biểu đồ được google hỗ trợ, bạn truy cập tại địa chỉ này.
Dưới đây là code apps script tham khảo và video bên kênh youtube hướng dẫn bạn thực hiện.
Một số project ứng dụng ngẫu nhiên:
- 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
- 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
Code trang : “Code.gs”
function doGet() {
let template = HtmlService.createTemplateFromFile('Index');
let html = template.evaluate().setTitle('Biểu đồ trên nền webapp');
html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
html.addMetaTag('viewport', 'width=device-width, initial-scale=1');
return html;
}
function getChartData() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
const dataRanges = [
{range: 'Data!A2:B12', key:'Range1'},
{range: 'Data!D2:G12', key:'Range2'},
{range: 'Data!I2:J12', key:'Range3'},
{range: 'Data!A16:B26', key:'Range4'},
{range: 'Data!D16:E26', key:'Range5'}
]
let data = [];
dataRanges.forEach(function(rangeObj){
let rangeData = sheet.getRange(rangeObj.range).getValues();
data.push({key:rangeObj.key, values:rangeData});
});
return data;
}
Code trang “Index.html”
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="https://www.gstatic.com/charts/loader.js"></script>
<style>
body{
min-height: 800px;
border: 4px solid black;
border-radius: 20px;
margin: 30px;
padding: 20px;
display: flex;
gap: 30px;
flex-wrap: wrap;
justify-content: center;
align-items: flex-start;
align-content: flex-start;
}
.title{
display: flex;
width:100%;
margin: 5px;
justify-content: center;
font-size: 40px;
font-weight: bold;
background: linear-gradient(to right, red, blue);
background-clip: text;
color: transparent;
}
.box{
flex-row: 1;
display: flex;
height: 400px;
width: 400px;
background-color: white;
padding: 5px;
border: 3px solid red;
border-radius: 10px;
box-shadow: 5px 5px 5px black,-5px -5px 5px white;
}
.box1{
display: flex;
height: 400px;
width: 600px;
background-color: white;
padding: 5px;
border: 3px solid red;
border-radius: 10px;
box-shadow: 5px 5px 5px black,-5px -5px 5px white;
}
</style>
<script>
// Load the Visualization API and the corechart package.
google.charts.load('current', {'packages':['corechart']});
google.charts.load('current', {'packages':['geochart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
google.script.run.withSuccessHandler(displayChart).getChartData();
}
function displayChart(data) {
var chart1Data = google.visualization.arrayToDataTable(getDataByKey(data,'Range1'));
var chart2Data = google.visualization.arrayToDataTable(getDataByKey(data,'Range2'));
var chart3Data = google.visualization.arrayToDataTable(getDataByKey(data,'Range3'));
var chart4Data = google.visualization.arrayToDataTable(getDataByKey(data,'Range4'));
var chart5Data = google.visualization.arrayToDataTable(getDataByKey(data,'Range5'));
// Set chart options
var chart1Options = {
title: 'Sales by Region',
colorAxis: {colors: ['#00853f', '#e31b23']},
};
var chart2Options = {
title: 'Hiệu suất chiến dịch tiếp thị',
series:{
0: {targetAxisIndex: 0}, // Impression on the left axis
1: {targetAxisIndex: 1},
2: {targetAxisIndex: 1}, // Clicks and Conversions on the right axis
},
vAxes: {
0: {title: 'Tỉ lệ hiển thị'},
1: {title: 'Số Click/Chuyển đổi'}
},
legend: {position: 'bottom'}
};
var chart3Options = {
title: 'Doanh số sản phẩm theo danh mục',
is3D: true,
legend: {position: 'bottom'}
};
var chart4Options = {
title: 'Kênh tạo khách hàng tiềm năng',
legend: {position: 'bottom'}
};
var chart5Options = {
title: 'Doanh số theo tháng',
legend: {position: 'bottom'}
};
// Instantiate and draw our charts, passing in some options.
var chart1 = new google.visualization.GeoChart(document.getElementById('chart1'));
var chart2 = new google.visualization.AreaChart(document.getElementById('chart2'));
var chart3 = new google.visualization.PieChart(document.getElementById('chart3'));
var chart4 = new google.visualization.ColumnChart(document.getElementById('chart4'));
var chart5 = new google.visualization.LineChart(document.getElementById('chart5'));
chart1.draw(chart1Data, chart1Options);
chart2.draw(chart2Data, chart2Options);
chart3.draw(chart3Data, chart3Options);
chart4.draw(chart4Data, chart4Options);
chart5.draw(chart5Data, chart5Options);
}
function getDataByKey(data, key) {
for (var i = 0; i < data.length; i++) {
if (data[i].key===key) {
return data[i].values;
}
}
return [];
}
</script>
</head>
<body>
<div class="title">BẢNG ĐIỀU KHIỂN</div>
<div class="box1" id="chart1"></div>
<div class="box1" id="chart2"></div>
<div class="box" id="chart3" ></div>
<div class="box" id="chart4" ></div>
<div class="box" id="chart5" ></div>
</body>
</html>