更有效的方式也查找特定列中的最后一行? [英] More efficient way too look up the last row in a specific column?

查看:94
本文介绍了更有效的方式也查找特定列中的最后一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,可以将列从一个表格导入到另一个表格。 .getLastRow方法将只应用于整个工作表,但不能用于获取列的最后一行。有一个问题打开请求此功能。

我在Google脚本示例中使用2D Array库的帮助编写了一些内容: https://sites.google.com/site/scriptsexamples/custom-methods/2d-arrays-library



我已经找到了一个可以在特定列中找到最后一行的工作版本,但是我怀疑它是相当无效的。

  function readRows(){
var sheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Sheet1);
var numRows = sheet.getLastRow();
var numColumns = sheet.getLastColumn();
var data = sheet.getRange(1,1,numRows,numColumns).getValues();

//获取头文件,搜索头文件和索引值
var headerArray = sheet.getRange(1,1,numColumns).getValues();
var flip = ArrayLib.transpose(headerArray)
var search =Greens;
var whereGreen = ArrayLib.indexOf(flip,0,search);


//获取具有匹配标题的列的值,并查找列长度。
var values = sheet.getRange(1,whereGreen +1,numRows,1).getValues();

//找到最后一个值,使字符串
用于(; values [numRows - 1] ==&& numRows> 0; numRows--){}
var lastValue = values [numRows - 1] .toString();

//字符串所在的索引,它给出列中最后一行的值-1。
var lastRowCol = ArrayLib.indexOf(values,0,lastValue);

Logger.log(lastRowCol +1);

}

任何人都可以帮我找到简化版吗?我确定JavaScript可以做到这一点,但我对该部门的知识非常了解。通过减少对电子表格服务的调用次数,可以使代码更有效。 以下代码更快:

  function readRows(){
var sheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName ( 工作表Sheet);
var data = sheet.getDataRange()。getValues();
var numRows = data.length;

//获取头文件,搜索头文件和索引值
var headerRow = data [0];
var search =Greens;
var whereGreen = headerRow.indexOf(search);

//找到最后一个值,使字符串
while(data [numRows - 1] [whereGreen] ==&& numRows> 0){
numRows - ;
}
var lastValue = data [numRows - 1] [whereGreen] .toString();

Logger.log('Last row:'+ numRows);
Logger.log('Last value:'+ lastValue);

//不清楚这是什么,需要更多信息?
//字符串所在的索引,它给出列中最后一行的值-1。
// var lastRowCol = ArrayLib.indexOf(values,0,lastValue);
// Logger.log(lastRowCol +1);
}

我用while循环取代了for循环,但这不应该做太多效率不同,使得它更具可读性。


I'm writing an app that will import columns from one sheet to another. The .getLastRow method will only apply to the whole sheet, but can't be used to get last row of a column. There is an issue open requesting this feature.

I've written something with the help of the 2D Array library from the folks over at Google Script Examples: https://sites.google.com/site/scriptsexamples/custom-methods/2d-arrays-library

I've gotten a working version that finds the last row in a specific column, but I suspect it's rather ineffecient.

function readRows() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var numRows = sheet.getLastRow();
  var numColumns = sheet.getLastColumn();
  var data = sheet.getRange(1, 1, numRows, numColumns).getValues();

//Get the Headers, Search for a value of the headers and index  
var headerArray = sheet.getRange(1, 1, 1, numColumns).getValues();
var flip = ArrayLib.transpose(headerArray)
var search = "Greens";
var whereGreen = ArrayLib.indexOf(flip, 0, search);


//Get the value of the column with matching headers, and looks up Column length. 
 var values = sheet.getRange(1, whereGreen +1, numRows, 1).getValues();

//finds last value, makes string
for(; values[numRows - 1] == "" && numRows > 0; numRows--) {}
   var lastValue = values[numRows - 1].toString();

//Indexes where the string is, which gives the value -1 of the last row in column.   
var lastRowCol = ArrayLib.indexOf(values, 0, lastValue);

 Logger.log(lastRowCol +1);

 }

Can anyone help me get to a streamlined version? I'm sure JavaScript could do it, but I'm rather light on my knowledge in that department.

解决方案

The code can be made more efficient by reducing the number of calls to the spreadsheet service. The following code is much faster:

function readRows() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var data = sheet.getDataRange().getValues();
  var numRows = data.length;

//Get the Headers, Search for a value of the headers and index  
  var headerRow = data[0];
  var search = "Greens";
  var whereGreen = headerRow.indexOf(search);

//finds last value, makes string
  while( data[numRows - 1][whereGreen] == "" && numRows > 0 ) {
    numRows--;
  }
  var lastValue = data[numRows - 1][whereGreen].toString();

  Logger.log( 'Last row: '+ numRows );
  Logger.log( 'Last value: '+ lastValue );

// Not clear what this does, what more information is needed?
//Indexes where the string is, which gives the value -1 of the last row in column.   
//var lastRowCol = ArrayLib.indexOf(values, 0, lastValue);
//  Logger.log(lastRowCol +1);
}

I replaced the for loop with a while loop, but that should not make much difference in efficiency, makes it a bit more readable.

这篇关于更有效的方式也查找特定列中的最后一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