Skip to content

Instantly share code, notes, and snippets.

@palumbo
Created May 29, 2022 14:17
Show Gist options
  • Save palumbo/e47839bcdb7a484d50a7c6e36c861928 to your computer and use it in GitHub Desktop.
Save palumbo/e47839bcdb7a484d50a7c6e36c861928 to your computer and use it in GitHub Desktop.
Google Apps Script that copies rows to a new sheet based on a cell value.
function onOpen(e) {
let ui = SpreadsheetApp.getUi();
ui.createMenu('🤖 Automation Tools')
.addItem('Move reps to individual sheets', 'moveRows')
.addToUi();
};
function moveRows() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const eddieSheet = SpreadsheetApp.openById('1wvrKeJd2zj3_tT1W0jkXoIZEbGf4_-0SSSesRgtK_Og').getSheetByName('Sheet1');
const eddieLastRow = eddieSheet.getLastRow();
const josephSheet = SpreadsheetApp.openById('1U0xTv-0_dyFsF3AwwDzT86cjqySjnJcZkn4d4n0MZ1k').getSheetByName('Sheet1');
const josephLastRow = josephSheet.getLastRow();
let lastRow = sheet.getLastRow();
let sortRange = sheet.getSheetValues(2,1,lastRow, 4);
Logger.log(lastRow)
// Logger.log(typeof(sortRange))
// Logger.log(sortRange.length);
let josephCounter = 1;
let eddieCounter = 1;
for (var i = 1; i <= sortRange.length; i++) {
let name = sheet.getRange(i,1).getValue();
console.log(i + " - " + name);
if (name == "Joseph Palumbo") {
let rowValues = sheet.getRange(i, 1, 1, 4).getValues();
josephSheet.getRange(josephLastRow+josephCounter, 1, 1, 4).setValues(rowValues);
josephCounter++;
}
if (name == "Eddie Jauregui") {
let rowValues = sheet.getRange(i, 1, 1, 4).getValues();
eddieSheet.getRange(eddieLastRow+eddieCounter, 1, 1, 4).setValues(rowValues);
eddieCounter++;
}
};
};
@lcbrown1
Copy link

I am totally new to writing scripts, so thanks in advance for your patience. When I try to run this script on my master sheet, I get "TypeError: Cannot read properties of null (reading 'getLastRow')"

Any advice? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment