I’m trying to organize my partner management using Google Sheets. I have a main spreadsheet where columns F-I are dynamic, meaning I want data from these columns to be transferred to columns A-D in the partner spreadsheet.
However, this should meet three conditions:
- The code should find "example.com" or its variations in the URL in column G.
- Data from columns F-I should only be transferred when "Need to be done" is selected in column I.
- When data is transferred to the destination sheet, the value in column I should automatically change from "Need to be done" to "In Progress."
This is my code. It was working well until I had 30 spreadsheets. After that, I noticed some bugs, and when I added 50 more spreadsheets, it almost stopped working. However, I need it to handle 300+ spreadsheets, and I want to fix it.
I’ve tried merging them all into one unified code and it didn’t help.
function Code() {
var sourceSpreadsheetId = 'XXX';
var sourceSheetName = 'Sheet1';
var destinationSpreadsheetId = 'YYY';
var destinationSheetName = 'Sheet2';
// Open the source spreadsheet and get the source sheet
var sourceSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetId);
var sourceSheet = sourceSpreadsheet.getSheetByName(sourceSheetName);
var lastRow = sourceSheet.getLastRow();
var sourceRange = sourceSheet.getRange('F2:I' + lastRow);
var sourceData = sourceRange.getValues();
// Filter rows that contain 'example.com' in column G and are marked as 'Need to be done'
var filteredData = sourceData.filter(row => row[1].includes('example.com') && row[3] === 'Need to be done');
// Open the destination spreadsheet and get the destination sheet
var destinationSpreadsheet = SpreadsheetApp.openById(destinationSpreadsheetId);
var destinationSheet = destinationSpreadsheet.getSheetByName(destinationSheetName);
// Append the filtered data to the destination sheet
if (filteredData.length > 0) {
var lastRowDestination = destinationSheet.getLastRow();
// Prevent appending if the last row is empty and it's the first run
var startRowDestination = lastRowDestination > 0 ? lastRowDestination + 1 : 1;
destinationSheet.getRange(startRowDestination, 1, filteredData.length, 4).setValues(filteredData);
}
// Update the status from 'Need to be done' to 'In progress' in the source sheet for the rows that were transferred
filteredData.forEach((row) => {
var rowIndex = sourceData.indexOf(row) + 2; // Calculate the index in the source sheet
sourceSheet.getRange('I' + rowIndex).setValue('In progress');
});
// Apply all pending Spreadsheet changes
SpreadsheetApp.flush();
}
2
Answers
Setup up a data sheet like this:
code:
If you don’t need the flush the don’t use it because it just takes more time. If you are not writing to the the same spreadsheet then I don’t think it’s an issue.