Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Scripts in Sheets: Script Experienced an Error (How to get the error!?)

My question is mighty simple: how do you get the error message!?

All I have to go off of is the red box at the top that says "Script CustomSort Experienced an Error," which is almost as vague as one can get!

Going deeper, the following worked about a week ago, now it no longer does. I haven't touched the script for at least a month. Rows have been added though. I reverted my spreadsheet back to a state when it used to work, but it still does not work.

function onOpen() 
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "Custom Sort", functionName: "CustomSort"}];
  ss.addMenu("Sort", menuEntries);

  CustomSort();
}

function CustomSort() 
{
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange(2,1,sheet.getMaxRows()-1,sheet.getMaxColumns()); 

  var headers = sheet.getRange(1, 1, 1, sheet.getMaxColumns()).getValues();
  var sortOrder = new Array;
  sortOrder.push({ column: headers[0].indexOf("Completion Date")+1, ascending: true });
  sortOrder.push(headers[0].indexOf("Owner")+1);
  sortOrder.push(headers[0].indexOf("Tentative Due Date")+1);
  sortOrder.push({ column: headers[0].indexOf("Priority")+1, ascending: false });
  sortOrder.push(headers[0].indexOf("Department")+1);

  range.sort(sortOrder); 
}

I can run the script within the script editor with no errors, but my understanding is that it runs the script with an empty sheet (is that true?), which may signal there is an issue within the sheet.

With an error or even a line number, I'd figure it out in a jiffy! I am trying to avoid ripping apart the rather large spreadsheet or code to figure this out.

I have done a bunch of Googling, and there are many references to similar issues being a Google problem, but I think this issue has existed for several days.

(I have also had no luck with logging ever, which further restricts my ability to debug. In short, I feel like I'm handicapped in the debugging tools department. My only strategy is to reduce everything down to the simplest test case and slowly adding line of code or row of spreadsheet at at time until it blows up. Surely there has to be a better way; oh great internet, please give me your wisdom!)

like image 228
Zoop Avatar asked Oct 21 '22 01:10

Zoop


1 Answers

Hmm, how about good 'ol try-catch +e-mail or logger to get at least hint on error and than debug watching carefully data in debugger (breakpoint at suspected line, review state of data, step by step debug):

function calculateSmthng()
{
  try {
       makeHardWorkForMe();
      } 
  catch (e) {
      MailApp.sendEmail("[email protected]", "Error report from ...", e.message); //either with e-mail
      Logger.log("Bad thing happened, catched at calculateSmthng(): " +e.message); //or logger
     }
}

It usually did the trick for me.

like image 127
PsychoFish Avatar answered Oct 24 '22 04:10

PsychoFish