如何在 Apps 脚本的 HTML 表单中使用户输入不区分大小写?

How can I make user input case insensitive in an HTML Form in Apps Script?

我在 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() 方法,但我不确定是否应包含这些方法以使用户输入不区分大小写。

提前致谢。

我假设你的意思是关于你的 if-statement“让用户输入不区分大小写”,所以我会控制那里的大小写 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)
}