Filling an array but get value 'null' in Google Apps Script - arrays

am confused. I have an array of 'keys' where each key has a number. I have an array of my current row of data.
Now I need to fill a new array, where we match the key and value like this:
var template = HtmlService.createTemplateFromFile('box'); //MAAK FORMULIER KLAAR, HIER ALLE VARIABELEN DIE UIT DE RECORD KOMEN
template.action = ScriptApp.getService().getUrl();
template.ID=data[keys.key_ID];
template.AccountManager=data[keys.key_AccountManager];
template.Status=data[keys.key_Status];
template.Terugbeldatum=data[keys.key_Terugbeldatum];
template.Schoolnaam=data[keys.key_Schoolnaam];
template.Bezoekadres=data[keys.key_Bezoekadres];
template.Huisnr=data[keys.key_Huisnr];
template.Postcode=data[keys.key_Postcode];
template.Plaats=data[keys.key_Plaats];
template.Telefoon=data[keys.key_Telefoon];
template.Website=data[keys.key_Website];
template.Voorletters=data[keys.key_Voorletters];
template.Voornaam=data[keys.key_Voornaam];
template.VVAS=data[keys.key_VVAS];
template.Achternaam=data[keys.key_Achternaam];
template.Functie=data[keys.key_Functie];
template.Vakantieregio=data[keys.key_Vakantieregio];
template.LAS=data[keys.key_LAS];
template.LASDatum=data[keys.key_LASDatum];
template.ZelfstandigeInkoop=data[keys.key_ZelfstandigeInkoop];
template.Gespreksverslag=data[keys.key_Gespreksverslag];
template.SeminarDatum=data[keys.key_SeminarDatum];
template.AfspraakDatum=data[keys.key_AfspraakDatum];
template.Statushistorie=data[keys.key_Statushistorie];
template.Gesprokenmet=data[keys.key_Gesprokenmet];
template.Verantwoordelijke=data[keys.key_Verantwoordelijke];
template.TelefoonnummerV=data[keys.key_TelefoonnummerV];
template.row=row;
template.Emailadres=data[keys.key_Emailadres];
template.EmailadresV=data[keys.key_EmailadresV];
template.welkom = false;
Result:
http://imm.io/UP5b
Just a few variables get it into the template array, the rest is 'null' ???
When I do a
Logger.log(data[keys.key_Exxx]);
the value is there just fine.
Cannot I assign so many variables into Template this way?

Looks like there is something wrong using Copy-Paste, template.TelefoonnummerV=data[keys.key_TelefoonnummerV]; It seems like there is an extra character after the _ ??? I have been using a regex-tool to replace some text. Strange. – Riel yesterday

Related

Rails Ransack, initialize from front end as an array

In the React front end, the ransack parameter is initialized like so:
const statusFilter = 'ransack[status_in]=incomplete';
In the backend, I've got
#params["ransack"]&.each do |filter, value|
so that the first line produces
filter = "status_in"
value = "incomplete"
Everywhere else in the app after initializing, the status_in parameter is used as an array, so that multiple statuses can be selected. So I would like to always treat this parameter as an array, not a string, if possible.
My desired output:
value = ["incomplete"]
I've tried
const statusFilter = "ransack[status_in]=['incomplete']";
But that results in
value = "['incomplete']"
I could probably just check whether the type is a string on the first usage, and then convert it to an array. Something like:
value = [value] if value.class == String
But it seems like there should be a way to accomplish this by slightly changing the front so that I don't need to add another line in the back.

How to use Array in JEXL?

Using JEXL, I am trying to initialize array and than adding elements into it, however below code gives me 'unsolvable property '0' error.
var abc=[];
abc[0]=5;
1) How can I initialize empty array and keep adding values in it?
2) Can I use it like List, where I do not need to specify size at the time of initialization ?
in JEXL syntax you can initialize objects with new function.
Other option is to add to context arraylist:
This is a working example with jexl2:
JexlEngine jexl = new JexlEngine();
String jexlExp = "var abc=new(\"java.util.ArrayList\", 1);abc[0]=5";
Expression e = jexl.createExpression( jexlExp );
List<Integer> abc = new ArrayList<>(1);
JexlContext jc = new MapContext();
//jc.set("abc", abc ); second option to add arraylist to context
Object o = e.evaluate(jc);
In JEXL, the syntax [] creates a Java array, not a List. As an array, it has a fixed size, so you cannot add values to it. However, JEXL 3.2 has a new syntax for creating an ArrayList literal. Basically, you add ... as the final element.
So in JEXL 3.2, your example could be written as:
var abc=[...];
abc.add(5);
See the JEXL literal syntax reference for more information.

appendRow from an array

