どのように一枚のシートから値をコピーして別の使用してGoogleスプレッドシートのマクロに貼り付けるには?

ハビエル・アロンソデルガド:

私は、構文について多くの知識がなくても、Googleスプレッドシートのマクロを書いています。

私は何をしたい以下の通りであります:

私は別のテーブルにソース行列に一致している値をコピーします。しかし、私はマクロとしてそれを記述する方法がわかりません。

私は、次のコードを書いています:

function CalcularCruces() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sourceSheet = spreadsheet.getSheetByName("Cruces Activo-Amenazas");
  var destinationSheet = spreadsheet.getSheetByName("Análisis de Riesgos");

  /** Total number of left column values from source table **/
  const maxAmenazas = 29;

  for(var i = 0; i < maxAmenazas; i++) {
    /** Now I need to get the column and row values which are matching with the checkbox 
        and paste them into another table **/
  }

};

ここでは、入力テーブルの一例とどのように出力テーブルには、マクロを実行した後のようになるはずです。

入力表シート 出力表シート

編集:

私は、この静的な列の隣に書き込まれるデータを必要とします:

実際の出力 実際の出力

所望の出力 所望の出力

カルキスのイアンブリコス:

あなたは、次の操作を行うことができます。

  • Retrieve the data from the source sheet via getDataRange and getValues.
  • For each row in this data (excluding the headers row, that has been retrieved and removed from the array with shift), check which columns have the checkbox marked.
  • If the corresponding checkbox is marked, write the corresponding values to the destination sheet with setValues.

It could be something like this:

function CalcularCruces() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sourceSheet = spreadsheet.getSheetByName("Cruces Activo-Amenazas");
  var destinationSheet = spreadsheet.getSheetByName("Análisis de Riesgos");
  destinationSheet.getRange("A2:B").clearContent();
  var values = sourceSheet.getDataRange().getValues(); // 2D array with all data from source sheet
  var headers = values.shift(); // Remove and retrieve the headers row
  for (var i = 1; i < values[0].length; i++) { // Iterate through each column
    for (var j = 0; j < values.length; j++) { // Iterate through each row
      var activo = values[j][0]; // Activo corresponding to this row
      if (values[j][i]) { // Check that checkbox is marked
        // Get the row index to write to (first row in which column A and B are empty):
        var firstRow = 2;
        var firstCol = 1;
        var numRows = destinationSheet.getLastRow() - firstRow + 1;
        var numCols = 2;
        var firstEmptyRow = destinationSheet.getRange(firstRow, firstCol, numRows, numCols).getValues().filter(function(row) {
          return row[0] !== "" && row[1] !== "";
        }).length + firstRow;
        // Write data to first row with empty columns A/B:
        destinationSheet.getRange(firstEmptyRow, firstCol, 1, numCols).setValues([[headers[i], activo]]);
      }
    }
  }
};

Notes:

  • All data is added to the target sheet every time the script is run, and this can lead to duplicate rows. If you want to avoid that, you can use clearContent at the beginning of your script, after declaring destinationSheet, to remove all previous content (headers excluded):
destinationSheet.getRange("A2:B").clearContent();
  • このサンプルでは、数は、amenazasハードコードされていないが、それは動的でソースシートの行数を取得しますgetValues().length私はあなたのために良い結果だと仮定しています。
  • UPDATE:あなたは対象シートに他の列を持っているので、あなたが使用することはできませんappendRowけどsetValuesまず、あなたは、列AとBが空にされた最初の行のインデックスを見つける必要があります。これは、列ABの値の配列をフィルタリングする2つの値が(と空された要素を除外して達成されるフィルタ)。

参照:

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=7949&siteId=1