I have a stock sheet where the items are moved from the stock checkout sheet if the array formula says move. This occurs onopen. But when the function runs sometimes it makes copies of the already moved entries. I have no idea why it is doing this. To test this just delete a row in Carpet or LVP Stock List, then close and reopen the sheet.
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var targetSheet = ss.getSheetByName('Past Checkouts'); // destination sheet
var excludes = ['LVP Stock List','Carpet Stock List','Misc Stock','Data']; // sheets to exclude
var dataColumns = [1,2,3,4,5,6,7,8]; // columns to copy values from
sheets.forEach(function(sheet) { // copy values
if (excludes.indexOf(sheet.getName()) != -1) return; // check sheet is not excluded
var found = 0;
sheet
.getDataRange()
.getValues()
.forEach(function(row, i) {
if (row[8] == 'Move') { // Gets the rows that have the 4th col Move
row.forEach(function(val, j) {
if (dataColumns.indexOf(j + 1) != -1) return;
row[j] = '';
});
targetSheet.appendRow(row);
sheet.deleteRow(i + 1 - found); // delete the source row
found += 1;
}
});
});
}