I’ve done a lengthy search and couldn’t find what I’m looking for. Maybe someone out there can kindly help?
I have two Google Spreadsheets, Spread1 and Spread2. I will only be referring to sheet1 of both spreadsheets.
I need a script that deletes rows containing “Hello” in column B of Spread1 but only if “Hello” appears in column B of Spread2.
If rows are deleted (if the same word appears in both columns), then I would like the script to go back to Spread2 and also delete the rows with “Hello” in column B.
So after the script has run, “Hello” will not appear in column B in either of the spreadsheets.
If anyone knows how to achieve this I will be very grateful indeed – your valuable help will be much appreciated.
Thanks for looking!
EDIT:
I have this script which deletes rows in Spread2 that contain “hello” in column B:
function deleteRow() {
var ss = SpreadsheetApp.openById("SPREADSHEET KEY");
SpreadsheetApp.setActiveSpreadsheet(ss);
var s = ss.getSheetByName('Sheet1'); // change to your own
var values = s.getDataRange().getValues();
for( var row = values.length -1; row >= 0; --row )
if (values[row][1] == 'Hello')
s.deleteRow(parseInt(row)+1);
};
but I only want it to do so when “Hello” appears in column B of Spread1. Any ideas? Thanks
To simplify this solution, I’m using the 2D Array Library, for
ArrayLib.find(). You could instead write a function that loops through rows in sheet2 to find ‘Hello’.You can further optimize this by removing the call to
deleteRow(), modifying Spread1 in array form, then callingclearContent()andsetValues()to write the modified array back to the sheet. This way, you replacenservice calls with 2, and save lots of run time.