Javascript区分大小写

时间:2010-12-06 23:47:41

标签: javascript google-apps-script google-sheets

如何使变量“Partnum”不区分大小写。当我运行脚本时,如果我正在搜索“abcd”,它将不会检测到“aBcd”。我不知道该怎么做。

 function doStuff() {
  var ss = SpreadsheetApp.getActiveSheet();

  var starting_row = 2; // starting row to scan for part# on column C

  // outer loop, loop over products sold
  for (var j=6;j<=16;j++) {
    var r = ss.getRange(j,2);
    // read inventory part number entered
    var partnum = r.getValue();
    if (!partnum) {
      continue; // attempt to skip over empty rows in the B6:B16 range
    }
    var partcount = parseInt(ss.getRange(j,1).getValue());
    if (isNaN(partcount) || partcount<=0) {
      // invalid quantity. skip.
      continue;
    }

//  Browser.msgBox("partnum = "+partnum);

    // get list of known part # from the spreadsheet
    var parts = ss.getRange(starting_row,3,9999,1).getValues();
    var found = false;
    for (var i=0,l=parts.length;i<l;++i) {
      if (parts[i]==partnum) {
        // we have found our part. grab inventory count cell.
        found = true;
        var count = ss.getRange(starting_row+i,1).getValue();
        if (count-partcount<0) {
          Browser.msgBox("Error: Inventory for part "+partnum+", is "+count);
        } else {
          // write back the count number for that part, decremented by 1.
          ss.getRange(starting_row+i,1).setValue(count-partcount);
//          Browser.msgBox("Inventory updated.");
        }
        break; // either way, we're done with that part.
      }
    }
    if (!found) {
      Browser.msgBox("Part# "+partnum+" not found.");

    }
  }

}

3 个答案:

答案 0 :(得分:5)

为了诚意,这就是答案。虽然@Freddie是第一个提到它的人。

// get list of known part # from the spreadsheet
var parts = ss.getRange(starting_row,3,9999,1).getValues();
var found = false;
for (var i=0,l=parts.length;i<l;++i) {
  if (parts[i]==partnum) {

变为:

// get list of known part # from the spreadsheet
var parts = ss.getRange(starting_row,3,9999,1).getValues();
var found = false;
for (var i=0,l=parts.length;i<l;++i) {
  if (parts[i].toLowerCase()==partnum.toLowerCase()) { // <-- NOTE CHANGE HERE

编辑对于非字符串对象

// get list of known part # from the spreadsheet
var parts = ss.getRange(starting_row,3,9999,1).getValues();
var found = false;
for (var i=0,l=parts.length;i<l;++i) {
  if ((parts[i]+"").toLowerCase()==(partnum+"").toLowerCase()) { // <-- NOTE CHANGE HERE

感谢Shadow Wizard

答案 1 :(得分:0)

也许你可以使用toUpperCase()?

答案 2 :(得分:0)

Shadow Wizard提出了解决我问题的答案

if((parts [i] +“”)。toLowerCase()==(partnum +“”)。toLowerCase()) -

由于