I have a table in a spreadsheet which I want to unpivot using google apps script: each one of the month rows of the original table have to become multiple rows in the new table. The problem is the code doesn't produce the expected result.
Insted of creating arrays that ends like this (each line of the table ends with one different month):
[[...,'April'],[...,'September'],[...,'December']]
It's producing this (each line ends with the last month value of that line in the original table):
[[...,'December'],[...,'December'],[...,'December']]
Can someone see the mistake?
function myFunction() {
var ay_datos = [
['State', 'Month1', 'Month2', 'Month3', 'Number of months', 'Month'],
['California', 'April', 'September', 'December', 3, ''],
['Texas', 'January', 'March', '', 2, ''],
];
var ay_new = [
['State', 'Month1', 'Month2', 'Month3', 'Number of months', 'Month'],
];
for (i = 1; i < ay_datos.length; i++) {
var num_months = ay_datos[i][4];
var ay_linea = ay_datos[i];
for (j = 0; j < num_months; j++) {
ay_linea[5] = ay_linea[1 + j];
ay_new.push(ay_linea);
}
}
}