0

I am looping through various cells and want to add their string content do an array, if the content is not already in the array. It works perfectly fine when I do it manually like so, trying to add 'eJobs' to the array (see below "var item = 'eJobs') which already containts 'eJobs':

var divisionarray = ['eJobs']  
  for (var i = 0; i < cells_users.length-1; ++i) {
    var row_users = cells_users[i];
    if (row_users[0] == user_ldap) {  
      
      var podarray = row_users[1].split(', ')
      for (j = 0; j < podarray.length; j++) { 
        for (var k = 0; k < cells_edit.length; ++k) {
          var row_edit = cells_edit[k]
          if (podarray[j] === row_edit[0]) {
            var item = 'eJobs' 
            if (!(divisionarray.indexOf(item) >= 0)) {
              divisionarray.push(item)
              }
          }
        }
      } 
      Logger.log(divisionarray)

As expected, the log file shows [17-10-08 19:11:04:111 BST] [eJobs], illustrating that the code works and 'eJobs' has not been added to the array as it is already in the array.

Now, when I change var item='eJobs' to values of a range

var item = sheet_pods_edit.getRange(startRow+k, startColumn+1).getValue();

the code does not work anylonger, as the log file shows:

[17-10-08 19:14:03:770 BST] [eJobs, eJobs, BestJobs, Vivre Deco, ...

Note I have a range of thousands of cells, so I get alot of duplicates added. What am I missing? Note the cells of the defined range are indeed just strings with a single word (e.g. 'eJobs').

1 Answer 1

1

The code is working and the log file is indicating what the problem is..

[eJobs, eJobs, BestJobs, Vivre Deco, 

In the second eJobs there is a white space before eJobs, so the first value and the second value don't match.

Without seeing your data and going by the 'just strings with a single word' I would say that using a .replace(" ", "") on the text string should work, this will find the first " " in the string and remove it. I.e. " eJobs" would become "eJobs".

2.

Is this line of code just for testing? You should never use a method like this in a script. It will be extremely inefficient

var item = sheet_pods_edit.getRange(startRow+k, startColumn+1).getValue();

Instead get the full range using .getValues()and iterate over it then.

3.

Is there a reason you are using === in if (podarray[j] === row_edit[0]) unless you need to check for type always use ==

Sign up to request clarification or add additional context in comments.

1 Comment

1. agree, but I need that space since I feed that string later into an angular autocomplete, which does not work without the spaces between the commas. It's too much code to paste here without explaining it in detail. 2. this line is not for testing, and I have no concerns with respect to efficieny. It runs in less than a second, good enough for my purposes. 3. typo, but doesn't really matter if you use == or === in apps script.

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.