Error Connecting to Google Server using Script Editor - google-apps-script

I am trying to use the Google Sheet's Script Editor to write a script for a Dependent List using the following script:
function setDataValid_(range, sourceRange) {
var rule =
SpreadsheetApp.newDataValidation().requireValueInRange(sourceRange,
true).build();
range.setDataValidation(rule);
}
function onEdit(){
var pageCell = SpreadsheetApp.getActiveSheet().getActiveCell();
var pageColumn = pageCell.getColumn();
if (pageColumn == 6 && SpreadsheetApp.getActiveSheet().getName() ==
'raw_data'){
var range = SpreadsheetApp.getActiveSheet().getRange(pageCell.getRow(),
pageColumn + 2);
var sourceRange =
SpreadsheetApp.getActiveSpreadsheet().getRangeByName(pageCell.getValue());
setDataValid_(range, sourceRange);
}
}
There are two different tabs I am using in this spreadsheet, "raw_data" and "Range". I had created a drop down list in column F of "raw_data" that lists values from a specified range in "range". Each of these values is also the title of a named range in that tab. The script above is suppose to read the value selected in column F and then two columns over, in column H, create another drop down list based on the values associated with that named range.
The problem I keep having is every time I click run I get the error message "Could not connect to server. Please save and try again" or if I try to set up a project trigger I get "We're sorry, a sever error occured. Please wait a bit and try again".
I have created new spread sheets, recreated the named ranges, created new script files and tried running the script from a separate computer but error message continues.
I haven't been able to find a solution to this and even when I try to use the dubug tool it gives me an error message. Does anyone know what the issue is or how I might resolve this?

I think this may be a legitimate server error at the moment because I'm getting the same error when I try to run any scripts. Same for a co-worker.

I have tried resaving several copies of my most recent script, all resulting in
Server error occurred. Please try saving the project again.
so I think that this is an issue on Google's side for some reason today.
My older scripts are running fine.

So I tried the script again this morning and it is working now. Hopefully it continues that way. Thanks for y'alls input!

Just checked the G Suite Status Dashboard and all products seems fine. This page offers performance information for G Suite services. Unless otherwise noted, this status information applies to consumer services as well as services for organizations using G Suite. No issues mentioned on Google's server as of this posting.
In script.google.com:
Resources -> Advance Google Services
Enable Drive API
You will be asked to enable it in the Google Dev console. Click the link and enable Drive API. Your script should run as intended now.

Related

Google blocks my Google Script that reads info from my Google Sheet to make slides in Google Slide project [duplicate]

