redirecting users according to their group or name in Google Sites - redirect

I am trying to make a site using Google Sites, and i need a way to know the user name or group and then redirect them to different pages.
Also if you have any Google script that can help, that will be great too, thanks.

This is a very simple answer but it can help.
We just need to get the user ID and compare it to the list of authorized users, if user is in the list then a link is created, if not, a label is created telling the user they need permission.
I don't need more than that for now, but in the future 2 more features can be cool to add:
1- using group names instead of an array with users to be more dynamic. (i don't know how yet)
2- auto redirect user instead of displaying a link. (i think that can be easy)
function doGet() {
var app = UiApp.createApplication();
var arr = new Array(4);
arr[0] = "user_001#company.com";
arr[1] = "user_002#company.com";
arr[2] = "user_003#company.com";
arr[3] = "user_004#company.com";
for (var i = 0; i < arr.length; i++) {
if (Session.getActiveUser().getUserLoginId() == arr[i]){
var label = app.createLabel('User: ' + Session.getActiveUser().getUserLoginId() + ', You have permission :)');//false
var link = app.createAnchor('Here is your link', 'http://www.umich.edu').setId("link").setVisible(true);//false
var flag = 1;
break;
//#####################
} else {
var label = app.createLabel('You need permission :(');//false
var flag = 0;
}
}
app.add(label);
if (flag == 1){app.add(link);}
return app;
}

Related

doGet failed on google addon script file

I am trying to trace email status with using inline image in an email.
For getting response i am using following code.
// handles the get request to the server
function doGet(e) {
Logger.log(e.parameter);
var method = e.parameter['method'];
switch (method) {
case 'track':
var email = e.parameter['email'];
updateEmailStatus(email);
default:
break;
}
}
function updateEmailStatus(emailToTrack) {
// get the active spreadsheet and data in it
var id = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheet = SpreadsheetApp.openById(id).getActiveSheet();
var data = sheet.getDataRange().getValues();
// get headers
var headers = data[0];
var emailOpened = headers.indexOf('status') + 1;
// declare the variable for the correct row number
var currentRow = 2;
// iterate through the data, starting at index 1
for (var i = 1; i < data.length; i++) {
var row = data[i];
var email = row[0];
if (emailToTrack === email) {
// update the value in sheet
sheet.getRange(currentRow, emailOpened).setValue('opened');
break;
}
currentRow++;
}
}
Here is the sheet
it works in a stand alone file but not working in a addon script project. Is there any way to trace out the send email using apps script ?
Any help on this issue will be highly appreciated. Thank you
I have solved the issue. I have change the code little bit and create a new draft as more than one inline image was added to the existing draft.
The change is here
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Email_Status');
After this few changes i deployed as new webapp and it started working. Thank you for contribution
Try this:
//var id = SpreadsheetApp.getActiveSpreadsheet().getId(); remove this
var sheet = SpreadsheetApp.openById(id);//change this by hardcoding the id

get value for specific question/item in a Google Form using Google App Script in an on submit event

I have figured out how to run a Google App Script project/function on a form submit using the information at https://developers.google.com/apps-script/guides/triggers/events#form-submit_4.
Once I have e I can call e.response to get a FormResponse object and then call getItemResponses() to get an array of all of the responses.
Without iterating through the array and checking each one, is there a way to find the ItemResponse for a specific question?
I see getResponseForItem(item) but it looks like I have to somehow create an Item first?
Can I some how use e.source to get the Form object and then find the Item by question, without iterating through all of them, so I could get the Item object I can use with getResponseForItem(item)?
This is the code I use to pull the current set of answers into a object, so the most current response for the question Your Name becomes form.yourName which I found to be the easiest way to find responses by question:
function objectifyForm() {
//Makes the form info into an object
var myform = FormApp.getActiveForm();
var formResponses = myform.getResponses()
var currentResponse = formResponses[formResponses.length-1];
var responseArray = currentResponse.getItemResponses()
var form = {};
form.user = currentResponse.getRespondentEmail(); //requires collect email addresses to be turned on or is undefined.
form.timestamp = currentResponse.getTimestamp();
form.formName = myform.getTitle();
for (var i = 0; i < responseArray.length; i++){
var response = responseArray[i].getResponse();
var item = responseArray[i].getItem().getTitle();
var item = camelize(item);
form[item] = response;
}
return form;
}
function camelize(str) {
str = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()#\+\?><\[\]\+]/g, '')
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
return index == 0 ? match.toLowerCase() : match.toUpperCase();
});
}
//Use with installable trigger
function onSubmittedForm() {
var form = objectifyForm();
Logger.log(form);
//Put Code here
}
A couple of important things.
If you change the question on the form, you will need to update your
code
Non required questions may or may not have answers, so check if answer exists before you use it
I only use installable triggers, so I know it works with those. Not sure about with simple triggers
You can see the form object by opening the logs, which is useful for finding the object names

