0

I have reviewed previously asked questions on this but still can't work out the code. I have a sheet with a significant number of rows of data in it. Rather than scroll down to the first empty row, I have created a button and on click of that button, I want the cursor to take me to column A of the first empty row. This is my script at this point but I keep getting errors:

enter image description here

Can someone please help me.

McChief
  • 423
  • 1
  • 3
  • 18
  • Does this answer your question? [How do I select the first empty row in google spreadsheets/scripts](https://stackoverflow.com/questions/27161792/how-do-i-select-the-first-empty-row-in-google-spreadsheets-scripts) – Tedinoz Nov 01 '19 at 06:59

6 Answers6

2

Try this:

function getLastRow() {
  var sss = SpreadsheetApp.getActiveSheet();
  sheet.getRange(sss.getLastRow()+1,1).activate();
}
  • not sure why, but this did work but doesn't anymore. There was a point that I had no blank rows but I do now but when I click the button (script assigned) nothing happens. I want empty cell A of the empty row to be activated. – McChief Nov 05 '19 at 20:26
1

If I understand your question correctly, you just want to go the first empty row in your sheet, not the first empty cell in column A. If that's the case, this small piece of code should do it:

function getLastRow() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  sheet.getRange(lastRow + 1, 1).activate();
}

I hope this is what you wanted to accomplish.

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • not sure why, but this did work but doesnt anymore. There was a point that I had no blank rows but I do now but when I click the button (script assigned) nothing happens. I want empty cell A of the empty row to be activated. – McChief Nov 05 '19 at 20:25
  • Have you checked whether the button is calling the script at all? You can try using Logger at the beginning of the function to check that. Also, if you share a copy of the spreadsheet as well as the rest of the code you are using we can try reproduce this behaviour. In any case, this script should work (it activates the first cell in the first empty row). – Iamblichus Nov 05 '19 at 20:42
  • @lamblichus - Logger didn't return anything. You describe exactly what I want. If you give me your email address I can share the ss with you. – McChief Nov 05 '19 at 22:02
  • Can't you just share a public copy, free of any sensitive information? – Iamblichus Nov 06 '19 at 09:57
0

Please try this:

function TestFindBlank() {
  //Jojo is rank that can be used for next 
  var Jojo=GetFirstBlank('Sheet1','F6');  
};

function GetFirstBlank(mysheetName, myrangeStr)
{
  var spreadsheet = SpreadsheetApp.getActive();
  var Mysheet=spreadsheet.getSheetByName(mysheetName);
  var Jojo=spreadsheet.getRange(myrangeStr).getNextDataCell(SpreadsheetApp.Direction.DOWN);   
  if (Jojo.offset(1,0).isBlank() ) {
    return  Jojo.offset(1,0);
  }

  var Jojo=spreadsheet.getRange(Jojo.getA1Notation()).getNextDataCell(SpreadsheetApp.Direction.DOWN);   
  return Jojo.offset(1,0);

}
user11982798
  • 1,878
  • 1
  • 6
  • 8
  • Excuse me. I don't understand what this script is intended to do. I have data in cells Q1, Q15 and Q16. I set the cursor in cell D3 and called `GetFirstBlank('Sheet1','Q1'); `. The result -> `Logger.log(Jojo.getA1Notation());` returns Q17, but the next empty row is Q2. – Tedinoz Nov 01 '19 at 07:13
  • Sorry, the consecutive blank can't use this code, just take the first blank. And jojo is a range, so you can use it to set value and get other information. From Q1 the script get Q15 and because under Q15 is not blank, it get last not blank: Q16 so the first blank is Q17. and for next Jojo=GetFirstBlank('Sheet1','Q17') for next non consecutive blank cell; And this script can be used in macro and google script, why I write it because in google script can't do automation to focus any cell or sheet, I suppose. – user11982798 Nov 01 '19 at 08:44
  • I have made another approach, please try it – user11982798 Nov 01 '19 at 13:14
0

Just run setupStartUpTrigger() one time to set it up. Then close and open again and give it a little time and you'll end up at the bottom.

function startUp() {//You can put your menu in here too
  SpreadsheetApp.getUi().createMenu('My Tools')
    .addItem('Item Name', 'functionName')
    .addToUi();  
  var sh=SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sh.getRange(sh.getLastRow()+1,1).activate();
}
//setup an installable onOpen() trigger
//this will fire on opening the spreadsheet
function setupStartUpTrigger(){
  var ss=SpreadsheetApp.getActive();
  if(!isTrigger('startUp')){
    ScriptApp.newTrigger('startUp').forSpreadsheet(ss.getId()).onOpen().create();
  }
}

