I'm trying to create a google app script attached to a spreadsheet that can set a google user's profile picture. According to the documentation, this should work:
var response = UrlFetchApp.fetch(url);
var photoBlob = response.getBlob();
var data = Utilities.base64EncodeWebSafe(photoBlob.getBytes());
AdminDirectory.Users.Photos.update({photoData: data}, this.email);
However this causes an exception:
GoogleJsonResponseException: API call to directory.users.photos.update failed with error: Invalid Input: photoData
The user running this script has permission to edit the profile picture
There must be a problem with your URL, the following works for me:
function myFunction() {
var url = "https://www.telegraph.co.uk/content/dam/Pets/spark/royal-canin/tabby-kitten-small.jpg"
var response = UrlFetchApp.fetch(url);
var photoBlob = response.getBlob();
var data = Utilities.base64EncodeWebSafe(photoBlob.getBytes());
var email = Session.getEffectiveUser().getEmail()
AdminDirectory.Users.Photos.update({photoData: data}, /*this.*/email);
}
Alternatively, you can also use an image from your Drive as following:
function myFunction() {
var url = "https://www.telegraph.co.uk/content/dam/Pets/spark/royal-canin/tabby-kitten-small.jpg"
var response = DriveApp.getFileById("1f8EFG6G5zNd6by_fNEecSh2D1My_p-_p")
var photoBlob = response.getBlob();
var data = Utilities.base64EncodeWebSafe(photoBlob.getBytes());
var email = Session.getEffectiveUser().getEmail()
AdminDirectory.Users.Photos.update({photoData: data}, /*this.*/email);
}
I'm using Parse.com give people an ability to share a URL from the app, to an individual "object" in Parse.com.
The below code works fine- EXCEPT for some reason the "LINK" (URL) is not coming through. All the other data comes through.
Is there a trick with Parse and sharing URL's?
My HTML is fine, I"ve pasted my javascript below.
var url = document.URL;
var objectId = url.substr(url.lastIndexOf('/') + 1);
var name;
var designer;
var itemDescription;
var price;
var link;
var image;
Parse.initialize("xxx", "xxx");
var garmentsAPI = Parse.Object.extend("garmentsAPI");
var query = new Parse.Query(garmentsAPI);
query.get(objectId, {
success: function(garments) {
console.log("success");
name = garments.get("name");
designer = garments.get("designer");
itemDescription = garments.get("itemDescription");
price = garments.get("price");
link = garments.get("link");
image = garments.get("smallImage1");
$("#designer").html(designer);
$("#name").html(name);
$("#itemDescription").html(itemDescription);
$("#price").html(price);
$("#image").attr("src", image.url());
$("#buyButton").attr('href', link);
console.log(image.url());
},
error: function(object, error) {
// The object was not retrieved successfully.
// error is a Parse.Error with an error code and message.
console.log("fail");
}
});
if your column name of file is
smallImage1 then
you can get url of file is as follows:
smallImage1._url
I am trying to extract part of the url and replace it with custom text using javascript.
For example, I want to fetch the current url such as:
mydomain.com/url_part_to_change/some-other-stuff
and then change that url to insert so that new new url is:
mydomain.com/new_url_part/some-other-stuff
Here is what I have:
function changeURL() {
var theURL = window.location.pathname;
theURL.replace("/url_part_to_change/", "/new_url_part/");
//Set URL
}
However, when I try to call the function changeURL(), it returns undefined instead of the new url.
For example if I do this:
alert(changeURL());
then what alerts is undefined
TL;DR
// update the pathname that will reload the page
window.location.pathname = myNewPathname;
Further Explanation:
Window.location ( image attached below ) provides you an object containing all the uri parts information. So, you can get this object via window.location and access the property pathname then do your stuffs. For example:
var locationObject = window.location;
var pathnameToChange = locationObject.pathname;
// do stuffs to "copy" of pathname, this will not reload the page
var myNewPathname = doSomethingMyPathname( pathnameToChange );
Additional Examples:
Alternatively, set new url using location.href. Check the MDN documentation for examples on location.assign(), location.replace(), location.reload() and notes on the different available functions
// ie.myNewUrl is something I created -> www.blah.com/updated/path
window.location.href = myNewUrl;
// or
window.location.assign(myNewUrl)
A window.location Object in Console
There are three references to further understand URI components
URI_scheme
Standards written by Tim Berners-Lee
MDN Location
Hope this helps.
This should work for you correctly:
function changeURL() {
// Get the url, just as you did
var theURL = window.location.pathname;
// Return the url
return theURL.replace("/url_part_to_change/", "/new_url_part/");
}
you are not returning any thing in function, Please make function like
function changeURL() {
var theURL = window.location.pathname;
return theURL.replace("/url_part_to_change/", "/new_url_part/");
//Set URL
}
As the others said, you don't return anything. What they are forgetting is that String.replace() just makes a copy of theURL and doesn't change theURL.
Try this:
function changeURL() {
var theURL = window.location.pathname;
theURL = theURL.replace("/url_part_to_change/", "new_url_part/");
//Set URL
return theURL;
}
alert(changeURL());
function changeURL() {
//set new path
window.location.pathname = "/new_url_part/";
//get new url
const newURL = window.location.href;
return newURL;
}
You forgot to return
function changeURL() {
var theURL = window.location.pathname;
var newURL = theURL.replace("/url_part_to_change/", "/new_url_part/");
//Set URL
return newURL;
}
alert(changeURL())//Now you won't see undefined.
This is quite an old post but just to add:
modifying window.location causes page navigations so if thats not desired create a new URL object and then you can modify the parts as needed.
in my case i needed to change the path to a value from a value in the querystring.
eg.
/*
* http://something.com/some/path?redirect=/some/other/path
* ->
* http://something.com/some/other/path
*/
let u = new URL(window.location.href)
u.pathname=u.searchParams.get("redirect")
u.searchParams.delete("redirect")
console.log(u.href)
I need to create a simple web site that gets the stock value based on the ticket.
Input: CSCO Output: 23.49. I'm very new to both webservices and javascript. The current YQL statement I am using is: select * from yahoo.finance.quotes where symbol="CSCO" which does not work.
function getprice()
{
var symbol = $('#stockquote').val();
var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22"+symbol+"%22)%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json";
$.getJSON(url, function (json)
{
var lastquote = json.query.results.quote.LastTradePriceOnly;
$('#stock').text(lastquote);
});
}
i have for example this URL
www.mypage/SessionPage.aspx?session=session%202#b
using this code i can track the complet URL
_gaq.push(['pageTrackerTime._trackEvent', 'category', 'action', document.location.href, roundleaveSiteEnd]);
BUt i would like only to take part of it like session%202
is there a way to do this in JavaScript
Use regular expression
function getSessionId() {
var re = /session=([^&=]+)/g;
var match = re.exec(document.location.href);
if (match) {
var sessionId = match[1];
return sessionId;
}
return null;
}