How do I select the first empty row in google spreadsheets/scripts

后端 未结 1 1650
遇见更好的自我
遇见更好的自我 2020-12-18 15:32

So I should start by saying I\'m new at Javascript/programming. I understand a good amount of it but don\'t really know what I\'m doing when it comes to actually writing it.

相关标签:
1条回答
  • 2020-12-18 16:17

    The referenced question provides scripts with a reference to the first empty row in a spreadsheet. (...although some of those solutions find the first empty cell in column A, which isn't necessarily an empty row. But I digress.)

    In a spreadsheet-attached script with access to the User Interface, you can affect the active view of the spreadsheet with several functions:

    • Spreadsheet.setActiveRange()
    • Spreadsheet.setActiveSelection() (multiple flavours)
    • Sheet.activate()
    • Range.activate()
    • ... there are other functions that will impact the concept of "active" as side-effects as well.

    Using whatever technique you wish to identify the "empty row" in the active spreadsheet, this function will move the selection to it, using .setActiveSelection():

    /**
     * Place the current user's cursor into the first cell of the first empty row.
     */
    function selectFirstEmptyRow() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      sheet.setActiveSelection(sheet.getRange("A"+getFirstEmptyRowWholeRow()))
    }
    

    The getFirstEmptyRowWholeRow() function, if you're curious:

    /**
     * Mogsdad's "whole row" checker.
     */
    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);
    }
    
    0 讨论(0)
提交回复
热议问题