Skip to content

Instantly share code, notes, and snippets.

@forksofpower
Created January 23, 2017 23:56
Show Gist options
  • Save forksofpower/1f3eefa5a5cf26683901158cd46dabaf to your computer and use it in GitHub Desktop.
Save forksofpower/1f3eefa5a5cf26683901158cd46dabaf to your computer and use it in GitHub Desktop.
Not fully implemented. Missing components in comments at the bottom.
function sortNewRowsToSheet() {
// basic sheet info
var sourceSpreadSheetName = 'CampaignFunnel'
, sourceSheetName = "Sheet1"
, sortColumnName = "Campaigns"
, dateColumnName = "Date"
;
/**
* If a destination sheet does not exist then there are two options:
* 'hard' - Create a new sheet with the name from the destinationSheet
* 'soft' - Send the row to an "undefined" sheet for further sorting
* Select either option in `sortingMethod`
*/
var undefinedSortingMethod = 'hard'
, undefinedSheetName = 'No Home'
;
// initate sheet
var spreadSheet = SpreadsheetApp.setActiveSpreadsheet(sourceSpreadSheetName);
// define source sheet
var sourceSheet = spreadSheet.getSheetByName(sourceSheetName);
// get new rows from sourceSheet
var rows = sourceSheet.getDataRange().getValues();
// , rowsToDelete = rows.length()
;
// optional: sort rows by date column
// iterate through new rows
for (row in rows) {
// - identify destinationSheet from specified column in current row
// check if sortColumn exists
var sortColumnIndex = row.indexOf(sortColumnName);
if (sortColumnIndex !== -1) {
var destinationSheetName = row[sortColumnIndex]
, destinationSheet = spreadSheet.getSheetByName(destinationSheetName)
;
// check if destinationSheet exists
try {
spreadSheet.setActiveSheet(destinationSheet);
} catch(e) {
spreadSheet.insertSheet(destinationSheetName);
spreadSheet.setActiveSheet(destinationSheet);
}
destinationSheet.appendRow(row);
// // - if destinationSheet is undefined then either create a new sheet or add to `undefined` sheet
// switch undefinedSortingMethod:
// case 'soft:
// // append to undefinedSheetName sheet
// // if undefinedSheetName does not exist then create it
// break
// case 'hard':
// /* falls through */
// default:
// // create a new sheet with column names from old sheet
// // append data to new sheet
// break
} else {
// sortColumn cannot be found
}
// - append row data to destinationSheet
// - delete row from sourceSheet
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment