Google Apps Script: Getting parameters of url without doGet(e) - google-apps-script

I'm currently using Apps Script to build a webpage. Is there a way to get the parameters in the URL without using doGet(e)? i.e. Can I use a getParams() function to retrieve the URL?

Answer
No, it is not possible
How Web-Apps work
When a user visits an app or a program sends the app an HTTP GET request, Apps Script runs the function doGet(e). When a program sends the app an HTTP POST request, Apps Script runs doPost(e) instead. In both cases, the e argument represents an event parameter that can contain information about any request parameters.
Ask for a feature request
Google has a tool called Issue Tracker that tracks bugs and feature requests during product development. You can open a new feature request there.

Related

Calling Google Apps Script API via a Google Cloud Function

It is possible to call Google Apps Scripts API directly from a Google Cloud Function? If so, how to call the script functions without requesting an Oauth permission to get the authentication token?
My motivation is using Cloud Scheduler to invoke some functions of the Google Apps Script regularly (the time-based triggers on Google App Scripts don't have the sufficient resolution for my project).
You don't need to publish the Apps Script Web App as an API, you can publish it as a plain Web App, which doesn't need to go through the OAuth process in order to trigger either the doGet() or doPost() functions. doGet() is triggered to run from a GET request to the published URL of the Web App, and doPost() is triggered by a POST request. You'd need to publish the Web App to be accessible to anyone. If you make a POST request, and send a password in the payload request, that could be a way to verify that the request is coming from your code. The payload will be inside of the event object. The event object is typically denoted by the letter "e".
function doPost(e) {
For information about the event object see:
https://developers.google.com/apps-script/guides/web?hl=en#request_parameters

How to get and send an OAuth token to Apps Script from an external service?

I'm attempting to make a post request from an Airtable script to Google Apps Script. I have a doPost() function set up in the Google Apps Script file, but the post request needs to be authenticated to run.
I believe I need to pass an OAuth token in the header of my request, but I'm unsure how to get this token in the first place. I've found this doc here about web apps in Google Apps Script but no luck finding how to generate an OAuth token from an external service.
I may be completely off the rails with my thinking, so if there's an easier way to make a post request and authenticate it from an external service, I'm all ears.
EDIT:
I don't really have any code at the moment. I'm using Postman to send the calls to Google to test. In Apps Script I have the following inside just to test.
doPost(e) { Logger.log("POST REQUEST") }
When I make the call from Postman, the function isn't triggered. In short, I know I need an authorization in the post request, but I don't know how or where to get it.

HTTP Request to a function in Google Scripts

Since I'm not experienced at all with HTTP Request and Google Scripts, I'm having trouble wraping my head around it.
So, my problem is the following:
I'm currently trying to get information in my lua script and send it to a google Spreadsheet. However, the way the google spreadsheet should save the info would be dependent on which function on the Google Script I'm calling and passing information.
SO, my question is: How would my lua script (that only gives me access to HTTP Requests at this time) connect to a specific function like the one bellow?
function callName(name) {
// Get the last Row and add the name provided
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(sheet.getLastRow() + 1,1).setValue([name]);
}
Also, I think my script is wrong as well, but I'm more worried about how to actually make the connection.
Answer:
You can publish your script as a Web Application and use URL parameters to pass the script the information you need.
More Information:
From the Google documentation about web apps:
If you build a user interface for a script, you can publish the script as a web app. For example, a script that lets users schedule appointments with members of a support team would best be presented as a web app so that users can access it directly from their browsers.
However, even without building a user interface, you can use this functionality to run scripts on your sheet by utilising HTTP requests.
Modifying your Script:
In order to allow your script to accept URL parameters, you must first modify your code so that processing is done on a HTTP GET request. You can do this with the Apps Script doGet() function and the event parameter e:
function doGet(e) {
callName(e.parameter.name);
}
function callName(name) {
// Get the last Row and add the name provided
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(sheet.getLastRow() + 1,1).setValue([name]);
}
Setting up the Web App:
From the Apps Script user interface, follow the Publish > Deploy as web app... menu item, and in the newly-opened modal window, you'll want to select the following settings:
Project version: New
Execute the app as: Me (your-email#address.here)
Who has access to the app: Anyone, even anonymous
And click Deploy. Here, you will be given a URL in a new, smaller modal in the following form:
https://script.google.com/a/your-domain.com/macros/s/some-script-id/exec
Making the request:
The rest of this is now trivial - you can make your HTTP request to the script URL in the previous step, but providing the URL parameter that you need in order to give te app the information of the value you wish to set.
For example, if you want to set the value to the number 20, make your get request as so:
GET https://script.google.com/a/your-domain.com/macros/s/some-script-id/exec?name=20
Note the ?name=20 at the end gives the Web App the parameter name with a value of 20. The doGet(e) function reads this from e.parameter.name and sends it to your callName(name) function for processing and execution.
References:
Web Apps | Apps Script | Google Developers
Request Parameters

Cannot call Google Apps Script doPost() method from Twilio number

Trying to receive an SMS at my Twilio number and send a POST request to a Google Apps Script app URL as a result of the received SMS.
I have this doPost() message:
function doPost(request) {
return ContentService.createTextOutput("User says: "+JSON.stringify(request));
}
Some text should be spit out containing the request data.
My doPost() method never gets called. I can't tell if the POST request is actually being sent by Twilio. I see in the Twilio number message log that my SMS is received by Twilio. But after that I can't tell. I have the Twilio number configured for webhook - HTTP POST, and the published URL of my Google Apps Script project. If I change that to HTTP GET my doGet() method DOES get called. I need to doPost() method called, though. any suggestions? TIA.
How about the following confirmation?
Confirmation points :
Redeploy Web Apps as a new version again.
When the script is updated, Web Apps is required to be redeployed as a new version for reflecting the update.
Confirm setting for Web Apps.
"Execute the app as:" is "Me".
"Who has access to the app:" is "Anyone, even anonymous".
Retrieve a log of request using Stackdriver.
The sample script is as follows. Please copy and paste it. And redeploy Web Apps.
Request POST.
On script editor, click View -> Stackdriver Logging
By this, when POST request is received, you can see the log.
Sample script :
function doPost(request) {
console.log(JSON.stringify(request)); // Here
return ContentService.createTextOutput("User says: "+JSON.stringify(request));
}
By above confirmation, the reason of your problem may be found. But if this was not useful for you, I'm sorry.
I decided to just use doGet(). See my response to the previous comment.

How can I call Google Charts API from UrlFetch service in Google App Script?

I'm trying to write sort of a proxy to be able to query a Google Spreadsheet with Google Chart API without giving rights directly to the spreadsheet to people accessing the visualization.
In order to do that, I want to replace the calls like
var query = new google.visualization.Query('http://spreadsheets.google.com/tq?key=THE_KEY');
query.send(handleQueryResponse);
by calls of my Apps Script proxy (with JSONP).
I wrote the proxy as following :
(I'll add later an option to encapsulate the result in a function - JSONP)
function doGet(e) {
return ContentService
.createTextOutput(
UrlFetchApp
.fetch("http://spreadsheets.google.com/tq?tq="+e.parameter.q+"&key="+e.parameter.key))
.setMimeType(ContentService.MimeType.JSON);
}
When trying to get a response, I obtain this error : google.visualization.Query.setResponse({"version":"0.6","status":"error","errors":[{"reason":"user_not_authenticated","message":"User not signed in","detailed_message":"\u003ca target=\u0022_blank\u0022 href=\u0022https://spreadsheets.google.com/spreadsheet/\u0022\u003eSign in\u003c/a\u003e"}]});
like if the user was not authenticated. I think it's weird because my script is deployed as a web app to be executed by my user, who has the rights on the spreadsheet. I guess we must use some OAuth, but I don't know how to authenticate the query that way.
So my question is :
How to authenticate a query with Apps Script to access data with the access rights I defined when deploying as web app ?
See the urlfetch docs. You are missing all the oauth stuff.
The way you are doing it will only work for a public ss.