I am unable to run any app script in my google account - even one created by me bound to a new document created by me. When I do, I get an "This App is Blocked" error. Note that this is different from the the "Sign in temporarily disabled for this app" failure mode mentioned elsewhere, and the solution for that problem has no effect on this one.
To test, I created a new spreadsheet while logged into my personal google account and put the value ONE in cell A1...
I then opened the Tools->Script editor from the menu bar of this spreadsheet and entered the following trivial script...
I then click on the run icon from the script editor menu bar with the function "myFunction" selected from the pulldown.
I get an "authorization required" popup and so I click "Review Permissions"...
I then get a "Choose an account" popup and click on my account (the only logged in account, and the same account I was logged into when I created the sheet and the script)...
I then get an "App Blocked" popup. Checking the execution log shows that the script did not run.
What is causing this popup and how can I prevent this so I can run app scripts?
NOTE: I see many other questions describing more complicated versions of this issue but none have useful answers. I am hoping this ultra simple version of the issue will help narrow it down and get a resolution.
More unexpected behavior:
If I put the code into the onLoad() function it works fine and never even asks me for authorization.
If I enter this code:
...and then quit out and reload the sheet, then I get this in the execution logs...
Again, this is with no authorization popups at all. The sheet loads without interruption and then the entry is in the execution logs. So this would seem to not be a case of not having the right permissions somewhere since code clearly can access the sheet.
Legacy editor
Identical behavior under the legacy editor...
Other accounts
I can repeat these exact same steps on a different google account and it works fine, so this problem appears to be linked to my account. Many others on the internet have noted the same finding. Seems like there might be some hidden (probably unintional) setting attached to the account that gets switched somehow and then thereafter the account is not able to manually authorize scripts to run.
Changing Project
I check and this script is in the "Default" project as expected...
According to this page,
For most applications and scripts, you never need to see or adjust this default GCP project—Apps Script handles all the necessary interactions with the Google Cloud Platform automatically.
Since I have nothing to lose, tried creating a new project in the Google Cloud Platform console, and then tried assigning this script to that new project. Unfortunately when I tried, I got the normal "Authorization needed" popup which lead to this opdd page...
Clicking on the "Troubleshoot this problem" link takes me to this page...
...which seems to say that I do not have the permissions to troubleshoot problems on my own account.
This again seems to suggest there is something misconfigured about my account on google servers. :/
Unfortunately there are no good answers there. Lots of people like me wake up one random morning to find that they can not run any new App Scripts in their account any more.
I've found a workaround that works great... but will make you very sad.
This issue does not affect code that runs automatically, so functions like onOpen() and onEdit() run just fine and have full access to the bound document. You heard that right- google blocks code that the user explicitly requests to run to protect their data, but code that runs silently and automatically anytime a sheet is opened or modified is free to run and access (and change!) whatever data it wants.
So to let the user run your code, you pick a cell inside the spreadsheet that either has a value that changes whenever you want your code to run, or you make a special cell called "Edit this cell to run the program".
Then you put your code inside the onEdit() and (if desired) check to see to see if the special cell was updated. If so, then run your arbitrary code. It has full access to the spreadsheet and can read and update cells at will and can also write to the log.
Note that you must close the sheet and re-open it for the code to take effect.
Here is what my demo spreadsheet looks like...
...and here is the demo code...
function onEdit(e) {
var range = e.range;
const triggerCell = "B2";
if( e.range.getA1Notation() === triggerCell){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var rangeB1 =sheet.getRange("B1");
var rangeB2 =sheet.getRange("B2");
var date = Utilities.formatDate(new Date(), Intl.DateTimeFormat().resolvedOptions().timeZone, "HH:mm:ss");
rangeB2.setValue("Code ran at " + date );
Logger.log( date + ": B1=" + rangeB1.getValue() );
}
}
Here is a demo video...
https://youtu.be/ypuLaUWn1R8
I've said it before, I'll say it again - if you were thinking about using Google Cloud Services for anything then think again. This is crap built on top of crap that no one at google understands and it occasionally breaks suddenly and catastrophically, and there is no one who can even tell you what is going on much less how or when or if it is going to get fixed.
I ran into this for the first time today but my issue seems to be due to:
created a sheet on a shared drive owned by a different organization
my organization uses the legacy apps script interface, the shared drive organization uses the new apps script interface
Using an account from the shared drive organization to create the script solved my issue.
This error
This app is blocked
seems to be a new error affecting certain Google accounts. This is reported to Google. Kindly star(add a star ★ to it on top left) to the following issues and comment to get Google developers to prioritize this issue:
https://issuetracker.google.com/issues/176138626
https://issuetracker.google.com/issues/181220763
Some workarounds:
#15 Creating a new GCP and linking it.
#12 Turning on Less Secure Apps
#10
Checking accounts security page
Turning off advanced protection
Unlock Captcha
This issue does NOT seem to be related to another issue 145162820:
"Sign in temporarily disabled for this app"
The error messages are different.

Can not run trivial app script bound to new blank document - get "This app is blocked"

