简体   繁体   中英

google sheets string.indexOf('string') not returning correctly

I have been trying to write a script that will copy rows from one sheet to other sheets based on the responses to certain columns. However I am running into problems with checking if a string var contains another string var.

function checkInterest(cell)
{
// assumes source data in sheet named Needed
// target sheet of move to named Acquired
// test column with yes/no is col 4 or D
ss = SpreadsheetApp.getActive()
s = ss.getSheetByName('Form Responses 1');
r = s.getActiveRange();
i = s.getMaxRows();
v = lastValue('P'); 
addByInterest('Lighting','Lighting');
//addByInterest('Sound','Sound');
addHM();
}

function lastValue(column) {
var lastRow = SpreadsheetApp.getActiveSheet().getMaxRows();
var values = SpreadsheetApp.getActiveSheet().getRange(column + "1:" +  
column + lastRow).getValues();

for (; values[lastRow - 1] == "" && lastRow > 0; lastRow--) {}
return values[lastRow - 1];
}

function addByInterest(interest,sheetName)
{
Logger.log(v);
Logger.log('Lighting, Sound'.indexOf('Lighting'));
Logger.log(v.indexOf(interest.valueOf()));
if(s.getName() == 'Form Responses 1' && 
(v.indexOf(interest.valueOf()) >= 0)){
  var row = r.getRow();
  var numColumns = s.getLastColumn();
  var targetSheet = ss.getSheetByName(sheetName);
  var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
  s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
function addHM()
{
Logger.log('RAN');
if(s.getName() == 'Form Responses 1' && 
 (v.indexOf('Hair') >= 0 || v.indexOf('Makeup') >= 0)){  
  var row = r.getRow();
  var numColumns = s.getLastColumn();
  var targetSheet = ss.getSheetByName('Hair & Makeup');
  var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
  s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}

Logger prints

[15-10-01 12:14:27:346 EDT] [Lighting, Sound]
[15-10-01 12:14:27:346 EDT] Lighting
[15-10-01 12:14:27:347 EDT] 0.0
[15-10-01 12:14:27:348 EDT] -1.0 //Should return greater than or equal to 0
[15-10-01 12:14:27:349 EDT] RAN

I have tried with and without .valueOf() for the strings. Somewhat new to JS and completely new to Google Scripts so this may just be a simple fix but I'm not sure. Any help is greatly appreciated.

You shouldn't need to use valueOf() .

.getValues(); returns an array of arrays and you're targeting a specific child array with return values[lastRow - 1]; , which is then assigned to v - based on the code you've shared.

Perhaps there's leading or trailing whitespace in the array values. When called on an array indexOf() looks for an exact match (whitespace would mess that up) vs indexOf() on a string will return the start position of the string you're trying to match:

[" foo"].indexOf("foo"); // returns -1 due to the leading whitespace
" foo".indexOf("foo"); // returns 1

Perhaps v[0].indexOf() will fix the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM