I'm trying to create a Google Apps Script to delete some email in bulk. The code for this doesn't really matter though as the GMailApp object isn't defined. I create a new project, add the following code:
function main() {
var threads = GMailApp.search('.....');
}
and I just get an error 'ReferenceError: GMailApp is not defined'. Do I have to enable the GMailApp? I thought the point of the app services was that they didn't need an API to be enabled, and that they can just be linked and used.
Any help appreciated,
Thanks
From menu select "Resources" -- "Advanced Google Services" and turn "Gmail API" on.
Pay attention to write in code GmailApp, not GMailApp.
It's easy to avoid typos by getting used to using the "Control-Spacebar" short cut.
When you are in the Apps Script IDE holding control and spacebar at the same time will give you a popup of all the classes and then you just select the one you want.
So in summary, the API object is GmailAPP, not GMailApp. The autocomplete with ctrl+space should help with problems like these however at some point I had it autocompleting my code so it must have got confused down the line. Thank you for your answers.
2021 Update for the Gmail API (not GmailApp)
If you're in the 2021 and later IDE, it's here:
Also, note that Gmail is different from GmailApp. In my experience Gmail is more customizable, because you're getting exposed to the Gmail API whereas GmailApp is a less-robust collection of methods, but it's simpler to use.
Related
I am creating an new Google Apps Script about GDrive/New/More/Google Apps Script.
The script is running perfect and on the end my intention is to send a Info on the Desk.
When I am using this line:
Browser.msgBox("TEST");
I get the error back:
Exception: Cannot call Browser.msgBox() from this context; have you tried Logger.log() instead?
Logger.log can't be the solution, because I don't see the result on the desktop, but only in the log files.
Is there another solution or is it possible to get a simply example what I can do?
In this case Browser.msgBox is a method for the Browser class, this class is specifically meant to be used in Spreadsheets as stated in the documentation:
Class Browser
This class provides access to dialog boxes specific to Google Sheets.
The methods in this class are only available for use in the context of a Google Spreadsheet.
If you want a notification you could use either the Browser.msgBox method or the Toast method, either way they both need to be applied in a Spreadsheet in order to work.
Does anyone know if there is a way to use the Google Sheets API to get the email address of a people chip? It seems like the only values it will give me are the person's full name as displayed in the cell.
While trying to figure this out I discovered that the name turns into an email address if you use "Format > Clear Formatting". I hoped I could hack this by copying the value into a hidden sheet, clearing the formatting, and then get the email address there. But though Google Apps Script has a function for executing that on a cell, I have found no way to do it with the Sheets API. I tried clearing all the possible formats I could via batchUpdate with an updateCellsRequest but that isn't turning the people chip into an email address.
I'm at the point where if I really want to automate this, I will need to use the scripts API as well, to deploy and execute a tiny Apps Script just to run clearFormat on a cell. I'd like to avoid this much complication, especially since Apps Script doesn't work for service accounts.
As Tanaike mentioned this is not possible at the moment but there is already a feature request in the Google Issue tracker related to adding the people chips to the Sheets API. You can check the request here.
I would suggest posting a comment on it and explain why you are interested in this feature. Or if you think your request has a different approach you can also submit a new feature request for the Sheets API here.
Is there a way using Google Apps Script GAS to turn on/off the 'Protected Ranges' view to give the stripped effect?
Yeap. The documentation has some pretty good examples, not going to post any of my own.
Link to documentation: https://developers.google.com/apps-script/reference/spreadsheet/protection
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
I have a few questions relating to publishing an add-on for Google Sheets.
I have a script right now set up with an on load trigger to run the script. Is it possible to have my script run the same way if it's published as an add-on? If not, I guess I can probably have the user click some sort of menu option to run it (which calls a specific function in my script)?
If I publish the script, can users see the code? The problem I have right now is that the script I'm using connects to an API that uses an API key to limit user requests. The key is meant to be private, and for now it's fine that I just have it in the code since I'm only using it privately. I don't have a way to hide the API key from the users if the code is public though, so it would really be a problem if it was public.
If you publish the Google Script as a web app or as an add-on, others cannot see the source code of the script.
"I have a script right now set up with an on load trigger to run the script."
If this means that you are trying to do anything apart from create a custom menu in the onOpen() it could fail as it may not have the authorisation to run; you are limited to what you can do in onOpen() in an add-on. Take a look at the add-on authorisation cycle.
The user can't see your add-on code. However if you were planning to open source your code at any point you can store private things like API keys using the Properties Service that can be manually set in the script editor (File>Project Properties>Script Properties) or by running a bit of code that you then delete.
Answer to question 1:
Yes, you can use the onOpen(e) simple trigger as outlined in the documentation.
(https://developers.google.com/apps-script/guides/triggers/
Answer to question 2:
As Amit Agarwal mentioned in their answer, the code for a published add-on will be private. Users will not be able to see your API key.