How To Create Chart From Spreadsheet Data Using Google Apps Script - by Dilip Kashyap - Medium
How To Create Chart From Spreadsheet Data Using Google Apps Script - by Dilip Kashyap - Medium
Open in app
Search Write
Last chance: Become a member and get 25% off the first year (Ends 1/31)
Hello Friends,
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 1/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
This article is going to be a step ahead in Google Apps Script. Now, we are
going to create chart from Google Spreadsheet data using Google Apps Script
and presenting it as HTML page to the browser with the help of Web API.
Using this method, you will be able to create charts dynamically from Google
Spreadsheets and present them on some platform without sharing your
Google Spreadsheet. When you update data in Google Spreadsheet, the chart
will update automatically as HTML webpage.
3. Write HTML, JQuery and JavaScript code for the chart presentation.
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 2/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
Step 2: Read data from Google Spreadsheet using Google Apps Script
Once data prepared, you need to open Google Script editor by navigating on
menu as Extension => Apps Script
Now, just write script code for these two functions in the script editor file,
which is code.gs file—
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 3/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
2. getSales() function: This function is to read your sales data from Google
Spreadsheet and pass it to the chart template.
doGet(): Function will read index file and present it to the browser as HTML
file.
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('index');
getSales(): To read the data from the Google Spreadsheet and passing it to
the getSale JavaScript function.
function getSales(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var salesSheet=ss.getSheetByName('Sheet1');
var getLastrow=salesSheet.getLastRow();
return salesSheet.getRange(2,1,getLastrow-1,2).getValues();
Step 3: Write HTML, CSS and JavaScript code for the chart presentation
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 4/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
We required CDN in your HTML file. Next open your HTML file i.e
index.html and we will include the cdn for jQuery and Chartjs which we will
be using to make our chart.
<script
src=”https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
”></script>
<script
src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.1/chart.js”
crossorigin=”anonymous” referrerpolicy=”no-referrer”>
HTML code
Create a canvas for the rendering charts. In the next step we complete our
html code by adding canvas for displaying our chart inside a div element in
the body tag as written below:
JavaScript/JQuery
Adding the jQuery Script Code. We initially write the standard jQuery
method which is called when the screen is fully loaded with HTML code and
we call getSales() method inside it which we will be defining in the next step.
$(document).ready(function(){
getSales();
});
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 5/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
getSales(): Function, which is creating data and label from the Google
Spreadsheet data for the chart.
function getSales(){
google.script.run.withSuccessHandler(function(ar){
var data=[];
var label=[];
ar.forEach(function(item,index){
data.push(item[1]);
label.push(item[0]);
});
});
}
in the above code we called the following syntax, which used to call the
JavaScript function in Google Apps Script.
google.script.run.withSuccessHandler(function(ar)){
}.getSales();
function getSales() {
google.script.run.withSuccessHandler(function (ar) {
var data = [];
var label = [];
ar.forEach(function (item, index) {
data.push(item[1]);
label.push(item[0]);
});
var barChartDiv =
document.getElementById("barchart").getContext("2d");
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 6/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
type: "bar",
title: {
display: true,
text: "Monthly Sales in Rs.",
},
data: {
labels: label,
datasets: [{
label: "Monthly Sales in Rs.",
data: data,
backgroundColor: [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)",
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)",
],
borderColor: [
"rgba(255,99,132,1)",
"rgba(255,99,132,1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
],
borderWidth: 1,
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 7/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
},
],
},
options: {
In the above code we are creating bar chart, you may choose anyone tpye of
chart as per your requirement.
After that, we have just get the data and label array and the called the canvas
element by using getElementById and then in the bar chart method we
passed all the required parameter such as —
data, label, background color, border color, title, type of chart, text etc…
Now, we will see the complete code for better understanding below.
Code.gs
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('index');
}
function getSales(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var salesSheet=ss.getSheetByName('Sheet1');
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 8/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
var getLastrow=salesSheet.getLastRow();
return salesSheet.getRange(2,1,getLastrow-1,2).getValues();
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.1/chart.js"
crossorigin=" anonymous"
referrerpolicy="no-referrer">
</script>
</head>
<body>
<div style='width:800px; height:400px;'>
<canvas id='barchart'
class='chartjs-render-monitor'>
</canvas>
</div>
<script>
$(document).ready(function () {
getSales();
});
function getSales() {
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 9/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
google.script.run.withSuccessHandler(function (ar) {
// console.log(ar);
var data = [];
data.push(item[1]);
label.push(item[0]);
});
display: true,
text: "Monthly Sales in Rs.",
},
data: {
labels: label,
datasets: [{
label: "Monthly Sales in Rs.",
data: data,
backgroundColor: [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
],
borderColor: [
"rgba(255,99,132,1)",
"rgba(255,99,132,1)",
"rgba(54, 162, 235, 1)",
},
],
},
options: {
legend: { display: false },
title: {
display: true,
text: "Monthly Sales",
},
},
});
})
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 11/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
.getSales();
}
</script>
</body>
</html>
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 12/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
Output
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 13/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
I hope you find this article helpful. For more such articles please upvote,
follow and share this with friends.
If you are interested to learn Google Apps Script and automate your Google
Workspace ? must try this e-Book on “Google Apps Script: A Beginners
Guide”
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 14/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
Automation
397 Followers
Dilip Kashyap
74 5.2K 62
Google’s Unexpected Layoff Warning Email The 10 Best VS Code Extensions in 2023
Sent to everyone
Dilip Kashyap
Alexander Nguyen in Level Up Coding
The 10 Best VS Code Extensions in
2023
Google’s Unexpected Layoff In the fast-evolving landscape of software
Warning Email Sent to everyone development, Visual Studio Code (VS Code)…
No one is safe.
1.5K 25 57
App Script For Google Sheets: The Ultimate Fetching data from Google Sheets using
Tutorial [Examples] Apps Script
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 16/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
App Script For Google Sheets: The Fetching data from Google Sheets
Ultimate Tutorial [Examples] using Apps Script
Learn Google Apps Script and unleash the One of the most potent capabilities of Google
true potential of Google Sheets. Sheets is its ability to interface with a myriad…
18
Lists
Henry Wu
9 min read · Dec 26, 2023 9 min read · Aug 19, 2023
1 5
Lalamove Scheduled Order with Gsheet and Previewing Links with Smart Chips: The
App Script Untold Potential of Apps Script
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 17/18
1/30/24, 11:16 AM How to create Chart from Spreadsheet data using Google Apps Script | by Dilip Kashyap | Medium
173 55
https://dilipkashyap15.medium.com/how-to-create-chart-from-spreadsheet-data-using-google-apps-script-89f2a654fc8d 18/18