From 132eab0161284a14df1a21ee9f353dab1437ed51 Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 15 Sep 2018 15:57:03 -0700 Subject: [PATCH] add error handling --- dist/application.gs | 47 ++++++++++++++++++++++++++++++--------------- lib/Util.js | 27 +++++++++++++------------- lib/main.js | 20 ++++++++++++++++--- 3 files changed, 62 insertions(+), 32 deletions(-) diff --git a/dist/application.gs b/dist/application.gs index 3676a8f..bcdeedb 100644 --- a/dist/application.gs +++ b/dist/application.gs @@ -810,6 +810,11 @@ Util.log = function(ss, values) { ).getSheetByName('Log'); } + // avoid placing entries that are too long + values = values.map(function(cell) { + return cell.slice(0, 4999); + }); + // gets last row with content. // getMaxRows() gets returns the current number of rows in the sheet, regardless of content. var lastRow = ss.getLastRow(); @@ -830,8 +835,7 @@ Util.log = function(ss, values) { .setValues([values]); } catch (e) { // Google sheets doesn't allow inserting more than 2,000,000 rows into a spreadsheet - ss - .insertRowAfter(lastRow) + ss.insertRowAfter(lastRow) .getRange(lastRow, startColumn, numRows, 1) .setValues([ [ @@ -977,19 +981,16 @@ Util.cleanup = function( } catch (e) { Util.log(ss, Util.composeErrorMsg(e)); } - ss - .getRange(2, 3, 1, 1) + ss.getRange(2, 3, 1, 1) .setValue('Complete') .setBackground('#66b22c'); - ss - .getRange(2, 4, 1, 1) - .setValue( - Utilities.formatDate( - new Date(), - properties.timeZone, - 'MM-dd-yy hh:mm:ss a' - ) - ); + ss.getRange(2, 4, 1, 1).setValue( + Utilities.formatDate( + new Date(), + properties.timeZone, + 'MM-dd-yy hh:mm:ss a' + ) + ); } }; @@ -1061,6 +1062,23 @@ function copy() { return; } + // Initialize logger spreadsheet + try { + ss = SpreadsheetApp.openById(properties.spreadsheetId).getSheetByName( + 'Log' + ); + } catch (e) { + try { + ss = SpreadsheetApp.openById( + PropertiesService.getUserProperties().getProperty('spreadsheetId') + ).getSheetByName('Log'); + } catch (e) { + // if the spreadsheet cannot be accessed, this should be considered a fatal error + // and the script should not continue + throw new Error('Cannot locate spreadsheet. Please try again.'); + } + } + // Create trigger for next run. // This trigger will be deleted if script finishes successfully // or if the stop flag is set. @@ -1068,9 +1086,6 @@ function copy() { var duration = timer.calculateTriggerDuration(properties); TriggerService.createTrigger(duration); - // Initialize logger spreadsheet - ss = SpreadsheetApp.openById(properties.spreadsheetId).getSheetByName('Log'); - // Process leftover files from prior query results // that weren't processed before script timed out. // Destination folder must be set to the parent of the first leftover item. diff --git a/lib/Util.js b/lib/Util.js index 2a053c8..60a6356 100644 --- a/lib/Util.js +++ b/lib/Util.js @@ -32,6 +32,11 @@ Util.log = function(ss, values) { ).getSheetByName('Log'); } + // avoid placing entries that are too long + values = values.map(function(cell) { + return cell.slice(0, 4999); + }); + // gets last row with content. // getMaxRows() gets returns the current number of rows in the sheet, regardless of content. var lastRow = ss.getLastRow(); @@ -52,8 +57,7 @@ Util.log = function(ss, values) { .setValues([values]); } catch (e) { // Google sheets doesn't allow inserting more than 2,000,000 rows into a spreadsheet - ss - .insertRowAfter(lastRow) + ss.insertRowAfter(lastRow) .getRange(lastRow, startColumn, numRows, 1) .setValues([ [ @@ -199,19 +203,16 @@ Util.cleanup = function( } catch (e) { Util.log(ss, Util.composeErrorMsg(e)); } - ss - .getRange(2, 3, 1, 1) + ss.getRange(2, 3, 1, 1) .setValue('Complete') .setBackground('#66b22c'); - ss - .getRange(2, 4, 1, 1) - .setValue( - Utilities.formatDate( - new Date(), - properties.timeZone, - 'MM-dd-yy hh:mm:ss a' - ) - ); + ss.getRange(2, 4, 1, 1).setValue( + Utilities.formatDate( + new Date(), + properties.timeZone, + 'MM-dd-yy hh:mm:ss a' + ) + ); } }; diff --git a/lib/main.js b/lib/main.js index e42c0eb..6626998 100644 --- a/lib/main.js +++ b/lib/main.js @@ -52,6 +52,23 @@ function copy() { return; } + // Initialize logger spreadsheet + try { + ss = SpreadsheetApp.openById(properties.spreadsheetId).getSheetByName( + 'Log' + ); + } catch (e) { + try { + ss = SpreadsheetApp.openById( + PropertiesService.getUserProperties().getProperty('spreadsheetId') + ).getSheetByName('Log'); + } catch (e) { + // if the spreadsheet cannot be accessed, this should be considered a fatal error + // and the script should not continue + throw new Error('Cannot locate spreadsheet. Please try again.'); + } + } + // Create trigger for next run. // This trigger will be deleted if script finishes successfully // or if the stop flag is set. @@ -59,9 +76,6 @@ function copy() { var duration = timer.calculateTriggerDuration(properties); TriggerService.createTrigger(duration); - // Initialize logger spreadsheet - ss = SpreadsheetApp.openById(properties.spreadsheetId).getSheetByName('Log'); - // Process leftover files from prior query results // that weren't processed before script timed out. // Destination folder must be set to the parent of the first leftover item.