Home > Software engineering >  Google AppScript Ignore blank rows
Google AppScript Ignore blank rows

Time:05-29

I have an output script which takes the cell contents of each individual cell in a row and outputs them to txt files (these text files are outputted to a folder which is named from a dynamic list). The page I am running this script on is dynamic so from time to time there are some rows which are empty - I'd like to exclude all blank rows from the output script but I cannot figure it out. Below is the current script (which is working perfectly except for the blank row issue)

    function Output() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getActiveSheet();
  var rg = sh.getRange(2, 2, sh.getLastRow(), 2);
  var vs = rg.getValues();
  vs.forEach(r => DriveApp.getFolderById(deleteOutput('1m_-D9loocLQEJ_2AMWS1EAm3pJeFycpV', r[0])).createFile('output.txt', r[1]));
}
function deleteOutput(parentId, name) {
  var parent = DriveApp.getFolderById(parentId);
  var id;
  var folders = parent.getFoldersByName(name);
  if (folders.hasNext()) {
    var folder = folders.next();
    id = folder.getId();
    var files = folder.getFiles();
    while (files.hasNext()) {
      var file = files.next();
      if (file.getName() == 'output.txt') { file.setTrashed(true) }
    }
  }
  else {
    var folder = parent.createFolder(name);
    id = folder.getId();
  }
  return id;
}

Can anyone help me or point me in the right direction so I can exclude these rows

Any help is very much appreciated :)

CodePudding user response:

Eliminate blank rows

function Output() {
  var ss = SpreadsheetApp.getActive();
  var sh = ss.getActiveSheet();
  var rg = sh.getRange(2, 2, sh.getLastRow()-1, 2);
  var vs = rg.getValues().filter(r => r.join(""));//eliminates blank rows
 
}

Array.filter

CodePudding user response:

Try:

var vs = rg.getValues().filter(r => r.every(Boolean));

Using .every(Boolean) with .filter() will remove all rows except for those with every cell containing values.

Learn More:

  • Related