Currently, I am making an expense tracker on Google Sheets. Here is a preview:
I currently have a function that checks how much money (in percentage) is spent on a particular brand.
Like this:
Here is the code block for it:
function perCent(categ){
var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var values = sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).getValues();
var total = 0;
var sum = 0;
values.forEach(function(row){
total+=row[1];
if (row[7]==categ){sum+=row[1]}
})
return (sum/total)*100 + "%" + " of your income has been spent on " + categ;
}
However, for this function to work, the user must manually type in a cell =perCent(categ), which is not user friendly.
For example:
To increase user friendliness, I introduced an HTML sidebar, as such:
Here is the code block for the sidebar:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Main Menu')
.addItem('My sidebar 1', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Clothing Main Menu');
SpreadsheetApp.getUi()
.showSidebar(html);
}
Here is the HTML code block:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1 class = "align ">Welcome to the clothing expense custom menu bar!</h1>
<h2 class = "align">Here are the custom functions you can use:</h2>
<p class = "align"> Per Cent Function</p>
<form>
<input class = "margin" type = "text" placeholder="Enter brand name">
<div>
<button type = "submit" class="align">Submit
</button>
</div>
</form>
</body>
<style>
body{
background-color: black;
color: red;
}
.align{
text-align: center;
}
.margin{
margin-bottom: 10px;
}
</html>
Is there any way I can make the perCent function run by clicking on the Submit button?
Thanks!




