简体   繁体   中英

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

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.

I'm trying to use google script so that when I open my spreadsheet, the cursor begins in the last row, so that I can use a combination of keyboard maestro and apple script to enter text. Problem is, I can't seem to get anything to work. I've tried to use what they have here: Faster way to find the first empty row but nothing seems to actually work properly. The function runs no problem, but it never actually does anything. I've used the suggested functions along with some of the developer ones of 'getLastrow" but nothing goes.

I think I have the second part down, in that to get it to work when I open the document I set a trigger 'on open', but I can't actually test it until I get the main function working.

Many thanks,

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM