Home > Enterprise >  How can I create a 2d array of a list of company teams and names so I can loop through it?
How can I create a 2d array of a list of company teams and names so I can loop through it?

Time:10-03

I have this spreadsheet filled with some information about each employee that I have to migrate to a different spreadsheets. Each spreadsheet will include a different mixture of employee names and info depending on which team I'm working on.

The team info goes like this:

Team 1 - 5 names

Team 2 - 3 names

Team 3 - 7 names

.

.

.

I have about 20 teams I need to work on.

I think I've figured out how to use for and if loops to get the info I need and to copy those rows to a new spreadsheet. I'm doing this by finding the cell that matches the "employee name" in my array and then I copy the info in that row.

The problem I'm having is how to create a 2d array that stores all the names so I can loop through them (especially since the rows are not the same length).

Does anyone know how to do this?? Thank you!

CodePudding user response:

Easy

var teamOne = ["names","names","names","names"];
var teamTwo = ["names2","names2","names2","names2"];
var teamThree = ["names3","names3","names3","names3"];

var teams = [teamOne ,teamTwo ,teamTheree]; 

and then loop teams

CodePudding user response:

You can achieve this by creating an object where team names are keys and team members are values.

const company = {
  team1: ["name", "name2", "name3"],
  team2: ["name4", "name7"],
  team1: ["name5", "name8", "name6"]
}

Then you can use combinations of Object.prototype.keys and/or Object.prototype.values as needed

CodePudding user response:

Loading Different Destination Sheets from a Single Source Sheet

Using different data sets for each destination sheet.

function differentliststodifferentsheets() {
  const ss = SpreadsheetApp.getActive();
  const ssh = ss.getSheetByName('Sheet1');
  const svs = ssh.getRange(1, 1, ssh.getLastRow(), ssh.getLastColumn()).getValues();
  const sh123 = ss.getSheetByName('Sheet2');
  sh123.clear();
  const sh145 = ss.getSheetByName('Sheet3');
  sh145.clear();
  let vs123 = [];
  let vs145 = [];
  svs.forEach((r, i) => {
    vs123.push([r[0],r[1],r[2]]);
    vs145.push([r[0],r[3],r[4]]);
  });
  sh123.getRange(1,1,vs123.length,vs123[0].length).setValues(vs123);
  sh145.getRange(1,1,vs145.length,vs145[0].length).setValues(vs145);
  Logger.log(JSON.stringify(vs123));
  Logger.log(JSON.stringify(vs145));
}

Sheet1:

COL1 COL2 COL3 COL4 COL5
1,1 1,2 1,3 1,4 1,5
2,1 2,2 2,3 2,4 2,5
3,1 3,2 3,3 3,4 3,5
4,1 4,2 4,3 4,4 4,5
5,1 5,2 5,3 5,4 5,5
6,1 6,2 6,3 6,4 6,5
7,1 7,2 7,3 7,4 7,5
8,1 8,2 8,3 8,4 8,5
9,1 9,2 9,3 9,4 9,5
10,1 10,2 10,3 10,4 10,5

Sheet2:

COL1 COL2 COL3
1,1 1,2 1,3
2,1 2,2 2,3
3,1 3,2 3,3
4,1 4,2 4,3
5,1 5,2 5,3
6,1 6,2 6,3
7,1 7,2 7,3
8,1 8,2 8,3
9,1 9,2 9,3
10,1 10,2 10,3

Sheet3:

COL1 COL4 COL5
1,1 1,4 1,5
2,1 2,4 2,5
3,1 3,4 3,5
4,1 4,4 4,5
5,1 5,4 5,5
6,1 6,4 6,5
7,1 7,4 7,5
8,1 8,4 8,5
9,1 9,4 9,5
10,1 10,4 10,5
  • Related