Search code examples
multiple-columnsmultiple-tablessetvaluegetvalue

Google Spreadsheet Script - Analyze table for value (1) and post several rows with this value in another


i want to analyze a sheet (name: 1) for a value (e.g. 1) and post some values from these rows with this value in another sheet (name: 2).

"search for all cells in column F which got the value (e.g. 1). copy columns A, E and G (if F is 1) and post it at the end of sheet 2.

My Script just post one result actually. Could you please help me? I think i just dont get any kind of loop?

function checkWS() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName("1"));
  var sheet = spreadsheet.getActiveSheet();

  // figure out what the last row is
  var lastRow = sheet.getLastRow();

  // the rows are indexed starting at 1, and the first row
  // is the headers, so start with row 2
  var startRow = 2;

  // grab column 5 (the 'month left' column) 
  var range = sheet.getRange(2, 6, lastRow - startRow + 1, 1);
  var numRows = range.getNumRows();
  var soll_values = range.getValues();

  // Now, grab the reminder name column
  range = sheet.getRange(2, 1, lastRow - startRow + 1, 1);
  var branch_values = range.getValues();

  // and the count column
  range = sheet.getRange(2, 7, lastRow - startRow + 1, 1);
  var count_values = range.getValues();

  var warning_count = 0;
  var msg = "";

  // Loop over the days left values
  for (var i = 0; i <= numRows - 1; i++) {
    var soll = soll_values[i][0];
    if (soll == 1) {
      // if it's exactly 1, do something with the data.
      var name = branch_values[i][0];
      var count = count_values[i][0];

      warning_count++;
    }
  }

  if (warning_count) {
    var ss = SpreadsheetApp.openById("1vn4xTwdNk7E1Av4VNugtW6KWgZns2LzKS1lN1F6CTPw");
    var Tab = ss.getSheetByName('2');
    var lastRow = Tab.getLastRow();

    Tab.getRange(lastRow + 1, 1).setValue(name);
    Tab.getRange(lastRow + 1, 5).setValue(count);
    Tab.getRange(lastRow + 1, 10).setValue(new Date());

    SpreadsheetApp.flush();
  }

}


Solution

  • now it's done. I just worked with a little helping-coloumn:

     function checkWS() {
      var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
      SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName("NAME"));
      var sheet = spreadsheet.getActiveSheet();
    
      // figure out what the last row is
      var lastRow = sheet.getLastRow();
    
      // the rows are indexed starting at 1, and the first row
      // is the headers, so start with row 2
      var startRow = 6;
    
      // grab date (the 'month left' column) 
      var range = sheet.getRange(6, 54, lastRow - startRow + 1, 1);
      var numRows = range.getNumRows();
      var soll_values = range.getValues();
    
      // Now, grab the branch column
      range = sheet.getRange(6, 11, lastRow - startRow + 1, 1);
      var branch_values = range.getValues();
    
      // and the count column
      range = sheet.getRange(6, 49, lastRow - startRow + 1, 1);
      var count_values = range.getValues();
    
      var warning_count = 0;
      var msg = "";
    
      // Loop over the days left values
      for (var i = 0; i <= numRows - 1; i++) {
        var soll = soll_values[i][0];
        if (soll == 1) {
          // if it's exactly 1, do something with the data.
          var name = branch_values[i][0];
          var count = count_values[i][0];
    
          warning_count++;
    
    //      if (warning_count) { -> not needed 
            var ss = SpreadsheetApp.openById("ID");
            var Tab = ss.getSheetByName('NAME_other_ss');
            var lastRow = Tab.getLastRow();
    
            Tab.getRange(lastRow + 1, 10).setValue(name);
            Tab.getRange(lastRow + 1, 14).setValue(count);
            Tab.getRange(lastRow + 1, 20).setValue(new Date());
    
            SpreadsheetApp.flush();
    //      }
        }
      }
    
    
    
    }