For a while now, I have been diving into the world of JavaScript and Apps Script. However, I've hit a roadblock that has kept me stuck for hours.
My setup involves a Google spreadsheet serving as my data source, containing a list of employees along with the cities they reside in. These employees access a web app through specific Google accounts. What I aim to achieve is an autocomplete feature that filters employees based on their respective city.
The data is structured in a 2-dimensional array - column 1 holds employee names, and column 2 stores their corresponding cities. After retrieving the user's email upon login, I successfully identify the city associated with them from the spreadsheet.
Next, my function scans through the list of employees, generating a single-dimensional array comprising only those individuals located in the specified city.
function getNames() {
var ss = SpreadsheetApp.openById(id);
var wsMail = ss.getSheetByName('MAILS');
var wsAteliers = ss.getSheetByName('ATELIERS')
var dataPerso = wsAteliers.getDataRange().getValues();
var data = wsMail.getDataRange().getValues();
var people = [];
var email = Session.getActiveUser().getEmail();
// TO FIND THE CITY, NO PROBLEMS
for (var i = 0; i < data.length; i++) {
if (data[i][0] == email) {
var atelier = wsMail.getRange('B' + (i + 1)).getValue();
}
}
//RETURNS ARRAY OF NAMES FROM CORRESPONDING CITY
for (var i = 0; i < dataPerso.length; i++) {
if (dataPerso[i][1] == atelier) {
people.push(dataPerso[i][0]);
}
}
return people;
}
The resulting 'people' array looks like this:
https://i.sstatic.net/ovm3R.png
However, when I attempt to convert this array into an object suitable for materialize autocomplete using the following snippet:
var options = {};
dataPerso.forEach(function(v){
options[v[0]] = null;
});
I end up with this unexpected outcome:
https://i.sstatic.net/GWLPd.png
The ideal result should resemble something like this:
{name1=null, name2=null etc...}
Feeling a bit lost here. Any guidance or pointers would be greatly appreciated.
Thank you,