Overview:
I'm attempting to copy records from one tab to another for a specified criteria. Copy source (Index) tab and destination (All Work Orders) tab.
When the function is initiated, the All Work Orders tab should only receive records where the value in Index column BR2:BR is equal to Index cell R6. For example: If cell R6 = 05/01/2025 - 05/15/2025, copy records that meet that criteria from the Index tab to All Work Order tab.
Current issue:
When the function is executed, it's coping All records from the Index tab regardless of the value in cell R6. Appears the where clause is not setup correctly.
Also, I noticed that the existing records in the All Work Orders tab are being overwritten by the copied values from the Index tab. The copied records should begin at the first available blank row and all previous records in the All Work Orders tab should be still intact.
function copyDatabase_25() {
var response=ui.alert("Complete Payment Processing Period", "Are you sure you want to finalize the Processing Period? By selecting YES, all Inspections within this designated date range will be copied to the All Work Orders tab.", ui.ButtonSet.YES_NO);
//checking the user response
if(response==ui.Button.NO)
{
return; //exit from this function
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Index");
var desinationsheet = ss.getSheetByName("All Work Orders");
var startRow = 2;
var numRows = sheet.getLastRow() - startRow + 1;
var startCol = 24;
var numCols = 44;
var startColTarget = 1;
var dataRange = sheet.getRange(startRow, startCol, numRows, numCols);
var data = dataRange.getValues();
var Copy = "Copy";
var firstEmptyRow = sheet.getRange("A2:AQ").getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow() + 1;
var dataToCopy = data.filter(row => row[2] !== Copy);
//Get the cell value
var selected_processing_pd = sheet.getRange("R6").getValue();
var rangeValues = sheet.getRange("BR2:BR").getValues();
// Compare and process
for (var i = 0; i < rangeValues.length; i++) {
for (var j = 0; j < rangeValues[i].length; j++) {
if (rangeValues[i][j] == selected_processing_pd ) {
desinationsheet.getRange(firstEmptyRow, startColTarget, dataToCopy.length, dataToCopy[0].length).setValues(dataToCopy);
}
}
}
ss.toast("The Inspections for the " + selected_processing_pd + " Payment Processing Pd, have been successfully copied to the All Work Orders tab")
}
Index, cell R6 options
05/01/2025 - 05/15/2025
05/16/2025 - 05/31/2025
Combined Date Range (Index, column BR)
05/01/2025 - 05/15/2025
05/01/2025 - 05/15/2025
05/16/2025 - 05/31/2025
05/16/2025 - 05/31/2025
05/16/2025 - 05/31/2025
05/16/2025 - 05/31/2025
05/01/2025 - 05/15/2025
05/16/2025 - 05/31/2025
05/16/2025 - 05/31/2025
05/16/2025 - 05/31/2025
05/01/2025 - 05/15/2025
05/01/2025 - 05/15/2025
05/01/2025 - 05/15/2025
05/01/2025 - 05/15/2025
05/01/2025 - 05/15/2025
05/01/2025 - 05/15/2025
The primary issue with the code is that it appends all of the dataToCopy
to desinationsheet
every time a row in rangeValues
equals selected_processing_pd
.
To make it work, use Array.filter()
to get only the rows that match your criteria, and append those rows just once, like this:
function copyDatabase_25() {
const ui = SpreadsheetApp.getUi();
const ss = SpreadsheetApp.getActive();
const period = ss.getRange('Report - Current Processing Period!D3').getValue();
if (ui.Button.YES !== ui.alert('Complete Payment Processing Period', `Do you want to copy Inspections within ${period} to the All Work Orders tab?`, ui.ButtonSet.YES_NO)) return;
const targetSheet = ss.getSheetByName('All Work Orders');
const data = ss.getRange('Index!X2:BR').getValues()
.filter(row => row[2] !== 'Copy' && row[46] === period);
if (data.length) {
appendRows_(targetSheet, data, 'A:AU');
ss.toast(`Copied Inspections for ${period} to All Work Orders tab.`);
} else {
ss.toast('No matching data.');
}
}
See Array.filter().
For the example code to work, you will need to paste the appendRows_()
and getLastRow_()
utility functions in your script project as well. You can get those functions from How to append data to a certain column.