I'm using Google Sheets's Script Editor to operate between the values of a table/array. I've got a table that looks like this:
| UserA | UserB | UserC |
Movie1 | 2 | 8 | 6 |
Movie2 | 4 | 6 | 0 |
Movie3 | 5 | 2 | 3 |
Movie4 | 2 | 7 | 2 |
I need to sum every user's rating with the first user's rating, obtaining another array with the results of each sum.
In this case, the result should be:
| UserA+A | UserB+A | UserC+A |
Movie1 | 4 | 10 | 8 |
Movie2 | 8 | 10 | 4 |
Movie3 | 10 | 7 | 8 |
Movie4 | 4 | 9 | 4 |
I'm running through all columns and rows with two "for" loops, but I can't manage to store the results back to a bidimensional array. This is what I've got:
var userRatings = [];
var mainUserRatings = [];
var ultraArray = [];
for (i = 0; i < database.length; i++) { // Run through all movies (rows)
var mainUserRating = database[i][1]; // Ratings from UserA
mainUserRatings.push(mainUserRating);
for (n = 1; n < database[0].length; n++) { // Run through all users (columns)
var userRating = database[i][n]; // Ratings from User "n"
userRatings[i] = userRating;
}
ultraArray[i] = userRatings;
}
return ultraArray;
This function returns an array that has as many rows and columns as the first table (only they're switched) and only the data from the last column is repeated through all the rows. In short: it's a disaster. And I haven't even got to the point where I sum the data with the first column...
I know there's something wrong about the way I'm pushing the data into the new array, and probably also on the logic of the loops, but I fail to find the right combination. Any help would be much appreciated.