Success/Error Message Handling

99 views
Skip to first unread message

Dan Klinker

unread,
Jul 1, 2022, 7:51:37 AM7/1/22
to Google Apps Script Community
Hi all, 

I have written my first Google App Script to run a series of ImportRanges on a number of Google Sheets. I'm looking to enhance the script by including some error handling to return a message to the user after it has run. If the script has run successfully I would like an message box to appear telling the user it's run successfully and vice versa if an error has occurred. So far I haven't been successful in creating anything like this and wondered if anyone could help or point me in the right direction. Many thanks. 

function runsies() {
 
  importRange(
    //Nordics
    "1Xrhx03ub1uXdeFsS0mFZa2zi5uZL-qcuQDbe9oIULQU",
    "DATADUMP: LE!A2:I",
    "1GY-FjVa1XFNpKiR7wx3tCtGUMC2FRbLTORj-wI3iBK8",
    "Nordics - LE!A1"
  );
 
importRange(
    //UK
    "13Y0NOJie1gYWXanQhtkSnTNsx4vt_bcqfUIXj0ePnac",
    "DATADUMP: LE!A2:I",
    "1GY-FjVa1XFNpKiR7wx3tCtGUMC2FRbLTORj-wI3iBK8",
    "UK - LE!A1"
  );

};
 

function importRange(sourceID, sourceRange, destinationID, destinationRangeStart){
 
  // Gather Source range values
  const sourceSS = SpreadsheetApp.openById(sourceID);
  const sourceRng = sourceSS.getRange(sourceRange);
  const sourceVals = sourceRng.getValues();
 
  // Get Destiation sheet and cell location.
  const destinationSS = SpreadsheetApp.openById(destinationID);
  const destStartRange = destinationSS.getRange(destinationRangeStart);
  const destSheet = destStartRange.getSheet();
 
  // Clear previous data.
  destSheet.clear();
 
  // Get the full data range to paste from start range.
  const destRange = destSheet.getRange(
      destStartRange.getRow(),//Start Row
      destStartRange.getColumn(),//Start Column
      sourceVals.length,//Row Depth
      sourceVals[0].length //Column Width
    );
  
  // Paste in the values
  destRange.setValues(sourceVals);
 
  SpreadsheetApp.flush();
};

Zack Reynolds

unread,
Jul 1, 2022, 8:01:27 AM7/1/22
to google-apps-sc...@googlegroups.com
If you wrap the body of the script in a try { //Code here }, you can end with a ss.toast('message here')

Then after the try {}, do your 
catch(e) { 
let ss = SpreadsheetApp.getActiveSpreadsheet();
ss.toast(`Error: ${e}`);
}

Keep in mind to use template literals ("${variable}"), you need to wrap it in backticks, not single quotes.
 
Registered Office: BMI Group Holdings UK Limited, Thames Tower Reading RG1 1LX.
Company number: 09984607
VAT number. GB294008404 

This email, including any attachments, is for the sole use of the intended recipient(s), and may contain information that is confidential or legally protected. If you are not the intended recipient, any disclosure, copying, distribution, or use of the contents of this information or any attachments is prohibited and may be unlawful. If you have received this electronic transmission in error, please reply immediately to the sender by return e-mail that you have received the message in error and delete it along with any attachments. Please note that the Internet is not a safe means of communication or form of media.

Whilst we have taken reasonable precautions to ensure that this e-mail and any attachment has been checked for viruses, we cannot guarantee that they are virus free and we cannot accept liability for any damage sustained as a result of software viruses. We would advise that you carry out your own virus checks, especially before opening an attachment.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/c3c68bf9-15ce-4141-8843-4121c33ce6a9n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages