Google Apps Scripts does not save logs - google-apps-script

I created a script a few years ago and recently revisited it, to try and get the logging working. I added various console.info functions and tested it. It worked fine and I was able to see all the logs during execution.
However, the logs never appear in the Google Apps Script dashboard.
At the moment this screenshot was taken, more than 3 hours had passed since the execution.
When consulting the GCP console (by clicking View in Cloud Logging), even when setting a generic query, no logs are found.
I have been reading the documentation and trying to understand what the problem may be. From what I understand, some logging features only work for standard GCP projects, mainly accessing info in the GCP console. My script does not belong to a standard GCP project, it belongs to a default GCP project (they're different things from what I understood).
But according to the documentation, a default GCP project allows for logs to be accessed through the Apps Script dashboard. So the logs should be visible in the executions. I'm probably missing something, but I'm having a hard time figuring out what. Any suggestions would be welcome.
Minimal Reproducible Example
Here is a code example, whose logs appear on execution but not after.
function main() {
const xml = UrlFetchApp.fetch('https://www.imdb.com/calendar/?region=pt').getContentText();
const xmlSliced = xml.slice(xml.search('<div id="main">'), xml.search('<div id="sidebar">') - 1);
const document = XmlService.parse(xmlSliced);
const dates = document.getRootElement().getChildren('h4');
console.info(`Parsed ${dates.length} release dates`);
}

Related

Is there a way to recover Google Cloud Function source code when the bucket does not exist, but it can still run?

I have 13 Google Cloud Functions running in my Google Cloud Project. Several of them are triggered by Google Cloud Scheduler. I went to make an edit to one of the functions the other day and got the following error when trying to view the "SOURCE".
When looking into this issue, the culprit seems to be that the Archive Bucket has been deleted or that the name has changed. When looking through the Container Registry as well as the buckets, I can confirm that they are no longer there. I think another developer in the project may have accidentally deleted them when cleaning up images from an old project.
That being said, when looking at the logs I have noticed that the functions being triggered by Google Cloud Scheduler are still successfully running on their schedules even though I can no longer access the source code. The functions are logging data the I console within the source code even though it is no longer there.
My question is whether or not these are recoverable and what it would take to find them.
I have tried looking for the images and the buckets to see if they were renamed and I could not find them. I also created a new function in the same region and it spun up a new image and bucket, but it did not contain any reference to the other missing functions.
It is not possible to access the source code of a Google Cloud Function if the bucket that contained the code has been deleted. The source code is stored in the bucket, so if the bucket is deleted, the code is also deleted. If the function is still running, it is because the code has been deployed and is being run from a different location. You can view the logs for the function to see what code is being executed, but you will not be able to retrieve the original source code.
If you need to access the source code for the function, you will need to redeploy the code to a new bucket. You can do this by creating a new bucket and uploading the code, or by using the Cloud Functions API or the gcloud command-line tool to deploy the code to the new bucket.

How to link a Project Number to a Script dynamically?

I'm creating forms on demand and i need to be able to set up the triggers. If i do it on the main AppsScript script that is handling the forms creation, i'll hit the 20 triggers max quota pretty soon. So i decided to add a dedicated script to every form (associating with parentID) so i won't hit the quota.
My problem is that when i try to add the triggers i'm getting permission denied since the scripts created for each form are using a default GCP project and it needs to be a standard GCP project due to OAuth.
I can add the project number manually, but that kind of defeats the idea of being able to generate google forms on demand.
I want to be able to link the script created with the standard GCP project that is already configured, but i just can't find anything on Google's documentation, i know its a long shot but i've decided to post here in hopes that someone that had the same problem managed to do so.
As requested on the comments below, the triggers that i'm using are:
onFormSubmit - get the response and send it to my endpoint in the backend server
onOpen - ensures that the editor hasn't removed anything default (this only works when an editor opens the form in edit mode)
This is the project number im referring to.
Thanks

Correct scope for Google App Script Execution API?

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.

Logging with Google Apps script

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])

Is it possible to export Google Apps Script Execution transcript

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.