ImportRange Script Quietly catch errors

172 views Asked by At

I created my own import range script because I had people inputting data in the middle of my import which then broke. Also I have hidden sheets that users copy that have imports which they would have to hit the "Request Access" button before it would work. Plus ImportRange is kinda finicky with large calculation spreadsheets.

Here is my code

//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`
//Script Based ImportRange

//Example importRange_('1Eb8a5Imw9FWz-Ifk2gUMNkg1TXDlLPemy1bmNv_AOcY','Sheet1','A:Z','Dest_Sheet','A1','y')
//Explanation importRange_('Importing Spreadsheet Key','Importing Spreadsheet Tab Name','Importing Spreadsheet Tab's Range','Destination Spreadsheet Tab Name','Destination Spreadsheet Tab's placement','Will add note to the first cell of import')

function importRange_(Source_Key,Source_Sheet,Source_Range,Set_Sheet,Set_Pos,Add_Note) 
{
  var Load = SpreadsheetApp.openById(Source_Key).getSheetByName(Source_Sheet).getRange(Source_Range).getValues();
  var Name = SpreadsheetApp.openById(Source_Key).getName();
  var RowVal = SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(Set_Pos).getRow();
  var ColVal = SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(Set_Pos).getColumn();
  if(Add_Note.toUpperCase() == 'Y')
  {
    SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(RowVal,ColVal,1,1).setNote("Import Script Updated On: " + Utilities.formatDate(new Date(), "PST", "MM-dd-yyyy hh:mm a")+"\nSS Name: "+Name+"\nRange: "+Source_Sheet+"!"+Source_Range+"\nSS Key: "+ Source_Key);      
  }
  SpreadsheetApp.getActive().getSheetByName(Set_Sheet).getRange(RowVal,ColVal,Load.length,Load[0].length).setValues(Load);
  SpreadsheetApp.flush();
  SpreadsheetApp.getActiveSpreadsheet().toast('At: '+Set_Sheet+'!'+Set_Pos,'Import Completed:');
}
//~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`~,~`

So when I try the try & catch(err) around:

var Load = SpreadsheetApp.openById(Source_Key) .getSheetByName(Source_Sheet) .getRange(Source_Range) .getValues();

I still get a hard error displayed to the end user on the spreadsheet side. Is there a way capture the message quietly and send to the Logger?

1

There are 1 answers

0
Bender405 On BEST ANSWER

I have had problems with try and catch(err). You should try to use catch(e) instead.

You can also try to do e.message, e.filename, or e.lineNumber to narrow down where the issue is popping up