Editing HTML5 LocalStorage through a function - json

I want to be able to add an "Ignore List" with the results being saved on the users browser.
The Ignored List is saved as a JSON array and looks like this:
[{"username":"test_user","date_added":"19/08/13","description":"Don't like this person."},{"username":"test_user_2","date_added":"19/08/13","description":"Don't like this person."}]
And the function required to add the users look like this:
function add_to_ignore_list()
{
var ignored_users = localStorage.getItem("ignore_list"); // returns ignore list
var username = return_current_username(); // returns test_user3
var date = return_current_date(); // returns 19/08/13
var description = prompt("Why do you want to ignore this user?"); // returns desc
add_to_list = {
"username" : username,
"date_added" : date,
"description" : description
};
ignored_users.push(add_to_list);
localStorage["ignore_list"] = JSON.stringify(ignored_users);
$(".user_wrapper").css("background-color","#B40404");
}
For some reason it isn't working and I can't see why Please help.

ignored_users is stored as a string.
When you retrieve it from localStorage, you need to parse it before you use it.
change:
var ignored_users = localStorage.getItem("ignore_list");
to (assumes it had previously been stored):
var ignored_users = JSON.parse(localStorage.getItem("ignore_list"));

Related

how to do to retrieve person.photos[0].default?

I tried to retrieve the variable Default from json of people api but it doesn't work with "default" . how to do it ? thank you for your answer
see my test script below, please (last line at bottom)
var person = People.People.get('people/' + accountId, {personFields: 'names,photos,phoneNumbers,addresses,birthdays,sipAddresses,organizations,genders'});
//** var val_displayName_P = person.names[0].displayName;
var val_photoUrl = person.photos[0].url.replace("=s100","=s128");
var primary_BLN = person.photos[0].metadata.primary;
var default_BLN = person.photos[0].default;
Seems like it is not returning default field in JSON response when a particular accountID contains user-provided photo(default : false), looks like a bug to me, not sure.
In case of User-provided photo, this is the log i am getting :-
And in case of default photo:-
Maybe that's why execution is breaking at last line.
If this is the case, for now you can try this :-
const photo= person.photos[0]
var default_BLN = photo.hasOwnProperty('default') ? photo.default : false;
Reference:
Photo
JSON is case sensitive, try person.photos[0].Default.

Google html service to sheets

