I have written some GScript that is called by a Google App (Awesome Tables).
How do I debug the GScript while it is running after being initiated by the Google App (I particularly want to see that values in variables as I step through the code)?
I found that Google Apps Script has a Logger Class. I used this to log messages when certain stages in my code was reached (some included variable values) and then emailed the whole log to myself. See https://developers.google.com/apps-script/reference/base/logger
I don't know of a way to do this. The best approach is probably to mimic the calling application by creating a test function in your Google Apps Script that calls the main function with the appropriate parameters. Then you can step through the code and see values using the debugger.
Try to check this Debugging part in the Google Apps Script documentation if it can help you.
It is stated here that there are some strategies for handling errors and investigating a script that is not running the way you expect.
Execution transcript
Logging custom messages
Using the debugger and breakpoints
For more information, check this Some Simple Debugging for Google Apps Script
Related
When developing Google Apps Scripts (specifically for Sheets in my case), you can use the Debug button to step through code and inspect variables, or you can use the Logger to output debug info.
What I can't figure out, however, is if there is any way to do these things when functions are triggered through the sheet itself. For example, from a custom menu item which fires a function, or an onEdit() trigger.
It makes it very difficult to debug issues because, as far as I can tell, GAS simply fails silently. Nothing useful appears in the browser JS console either.
I'm writing this answer as a community wiki, since the issue was resolved from the comments section, in order to provide a proper response to the question.
As mentioned in the comments, you can use the Apps Script execution log in the script editor. You can use either the Logger or console logging service.
In addition, to test a trigger function and check if there are any errors, you can do the following:
Since you're using onEdit trigger, edit a cell in the sheet.
Go to the Apps Script project.
If there are errors, you can view the logs in the "Executions" section and you'll find a list of executions which you can filter by Failed and Time out.
Reference:
Apps Script mechanisms for logging
I have been attempting to find a method to log any errors that occur after a doPost(e) method executes with Apps Script (using JS). The previously answered question regarding this topic mentioned StackDriver logging with reference to the old editor provided by Google. However, how do I access a similar function with the new editor?
For some more context, if I execute functions from script editor, the Google Sheet they are editing, or through a trigger, I am able to view the errors through the Execution Log tab. But for any calls made through doPost, I am unable to view the causes of error. How do I log these errors?
Edit: I believe there might be a slight misunderstanding. The programmed doPost(e) takes user input from a Telegram bot and responds accordingly. Till date, the bot has been functioning as designed; however, for future debugging I wish to be able to somehow see any caused errors. This would provide more information than simply a "Failed" in the execution log. Any help is appreciated.
Apps Script Logging
Note:
Error Reporting interface in the Developer Console is pretty much similar with option 2 so I will not provide any further example.
Sample WebApp Code:
function doGet(e) {
var params = JSON.stringify(e);
console.log(params);
}
Sample Request:
https://script.google.com/..../exec?username=sample2&password=1234
Option1: Check the built-in Apps Script execution log
You can check the logs when you open your script and go to Execution tab
You can also check the logs in https://script.google.com/home/executions
Option2: Using Cloud Logging interface in the Developer Console
Assuiming you already attached a standard GCP project with your script project
You can view Cloud logs and error reports in the Google Cloud Platform console
Under Log Fields -> Select Apps Script Function
It will display the results under Query Results Tab
I have two questions regarding logging with apps script:
1/ I have two .gs files (in the same project). One (F1) is calling the other (F2) via urlfetch (POST request). The Logger.log works just fine in F1, but doesn't in F2 (maybe it works too, but I just can't retrieve the logs). Do you know if it's normal or where can I find the logs of F2 ?
(If you're wondering, it's made this way so we can call the same function multiple times in parallel).
2/ My scripts are linked to a Google Cloud project and so I've seen we can retrieve some logs via Stackdriver. What logs ? Because those with Logger.log don't seem to be visible from there.
Thanks for your help
Regarding your second question: Stackdriver Logging with Apps Script is a new feature, only accessible to G Suite Business customers that applied for early access.
The following functions should work
console.log([data][,...])
console.info([data][,...])
console.warn([data][,...])
console.error([data][,...])
console.time([label])
I have a written a web Apps Script which is deployed to run in the context of user accessing the web app.
Is it possible to export web Apps Script's Execution Transcript programatically?
Not directly.
You won't get a full stack trace, but you can be meticulous with Logging everything you want to be notified with. At the end of the script you could extract the log and email that.
Logger.getLog();
If you really wanted to be fancy, there are a few implementations of custom Loggers which extent the standard Logger.log(). For instance if you had an object that every time you send a log message to it, it would time-stamp overall duration, time since last log and the message passed to it, all to Logger.log. Same object could have a getLog(0 method of its own which did extra stuff - should you wish.
There is a notable logger library at Google Apps Script Examples by Peter Herrmann which will also allow you to log to a spreadsheet.
But in short, no, you cannot export the execution log.
I keep getting this error message when I try to view my deployed app.
Authorisation is required to perform that action
How do I get around the error?
Services in use in the script:
ScriptDB
UiApp
(I think that is all)
I can't find the answer in the documentation, so I would be very grateful for some help.
You need to go the script editor and attempt to run any function manually first.
A first authorization popup is shown when you first deploy as web app, but if you go and change the code accessing new services you have to re-authorize it.