Context
In google app script for gsheets I am running this function that searches the names of items in a comma separated list within a single cell per row, against a list of item name and ids in 2 columns.
The function is called within another function that iterates through each row of data in the Data Table.
The goal is to replace the names with the ids in a comma separated list.
The Problem
The nested loop just doesn't run! When I set up training outputs to test where the code gets blocked it's at the inner for loop, it breaks at that line and doesn't execute at all. I cannot figure out why that would be as I can't see a problem with my syntax.
Any ideas where I could be going wrong please?
The data looks like this:
Item Directory Table:
Item ID | Item Name
--------------------
1234 | Item 1
46976 | Item 2
3526 | Item 3
32255 | Item 4
425 | Item 5
Data table:
Data Name | Items
--------------------
Row 1 | Item 1, Item 2, Item 5
Row 2 | Item 5, Item 2
Row 3 | Item 4
Row 4 |
Row 5 | Item 1
Desired output:
Row 1 = [1234, 46976, 425] // updated item 2
Row 2 = [425, 46976]
Row 3 = [32255]
Row 4 = []
Row 5 = [1234]
This is my function:
function findItems(x) {
var items = [];
var j = 0;
var i = 0;
var itemNames = Items.getRange(2 ,11).getValue().split(', ');
var itemIDs = Items.getRange(3 ,11).getValue().split(', ');
if (data.getRange(x, 6).getValue() != '') {
items = data.getRange(x, 6).getValue().split(', ');
for (i = 0; i < items.length; i++) {
output.getRange(2, 1).setValue(items[i]);
for (j = 0; j < itemNames.length; j++) {
if (items[i] === itemNames[j]) {
items[i] = itemIDs[j];
}
}
}
}
return items
}
