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
1 Answer
Reset to default 2getLastRow
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
版权声明:本文标题:javascript - How to use the function .getLastRow() in google script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741600693a2387682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论