title: Hiding rows based on range length and client's name
body: <p>I have this code, I'm trying to hide some rows based on the result of a filter. <a href="https://docs.google.com/spreadsheets/d/1k3jQQW68N0Ai_7MFiOzrDqxFenzHq3ahjviEtsQehzU/edit?usp=sharing" rel="nofollow noreferrer">Here</a> a sample sheet</p>
  <p>Basically, I'm trying to build a bill based on a list of transactions/Clients. I use filter and query to have the transaction I want in the sheet &quot;facture&quot; which is my template for the bill.</p>
  <p>In row 1003 I have the total of the bill + VAT
  I would like to hide rows 19 to 23 if the client's name is not &quot;PUB&quot;
  I would like to hide rows 1016 to 1042 if the client's name is not VSPE and finally I would like to hide every row between the last row of the bill from row 22 to row 999 that is empty.</p>
  <pre><code>function hideBill() {
   // Define your variables here
  var ss = SpreadsheetApp.getActive();
  var facture_sheet = ss.getSheetByName(&quot;Facture&quot;);
  var input_sheet = ss.getSheetByName(&quot;Input&quot;);
  var clientname=input_sheet.getRange(&quot;ClientName&quot;).getValue();
  
  if(clientname=='PUB'){
        facture_sheet.showRows(19,1042);
        facture_sheet.hideRows(1018,24);
      }else if (clientname=='C'){
        facture_sheet.hideRows(19,3);
        facture_sheet.showRows(22,1022);
       }else {
        facture_sheet.hideRows(19,3);
        facture_sheet.showRows(22,1017);
        facture_sheet.hideRows(1018,24);
      }
  
  
  facture_sheet.getRange(&quot;a22&quot;).activate();
  facture_sheet.getRange(&quot;a&quot;+(getLastDataRow(facture_sheet)+1)+&quot;:g999&quot;).activate();
  facture_sheet.hideRows(ss.getActiveRange().getRow(), ss.getActiveRange().getNumRows());
  facture_sheet.getRange(&quot;a22&quot;).activate();
  }
  
  
  function getLastDataRow(sheet) {
    var lastRow = sheet.getLastRow();
    var range = sheet.getRange(&quot;e&quot; + lastRow);
    if (range.getValue() !== &quot;&quot;) {
      return lastRow;
    } else {
      return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
    }              
  }
  </code></pre>
  <p>It is not working correctly and I can't figure out where my mistake is.
  Can someone help me to find the problem? thanks a lot !!
  j.</p>