Skip to content

Commit

Permalink
add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyd committed Sep 15, 2018
1 parent 3744163 commit 132eab0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
47 changes: 31 additions & 16 deletions dist/application.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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([
[
Expand Down Expand Up @@ -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'
)
);
}
};

Expand Down Expand Up @@ -1061,16 +1062,30 @@ 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.
timer.update(userProperties);
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.
Expand Down
27 changes: 14 additions & 13 deletions lib/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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([
[
Expand Down Expand Up @@ -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'
)
);
}
};

Expand Down
20 changes: 17 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,30 @@ 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.
timer.update(userProperties);
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.
Expand Down

0 comments on commit 132eab0

Please sign in to comment.