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

6

35

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?

Ehlers answered 14/9, 2013 at 17:48 Comment(4)
Just try to simplify your script by commenting suspicious parts until it works and rebuild it step by step. Use the debugger and the execution transcript to see exactly where it fails... no doubt that when you'll find the issue it will appear so obvious.:-)Ancier
@Sergeinsas: I commented different parts of the function but issue is still there, I even commented everything in the function but the above error is still coming when i run that function.Ehlers
Should i post the function code?Ehlers
I am still researching, but I think this is a generic message for an unhandled back end error.Sickener
H
17
  • 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.

Helton answered 14/10, 2020 at 11:13 Comment(3)
I don't see "Google Cloud Platform API Dashboard". Where is this? How else can we get here?Acicular
Go to console.cloud.google.com. Then, go to navigation menu (in top of the left) and click the APIs & Service. Otherwise you can search APIs from the search bar.Helton
This solution only might work when the script is using Google Drive. (See Lexcel Atmadata's answerTutelage
E
16

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.

Ehlers answered 21/9, 2013 at 17:51 Comment(5)
3.5 years on, the error still persists. I got the same error because I fed undefined variable to Utilities.formatString. GAS should really fix the debugging messages there.Kelleekelleher
Was getting this same error due to an invalid backreference. Good to know at least that GAS wasn't throttling usage.Bullwhip
Thanks for this. My case was if( typeof i == 'string'){..} where variable i turned out to be undefined. Changing it to if( i && typeof i == 'string'){..} did the trick.Sitton
I was facing the issue and was not getting any clue to solve it. The solution gave me a clue about how to proceed. In my case, some of the attributes of the object that i was passing to the server was having "NaN" as the value. Which was why it was not even entering the function. GAS should atleast show some meaningful errors.Protective
Almost 9 years now and the error still persists. I forgot to pass folder id to DriveApp.getFolderById() funcCzerny
C
4

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.

Clementina answered 19/1, 2021 at 11:16 Comment(1)
Hemake Ranveen's answer only might work when the script is using Google Drive. (See Lexcel Atmadata's answerTutelage
N
2

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".

Napper answered 16/7, 2021 at 13:6 Comment(0)
E
0

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.

Embarrassment answered 13/3, 2022 at 8:19 Comment(0)
C
0

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

Cubitiere answered 14/2, 2023 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.