I am unable to run any app script in my google account - even one created by me bound to a new document created by me. When I do, I get an "This App is Blocked" error. Note that this is different from the the "Sign in temporarily disabled for this app" failure mode mentioned elsewhere, and the solution for that problem has no effect on this one.
To test, I created a new spreadsheet while logged into my personal google account and put the value ONE in cell A1...
I then opened the Tools->Script editor from the menu bar of this spreadsheet and entered the following trivial script...
I then click on the run icon from the script editor menu bar with the function "myFunction" selected from the pulldown.
I get an "authorization required" popup and so I click "Review Permissions"...
I then get a "Choose an account" popup and click on my account (the only logged in account, and the same account I was logged into when I created the sheet and the script)...
I then get an "App Blocked" popup. Checking the execution log shows that the script did not run.
What is causing this popup and how can I prevent this so I can run app scripts?
NOTE: I see many other questions describing more complicated versions of this issue but none have useful answers. I am hoping this ultra simple version of the issue will help narrow it down and get a resolution.
More unexpected behavior:
If I put the code into the onLoad() function it works fine and never even asks me for authorization.
If I enter this code:
...and then quit out and reload the sheet, then I get this in the execution logs...
Again, this is with no authorization popups at all. The sheet loads without interruption and then the entry is in the execution logs. So this would seem to not be a case of not having the right permissions somewhere since code clearly can access the sheet.
Legacy editor
Identical behavior under the legacy editor...
Other accounts
I can repeat these exact same steps on a different google account and it works fine, so this problem appears to be linked to my account. Many others on the internet have noted the same finding. Seems like there might be some hidden (probably unintional) setting attached to the account that gets switched somehow and then thereafter the account is not able to manually authorize scripts to run.
Changing Project
I check and this script is in the "Default" project as expected...
According to this page,
For most applications and scripts, you never need to see or adjust this default GCP project—Apps Script handles all the necessary interactions with the Google Cloud Platform automatically.
Since I have nothing to lose, tried creating a new project in the Google Cloud Platform console, and then tried assigning this script to that new project. Unfortunately when I tried, I got the normal "Authorization needed" popup which lead to this opdd page...
Clicking on the "Troubleshoot this problem" link takes me to this page...
...which seems to say that I do not have the permissions to troubleshoot problems on my own account.
This again seems to suggest there is something misconfigured about my account on google servers. :/
Unfortunately there are no good answers there. Lots of people like me wake up one random morning to find that they can not run any new App Scripts in their account any more.
I've found a workaround that works great... but will make you very sad.
This issue does not affect code that runs automatically, so functions like onOpen() and onEdit() run just fine and have full access to the bound document. You heard that right- google blocks code that the user explicitly requests to run to protect their data, but code that runs silently and automatically anytime a sheet is opened or modified is free to run and access (and change!) whatever data it wants.
So to let the user run your code, you pick a cell inside the spreadsheet that either has a value that changes whenever you want your code to run, or you make a special cell called "Edit this cell to run the program".
Then you put your code inside the onEdit() and (if desired) check to see to see if the special cell was updated. If so, then run your arbitrary code. It has full access to the spreadsheet and can read and update cells at will and can also write to the log.
Note that you must close the sheet and re-open it for the code to take effect.
Here is what my demo spreadsheet looks like...
...and here is the demo code...
function onEdit(e) {
var range = e.range;
const triggerCell = "B2";
if( e.range.getA1Notation() === triggerCell){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var rangeB1 =sheet.getRange("B1");
var rangeB2 =sheet.getRange("B2");
var date = Utilities.formatDate(new Date(), Intl.DateTimeFormat().resolvedOptions().timeZone, "HH:mm:ss");
rangeB2.setValue("Code ran at " + date );
Logger.log( date + ": B1=" + rangeB1.getValue() );
}
}
Here is a demo video...
https://youtu.be/ypuLaUWn1R8
I've said it before, I'll say it again - if you were thinking about using Google Cloud Services for anything then think again. This is crap built on top of crap that no one at google understands and it occasionally breaks suddenly and catastrophically, and there is no one who can even tell you what is going on much less how or when or if it is going to get fixed.
I ran into this for the first time today but my issue seems to be due to:
created a sheet on a shared drive owned by a different organization
my organization uses the legacy apps script interface, the shared drive organization uses the new apps script interface
Using an account from the shared drive organization to create the script solved my issue.
This error
This app is blocked
seems to be a new error affecting certain Google accounts. This is reported to Google. Kindly star(add a star ★ to it on top left) to the following issues and comment to get Google developers to prioritize this issue:
https://issuetracker.google.com/issues/176138626
https://issuetracker.google.com/issues/181220763
Some workarounds:
#15 Creating a new GCP and linking it.
#12 Turning on Less Secure Apps
#10
Checking accounts security page
Turning off advanced protection
Unlock Captcha
This issue does NOT seem to be related to another issue 145162820:
"Sign in temporarily disabled for this app"
The error messages are different.

