移动到第一行为空白的Google App脚本是什么?

时间:2019-11-01 03:45:47

标签: google-apps-script google-sheets

我已经审查了之前对此提出的问题,但是仍然无法确定代码。我有一张工作表,里面有很多行数据。我没有向下滚动到第一空白行,而是创建了一个按钮,并单击该按钮,希望光标将我带到第一空白行的A列。这是我的脚本,但是我不断收到错误消息:

enter image description here

有人可以帮我吗?

6 个答案:

答案 0 :(得分:3)

尝试一下:

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

答案 1 :(得分:1)

如果我正确理解了您的问题,您只想转到工作表中的第一个空行,而不是A列中的第一个空单元格。如果是这种情况,那么这段小代码应该可以做到:

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

我希望这是您想要实现的目标。

答案 2 :(得分:0)

请尝试以下操作:

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);

}

答案 3 :(得分:0)

只需运行一次setupStartUpTrigger()进行设置。然后关闭并再次打开,再花一点时间,您将到达底部。

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;
}

答案 4 :(得分:0)

这里是下一个空白或下一个非空白单元格的下一个替代方法:

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;
}

答案 5 :(得分: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;
}