google spreedsheets & URI.js - javascript

I'm traying to use http://medialize.github.io/URI.js/ to get a url form a cell in my google spreedsheet without the query part.
original url http://test.com/file.php?this=1&that=2 --> from this url i like to get http://test.com/file.php
following this site http://googleappscripting.com/working-with-urls/ i added the example code in my spreedsheet and i can get to functions to work like:
function urlPath(url){
return URI(url).path()
}
to get the url as i need, i created another function following :the documents here http://medialize.github.io/URI.js/
eval(UrlFetchApp.fetch('https://rawgit.com/medialize/URI.js/gh-pages/src/URI.js').getContentText());
function url_whitout(url){
return URI(url).search("")
}
but i get an error : TypeError: undefined is not a valid argument for URI
Any ideas how i can get this to work?
thanks

This is not a URI.js issue, it is a Google Apps Script issue. You need to get the url into the function first.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var url = ss.getActiveSheet().getRange(1,1).getValue();
The getRange() function has several different versions so choose wisely Range Docs
You have to get the value into the function for it to read it.
As a side note I recommend using the function
Logger.log(url);
to clarify what you are doing after you run the function.

Related

What is getParameterByName in Firebase?

Firebase says that in the customize email action handler that they will implement getParameterByName. What does that exactly mean?
firebaser here
I'll assume you're referring to this page of the Firebase Authentication documentation, which contains the following code snippet:
// TODO: Implement getParameterByName()
// Get the action to complete.
var mode = getParameterByName('mode');
// Get the one-time code from the query parameter.
var actionCode = getParameterByName('oobCode'};
// (Optional) Get the API key from the query parameter.
var apiKey = getParameterByName('apiKey'};
Note that I only copied enough of the code to answer your question. Refer to the link for full code.
The custom email handler is an HTML page that is invoked by Firebase when there is an action that you may want to respond to. The Firebase back-end informs your page of the action and its data, by passing these as URL parameters when invoking your page.
So say you have your custom handler in a page called my_email_handler.html, it may invoked your page as: my_email_handler.html?mode=resetPassword&oobCode=123456&apiKey=AZdfshjsdfhj
The/your page then parses these URL parameters and takes the appropriate (custom) action.
The comment is a TODO for you as the application developer, you will need to implement a getParameterByName() method that retrieves the value from a URL parameter with the given name. If you do a search for getParameterByName you'll find quite some implementations of such a function.

You do not have permission to use copyTo

I'm trying to copy a range from one sheet to another (whilst preserving the formulas). I wrote a simple script using copyTo:
function copyRangeAcrossSheets(source_sheet,source_range,target_sheet,target_range) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var source_sheet = spreadsheet.getSheetByName(source_sheet);
var target_sheet = spreadsheet.getSheetByName(target_sheet);
var source_range = source_sheet.getRange(source_range);
var target_range = target_sheet.getRange(target_range);
source_range.copyTo(target_range);
}
Which I call as followed:
=copyRangeAcrossSheets("TEST_source","A1:A3","TEST_target","A1:A3")
And I'm getting the below error:
You do not have the permission to call copyTo
I did some digging around and found that functions have to use special triggers (installable) in order to modify another file. However here I'm modifying the same file.
Q1: Why is copyTo failing here?
Q2: How can I workaround the issue without having to define installable triggers? (I just want to copy ranges whilst preserving formulas)
Why is it failing?
You cannot modify other any documents that require authorization via a custom function. The reason for this is that your function is executed as an anonymous user, which cannot obtain the necessary authorization to edit other sheets or documents of yours.
Reference: https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services
Specific to you is this snippet:
Spreadsheet: Read only (can use most get*() methods, but not set*()).
Cannot open other spreadsheets (SpreadsheetApp.openById() or SpreadsheetApp.openByUrl()).
Also:
If your custom function throws the error message "You do not have permission to call X service.", the service requires user authorization and thus cannot be used in a custom function.
How can you work around this?
Write an Apps Script function that is executed via a trigger or manually, you can use onEdit or onChange triggers, or a time-based trigger. You can even manually run the function in the Apps Script IDE when you need to. This is the intended behavior of Apps Script.
Not sure about whether or not your data is persistent in your source spreadsheet, but you could always use the built-in IMPORTRANGE() function. Syntax is:
=IMPORTRANGE("SPREADSHEET_ID","SOURCE_SHEET!RANGE_START:RANGE_END")
Where SPREADSHEET_ID is the ID of the file you're working on.

Pass Variable From HREF to Google HTMLSERVICE App