//this is nice to use to keep you from creating unnecessary triggers
function isTrigger(funcName){
  var r=false;
  if(funcName){
    var allTriggers=ScriptApp.getProjectTriggers();
    for(var i=0;i<allTriggers.length;i++){
      if(funcName==allTriggers[i].getHandlerFunction()){
        r=true;
        break;
      }
    }
  }
  return r;
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • `sh.getRange(sh.getLastRow()+1,1).activate();` This will take you to the row after the last row in column A or, as you put "you'll end up at the bottom". But I think the OP doesn't want to go to the bottom, they just want to go to the first empty row – Tedinoz Nov 01 '19 at 06:59
  • @Cooper - Thanks for this but I keep getting the message "TypeError: Cannot call method "getRange" of null." when I run the script. – McChief Nov 05 '19 at 01:35
  • Did you run `setupStartUpTrigger()`? – Cooper Nov 05 '19 at 02:09
  • Perhaps you have to change the sheetname from Sheet1 to whatever you have. – Cooper Nov 05 '19 at 02:10
  • Yes - I ran the Trigger and yes, I did change the sheetname to 'Events/Incidents' – McChief Nov 05 '19 at 20:17
  • I just opened up a new spreadsheet and pasted my entire answer into the script editor and ran `setupStartUpTrigger()`. I authenticated the app and it ran and created a trigger. I then added a couple hundred lines to the spreadsheet and closed it. When I opened it up it went to the bottom of the page for me. – Cooper Nov 05 '19 at 21:52
0

Here next alternative to next blank or to next non blank cell:

function TestNextBlank_or_NotBlankUse() {
  var spreadsheet = SpreadsheetApp.getActive();
  var mysheet=spreadsheet.getSheetByName('Sheet1');
  var myrange=mysheet.getRange('Q1');

  var myrange=nextNonBlank(myrange); //First Non Blank
  Logger.log(myrange.getA1Notation());

  var myrange=nextNonBlank(myrange); //Second Non Blank
  Logger.log(myrange.getA1Notation());

  var myrange=nextNonBlank(myrange); //Third Non Blank
  Logger.log(myrange.getA1Notation());


  var myrange=mysheet.getRange('Q1');

  var myrange=nextBlank(myrange); //First Blank
  Logger.log(myrange.getA1Notation());

  var myrange=nextBlank(myrange); //Second Blank
  Logger.log(myrange.getA1Notation());

  var myrange=nextBlank(myrange); //Third Blank
  Logger.log(myrange.getA1Notation());

};


function nextBlank(theRange)
{
  if (theRange.offset(1, 0).isBlank()) return theRange.offset(1, 0);
  theRange=theRange.offset(1, 0);
  if (theRange.offset(1, 0).isBlank()) return theRange.offset(1, 0);
  theRange=theRange.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1, 0);
  return theRange;
}

function nextNonBlank(theRange)
{
  if (theRange.offset(1, 0).isBlank()==false) return theRange.offset(1, 0);
  theRange=theRange.offset(1, 0);
  if (theRange.offset(1, 0).isBlank()==false) return theRange.offset(1, 0);
  theRange=theRange.getNextDataCell(SpreadsheetApp.Direction.DOWN);
  return theRange;
}
user11982798
  • 1,878
  • 1
  • 6
  • 8
  • @Iamblichus - this worked well. Thank you for your input. Since asking the question I have added an automatic N/A to Column U which means the script looking for the first empty row now doesn't work. I would be happy if it looked for the first empty cell in column A. – McChief Nov 04 '19 at 02:54
  • @Cooper - Thank you for your input. Since asking the question I have added an automatic N/A to Column U which means the script looking for the first empty row now doesn't work. I would be happy if it looked for the first empty cell in column A. – McChief Nov 04 '19 at 02:55
  • I have made another method to find Empty Row, First, Next and Last: – user11982798 Nov 04 '19 at 08:27
0
function TestFindBlankRow() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh=ss.getSheetByName('Sheet1');
  //Column A to T because you have formula in column U
  var Rslt=FirstBlankRange(sh,'A','T');  //0 : If Not Found
  Logger.log(Rslt);
  var Rslt=NextBlankRange(sh, Rslt, 'A','T');  //0 : If Not Found
  Logger.log(Rslt);
  var Rslt=LastBlankRange(sh,'A','T');  //0 : If Not Found
  Logger.log(Rslt);

};


function FirstBlankRange(Sheet, FirtColumn, LastColumn)
{
  for (a=1;a<Sheet.getLastRow()+1;a++)
  {
    var rr=Sheet.getRange(FirtColumn + a + ':' + LastColumn + a);
    var Rslt=rr.getValues().toString();
    if ((Rslt.length+1)==rr.getNumColumns()) 
    {
      return a;
    }
  }
  return 0;
}

function NextBlankRange(Sheet, CurPos, FirtColumn, LastColumn)
{
  for (a=CurPos+1;a<Sheet.getLastRow()+1;a++)
  {
    var rr=Sheet.getRange(FirtColumn + a + ':' + LastColumn + a);
    var Rslt=rr.getValues().toString();
    if ((Rslt.length+1)==rr.getNumColumns()) 
    {
      return a;
    }
  }
  return 0;
}

function LastBlankRange(Sheet, FirtColumn, LastColumn)
{
  for (a=Sheet.getLastRow();a>1;a--)
  {
    var rr=Sheet.getRange(FirtColumn + a + ':' + LastColumn + a);
    var Rslt=rr.getValues().toString();
    if ((Rslt.length+1)==rr.getNumColumns()) 
    {
      return a;
    }
  }
  return 0;
}
user11982798
  • 1,878
  • 1
  • 6
  • 8