Я сделал пользовательскую функцию в Google Sheets, которая позволяет пользователю видеть, сколько он потратил на расходы.
Боковая панель HTML:
Вот функция JavaScript:
function perCentBrand(brand){
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[6]==brand){sum+=row[1]}
})
var val = "You spent a total of " + sum + " on " + brand + " out of " + total + " ." + " Additionally, " + (sum/total)*100 + "%" + " of your income has been spent on " + brand;
var ui = SpreadsheetApp.getUi();
ui.alert(val)
}
А вот код HTML-формы:
<form onsubmit = "runFunc()">
<input class = "u-full-width " id = "brand" type = "text" placeholder = "Enter brand name">
<div class = "u-full-width" style = "display:flex; justify-content: center">
<button type = "submit" class = "button-primary">Submit</button>
</div>
</form>
Я читал о методах toLowerCase()
и toUpperCase()
, но я не уверен, следует ли их включать, чтобы сделать пользовательский ввод нечувствительным к регистру.
Заранее спасибо.
Я предполагаю, что вы имеете в виду «сделать пользовательский ввод нечувствительным к регистру» в отношении вашего оператора if, поэтому я бы контролировал регистр там if (row[6].toLowerCase()==brand.toLowerCase()){sum+=row[1]}
.
function perCentBrand(brand){
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[6].toLowerCase() == brand.toLowerCase()){sum+=row[1]}
})
var val = "You spent a total of " + sum + " on " + brand + " out of " + total + " ." + " Additionally, " + (sum/total)*100 + "%" + " of your income has been spent on " + brand;
var ui = SpreadsheetApp.getUi();
ui.alert(val)
}