Adding Facebook user's profile data using Parse?

I tried to follow the tutorial “Integrating Parse-Facebook in Unity” at: https://www.parse.com/tutorials/integrating-facebook-in-unity
Everything works fine but when I want to add user’s email, gender, name, birthday and location. , by using the following method :
private IEnumerator saveUserProfile(Dictionary<string, string> profile) {
var user = ParseUser.CurrentUser;
user["profile"] = profile;
user["email"] = profile["email"] ;
user["gender"] = profile["gender"] ;
user["name"] = profile["name"] ;
user["birthday"] = profile["birthday"] ;
user["location"] = profile["location"] ;
// Save if there have been any updates
if (user.IsKeyDirty("profile")) {
var saveTask = user.SaveAsync();
while (!saveTask.IsCompleted) yield return null;
UpdateProfile();
}
}
The only value added is “gender” and the other values were NOT created. Please see the below screenshot:
Also , when I presses on Facebook :” Profile No.” row in “authData” column in Parse database side , the Facebook website open with the following message :
Profile Unavailable
Sorry, this profile is not available at the moment. Please try again shortly
Please what I can do to add the above values and to get the correct profile no. for the users
Thank you
Waheed
try to check your dictionary what are the things being stored.
foreach( KeyValuePair<string, string> kvp in profile)
{
Debug.Log("Key = " + kvp.Key + " Value = " + kvp.Value);
}

Can the Insufficient permission message in Google sites be changed or customized?

I am using Google Sites, and if a user does not have the right to view a page they get a message that says "You need permission" or "Insufficient permission".
Can this message be edited or customized?
Or even can this unauthorized user be redirected to another page automatically?
Is there a script that might help?
I got this for the second part of the question but still couldn't find an answer for the first part even in Google documentation.
This is a very simple answer but it helped. We just need to get the user ID and compare it to the list of authorized users, if user is in the list then a link is created, if not, a label is created telling the user they need permission.
function doGet() {
var app = UiApp.createApplication();
var arr = new Array(4);
arr[0] = "user_001#company.com";
arr[1] = "user_002#company.com";
arr[2] = "user_003#company.com";
arr[3] = "user_004#company.com";
for (var i = 0; i < arr.length; i++) {
if (Session.getActiveUser().getUserLoginId() == arr[i]){
var label = app.createLabel('User: ' + Session.getActiveUser().getUserLoginId() + ', You have permission :)');//false
var link = app.createAnchor('Here is your link', 'http://www.umich.edu').setId("link").setVisible(true);//false
var flag = 1;
break;
//#####################
} else {
var label = app.createLabel('You need permission :(');//false
var flag = 0;
}
}
app.add(label);
if (flag == 1){app.add(link);}
return app;
}

Get Child name (relation) out of Contacts App

I'm extracting contacts with ContactsApp and can get email and namnes, but can not find out how to get "Child"-namne out of the contacts.
I tried with "CustomField". I can create a custom field and read it, but there is no "Child"-field.
How do I get "Child"-namne out of the contacts?
Test code for Google Apps script
function doGet(e) {
var GroupObject = ContactsApp.getContactGroup('TestGrupp');
var ContactsTestGrupp = GroupObject.getContacts();
var nrContacts = ContactsTestGrupp.length;
var objectContact;
var emailString;
var GivenNameString;
var FamilyNameString;
for(var i = 0; i < nrContacts; i++) {
objectContact = ContactsTestGrupp[i].getEmails();
emailString = objectContact[0].getAddress();
Logger.log(emailString);
GivenNameString = ContactsTestGrupp[i].getGivenName();
FamilyNameString = ContactsTestGrupp[i].getFamilyName();
Logger.log(GivenNameString + ' ' + FamilyNameString);
}
}
I don't think Google Contacts has a field for child name, at least not that I can find.