Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 更有效的方法是查找特定列中的最后一行吗?_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 更有效的方法是查找特定列中的最后一行吗?

Google apps script 更有效的方法是查找特定列中的最后一行吗?,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在编写一个应用程序,将列从一张工作表导入到另一张工作表。.getLastRow方法将仅应用于整个工作表,但不能用于获取列的最后一行。请求此功能时存在一个问题 在Google脚本示例中,我在2D数组库的帮助下写了一些东西: 我得到了一个可以在特定列中找到最后一行的工作版本,但我怀疑这是相当不有效的 function readRows() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

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

在Google脚本示例中,我在2D数组库的帮助下写了一些东西:

我得到了一个可以在特定列中找到最后一行的工作版本,但我怀疑这是相当不有效的

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

 }

有人能帮我找到精简版吗?我相信JavaScript可以做到这一点,但我对该部门的知识相当浅薄

在我看来,就效率而言,这与效率差不多。就更清洁的解决方案而言,我现在似乎想不出一个。如果我想到任何事情,都会更新。

在效率方面,我认为这是最接近效率的。就更清洁的解决方案而言,我现在似乎想不出一个。如果我想到什么,就会更新。

通过减少对电子表格服务的调用次数,可以提高代码的效率。下面的代码要快得多:

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

我将for循环替换为while循环,但这不会对效率产生太大影响,使其更具可读性。

通过减少对电子表格服务的调用次数,可以提高代码的效率。下面的代码要快得多:

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

我将for循环替换为while循环,但这不会对效率造成太大影响,使其更具可读性。

谢谢,很高兴听到这个消息。我觉得我到处都在写。谢谢,很高兴听到你这么说。我觉得我到处都在写它。我偷了它,但它看起来也很奇怪。而这更有意义。我偷了那张票,但对我来说也很奇怪。而这更有意义。投票表决。