Home > Blockchain >  3d Array in Apps Script
3d Array in Apps Script

Time:08-11

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

CodePudding user response:

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?

  • Related