Javascript case sensitive
Asked Answered
Y

4

5

How do I make the variable "Partnum" not be case sensitive. When I run the script, it wont detect "aBcd" if I'm searching for "abcd" . I'm not sure what to do.

 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.");

    }
  }

}
Yser answered 6/12, 2010 at 23:47 Comment(4)
Adam, How did you make that whole segment identified as code. I have to manually add 4 spaces if I want to. Is there a quicker way?Yser
Select your code and press the code button. Also, try if (parts[i].toLowerCase()==partnum.toLowerCase()) {Tomtit
Where do i insert that into this code?Yser
replace your old if (parts[i]==partnum) { with that, just above your "we have found our part" comment.Tomtit
T
6

For calrity's sake, here is the answer. Though @Freddie was the first to mention it.

// 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) {

becomes:

// 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

EDIT For non-string objects

// 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

thanks to Shadow Wizard

Tomtit answered 6/12, 2010 at 23:56 Comment(0)
I
1

Now you can also use the String method localeCompare:

if (partnum.localeCompare(parts[i], 'en', {sensitivity: 'base'}) === 0)
Immanuel answered 27/9 at 23:49 Comment(0)
C
0

maybe you can use toUpperCase() ?

Corticosteroid answered 6/12, 2010 at 23:51 Comment(1)
toLowerCase() makes more sense.Fixed
Y
0

Shadow Wizard came up with the answer that solved my problem

if ((parts[i] + "").toLowerCase() == (partnum + "").toLowerCase()) –

Thanks

Yser answered 7/12, 2010 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.