admin管理员组

文章数量:1295296

I'm pretty new to Google Script and I've been working on a line of code that is bothering me right now. What I'm trying to do is make the puter read through a range of cells in the spread sheet, then print them all out, but stop at the last row. But, as I've tried using the function .getLastRow() in the for loop or just making a variable, it doesn't work. (There are some parts where the code is redundant and not related to the problem so you can ignore that.)

function myFunction()
{

  var spreadSheet = SpreadsheetApp.openById("1F4zEJXKN3iobK8b89Ub94dd77Tdk70H0X8aFCSCmvRs");
  var activeSheetOne = spreadSheet.getSheetByName("New Family Responses");
  var activeSheetTwo = spreadSheet.getSheetByName("Existing Family Responses");

  var readingGender = activeSheetOne.getRange("E3:E").getValues();
  var lastRow = readingGender.getLastRow();
  var loopVar = 0;

  for (var i=0; i < lastRow; i++) {
    Logger.log(readingGender[loopVar][0]);
    loopVar = loopVar + 1;
  }

}

I'm pretty new to Google Script and I've been working on a line of code that is bothering me right now. What I'm trying to do is make the puter read through a range of cells in the spread sheet, then print them all out, but stop at the last row. But, as I've tried using the function .getLastRow() in the for loop or just making a variable, it doesn't work. (There are some parts where the code is redundant and not related to the problem so you can ignore that.)

function myFunction()
{

  var spreadSheet = SpreadsheetApp.openById("1F4zEJXKN3iobK8b89Ub94dd77Tdk70H0X8aFCSCmvRs");
  var activeSheetOne = spreadSheet.getSheetByName("New Family Responses");
  var activeSheetTwo = spreadSheet.getSheetByName("Existing Family Responses");

  var readingGender = activeSheetOne.getRange("E3:E").getValues();
  var lastRow = readingGender.getLastRow();
  var loopVar = 0;

  for (var i=0; i < lastRow; i++) {
    Logger.log(readingGender[loopVar][0]);
    loopVar = loopVar + 1;
  }

}
Share edited Apr 14, 2016 at 22:50 user3717023 asked Apr 14, 2016 at 14:20 Marcus FongMarcus Fong 912 gold badges2 silver badges10 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 2

getLastRow is a method of classes Sheet and Range, which simply means that whenever you write something.getLastRow(), that something must be a sheet or a range. (As Zig Mandel noted, there is an issue concerning the functionality of the range method; normally it's used for a sheet.)

In your code, activeSheetOne is a sheet, applying getRange("E3:E") you get a range, and then applying getValues() you get just an array of values. This means an ordinary JavaScript array with which you work using JavaScript methods and properties, not Google Apps Script methods.

So, replace the line var lastRow = readingGender.getLastRow(); by

var lastRow = readingGender.length;

where length is the length of an array.

Also, your code with both i and loopVar is a mix of for and while loops. You don't need both; a for loop will do.

for (var i=0; i < lastRow; i++) {
  Logger.log(readingGender[i][0]);
}

Suggestion: learn the fundamentals of JavaScript, e.g., with Codecademy, before working with Google Apps Script.

本文标签: javascriptHow to use the function getLastRow() in google scriptStack Overflow