0

i wrote a code that should take a value from a cell and than convert it to the string and than into the array. its working fine. I can see the value of arr in Logs as an array with the input of the cell. For example, in the cell are " dog, cat " and tha arr value in Logs is [dog, cat].

But after i create this array, i would like to make a loop on it. And than i become in Logs, that arr is undefined. Can somebody help me please? im working on it for 2 days :(

Here is my code:

function animal (s,z){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getRange("SM");
var columnNumber = getColumnNumberOfSM(s);
var rowNumber = getRowNumberOfSM(z);
var colAction = columnNumber + 1;
var action = sheet.getRange(rowNumber, colAction, 1, 1).getValues();
var bar = action.toString();
var arr = [{}];
arr = bar.split(", ");
//return arr; // returns an array [dog, cat]
var foo = arr; // underfined
for (var i = 0; i <= foo.length; ++i) {
if (foo[i] == "dog") {
     Logger.log(upload());
  }
 }
} 

now i edit my code and its working fine,but only with "dog" but not with "cat"

function animal(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var action = sheet.getRange("C3").getValue();
var bar = action.toString();
var arr = bar.split(", ");
for (var i = 0; i <= arr.length; ++i) {
if (arr[i] == "dog") { // works
     Logger.log(upload());
  }
if (arr[i] == "cat") { // doesn't work
     Logger.log(upload());
  }
}

}

1
  • Just as with your previous question on this same topic, you are accessing an undefined index when you iterate your array foo: stackoverflow.com/questions/6571451/… Your question and title do not match - you are not returning anything. Please edit your question to clearly state your issue and your research into the issue Consider also logging the value of each array element as you iterate, i.e. Logger.log(arr[i]); if (arr[i] == ...) Commented Mar 27, 2018 at 13:59

1 Answer 1

1

You don't need to initiate arr as blank you can directly initialize it as a result to split method , also one point I didn't get is why do you need to assign arr to foo? You can directly iterate through arr.length and perform the required operation.

Here's my working snippet.

var parts = path.split(",");
  for (var i = 0; i < parts.length; i++) {
    if (parts[i] == 'dog'){ 
        Logger.log(parts[i]);
    }
  }
Sign up to request clarification or add additional context in comments.

2 Comments

thank you a lot for you quick answer.I edit my function as you said, but its working only with the first name in it ("dog"), but not with ("cat"). Any ideas?
Please provide excel sheet value sample to check further. Also this issue seems to be more specific with Javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.