Unable to authorize ANY external Google Apps Script scope on any ANY file [duplicate]

I am unable to run any app script in my google account - even one created by me bound to a new document created by me. When I do, I get an "This App is Blocked" error. Note that this is different from the the "Sign in temporarily disabled for this app" failure mode mentioned elsewhere, and the solution for that problem has no effect on this one.
To test, I created a new spreadsheet while logged into my personal google account and put the value ONE in cell A1...
I then opened the Tools->Script editor from the menu bar of this spreadsheet and entered the following trivial script...
I then click on the run icon from the script editor menu bar with the function "myFunction" selected from the pulldown.
I get an "authorization required" popup and so I click "Review Permissions"...
I then get a "Choose an account" popup and click on my account (the only logged in account, and the same account I was logged into when I created the sheet and the script)...
I then get an "App Blocked" popup. Checking the execution log shows that the script did not run.
What is causing this popup and how can I prevent this so I can run app scripts?
NOTE: I see many other questions describing more complicated versions of this issue but none have useful answers. I am hoping this ultra simple version of the issue will help narrow it down and get a resolution.
More unexpected behavior:
If I put the code into the onLoad() function it works fine and never even asks me for authorization.
If I enter this code:
...and then quit out and reload the sheet, then I get this in the execution logs...
Again, this is with no authorization popups at all. The sheet loads without interruption and then the entry is in the execution logs. So this would seem to not be a case of not having the right permissions somewhere since code clearly can access the sheet.
Legacy editor
Identical behavior under the legacy editor...
Other accounts
I can repeat these exact same steps on a different google account and it works fine, so this problem appears to be linked to my account. Many others on the internet have noted the same finding. Seems like there might be some hidden (probably unintional) setting attached to the account that gets switched somehow and then thereafter the account is not able to manually authorize scripts to run.
Changing Project
I check and this script is in the "Default" project as expected...
According to this page,
For most applications and scripts, you never need to see or adjust this default GCP project—Apps Script handles all the necessary interactions with the Google Cloud Platform automatically.
Since I have nothing to lose, tried creating a new project in the Google Cloud Platform console, and then tried assigning this script to that new project. Unfortunately when I tried, I got the normal "Authorization needed" popup which lead to this opdd page...
Clicking on the "Troubleshoot this problem" link takes me to this page...
...which seems to say that I do not have the permissions to troubleshoot problems on my own account.
This again seems to suggest there is something misconfigured about my account on google servers. :/
Unfortunately there are no good answers there. Lots of people like me wake up one random morning to find that they can not run any new App Scripts in their account any more.
I've found a workaround that works great... but will make you very sad.
This issue does not affect code that runs automatically, so functions like onOpen() and onEdit() run just fine and have full access to the bound document. You heard that right- google blocks code that the user explicitly requests to run to protect their data, but code that runs silently and automatically anytime a sheet is opened or modified is free to run and access (and change!) whatever data it wants.
So to let the user run your code, you pick a cell inside the spreadsheet that either has a value that changes whenever you want your code to run, or you make a special cell called "Edit this cell to run the program".
Then you put your code inside the onEdit() and (if desired) check to see to see if the special cell was updated. If so, then run your arbitrary code. It has full access to the spreadsheet and can read and update cells at will and can also write to the log.
Note that you must close the sheet and re-open it for the code to take effect.
Here is what my demo spreadsheet looks like...
...and here is the demo code...
function onEdit(e) {
var range = e.range;
const triggerCell = "B2";
if( e.range.getA1Notation() === triggerCell){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var rangeB1 =sheet.getRange("B1");
var rangeB2 =sheet.getRange("B2");
var date = Utilities.formatDate(new Date(), Intl.DateTimeFormat().resolvedOptions().timeZone, "HH:mm:ss");
rangeB2.setValue("Code ran at " + date );
Logger.log( date + ": B1=" + rangeB1.getValue() );
}
}
Here is a demo video...
https://youtu.be/ypuLaUWn1R8
I've said it before, I'll say it again - if you were thinking about using Google Cloud Services for anything then think again. This is crap built on top of crap that no one at google understands and it occasionally breaks suddenly and catastrophically, and there is no one who can even tell you what is going on much less how or when or if it is going to get fixed.
I ran into this for the first time today but my issue seems to be due to:
created a sheet on a shared drive owned by a different organization
my organization uses the legacy apps script interface, the shared drive organization uses the new apps script interface
Using an account from the shared drive organization to create the script solved my issue.
This error
This app is blocked
seems to be a new error affecting certain Google accounts. This is reported to Google. Kindly star(add a star ★ to it on top left) to the following issues and comment to get Google developers to prioritize this issue:
https://issuetracker.google.com/issues/176138626
https://issuetracker.google.com/issues/181220763
Some workarounds:
#15 Creating a new GCP and linking it.
#12 Turning on Less Secure Apps
#10
Checking accounts security page
Turning off advanced protection
Unlock Captcha
This issue does NOT seem to be related to another issue 145162820:
"Sign in temporarily disabled for this app"
The error messages are different.

