在Google表格中前往数据的最后一行。

3

我查了一下,并找到了以下代码,可以将我们Google电子表格中的数据前进到最后一行 - 至少在我们在第297行之后添加更多行之前是这样。

function myFunction() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var range = ss.getSheets()[0].getRange("B297");
   SpreadsheetApp.setActiveRange(range);
}

我正在努力想出如何编写这个脚本,使它能够到达数据的最后一行,而不管行号是多少。

是否有一些修改可以在这段代码中实现这一点?

3个回答

6

getLastRow()方法可以告诉您包含数据的工作表的最后一行。这可用于将选择移动到该行。另一个更改工作表选择的事情是:您的脚本始终在第一个工作表上运行;在当前激活的任何工作表上操作更有意义。

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange(sheet.getLastRow(), 1);
  SpreadsheetApp.setActiveRange(range);
}

可以使用onOpen将此功能放置在电子表格菜单中。

顺便说一下,如果您在每行都有一些数据的列(如日期或ID列)中按Ctrl + ArrowDown,则会执行相同的操作。


2
以下脚本允许您跳转到包含A列内容的最后一个单元格。即使列A中某些单元格包含公式,它也可以正常工作。
修改括号中的数字lastRowOfCol(1),以便您可以到达另一列的最后一个单元格。
此外,您还可以将焦点更改为最后一个具有内容的单元格之后的第一个空单元格。
function onOpen(){
  lastRowOfCol(1); //Enter the column number you want to use as the base of the search
}

function lastRowOfCol(column){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var total = sheet.getMaxRows();
  var values = SpreadsheetApp.getActiveSheet().getRange(1,column,total).getValues();
  for(var i=total-1;values[i]=="" && i>0; i--){}
  var last = sheet.getRange(i+1,column);
  //var last = sheet.getRange(i+1,1); //Option to fetch the last row of a given column, but to position in column 1
  //var last = sheet.getRange(i+2,column); //Option to go to the cell below the last one with content 
  sheet.setActiveSelection(last);
}

这里是来自Marcelo Camargo的脚本,可在此论坛上查看。


0

适用于所有非隐藏工作表的当前相关选项,而不仅仅适用于活动工作表:

function onOpen() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const sheets = ss.getSheets()
  const properties = PropertiesService.getDocumentProperties()
  const lastActiveSheetName = properties.getProperty("lastActiveSheetName")
  let lastActiveSheet

  for (let sheet of sheets) {
    if (!sheet.isSheetHidden()) {
      const sheetName = sheet.getName()
      const lastEdit = properties.getProperty(sheetName)  
      if (lastEdit) {        
        if (sheetName !== lastActiveSheetName){
          sheet.getLastRow() // Without this magic does not work - I could not figure out the reasons
          sheet.getLastColumn() // Without this magic does not work - I could not figure out the reasons
          const [lastRow, lastCol] = lastEdit.split(',')
          sheet.getRange(Number(lastRow), Number(lastCol)).activate() // With focus set to this cell
          //sheet.setActiveSelection(sheet.getRange(Number(lastRow), Number(lastCol))) // Without setting focus to this cell
        }    
        else {
          lastActiveSheet = sheet
        }
      }
    }
  }

  if(lastActiveSheet){
    lastActiveSheet.getLastRow()
    lastActiveSheet.getLastColumn()
    const [lastRow, lastCol] = properties.getProperty(lastActiveSheetName).split(',')
    lastActiveSheet.getRange(Number(lastRow), Number(lastCol)).activate() 
  }
}

function onEdit() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const sheet = ss.getActiveSheet() 
  if (!sheet.isSheetHidden()) { 
    const cell = ss.getActiveCell()
    const row = cell.getRow()
    const column = cell.getColumn()
    if (row !== 1 || column !== 1) { // Protection from the evil magic of "self-editing" the first cell
      const sheetName = sheet.getName()
      PropertiesService.getDocumentProperties().setProperty(sheetName, `${row},${column}`)
      PropertiesService.getDocumentProperties().setProperty("lastActiveSheetName", sheetName) 
    }
  }
}

附注:请注意,在我的代码中我不使用分号作为分隔符,这对于我来说更方便。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接