If I have an array, [Joe, John, Adam, Sam, Bill, Bob] and I want to try to add this to a new row by doing SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow([array]); , what happens is that the entire list of names goes into 1 cell. Is there a way to break this up so they file away into the same row, but different columns? I need to continue using appendRow however.
I get this:
But I really want to have it look like this:
var my2DArrayFromRng = datasheet.getRange("A:A").getValues();
var a = my2DArrayFromRng.join().split(',').filter(Boolean);
var array = [];
for (d in a) {
array.push(a[d]);
}
SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow([array.toString()]);
You are converting your array to a string before you post it which is causing your issue.
Do not use the array.toString() method inside append row. Instead just append the array as it is.
SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow(array);

google visualization data.addRow - get data out of html data attribute

I have data inside a data attribute, like so:
<div class="dashboard-module" data-rows="new Date(2013,10,04),12,"OR"##new Date(2013,10,17),2,"OR"##new Date(2013,10,09),2,"CA""></div>
Im trying to split this string up and use it in the data.addRow function:
rows = el.data('rows');
rowsarray = rows.split('##');
// Error: Row given with size different than 3 (the number of columns in the table).
$.each(rowsarray, function(index, value) {
data.addRow( [value] );
});
// the following works
data.addRow([new Date(2013,10,04),12,"OR"]);
data.addRow([new Date(2013,10,09),2,"CA"]);
data.addRow([new Date(2013,12,12),14,"AL"]);
I guess the commas inside the new date are being counted as different parts of the array?
I'm assuming that the double-quotes inside your data-rows attribute are escaped (otherwise the HTML is malforned).
When you call rowsarray = rows.split('##');, you are getting an array of strings, like this:
[
'new Date(2013,10,04),12,"OR"',
'new Date(2013,10,17),2,"OR"',
'new Date(2013,10,09),2,"CA"'
]
not an array of arrays. If you want to store your data in an HTML attribute, your best bet is to use a JSON-compatible format. The problem then becomes storing dates, since Date objects are not JSON-compatible, but that is easy to work around. Store your data like this instead:
[["Date(2013,10,04)",12,"OR"],["Date(2013,10,17)",2,"OR"],["Date(2013,10,09)",2,"CA"]]
I did two things with the data-rows attribute: first, I changed the dates from a format like new Date(2013,10,17) to a string like "Date(2013,10,17)". Second, I converted the string to a JSON string representation of an array of arrays (which uses the standard javascript array brackets [ and ]). Note that JSON requires the use of double-quotes for all internal strings, so you must either escape all internal strings to use with the data-rows attribute, or use single-quotes around the data-rows attribute string (eg: data-rows='<string>').
You can then parse that string for entry into your DataTable:
rows = JSON.parse(el.data('rows'));
// convert date strings to Date objects
for (var i = 0; i < rows.length; i++) {
var dateStr = rows[i][0];
var dateArr = dateStr.substring(5, dateStr.length - 1).split(',');
rows[i][0] = new Date(dateArr[0], dateArr[1], dateArr[2]);
}
data.addRows(rows);

How can I add strings to array in javascript

I wanna create an array in javascript which looks like this:
[0,0,0,0,0,1,1,0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1,0,1,0,1,1,1,0],[0,0,0,0,1,1,1,1,0,1,0,0,1,1,0],[0,0,0,0,0,1,1,0,1,0,1,0,1,0,0]
My problem is that I don't know how to add the opening and closing square brackets to the start and the end of the output string.
here's my code:
game = new Array();
for(row=0;row<matrix.length;++row){
game[row]=matrix[row].join(',');
}
document.getElementById('jsvalue').value=game.join('],[');
document.getElementById('name2').value = name;
I tried a few things, but they didn't seem to work and all I got were errors or this output:
0,0,0,0,0,1,1,0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1,0,1,0,1,1,1,0],[0,0,0,0,1,1,1,1,0,1,0,0,1,1,0],[0,0,0,0,0,1,1,0,1,0,1,0,1,0,0
How could I add them? Is there a simple array method that I missed and would solve my problem?
Thanks in advance!
It looks like you are trying to set the value of an HTML element to the format you described in your question. However, you are not setting the value of that HTML element to an Array - you are setting it to a string. the .join function outputs a string. If indeed you want the value to be set to a string formatted in the way you described, then you could take advantage of .join, but have to do a little bit in addition to what you are doing:
game = new Array();
for(row=0;row<matrix.length;++row){
game[row]= "[" + matrix[row].join(',') + "]";
}
document.getElementById('jsvalue').value=game.join(',');
document.getElementById('name2').value = name;
If you are using join to create the string, then why not just manually add the brackets?
For example:
document.getElementById('jsvalue').value= '[' + game.join('],[') + ']';

Resources