Reputation: 147
I have a script that does the following.
From innermost to outermost:
Iterates over each object property. (For in) !
for (var j in jsobj) {
var headerRow = Object.keys(jsobj).sort();
var row = headerRow.map(function(key){ return jsobj[key]});
var contents = [row];
var headers = [headerRow];
sheets[counter].getRange(1+i, 1, contents.length, headerRow.length ).setValues(contents);
}
Iterates over each column containing an object
for (var i=0; i < col-5; i++) {
var json = sheet.getRange(arr[m-1],i+6).getValue(); //First value should be row position from VLOOKUP.
var jsobj = JSON.parse(json);
Now from this point, I want to run another loop, var m is a changing value and thus should give different results.
for (var m=item; m > 0; m--) {
counter++
I have an enclosed loop that will generate an array of values for "m".
var arr = [];
for (var y = 0; y < folios.length; y++) {
if(folios[y] == name){
arr.push(y);
}
}
First two innermost loops work as intended however I can't seem to be able to get it working for the outer loop. I'll provide a copy of the spreadsheet and the full code below:
function createTable() {
var doc = SpreadsheetApp.openById("1Jv3E51r5zMuMLmn0qkCRjQBBRJHK_QTSdOeCSLRG7Do");
var sheet = doc.getSheetByName("json");
var sheet2 = doc.getSheetByName("tabla de frecuencias");
var sheet7 = doc.getSheetByName("tf2");
//Variable framework
var prelast = sheet.getRange("A1:A").getValues();
var last = prelast.filter(String).length;
var folio = sheet.getRange(last,3).getValue();
var folios = sheet.getRange("D1:D").getValues();
var range = sheet.getRange("C1:C");
var name = sheet.getRange(last,4).getValue();
var sheets = [sheet2,sheet7];
var counter = -1
var col = sheet.getLastColumn();
var arr = [];
for (var y = 0; y < folios.length; y++) { //This creates an array for later use.
if(folios[y] == name){
arr.push(y);
}
}
var item = arr.length;
for (var m=item; m > 0; m--) { //This is the loop that is not working.
counter++ //When the first iteration completes, this should increase for later use.
for (var i=0; i < col-5; i++) { //This and the below loop work as intended.
var json = sheet.getRange(arr[m-1],i+6).getValue(); //This is a critical point where M decreases its value all the way to 0 to fetch an entire row from my JSON sheet. It works in testing only for one row. I want it to go through each row in the array and write the values on different sheets, or even in the same sheet.
var jsobj = JSON.parse(json);
for (var j in jsobj) { //Works!
var headerRow = Object.keys(jsobj).sort();
var row = headerRow.map(function(key){ return jsobj[key]});
var contents = [row];
var headers = [headerRow];
sheets[counter].getRange(1+i, 1, contents.length, headerRow.length ).setValues(contents); //Let me explain: var sheets contain an array of two sheets, I want the script to write over each sheet based on the outermost loop.
}
}
}
Finally my spreadsheet.
https://docs.google.com/spreadsheets/d/1Jv3E51r5zMuMLmn0qkCRjQBBRJHK_QTSdOeCSLRG7Do/edit?usp=sharing
Thanks for stopping by.
Upvotes: 0
Views: 115
Reputation: 10796
Your issue at the that it fails within the first iteration of the loop.
col
is the last column index in the spreadsheet, in all rows in the json sheet this is empty and trying to parse the empty string as a JSON breaks the script.
After var json = sheet.getRange(last,i+6).getValue();
or your arr[m]
alternative you need to check if the string is empty and if so break or continue.
if (json === "") {break;}
After doing that it just fails because it then tries to write to the third sheet , because there are 3 instalaciones rows but only two sheets to write to.
Upvotes: 2