Error "Cannot find method insertChart(EmbeddedChartBuilder)" when using Apps Script to create Embedded Charts

I will like to take data from a spreadsheet "dataSheet" (shared with me as View only) to create a chart and output to my own spreadsheet "sheet".
I use Apps Script to create an embeddedChart and encountered this error: Cannot find method insertChart(EmbeddedChartBuilder).
I have followed Google's guide closely https://developers.google.com/apps-script/reference/spreadsheet/embedded-chart but can't resolve the error.
I have debugged step-by-step and realised the error happens at .insertChart (last line of code).
var chartBuilder = sheet.newChart();
chartBuilder.addRange(dataSheet.getRange('R'+earliestRowUsed + 'C1:R'+latestRowUsed + 'C1'))
.setChartType(Charts.ChartType.COLUMN)
.setPosition(5,5,0,0);
//collect data from dataSheet to add to Range.
for (indicator = 0; indicator < indicatorP.length; indicator++) {
indicatorIndex = indicatorList[0].indexOf(indicatorP[indicator]);
chartBuilder.addRange(dataSheet.getRange('R'+earliestRowUsed + 'C'+(dataCol + indicatorIndex) +':'+ 'R'+latestRowUsed + 'C'+(dataCol + indicatorIndex)));
}
chartBuilder.setMergeStrategy(Charts.ChartMergeStrategy.MERGE_COLUMNS); //join columns to create a table
chartBuilder.build();
sheet.insertChart(chartBuilder); //error takes place here.
I am open to using Charts or EmbeddedCharts to create my chart for my purpose (which is to take data from a shared view-only sheet to create a chart and embed in my sheet).
For working with Charts and Sheets, I found this Google guide https://developers.google.com/chart/interactive/docs/spreadsheets#embedding-achart-in-a-spreadsheet.
However the guide only shows a manual process to create Charts, and not with Apps Script. If anyone knows how, will appreciate your sharing as I haven't been able to find anything online.
Thank you.
I think your problem is the "View Only" permissions. Pretty sure that if you want to take data from a sheet programmatically and use it somewhere else you'll need to ask for Edit privileges.
After trialing a few scenarios, I found the problem to be due to EmbeddedCharts not being able to use data from another sheet file (view-only or owned). Using data from any sheets within the file EmbeddedCharts is to be created in works.

