Last active
March 26, 2019 06:03
-
-
Save franklinokech/4d00983fcf35b6180009f860e5edf3bd to your computer and use it in GitHub Desktop.
a working google app script to archive multiple rows based on the selected column values in a single command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onOpen() { //Add a custom menu to the menu bar | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Admin') | |
.addItem('Archive', 'archive') | |
.addToUi(); | |
} | |
//NOTE!!! IMPORTANT!!! | |
//For testing and debugging - Remove the slashes in front of the Logger.log() lines - run the | |
//code and then in the VIEW menu - VIEW the LOGS - Provide the LOG print out if there is an error | |
function archive() { | |
var arrayOfRowsToArchive,columnNumberOfValuesToTestFor,data,i,lastColumn,L,numberOfRowsToArchive,numRows, | |
range,rowsToDelete,ss,sheet,sheetName,sheetToMoveTheRowTo,startRow,status,targetSheet,thisRowsData, | |
valuesToTestFor; | |
//USER INPUT: | |
startRow = 5; //The row number to start from where the data starts - exclude header rows | |
valuesToTestFor = "Complete";//Type one or more values to test for - If multiple values seperate by comma | |
columnNumberOfValuesToTestFor = 0; | |
sheetName = "Scheduled";//Name of sheet tab where the data will be removed from | |
sheetToMoveTheRowTo = "Archive"; | |
//END OF USER INPUT | |
ss = SpreadsheetApp.getActiveSpreadsheet(); | |
sheet = ss.getSheetByName(sheetName);//Get the sheet tab with the data to be searched | |
numRows = sheet.getLastRow(); | |
lastColumn = sheet.getLastColumn(); | |
arrayOfRowsToArchive = []; | |
rowsToDelete = []; | |
//The getRange() method uses parameters of (start Row, start Column, Number of Rows to get, Number of Columns to get) | |
data = sheet.getRange(startRow, 1, numRows - startRow, lastColumn).getValues();//Get all values except the header rows | |
L = data.length;//Get the number of rows that are in the data | |
for (i=0; i<L; i++) {//Loop through all the rows of data | |
status = data[i][columnNumberOfValuesToTestFor]; | |
//Logger.log('status: ' + status); | |
if (valuesToTestFor.indexOf(status) !== -1) {//Search for a match between the cell value and values to look for | |
thisRowsData = data[i];//Only get inner array of data | |
//Logger.log('thisRowsData: ' + thisRowsData) | |
arrayOfRowsToArchive.push(thisRowsData);//Push one row of data to outer array | |
rowsToDelete.push(i+startRow);//Get the row number to delete later | |
} | |
} | |
if (arrayOfRowsToArchive) { | |
numberOfRowsToArchive = arrayOfRowsToArchive.length; | |
if (!numberOfRowsToArchive) { | |
return;//No rows were found to archive | |
} | |
} else {//There is no good value for arrayOfRowsToArchive- There was an error | |
Logger.log('There is a problem with arrayOfRowsToArchive'); | |
} | |
targetSheet = ss.getSheetByName(sheetToMoveTheRowTo); | |
targetSheet.getRange(targetSheet.getLastRow()+1, 1, numberOfRowsToArchive, arrayOfRowsToArchive[0].length) | |
.setValues(arrayOfRowsToArchive); | |
//Logger.log('rowsToDelete: ' + rowsToDelete) | |
for (i=rowsToDelete.length;i>0;i--) {//Delete from bottom up | |
Logger.log((i-1).toString()) | |
Logger.log('rowsToDelete[i-1]: ' + rowsToDelete[i-1]) | |
sheet.deleteRow(rowsToDelete[i-1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment