0

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

4

2 に答える 2

1

同じスクリプトで両方のスプレッドシートを参照し、ステップで2つの配列を処理できます

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();
      var ss1 = SpreadsheetApp.openById("SPREADSHEET KEY1"); 
      var s1 = ss1.getSheetByName('Sheet1'); 
      var values1 = s1.getDataRange().getValues();
            // assumes sheets have same number of rows and you wish to delete corresponding rows in each
      for( var row = values.length -1; row >= 0; --row )
         if (values[row][1] == 'Hello' || values1[row][1] == 'Hello')
          s.deleteRow(parseInt(row)+1);  // parseInt is not required
          s1.deleteRow(parseInt(row)+1);
    }; 
于 2013-01-17T21:54:15.923 に答える
1

このソリューションを単純化するために、私は2D配列ライブラリを使用していArrayLib.find()ます。代わりに、sheet2の行をループして「Hello」を見つける関数を作成することもできます。

function deleteRow() {
  // This assumes script is independent of either sheet
  var s1 = SpreadsheetApp.openById("SPREADSHEET KEY1").getSheetByName('Sheet1'); 
  var s2 = SpreadsheetApp.openById("SPREADSHEET KEY2").getSheetByName('Sheet1'); 

  var values1 = s1.getDataRange().getValues();
  var values2 = s2.getDataRange().getValues();

  // Check if "Hello" appears in column B of Spread2.
  if (ArrayLib.find(values2, 1, 'Hello') != -1) {
    // Yes it does, so delete rows containing "Hello" in column B of Spread1
    for( var row = values.length -1; row >= 0; --row )
       if (values1[row][1] == 'Hello')
        s1.deleteRow(parseInt(row)+1);
    }
  }
}

deleteRow()の呼び出しを削除し、Spread1を配列形式で変更してから、呼び出しclearContent()て変更した配列をシートに書き戻すことで、これをさらに最適化できますsetValues()。このようにして、サービス呼び出しを2に置き換えn、実行時間を大幅に節約します。

于 2013-01-18T03:11:17.663 に答える