1

I believe what I'm attempting to create is a 3d array in JS. The data I'm pulling is from a Google sheet, and I've created a dummy sheet with a sudo-console log showing what the ideal end result would be.

In short, I want a list of Client-Project pairings where the pair looks like this: [[Client], [Project(s)]].

However, I'm producing a list where each client is assigned to every project in list, not just the projects they share a row with (please reference the dummy sheet for context).

How should I restructure this code to get the result I'm looking for?

Thank you! All help is appreciated :D

const ss = SpreadsheetApp.openById("1BWutWGYPW9RjewRNdgLdHCn-Rytbyi63xbPF2Hd3tPg");
function getProjects(){
/** Getting List of Projects */
  const projectsSH = ss.getSheetByName("Projects");
  const projects = projectsSH.getRange(2,8,projectsSH.getLastRow()-1).getValues();
  const clients = projectsSH.getRange(2,2,projectsSH.getLastRow()-1).getValues();
  for (i = 0; i<clients.length; i++){
    for(j = 0; j<projects.length; j++){
      Logger.log([clients[i], projects[j]])
    }
  }
}

https://docs.google.com/spreadsheets/d/1CqZSMtSsxzYZwgq5XlwWxN_vSfpgP35rSV0dnCVLeVU/edit#gid=0

2
  • Not sure if I understand the task. To me it looks more like an object: {client1: [project1, project2], client2: [project3, project4,...], ...etc} Commented Aug 9, 2022 at 17:34
  • Maybe this would function better as an object, I have yet to work with objects in JS. One adjustment I'd make to your example is: {client1: [project1, project2], client2: [project1, project2,...], ...etc} Commented Aug 9, 2022 at 17:36

1 Answer 1

2

If I understand right and this is an object you can try this code:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getRange('A2:B29').getValues();
  var obj = {};
  
  for (let [client, project] of data) {
    if (client in obj) obj[client].push(project);
    else obj[client] = [project];
  }

  console.log(obj);
}

It gets me the object like this:

{
    'Advocacy Development Partners':         ['San Dimas Memory Care'],
    'ADC Real Estate / Amoroso Real Estate': ['Amoroso on La Cienega'],
    'AMA Project Management':                ['2847 Leeward'],
    'American Team Properties':              ['6845 N Figueroa', '4541 Santa Monica'],
    'Apogee Pro Services':                   ['Gerald Ford Dr & Cook St'],
    'ARD Group':                             ['Green Street Lofts'],
    'Araz Development':                      ['10192 Blix St Apartments'],
    'AUX Architecture':                      ['4920 Pico Blvd', '910 S Olive', '1036 Grand Ave'],
    'AHV Communities':                       ['Carmelia'],
    'Avenue Homes':                          ['Verdugo'],
    'Bastion Development':                   ['K Town West - 975 S Manhattan Pl'],
    'B&F LTD':                               ['Beverly and Fairfax'],
    'Boos Development':                      ['3201 Wilshire',
                                              '13760 Sherman Way',
                                              '11700 National Blvd',
                                              '16358 Ventura Blvd',
                                              '1130 N Sepulveda Blvd',
                                              'Ventura - Encino',
                                              'Wild Fork Foods Long Beach',
                                              'Wild Fork Foods Huntington Beach',
                                              'Wild Fork Foods Manhattan Beach',
                                              'Wild Fork Foods - 11700 National Blvd',
                                              'Wild Fork Foods - San Diego Pacific Beach',
                                              'Wild Fork Foods - Encinitas',
                                              '2570 Lincoln Blvd']
}

But it eludes me what do you want to do with the '3d array' or the object?

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

1 Comment

Wow that's awesome! I could use this for many things, but right now I'm looking to make labels and sub-labels in Gmail for my department; Parent Labels = Clients, sub-labels = Project(s)

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.