So I began experimenting with Google Apps script (to automate a google slide maker), so I created a test function. This is all the code:
function myFunction() {
Browser.msgBox("Hello World");
}
This gives me an error: Exception: Cannot call Browser.msgBox() from this context; have you tried Logger.log() instead? (line 2, file "Code"). Before I verified and allowed authorization, when I typed Browser. it would give me options. After I authorized, nothing popped up. This means that the authorization is the problem. Does anyone have a fix for this?
Also, google never made me type "continue" during the authorization.
The Browser class is only available for Google Sheets, not Google Slides. See its documentation:
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. Please use Google Workspace dialogs instead.
Related
I'm currently developing an Editor Add-on for Google Sheets using Apps Script. There's an onOpen function that sets up the menu items.
I also have a CONFIG variable in the root (not in any function) like this:
const CONFIG = {}
function setProperties_(){
CONFIG.tmpSheetId = PropertiesService.getScriptProperties().getProperty('TMP_SHEET_ID');
}
setProperties_();
If I run any functions from within the script editor, everything runs fine. No issues. However, when I do a test deployment I get the error below from the moment the onOpen() runs:
You do not have permission to call getScriptProperties
I've tried adding various script scopes from here but nothing works.
When you first install the add-on (the entry for the add-on becomes visible in the sheet menu), it runs in AuthMode.NONE that allows you to inspect only the current user's locale. Please refer to the table here Google Add-On Authorization Lifecycle
As Cooper pointed out, calling setProperties_() in global scope occurs before your add-on is authorized by the user. Move the function call to a nested function and make sure you are through with the authorization flow first.
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.
Why am I not able to give permission/authorization to a Google Apps Script that I also made using the same Google account?
It seems like Google doesnt trust myself to use my own Google Apps Script with my own Spreadsheet.
Here is the line of code that breaks everything. If this line doesnt exist, I'm not asked for permission.
var sheet = SpreadsheetApp.getActiveSheet();
So it's trying to access the spreadsheet that created this Google Apps Script, also made using my account but I cant grant permission.
When I run the line of code above, I am told I need to give permissions, so I do by selecting the account name I am already logged into. I am greeted by this error,
This app isn't verified
which unfortunately does not provide competent documentation to troubleshoot.
Any feedback or help would be much appreciated! Thanks!
Click on the "Advanced" link and you'll be able to authorize your script.
To reduce the scope of permissions you request, you also have the option of declaring your script project to be only able to interact with the bound document:
/* #OnlyCurrentDoc */
function myFunction() {
...
This declaration is incompatible with some methods (such as SpreadsheetApp.openById()), and using an incompatible method results in an error in the application execution.
Successfully adding it to your project is generally sufficient to remove the "This application is unsafe" layer of the authentication flow, meaning the authorization and permission list is not hidden behind the "Advanced" tab.
In addition to declaring as current document only, manually editing the requested scopes of your project in its project manifest can help reduce the perceived threat from an unverified application (for example, retaining only the "read_only" version of certain scopes, where applicable). Apps Script documentation offers more details on project manifests.
Brand new at this.
I thought the following code in Google Apps Script would simply open a sheet in another window. But nothing happened. Nothing.
Here's my code:
function OpenaSheet() {
SpreadsheetApp.openByUrl('https://docs---------Blanked out on purpose-------------')
}
The SpreadsheetApp openByUrl(url) was meant to access your google spreadsheet as shown in the docs.
Another way to access your gsheet is by openById. So openByUrl doesn't literally mean it will open a provided Url but instead a means to access the spreadsheet. Check this SO post for that purpose.
Problem: When I run the script, Google tells me,
You do not have permission to call openById
I had copied a script from another one of my Google spreadsheets and changed the target_ssKey variable's cell reference and created properly-sized Named Ranges in both the Source and Target spreadsheets.
Google Apps Script documentation says nothing about reasons why it might not be working:
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#openById%28String%29
Another Google Apps Script documentation says that it should work for me because I invoke it from a custom menu:
https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services
The second link above says:
If your custom function throws the error message You do not have
permission to call X service., the service requires user authorization
and thus cannot be used in a custom function.
To use a service other than those listed above, create a custom menu
that runs an Apps Script function instead of writing a custom
function. A function that is triggered from a menu will ask the user
for authorization if necessary and can consequently use all Apps
Script services.
I tried putting the function into a "Custom Functions" project and then into an "Add-on" project, but still got the same error message.
Any ideas on what I am doing wrong and how to make this work?
Here is my exact code:
function exportData_SStoSS() {
// Get the source data.
var source_ss = SpreadsheetApp.getActiveSpreadsheet();
var data = source_ss.getRangeByName("exportData").getValues();
// Identify the target.
var controls_sh = source_ss.getSheetByName("Controls");
var target_ssKey = controls_sh.getRange('C2').getValue();
var target_ss = SpreadsheetApp.openById(target_ssKey);
// Paste the data to the target.
target_ss.getRangeByName("importData").setValues(data);
};
I thought that I would throw in a similar issue that I had which brought me to this question, where I received the error You don't have permission to call by openById.
In my case I was trying to call functions from translate.gs which I copied from this example:
https://developers.google.com/apps-script/quickstart/docs
Note that at the top of translate.gs
/**
* #OnlyCurrentDoc
*
* The above comment directs Apps Script to limit the scope of file
* access for this add-on. It specifies that this add-on will only
* attempt to read or modify the files in which the add-on is used,
* and not all of the user's files. The authorization request message
* presented to users will reflect this limited scope.
*/
The culprit here is the #OnlyCurrentDoc comment. See here for reference:
https://developers.google.com/apps-script/guides/services/authorization
Removing #OnlyCurrentDoc fixed this issue for me
I could resolved this issue with this autorization guide of google developers.
https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes
This entry It's necesary in json file.
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets.readonly",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/spreadsheets"
],
I found this official note which I believe clears up what caused the issue.
If your function is a custom function, that is one which can be used like a regular spreadsheet function in the sheet itself, then it has limited access to things and cannot open other spreadsheets.
The same script can however open other spreadsheets from a menu button or similar.
Link: Documentation at developers.google.com
The method openById can be called from a "Blank Project" but not a "Custom Functions in Sheets" nor a "Google Sheets Add-on" project.
I thought a "Blank Project" would create a project that was not connected to my spreadsheet, but I was wrong. The Blank Project is connected to my spreadsheet. The other types of projects that I tried to use seem to be limited-scope versions of script projects, not able to carry out some GAS methods.
Had this same issue and came to share my solution. In my case I had two spreadsheets, call them A and B. Both used scripts bound to each respective spreadsheet. Spreadsheet B was able to write data to a tab of spreadsheet A. But Spreadsheet A kept getting the "You do not have permission to call openById" error when trying to read from spreadsheet B. I then tried adding it as a custom menu item but still the same issue.
The solution in my case turned out to be really simple. I created a new unbound script in script.google.com that calls both spreadsheets using openById. The first time running put a smile on my face as it asked for authorization. Thereafter smooth sailing.