0
votes

So I'm looking to auto-hide completely empty rows. With the code, I have now it is autohiding everything with an empty A column when I would still like the columns and rows with N/A shown, reference screenshot#1. I would also like for it to update onOpen or onChange and "unhide" previously empty rows that now have values. By this, I mean this spreadsheet is based on a query & importrange from another spreadsheet that only pulls over certain columns. So the other sheet is the sheet that gets edited, and this one updates with importrange. Hence, if something is added to a previously empty row (for the purpose of this spreadsheet, an assignment to an individual), I would like it to unhide the rows that now contain data as well.

Additionally, this only seems to work when I'm logged in with the ability to "edit" not when view-only and accessible by the public.

Screenshot#1 (All Rows showing)

https://imgur.com/a/9SgJj8C

Screenshot#2 ("Blank" rows hiding)

https://imgur.com/a/Jzka2CG

Screenshot#3 (View-only mode does not update with script)

https://imgur.com/a/3CWnfIE

This is to clean up formatting by hiding empty rows. I've gotten the progress I have by searching solutions, and have come to a dead end.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());

function onOpen() {
    var menuEntries = [];
  menuEntries.push({name: "Show All Rows", functionName: "showAllRows"});
  menuEntries.push(null);
  menuEntries.push({name: "Hide Empty Rows", functionName: "hideEmptyRows"});

  ss.addMenu("Row Visibility", menuEntries);
}{

    //get the values to those rows
    var values = range.getValues();

    //go through every row
    for (var i=0; i<values.length; i++){

        //if row value is equal to empty  
        if(values[i][0] === ""){

        //hide that row
        sheet.hideRows(i+1);   
        }   
    }
}

function showAllRows(){
    sheet.showRows(1,sheet.getLastRow());
}

function hideEmptyRows(){
        //get the values to those rows
    var values = range.getValues();

    //go through every row
    for (var i=0; i<values.length; i++){

        //if row value is equal to empty  
        if(values[i][0] === ""){

        //hide that row
        sheet.hideRows(i+1);
        }
    }
}
2
Welcome. Would you define exactly what you mean by an "empty row"? and... You said '"unhide" previously empty rows that now have values.' - would you please explain, if a row is hidden, how is it possible for a cell in that row to get a value?Tedinoz
@Tedinoz This sheet is populated with a query and import range formula to pull certain columns from another sheet. This sheet itself is never manually edited, and only pulls information from the imported range. The purpose of this sheet is to provide assignments to people, when they don't have an assignment, their row shows N/A with nothing in Column A. I still want that showing. But we occasionally add new people to the sheet, and that is the empty rows that I would like to be "un-hidden". The blank rows are inserted due to formatting restrictions on google sheets.Beau Young
@Tedinoz Additionally, Not a duplicate of that post. I don't need it to start editing the first empty row. I need it to hide empty rows, and show rows with values.Beau Young
Please share a copy of your spreadsheet (not including any private or confidential information), and include an example of a successful outcome. In sharing the spreadsheet, please ensure that users can can identify the query/queries, the import function, and any sheets that are used to capture data and act as a source for your output sheet.Tedinoz
Here is the Editable (backend) sheet: docs.google.com/spreadsheets/d/… Here is the public view only sheet: docs.google.com/spreadsheets/d/… Everything is in these sheets. The goal is to show all rows with values, to include the "N/A"s in them. But I want it to hide all rows with NO values at all. Currently it hides all rows with no values in A column, and will not automatically show rows with values if they were added after originally hidden.Beau Young

2 Answers

1
votes

Several points:

I would still like the columns and rows with N/A shown

Your current code

if(values[i][0] === ""){

        //hide that row
        sheet.hideRows(i+1);   
        }   

hides the whole row if the cell in the first column is empty. To hide the row only if ALL cells are empty, you need to iterate through all of them:

function hideEmptyRows(){
    //go through every row
    for (var i=0; i<values.length; i++){
        var empty=true;
        //go through every column
        for (var j=0; j<values[0].length; j++) {
         if(values[i][j] != ""){
           empty=false;
           break;
           }
          }
        if(empty == true){
        //hide that row
        sheet.hideRows(i+1);   
        }   
    }
}

this only seems to work when I'm logged in with the ability to "edit" not when view-only

As you can verify consulting Restrictions section of the documentation, unfortunately both simple and installable triggers do not run if a file is opened in read-only (view or comment) mode.

I would also like for it to update onOpen or onChange and "unhide" previously empty rows that now have values.

Unfortunately, changes made to the sheet programmatically (with a query) will not fire: Script executions and API requests do not cause triggers to run.

However, you can still first unhide all rows onOpen() and then hide the empty ones. For this, you just need to embed your functions showAllRows() and hideEmptyRows() into onOpen():

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
var values = range.getValues();
function onOpen() {
  showAllRows();
  hideEmptyRows();
  var menuEntries = [];
  menuEntries.push({name: "Show All Rows", functionName: "showAllRows"});
  menuEntries.push(null);
  menuEntries.push({name: "Hide Empty Rows", functionName: "hideEmptyRows"});
  ss.addMenu("Row Visibility", menuEntries);
}

Additionally:

If you want your rows to update periodically, even if the sheet has not been closed and reopened, you can install a time-driven trigger that will periodically update the hidden rows. For this, you need to bind it to your already existing onOpen function and select a time interval of your choice:

enter image description here

0
votes

Purely for the sake of providing an alternative (and proving that there is nothing new under the sun). This is the take from @Mogsdad from 2014.


/**
 * Mogsdad's "whole row" checker.
 //https://stackoverflow.com/a/27179633/1330560
 */
function getFirstEmptyRowWholeRow() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getDataRange();
  var values = range.getValues();
  var row = 0;
  for (var row=0; row<values.length; row++) {
    if (!values[row].join("")) break;
  }
  // return (row+1);
  Logger.log("the row to hide is = "+(row+1));
}