I'm not a big fan of google forms so I made the form for my user input in the html service. I found a way to push the data out of the form and into google sheets using all of my variables in the html file like this:
<textarea type="text" name="Special Instructions" id="instructions"></textarea>
...
var instructions = document.getElementById("instructions").value;
...
google.script.run
.formsubmit (instructions,...)
google.script.host.close()}
in combination with the following in the code file:
function formsubmit(instructions,...)
var ss = SpreadsheetApp.getActive().getSheetByName("Sheet1");
ss.getRange(ss.getLastRow(),7,1,1).setValue(instructions);
...
The problem is, not only is the code very slow to output results, but if I have more than 37 or so variables defined, it glitches out and rather than closing the dialog box and recording the values in a spreadsheet, it opens a blank web page.
I know there has to be better (and more efficient) way, but I'm afraid I don't know it.
On the "client side", put all of your variables into a JSON object or an array, the stringify it, and send that string to the server.
var objectOfData;
variableOne = "one";
variable2 = "two";
objectOfData = {};
objectOfData['varOne'] = variableOne;//Create a new element in the object
objectOfData['var2'] = variable2;//key name is in the brackets
objectOfData = JSON.stringify(objectOfData);//Convert object to string
google.script.run
.formsubmit(objectOfData);
And then convert the object as a string back to a real object:
function formsubmit(o) {
var arrayOfValues,k,myData,outerArray;
myData = JSON.parse(o);//Convert string back to object
var ss = SpreadsheetApp.getActive().getSheetByName("Sheet1");
arrayOfValues = [];
for (k in myData) {//Loop through every property in the object
thisValue = myData[k];//
Logger.log('thisValue: ' + thisValue);//VIEW the LOGS to see print out
arrayOfValues.push(thisValue);
}
outerArray = [];
outerArray.push(arrayOfValues);
ss.getRange(ss.getLastRow() + 1,7,1,arrayOfValues.length).setValue(outerArray);
...
Note that the last parameter of getRange('start row', start column, number of rows, number of columns) uses the length of the inner array named arrayOfValues. This insures that the parameter value will always be correct regardless of how the array is constructed.

Create student roster from Classroom

I'm trying to pull the student information from a google classroom roster. Here is what I have so far:
function studentRoster() {
var optionalArgs = {
pageSize: 2
};
var getStudents = Classroom.Courses.Students.list("757828465",optionalArgs).students;
Logger.log(getStudents);
}
Sandy's answer below helped solve part of my problem and I get this as a log (names, id's, emails and such changed):
[16-01-05 17:44:04:734 PST] [{profile={photoUrl=https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg, emailAddress=jsdoe#fjuhsd.org, name={givenName=John, familyName=Doe, fullName=John Doe}, id=108117124004883828162}, courseId=757828465, userId=108117124004883828162}, {profile={photoUrl=https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg, emailAddress=jhdoe#fjuhsd.org, name={givenName=Jane, familyName=Doe, fullName=Jane Doe}, id=115613162385930536688}, courseId=757828465, userId=115613162385930536688}]
So my question now is: How do I extract only certain pieces of this information (like full name and email)?
The end result will be pushing it to a google sheet.
The Collection sections under the Classroom API Reference can help clarify some of the object chaining required to access the fields you want.
The following code will generate a list of student names, their email, and associated course ID.
function listStudents() {
var optionalArgs = {
pageSize: 0 // Max output
};
var response = Classroom.Courses.Students.list(xxxxxxxxx; // Put CourseID here
var students = response.students;
if(students && students.length > 0){
for( i = 0; i < students.length; i++){
var student = students[i];
// fullName is normally prefixed with s/ use of .substring removes first two characters
Logger.log('%s %s %s', student.profile.name.fullName.substring(2), student.profile.emailAddress, student.courseId );
}
} else {
Logger.log('No students in this course');
}
}
Collection courses.students
The Quickstart example is using the list method, and the list method takes optional query parameters. One of them being pageSize.
Query parameters for List
You are using the get method.
Documentation - Get method
The only options that the get method has is for the Path. And there is only one setting, the id.
So, that object literal named optionalArgs, you don't need for what you are doing. optionalArgs is an object because it has curly braces, and has elements that are "key/value" pairs. It's "literal", because it's typed into the code, as opposed to building the object with code.
If you did a search, (open the search dialog with Ctrl + F), and searched for optionalArgs, you'll see that it's not being used anywhere. So, the optionalArgs object with the pageSize property is not needed when using the get method.

Update Phone number on Google Apps user

Hi im using Advanced Services in Google Apps Script.
Im trying to add a number to the users profile.
var userValue = 'test#company.com';
var phoneValue = 017236233;
var users = AdminDirectory.Users.get(userValue);
for (var i = 0; i < users.length; i++) {
AdminDirectory.Users.update(users[i].phones[].primary, phoneValue);
}
The last part is the one i am not certain on. It fails with "Syntax error. (line 22, file "Code")"
Looking at the autocomplete on the update method you have to give it a User resource and a userKey (the user primary e-mail).
So this line of code should be:
AdminDirectory.Users.update(userResource, userPrimaryEmail);
Since you just want to add a phone your User resource can only contain this:
var userResource = {
phones:[{
value: phoneValue
}]
}
However be aware that this would update the whole list of phones and overwrite older values.
Also, note that the get method your are using doesn't return a list of User resources but a single User resource. You could use that same resource, update it and send it back.
So what you're looking would be:
var userPrimaryEmail = 'test#company.com';
var phoneValue = 017236233;
var user = AdminDirectory.Users.get(userPrimaryEmail);
// If user has no phones add a 'phones' empty list to the user resource
if (! user.phones){
user.phones = [];
}
user.phones.push(
{
value: phoneValue,
type: "mobile" // Could be 'home' or 'work' of whatever is allowed
}
)
AdminDirectory.Users.update(user, userPrimaryEmail);
A syntax error means that the code you wrote isn't valid. More information on syntax errors is available on the Apps Script troubleshooting guide.

AS3: Remove and add to variable after a character

I am pulling data from a php script that gives me the names of a person on facebook along with there ID in this format:
Person Name:123456789
I would like to know if there is a away to split after the ":" and add the number (ID) to on array and the person name to another array.
Thanks for any help
Eli
Something like this:
var personArray:Array = [];
var idArray:Array = [];
var stringToSplit:String = "Person Name:123456789";
var splitArray:Array = stringToSplit.split(":");
personArray.push(splitArray[0]);
idArray.push(splitArray[1]);
trace(personArray); // outputs "Person Name"
trace(idArray); // outputs "123456789"