Home > OS >  Printing rows with certain text in column 2 with app script
Printing rows with certain text in column 2 with app script

Time:04-01

I need to print off certain rows in a google sheet depending on what is in column 2 of that row. I know how to find the rows with a for loop but the rest eludes me. Perhaps my googling skills are rusty.

This is what I have.

var app = SpreadsheetApp;
var rows = app.getActive().getSheetByName("Sheet").getMaxRows().toString();
var rows = rows.replace(".0","");

function findRows(){
for (var counter = 1; counter <= rows; counter = counter 1){
  if(app.getActive().getSheetByName("Sheet").getRange(counter, 2) == "example" || "example2"){
     
  }
}

CodePudding user response:

Find the correct rows

function findrows() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  const osh = sh.getSheetByName("Sheet1");
  const vs = sh.getDataRange().getValues();
  let s = vs.map(r => {
    if(r[1] == "Example" || r[1] == "example2") {
      return r;
    }
  }).filter(e => e);
  Logger.log(JSON.stringify(s));
  //you can output to a sheet with something like
  //sheet.getRange(1,1,s.length,s[0].length).setValues(s);
  osh.getRange(1,1,s.length,s[0].length).setValues(s);//put on another sheet
}

Execution log
4:56:34 PM  Notice  Execution started
4:56:35 PM  Info    [[2,"Example",4,5],[5,"Example",7,8],[9,"Example",11,12],[12,"Example",14,15]]
4:56:35 PM  Notice  Execution completed

Data:

COL1 COL2 COL3 COL4
1 2 3 4
2 Example 4 5
3 4 5 6
4 5 6 7
5 Example 7 8
6 7 8 9
7 8 9 10
8 9 10 11
9 Example 11 12
10 11 12 13
11 12 13 14
12 Example 14 15
13 14 15 16

BTW Printing is not easily done from Javascript or Google Apps Script

  • Related