I have built a rather useful tool for my work place that allows me to keep track of employee attendance. My latest addition is, when an infraction is flagged an email goes out with a link to that infractions ID. I want to be able to click that link, load the html page and pass the infraction ID to the page. I have been doing a lot of searching, but can't seem to find a clear answer, nor a case that closely resembles mine. The accepted answers I have found don't seem to apply to my situation.
Here is the example:
URL: https://script.google.com/a/macros/mycomp.com/s/blahblahmyID/dev
HREF: https://script.google.com/a/macros/mycomp.com/s/blahblahmyID/dev?cID=1457989239035-cc3050b8
I want to pass the 1457989239035-cc3050b8 value to my page. Once I get that on the page I can handle it from there, just can't find a way to pull it from a HREF link in an email. One of the drawbacks of not using a traditional website.
Here is my doGet() code to load the Index.html from the app:
function doGet(e) {
return HtmlService
.createTemplateFromFile('Index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
Not sure how useful that code is though as it is pretty standard. Am I pretty out of luck trying to do this?
UPDATE:
I realized that I haven't given enough information on how this system works. I am using HTML to serve information stored on spreadsheets (google sheets). The ID in question is a reference to an entry in the spreadsheet (column 1). It isn't a part of the app, I am just looking to pass the value of the row I need to get data from. If I have this value I can run a script to get all the other values from the spreadsheet.
The ID is an attribute of the e.parameter object, you should get it using something like id=e.parameter.id
You can pass it just as you show it in your question , right after the question mark in the url
HREF:https://script.google.com/a/macros/mycomp.com/s/blahblahmyID/exec?id=1457989239035-cc3050b8
The server code will get the value, store it somewhere and be ready to send it back when the client javaScript asks for it.
See shematic example below:
function doGet(e) {
var id = e.parameter.id
PropertiesService.getScriptProperties().setProperty('id',id);
return HtmlService
.createTemplateFromFile('Index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function returnID(){
return PropertiesService.getScriptProperties().getProperty('id');
}
// in your client javascript, add a call like this (outside of any function so it is executed on page load... or use onload="theFunctionName"...):
google.script.run.withSuccessHandler(getID).returnID();
function getID(id){
console.log(id);
}

How to pass object argument to PlusDomains.Circles.list in Apps Script

Recently was enabled Google + Domains API For Apps Script, I have explored some options and it seems is going to work, but in the specific case of PlusdDomains.Circles.list I don't know how to pass the second argument what is an object, I can not obtain several fields in the response, this is my code.
function getProfile() {
var userId = 'me';
var post = { maxResults: 2, fields:"title"};
var profile = PlusDomains.Circles.list(userId, post);
Logger.log('all: %s', JSON.stringify(profile));
}
this is the output,all: {"title":"Google+ List of Circles"}
if I try to get another field I don't know if this is correct, I put this:
var post = { maxResults: 2, fields:["title", "items"]};
but I get the same result:all: {"title":"Google+ List of Circles"}
If I try to get the result value for items, I get undefined. I don't how to pass the object correctly or if this is a bug in the Apps Script, somebody has idea??
I'm trying to get this working too. From the public google+ domain api docs it looks that the fields property expects a string with field names comma separated, i.e.
var post = { maxResults: 2, fields:"title,items"};
I don't seem to get the items populated (all the properties are undefined) in my google apps script. But when I use the API explorer "try it" console with OAuth2 enabled for scopes https://developers.google.com/+/domains/api/circles/list and https://developers.google.com/+/domains/api/circles/list I do see the items populated, so I'm thinking there may be an issue with my scripts authorization scopes or a bug in the google apps google+ domain service.

How to programmatically subscribe a user to a google calendar using javascript?

Hey i'm able to authenticate and authorize a user with the javascript API for google calendar. what i want to do next is subscribe that user to a public calendar. I was thinking i could just call the google.gdata.calendar.CalendarEntry constructor with the id of the calendar but that didn't work
var entry = google.gdata.calendar.CalendarEntry("idOfCalendar");
i also tried creating an instance of a entry id with google.gdata.atom.Id("idOfCalendar"); and adding that to the CalendarEntry constructor. Using the set methods didn't work either.
I use the InsertEntry method to add the entry but i get the following error
Error: Valid calendar id must be supplied for adding calendar to favorites list in allcalendars projection.
I can access the events of this calendar using google.gdata.calendar.CalendarEventQuery()
The google api for javascript doesn't give a lot of examples anyone know the answer to my problem or a good resource for working with the google calendar api? do you think i should be using php or jason instead?
** Edit
I found an example of what I want in the Java Api link so i tried
function addSubscriptionToCalendar() {
var feedUri = "http://www.google.com/calendar/feeds/default/allcalendars/full";
var calendarEntry = new google.gdata.calendar.CalendarEntry();
calendarEntry.setId("nhl_21_%54oronto+%4daple+%4ceafs#sports#group.v.calendar.google.com");
calendarService.insertEntry(feedUri, calendarEntry, function(){alert("calendar added")}, handleError);
}
but i got the same error
You should be using owncalendars feed to modify/add calendar entry for the authenticated user not allcalendars.
Sounds like your calendar id is invalid. The id from your second example appears to be URL encoded. Try using the decoded value:
nhl_21_Toronto Maple Leafs#sports#group.v.calendar.google.com
The gdata API for calendars has been switched off; the new way to do this is via: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert

Categories

Resources