Is there an “easy” way of converting a GAS string array (in .gs file) to a javascript array that exists in an HTML file? I’m trying to use jquery to do stuff, and it requires (from what I can tell) values to be in a javascript array. The first piece of code is the function that gets email addresses from contacts app and returns an array of strings. The rest are samples of HTML with a link break to separate their cajoled result (at least I’m pretty sure their cajoled result). Also, this is basically what I’m using the availableTags variable for you’ll see below: http://jqueryui.com/autocomplete/
Script File, returns a string array of email addresses
function getAllContacts(){
var contacts = ContactsApp.getContactsByGroup(ContactsApp.getContactGroup("ContactsAppTest"));
var email = new Array();
for(var i=0;i<contacts.length;i++){
if(contacts[i].getPrimaryEmail() != ""){
email.push(contacts[i].getPrimaryEmail());
}
}
return email;
}
Try 1, create an empty js array, and manually populate from getAllContacts function. This works, but thinking this is not very efficient.
var availableTags = [];
<?
var temp = getAllContacts();
for (var i=0; i<temp.length; i++) { ?>
availableTags.push(<?= temp[i] ?>);
<?} ?>
availableTags.push_m___?availableTags.push('email1@test.com'):availableTags.m___('push',['email1@test.com']);availableTags.push_m___?availableTags.push('email2@test2.com'):availableTags.m___('push',['email2@test2.com']);availableTags.push_m___?availableTags.push('email3@test3.com'):availableTags.m___('push',['email3@test3.com']);
Try 2, set availableTags = getAllContacts function
var availableTags = <?=getAllContacts()?>;
availableTags='email1@test.com,email2@test2.com,email3@test3.com'
Normal js array
var availableTags = ["email1@test.com","email2@test2.com","email3@test3.com"];
availableTags=['email1@test.com','email2@test2.com','email3@test3.com'];
Try something like this out:
That should initialize the array on the client side all-at-once. (I wouldn’t worry too much about efficiency anyway, unless you’re dealing with hundred or thousands of these contacts. Myself, I’d shoot for what I found most readable.)
UPDATE: as noted in comment, this won’t work, as the result appears to be enquoted.
Other efforts to use
new Function()oreval()on that string fail, likely due to the Caja sanitizer GAS uses on output. This should work, instead: