我在 Google 表格上創建了??一個自定義功能,允許用戶查看他們花費了多少費用。
這是示例輸出:

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()方法,但我不確定是否應該包含這些以使用戶輸入不區分大小寫。
提前致謝。
uj5u.com熱心網友回復:
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)
}
uj5u.com熱心網友回復:
我假設您的意思是“使用戶輸入不區分大小寫”關于您的 if 陳述句,所以我會在那里控制 case if (row[6].toLowerCase()==brand.toLowerCase()){sum =row[1]}。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/446188.html
標籤:javascript html 谷歌应用脚本 谷歌表格