"We're sorry, a server error occurred. Please wait a bit and try again" error when running a function from a custom menu

I have received 341 error notification emails for the below error
We're sorry, a server error occurred. Please wait a bit and try again.
The notification email only tells about the name of function in which the issue is occurring.
The script is not running now. Any try to run any function in the script gave the above error. Also my custom menu that i have added into the Spreadsheet using the script is gone. I found no way to debug the script as every action on the script giving me the above error.
Below is the function in which error is coming
function crunchEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var gLabel = sheet.getRange("gmailLabel").getValues();
// check the current SpreadSheet for limits and create a
// new SpreadSheet if limits are approaching
checkSpreadSheet();
var gFolder = sheet.getRange("outputFolderName").getValues();
var gSpreadSheetName = sheet.getRange("outputSpreadsheetPrefix").getValues();
var threadsPerSearch = sheet.getRange("threadsPerSearch").getValues();
// Number of emails per search:
var threads = GmailApp.search("-label:" + gLabel, 0, threadsPerSearch);
for (var x=0; x<threads.length; x++) {
var messages = threads[x].getMessages();
// getting null...a thread without messages is weird
if(messages != null) {
for (var y=0; y<messages.length; y++) {
updateSpreadSheet(messages[y]);
}
}
GmailApp.getUserLabelByName(gLabel).addToThread(threads[x]);
}
}
I am stuck in the middle of nowhere, have no clue about why this is happening?
Could anyone please guide to resolve the issue?
Problem Solved.
The problem is with string replace function in some other function. I am using the string replace function with regular expression as input to replace function. The regular expression was incorrect due which i am getting the above error.
That's really weird, at least GAS should give some proper error.
In the script -> Resources -> Advanced google services -> turn on Drive API
Click in below message " Google Cloud Platform API Dashboard" to open thee cloud project for the script.
With the project selected, search in the search bar "Drive API" -> ENABLE, do the same for "Google Drive API".
Done, no more "we're are sorry server error" for DriveApp functions in the script.
The answer provided by Hemaka Raveen seems correct, it helped me in the same problem (it appeared when one of my functions wanted to access API which was not enabled for me).
However in 2020 Google introduced new code editor for Apps Script, Hemaka Raveen's answer is related to the legacy editor. To enable Google Drive API in the new editor, open your script, find Services tab on the left side, click on the + icon, select Drive API and click Add.
Keep in mind that enabling it here is only one part of the proccess. You have to enable it in Google Cloud Platform, but that step is the same as in Hemaka Raveen's description.
It seems a general "I haven't got a clue what is going wrong" error message.
In my case I added a library and other users of the script that used the library got this message, whatever function was called or trigger fired (even onOpen())
It was solved when I deployed the library as "version 1".
I googled this exact error, & found this page.
toddmo commented below question:
I am still researching, but I think this is a generic message for an
unhandled back end error.
This made me check my whole script. I was calling a function at the end of script; which was calling itself in the function, creating a recursive call of that function. I fixed that bug, & no more this error.
Problem Solved.
For me, this turned out to be a legacy script trying to run an Advanced Google service. I made a copy of the Google sheet and then ran it to enable permissions.
If your script project uses a default GCP project created on or after April 8, 2019, the API is enabled automatically after you enable the advanced service and save the script project. If you have not done so already, you may also be asked to agree to the Google Cloud Platform and Google APIs Terms of Service as well.
Advanced Google services