简体   繁体   中英

Google Sheets Script - Get first blank cell

I want to find the first blank cell in a range. This code works, except it seems to be linked to column B.

 function selectFirstEmptyRow() { var sheet = SpreadsheetApp.getActiveSpreadsheet(); sheet.setActiveSelection(sheet.getRange("D"+getFirstEmptyRow())) } function getFirstEmptyRow() { var sheet = SpreadsheetApp.getActiveSheet(); var range = sheet.getDataRange(); var values = range.getValues(); var row = 26; for (var row=26; row<values.length; row++) { if (!values[row].join("")) break; } return (row+1); } 

When I run it, it selects the cell in D, however finds the blanks based on B.

Say we are working in rows 10-20. In column B there is stuff in 10-12. I run this (which should run on D as far as I can see), it will select D13 (because there is stuff in B10:B12. Even though nothing is in D, it should select D10.

UPDATE:::

So I made some changes you suggested.

 function selectFirstEmptyRow() { var sheet = SpreadsheetApp.getActiveSpreadsheet(); sheet.setActiveSelection(sheet.getRange("B"+getFirstEmptyRow())) } function getFirstEmptyRow() { var sheet = SpreadsheetApp.getActiveSheet(); var lastRow = sheet.getLastRow(); //getRange(row, column, numRows, numColumns) var range = sheet.getRange(28, 2, lastRow - 28, 1); var values = range.getValues(); var row = 28; for (var i=0; i<values.length; i++) { if (!values[i].join(" ")) break; } return (row+1); } 

It always selects row 29 (I assume because it's starting on 28 and I have return(row+1)?

This line:

var row = 26;

hard codes a starting number of 26. The loop count starts at 26:

for (var row=26; row<values.length; row++) {

If you want the loop to loop the number of times from a start row of 10, to the last row, then change that line to this:

for (var i=0; i<values.length; i++) {

Your code was actually defining the variable row twice. Once just above the for loop, and once inside of the for loop. The row variable shouldn't be used in the loop at all. The for loop needs it's own counter.

In this line of code:

for (var row=26; row<values.length; row++) {

The JavaScript Array join() Method returns a string from an array.

The getDataRange() method from this line of code:

var range = sheet.getDataRange();

Returns a two dimensional array of all the rows and columns in the sheet. The range starts in the upper left hand side of data in the sheet. If you want to exclude the columns up to and including column C, then use something different to get a different range.

So, I would change this line:

var range = sheet.getDataRange();

Maybe change the code to:

var lastRow = sheet.getLastRow();
//getRange(row, column, numRows, numColumns)
var range = sheet.getRange(10, 4, lastRow - 10, 1);

The above code will get a two dimensional array of data, starting in row 10, Column 4, and get 10 less rows than the total number of rows, and only get one column of data from column D (4th column)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM