Reputation: 1433
Using the function load_table()
below, I'm loading the content of a google doc spreadsheet into a hash variable tbl
. Using the helper function test_hash()
I can see that tbl
is visible within load_table()
, along with bt
, cod
and st
which also contain data from the spreadsheet (cod
is a constant). Everything is as expected.
When I try to return all these variables by putting them into another hash variable res
, I loose tbl
: for some reason tbl
is blank while the other variables are there as expected. Any idea why this is happening? Thanks.
function load_table( ss_data , colPos ) {
// create hash from ss_data
// ========================
var bt = [];
var st = [];
var tbl = [];
var cod = '';
for (var i = 1; i < ss_data.length; i++) {
cod = ss_data[i][0]
bt[ ss_data[i][1] ] = 1;
st[ ss_data[i][2] ] = 1;
tbl[ ss_data[i][1] + '~' + ss_data[i][2] ] = ss_data[i][colPos];
}
test_hash(tbl);
var btLvls = Object.keys(bt).sort();
var stLvls = Object.keys(st).sort();
var res = {};
res['cod'] = cod;
res['bt'] = btLvls;
res['st'] = stLvls;
res['tbl'] = tbl;
test_hash(tbl);
test_hash(res);
return res;
}
test_hash()
is a simple debug function:
function test_hash( h ) {
Logger.log( " --------- HASH START -------- " );
for ( var index in h ){
Logger.log( index + ':' + h[index] )
}
Logger.log( " --------- HASH END -------- " );
}
This is the corresponding doGet()
function:
function doGet(){
// counts by yearmo
var liveid = 'xxxxx'
var testid = 'yyyyy'
var ss = SpreadsheetApp.openById( testid );
var data = ss.getDataRange().getValues();
var rencat_nbtids = load_table( data , 3 );
var rencat_nstids = load_table( data , 4 );
var rencat_nsubs = load_table( data , 5 );
Logger.log( rencat_nbtids );
}
Upvotes: 0
Views: 2435
Reputation: 45750
As described in Objects are Maps, Arrays are Lists and The Associative Array, the problem is that tbl
is actually an associative array aka map (Object) but you've declared it as a list (Array).
This little change in line 8 lets tbl
survive the return
:
var tbl = {};
PS: Did you mean to start from 1 in line 11? for (var i = 1; ...
Arrays start at 0.
Upvotes: 1