Logging with Google Apps script - 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])

Related

Where can i see the analytics of an Google App Script Deployment?

I created a small app on top of a spreadsheet (with GAS and HTML, CSS) and I deployed it.
Users can access to it without having to enter in the spreadsheet.
It works really well but i'm not able to see even the basic analytics (for eg. the number of viewers)
Thanks
Go to the project overview page. In the Google Apps Script web IDE, on the lefmost sidepanel click on Overview.
Also, if you have starred your project, go to https://script.google.com/home/starred
Rather than "viewers" you will see "users". If you have set your web-app as execute as you by anyone even anonymous, you will see only one user, you, as this page show the users that exectuted the scripts like the doGet function and the server-side functions called through google.script.run.
Note: https://script.google.com keep execution logs for the last 7 days. If you need to keep the logs longer you have to use another place to keep these logs, i.e. Cloud Logging (requires a Google Cloud standard project), Google Sheets spreadsheet.
Resources
https://developers.google.com/apps-script/guides/logging
Related
Effective way to debug a Google Apps Script Web App

How to use GAS functions in a spreadsheet created by a service account in a team drive?

In my company we use a team drive, where we store a template spreadsheet which our API server copies a lot of times. The spreadsheet contains a few custom Google App Script functions which should be copied as well.
Right now when the API copies the spreadsheet, the scripts stop working in the copy. According to this issue https://issuetracker.google.com/issues/36762799 , service accounts can't execute scripts. However, in a Team Drive there are no owners. I even checked the permissions with Permissions.List from the Drive API and I can see that everyone is an organizer and the API does not allow me to specify another user as an owner, but the scripts still won't work (Error: Please try saving the project again). On the other hand, if I go and copy the generated sheet by hand, scripts execute just fine.
I tried moving everything to an add-on, however to my displeasure, it appears that service account-owned files can't execute add-on scripts as well (if the file is created by the bot, custom function names simply fail to resolve).
Does anyone know of a way to deal with this situation? Is there a more elegant solution besides using an actual account for the copying if that's even possible to do programmatically?

Debugging GScript called by Google App

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

How can I create spreadsheet with included gs script by API?

I'd like to manage spreadsheets (uploaded by a client via API) over google engine. I created test spreadsheet extended by functions in .gs script (which works great if I add them via online editor) but I do not know how to apply such a .gs script to every uploaded spreadsheet automatically (possibly via API).
Moreover, I do not want to share this .gs file by publishing it as an extension (because of security policies).
Updated: I also tried copying existing spreadsheet with working gs script and overwriting its spreadsheet data via API (instead of uploading new one) - it's overwritten data successfully but unfortunately lost gs script. Details are described in separate (bounty) question: Custom google app script doesn't work after copying spreadsheet with google java client
So I've even tried copying using plain HTTP methods since I thought it might be a problem with Java SDK but it still didn't work which means that there is some problem at google side (or something is really unclear) with service accounts.
After all I solved the problem with another approach.
First I copy spreadsheet with script attached to it to my service account (as I described in this Custom google app script doesn't work after copying spreadsheet with google java client topic). The file is copied with broken script.
Then I make user authorize using google's oauth (this tutorial shows how https://developers.google.com/identity/protocols/OAuth2WebServer#overview)
And then on his behalf I perform copying.
Finally the file is on his private drive which is the biggest downside of this solution but at least the script works which was my main goal.
Hope it helps ;)
You can publish a google script without it going on the Marketplace. If you make a Google group you can publish in such a way that only members of your google group can see it. If your google group only has you, only you can see it. Then it will be on all of your spread sheets.
The downside being that google will never let you remove it from their server, only unpublish it.
You can create a stand-alone script that is independent of any spreadsheet. This script would contain any methods/functions needed to create and manipulate spreadsheets according to your use case. Furthermore you could deploy the script as a web-app and implement a doGet() or doPost() method. Whenever a user uploads spreadsheet data via API you can call the web app with the information and have it create spreadsheets as needed.

Turning of captcha on Google Apps

I am generating Drive-folders on a Google Apps For Education domain.
Im running a Google Apps Script-program as an admin that uses the DriveApp. Every folder is shared to a group or user.
After about 100 folders i got an error and on the rerun I got in on the first folder.
When doing the equivalent task manually, I get a captcha-popup.
Conclusion: The captcha-requests renders my program useless. I most turn of captcha or make my program to handle them.
What can I do?