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
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
My Google Apps script is failing occasionally during time-driven execution. It works perfectly fine when I run it from the Google App Scripts Editor.
I'd like to see the error reports for why it's failing. Nothing is showing up in the simple logs in the execution console. My project is not a GCP standard project so I can't view the logs on GCP.
I would prefer not to convert my project to a standard GCP project, as we are already running in production and I don't want to break anything.
Is there a way to view Stackdriver error reports without having a standard GCP project?
As a test I created an Apps Script file, and created an error, then viewed the executions.
function myFunction() {
var sum = valOne + 2;
}
After running the above function from the code editor, I opened the Executions. In the Executions, there is a line with the status "Failed" and the error can be viewed by clicking the "Expand" icon.
If you use the "View" - "Stackdriver Logging" or
"View" - "Stackdriver Error Reporting" menu items, you'll get a dialog box that shows:
So, there is no way to view the Stackdriver logs in the Google Cloud Platform for an Apps Script Project that is using the default Apps Script–managed Cloud Platform project.
But, if there is a failure, the error message is shown in the expanded view including the file name and line number where the error occurred. If you are catching the error, then the status in the executions will be "completed" not "Failed" but you can log an error and it will show up.
function myFunction() {
try{
var sum = valOne + 2;
}catch(e) {
console.error('myFunction failed: ' + e)
}
}
Executions Log for caught error:
Your issue is related to a time based trigger. If the time-based trigger isn't running it's function, there wouldn't be any error. You can't get an error from code that never ran.
I run the GAS associated with submitting from Google Form,
Please let me know how to do it in debug mode.
I want to see various data such as what kind of content is received from the form,
It's troublesome to write every Logger.
I am guessing your issue is you have some kind of event which needs to be passed in order for you script to be able to run. (This I am guessing is why you cannot just use the debug tool in the IDE.)
An alternative I guess would be using chrome dev tools.
https://developers.google.com/web/tools/chrome-devtools/javascript/step-code
You can use the StackDriver console to log all message persistently.
function onSubmit(e) {
console.log(e);
// your code here.
}
Inside the Script Editor, go to View > Stackdriver Logging to view the logged messages.
I'm hoping to automate some HR work by running a Google App Script via the Execution API. Without getting too much into the details, I'd like to pass employee evaluation data as a parameter into the App Script. The script will then use this data to compile an "Employee Review" GDoc.
So far, I have ran a simple test App Script using the Execution API. For example, I can successfully run a simple function which logs a string or interacts with spreadsheets. So far so good.
But I run into problems when trying to write to a GDoc (which is unfortunately integral to my task). Here's my paired down script:
// TODO: Eventually, we'll pass these variables as arguments
var docId = "MY-DOC-ID";
// Find the team member review doc
var doc = DocumentApp.openById(docId);
// Replace placeholder text
var docBody = doc.getActiveSection();
docBody.replaceText('{{DATE}}', "Date set by App Script!!!");
doc.saveAndClose();
This script works when I press the "Run" button in the App Scripts web UI. But when I try to run via the Execution API, I get:
{
"error": "unauthorized_client",
"error_description": "Unauthorized client or scope in request."
}
So apparently I haven't provided the correct scope? Following the docs, I can find the necessary scope(s) in Project Properties > Scopes which says:
But when I try adding that scope, it wont work. As I said other scopes (e.g. https://www.googleapis.com/auth/spreadsheets) work just fine. Perhaps the auth/documents scope is no longer supported or there's a bug in their API?
Questions
What is the correct scope? I can see a big list here but I don't see https://www.googleapis.com/auth/documents, so?
Any other suggestions? For example, is it possible to write to a Google Doc using the Google Client API directly (i.e. without using App Scripts)?
Doh. I figured out the solution to my problem. While it was a dumb mistake, it's nevertheless worth posting as it may save others confusion in the future.
First, a little context about my setup. I'm authenticating to the Google Client API using a Service Account. Furthermore, as is common when using a service account setup, I am impersonating a user within our organization (specifically my own account).
My missing step (obvious in hindsight)...
Log into the App Script web UI as the person you are impersonating.
Manually run the script by pressing the play button
If the impersonated user has not already granted permissions to access the required scopes, you will be prompted to do so.
After granting access (specifically for the https://www.googleapis.com/auth/documents scope), my authorization error disappeared.
So the lesson: Make sure the account you are impersonating has granted access for all the scopes which your script requires.
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