Reputation: 63
I need to move rows from a pending sheet to an archive sheet based on the background color. I found a script and managed to edit it to adapt to my needs except for a few things I can't figure out (i don't know how to code except for some basic school stuff). What I worked to is this:
function onEdit(event) {
// assumes source data in sheet named Pendientes
// target sheet of copy to named Enviados
// getColumn with color is currently set to colu 3 or C
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Pendientes" && r.getColumn() == 3 && r.getBackground() == "#00ff00") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Enviados");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target, {contentsOnly:true});
s.deleteRow(row);
targetSheet.getRange(targetSheet.getLastRow(), 7).setValue(new Date()).setNumberFormat("dd-mm-yy");
targetSheet.insertRowAfter(targetSheet.getLastRow())
}
}
What I can't figure out:
The script originaly runs when you edit the content of the cell (the background color doesn't count, took me a while to figure out why it wasn't working) and moved the rows when a value changed from false to true.
What I would like to do is to change the trigger to a custom menu I built, so when you click on it it looks at all the rows, finds the cells with green background and moves them to the archive. Because the original script uses an event which triggers the script every time an edit is made and analyses that single moment I'm not sure how to do it.
I'd also like if possible for the cells to transfer in order based on column 2, so first it looks for green cells in column 3 with OptionA in column 2 and moves it, then with OptionB, and so on.
I'm also not sure if the line I added to insert 1 row for each row moved will work when moving rows masively, meaning if it moves 10 rows will it insert 10 new rows or 1 single row.
I made a sample sheet in which i've been experimenting:
https://docs.google.com/spreadsheets/d/1tzGnuT107u1msx6BchZAEHE7NbsxIVcqXasGilQeTbA/edit?usp=sharing
Thanks!
Upvotes: 0
Views: 165
Reputation: 64140
This function gets the data and the background colors in the same range. Finds the color anywhere in the row while I'm waiting for you to explain the rest of your question. In then takes the corresponding row in the data Array and pushes a timestamp on the end and then appends that to the destination sheet. Using the i index it deletes the row and counts the deleted rows with the delete counter d. Because while rows were deleted from the sheet they were not deleted from the vA or cA array.
function deleteRowWithGreenBackGroundAnyWhere() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Penientes');
const rg=sh.getRange(3,1,sh.getLastRow()-2,sh.getLastColumn());
const vA=rg.getValues();
const cA=rg.getBackgrounds();
const tsh=ss.getSheetByName("Enviados");
const ts=Utilties.formatDate(new Date(),Session.getScriptTimeZone(),"dd-MM-yy");
var d=0;
cA.forEach(function(r,i){
r.forEach(function(c,j){
if(c=='#00ff00') {
var tA=vA[i].slice();
sh.appendRow(tA.push(ts));
sh.deleteRow(i+3-d++);
}
});
});
}
Upvotes: 1