ReferenceError using Session.getActiveUser() in Google Apps Script - google-apps-script

I have a Google Apps Script and am trying to use Session.getActiveUser() but get an "Uncaught ReferenceError: Session is not defined" error in the Developer Tools view. This is a Published Web App running as the User. It is happening when testing with the latest code. It is in a Google Apps for Education domain.
This may or may not be related: I had also tried to use Utilities.sleep() which gave me the same error for Utilities. I worked around this issue.
Is there an Advanced Google Service I need to turn on or some other setting I need to make? My ultimate goal is to be able to add the name of the person running the script to a cell in the spreadsheet. Once I can retrieve their name I have the rest.
Regards,
Karl

you are calling the appscript api from the HtmlService clientside javascript. no apps script apis can get called client-side, must be in your .gs or in templated html.
To call a server function from the client side, see https://developers.google.com/apps-script/guides/html/reference/run

Related

Can I deploy google app script project locally or from another platform?

I am creating a web application using a google app script. In this application I integrated google sign in as a feature. When I deploy the app through the google app script IDE, everything is fine until I press the button "sign in with google" and it gives me an error which is saying mismatch with the JavaScript origin. When I try to add the JavaScript origin it will not accept it and says either it is invalid or it is forbidden. When I try to do that locally it works perfectly (I added the JavaScript origin as my localhost) but the rest of the functionalities like adding data to a spreadsheet does not work. I know that there is no solution for the first bit (to use the sign in button when I deploy the app through Google App Script IDE) because I have done exhaustive research about it. So, I was wondering if there is another way. like another platform that can support the deployment of my project, meaning there is no problem with the google sign in and the interaction with the google spreadsheet.
For security reasons, Google Apps Script sandboxes your HTML Service (see restrictions documentation). In the case of Content Services, it redirects to a different domain every time it’s executed (see documentation). Because of this, you have two options: use Google Apps Script to execute as the visiting user and to get a static site hosting.
1. Using Google Apps Script
Instead of running the calls to the APIs in the front end, you can make functions in Google Apps Script and then call them using google.script.run in the frontend. It will also simplify your code as all the OAuth is handled by Google Apps Script.
// Google Apps Script side
function doSomething() {
const ss = SpreadsheetApp.openByUrl('…')
// […]
return result
}
And to call it I’d use something like:
// Client side
function doWork() {
google.script.run
.withSuccessHandler(successCallback)
.doSomething() // Function name in the backend
}
Make sure to deploy the application with the option Execute as: with the value User accessing the web app so it’s not executed as you.
With this setup the user will be asked to give the required permissions before opening the page.
2. Use a web hosting
The second solution is to find a web hosting provider. Because you don’t seem to have a backend it can be for static pages (some are free). This will give you a stable URL and should work exactly the same as in your localhost.
References
HTML Service: Restrictions (Google Developers)
Content Service Redirects (Google Developers)
google.script.run (Google Developers)

How can I use Advanced Google Services within a Custom Google Sheet Function?

I am trying to create a custom Google Sheets function that pulls data from the Analytics Reporting V4 API. When I run this function from the App Scripts console, it successfully returns data.
When I run the function from within my spreadsheet, I get the following error:
API call to analyticsreporting.reports.batchGet failed with error: Request is missing required
authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication
credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (line 59).
Here is my function defined in the Apps Script Editor (again, this works fine when I press the run button from here):
And here is my error-producing spreadsheet implementation:
Is there a way that I can make this work without having to use OAuth credentials? From my understanding, the benefit of using Advanced Google Services is the ability to avoid this authentication flow, and I would like to take advantage of it.
In the documentation:
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.
To use a service other than those listed above, create a custom menu that runs an Apps Script function instead of writing a custom function. A function that is triggered from a menu will ask the user for authorization if necessary and can consequently use all Apps Script services.
Using Apps Script Services

Running google app script from another app script

Is there a way to run a google app script from a different app script?
im just getting into google app scripts and i couldnt find anything on the internet
It seems like you are looking for libraries functionality in Google App Script. Documentation
You can save a version of the script that you have the functions in it, and import the functions on to a second script file through menu>resources>libraries. You need to use script id for importing libraries, which you can find from menu>File>Project properties>Script ID
You can then execute the function using the script file name as the reference.
You can use the Apps Script API, you may find this helpful Executing Functions using the Apps Script API
"The Apps Script API provides a scripts.run method that remotely executes a specified Apps Script function"
You can call google app script form javascript or web page.
You have to implement doGet(or post) and publish your google app script.
I found good example.
Cheers.

Call Google Spreadsheet script function from external app

I have Google spreadsheet with script attached to it (with the Tools->Script Editor tool).
I want to call one of script function from external Android App. Is it possible? Should I use Spreadsheet API?
Unfortunately, you cannot directly call your script from any external application.
However, you can change it to be a web application, then invoke it via an HTTP GET or POST from almost anywhere, including an Android app.
Insert new rows into Google Spreadsheet via cURL/PHP - HOW? provides an example of a web app that you could adapt.
Should I use Spreadsheet API?
Your question doesn't actually say what it is you want to do, so the answer is "it depends". You still won't be able to invoke an embedded script using the Spreadsheet API, but you will be able to read and write spreadsheet content.
Obviously the answer depends on your usecases, but have you checked the Google Appscript API?
As stated it
provides methods to remotely execute Apps Script functions

Authorisation google urlfetch api only in script editor possible, not with web app/service UI

We build a service with google apps script (deployed as web app).
This service script uses a google api with UrlFetchApp and oAuthConfig. Our script and service is working without problems.
Problem:
If a new user wants to use our service, he starts the service url and the service opens. In this process he has to authorise everything the script function uses without the with urlfetch used api. So, if he starts the service again, he get the error message: "Authorization is required to perform that action".
The only way we found to authorise the urlfetch-api is that the user go into the scripteditor and start the function from the editor UI. If he do so, he can authorise this api. Afterwords he can start the service via url and everything is working well.
Do anybody know a better way to authorise the user without doing this in the editor?
There is an open issue since July 2011, issue 677 and a lot of people are waiting for that...
User can run script directly as url from browser and will get the standard authorisation dialogue at that point. You can make it a bit more user friendly by providing a link and a tailored response in html.