[英]Google Script - get last row of specific column
我正在尋找一種方法來查找特定列的最后一行(帶有數據)。 假設 A 列第 100 行的最后一個數據項,B 列第 50 行的最后一個數據項。
.getLastRow(); 將永遠返回 100 給我。
我需要一種簡單的方法來獲得 50(這是 B 列中最后一個非空行的行號)。
我不能使用循環。 這非常重要,因為有數千行,它只會花費太多時間。
我用谷歌搜索了很多,但所有的答案都涉及循環。
雖然不知道這個方法好不好,但是這個方法呢? 此方法不使用循環。 請將此作為樣本之一進行檢查。
getLastRow()
檢索最后一行的編號。示例腳本:
var col = ##; // Here, please input a column number.
var ss = SpreadsheetApp.getActiveSheet();
var coldata = ss.getRange(1, col, ss.getLastRow(), 1).getValues();
var tempss = SpreadsheetApp.create("temporary_sheet");
tempss.getActiveSheet().getRange(1,1,coldata.length, coldata[0].length).setValues(coldata);
var last_row = tempss.getLastRow(); // The number of last row
Drive.Files.remove(tempss.getId()); // In this case, the file is removed using Drive API.
筆記 :
在上述情況下,即使列有空單元格,也可以檢索最后一行的編號。 如果該列沒有空單元格,您可以通過以下腳本檢索特定列的最后一行的編號。 這不會創建臨時表。
var last_row = ss.getRange(1, col, ss.getLastRow(), 1).getValues().filter(String).length;
在這種情況下,我想使用以下 2 種模式進行處理。
通過從工作表的頂部搜索來檢索特定列的第一個空單元格
通過從工作表的底部搜索來檢索特定列的第一個非空單元格
為了實現上述,我認為可以使用以下2種模式。
檢索列的值並使用循環搜索結果。
使用 Google Apps Script 的內置方法直接檢索結果。
我測量了它們的過程成本。 結果,發現以下 2 個腳本是所有方法中最低的。
Object.prototype.get1stEmptyRowFromTop = function (columnNumber, offsetRow = 1) {
const range = this.getRange(offsetRow, columnNumber, 2);
const values = range.getDisplayValues();
if (values[0][0] && values[1][0]) {
return range.getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow() + 1;
} else if (values[0][0] && !values[1][0]) {
return offsetRow + 1;
}
return offsetRow;
};
// Please run this function.
function main() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const res = sheet.get1stEmptyRowFromTop(3);
console.log(res); // Retrieve the 1st empty row of column "C" by searching from TOP of sheet.
}
const res = sheet.get1stEmptyRowFromTop(3, 2);
.在這個問題中,我認為這種模式可能是合適的。
Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) {
const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious();
return search ? search.getRow() : offsetRow;
};
// Please run this function.
function main() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const res = sheet.get1stNonEmptyRowFromBottom(3);
console.log(res); // Retrieve the 1st non empty row of column "C" by searching from BOTTOM of sheet.
}
const res = sheet.get1stNonEmptyRowFromBottom(3, 2);
.當使用上述腳本時,得到以下結果。
當“C”列使用“通過從工作表的頂部搜索來檢索特定列的第一個空單元格”的腳本時,將獲得第 6 行。
當“C”列使用“通過從工作表的底部搜索來檢索特定列的第一個非空單元格”的腳本時,將獲得第 9 行。
您還可以使用以下代碼:
function findTheLastRow(){
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("B1:B").getValues();
var filtered_r = range.filter(String).length;
ui.alert("Column B's last cell is number: " + filtered_r + " and its value is: " + range[filtered_r - 1][0]);
}
此腳本計算列中具有值的單元格的數量,因此最后一個單元格上方的單元格需要具有值才能獲得正確的結果。
這對我有用:
var ss = SpreadsheetApp.openById(YourSpreadsheetID);
var main_sheet = ss.getSheetByName(YourMainSheet);
main_sheet.getRange('K16').activate(); // substitute your cell from where you want to count
main_sheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
var last_row_submissions = main_sheet.getCurrentCell().getRowIndex();
我稍微改變了 Tanaike 的回答。 此版本創建工作表而不是電子表格。
var col = 1; // Here, please input a column number, in this case it is the number of A column(1).
var ss = SpreadsheetApp.getActiveSheet();
var coldata = ss.getRange(1, col, ss.getLastRow(), 1).getValues();
var tempss = SpreadsheetApp.getActiveSpreadsheet().insertSheet("temporary_sheet");
tempss.getRange(1,1,coldata.length, coldata[0].length).setValues(coldata);
var last_row = tempss.getLastRow(); // The number of last row
SpreadsheetApp.getActiveSpreadsheet().deleteSheet(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("temporary_sheet"));
這似乎有效:
function myFunction() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('B1').activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
var LastDataRow = spreadsheet.getCurrentCell().getRowIndex();
Logger.log(LastDataRow);
};
這對我來說是最好的方法,獲取參考列然后獲取最后一行
var ssm = SpreadsheetApp.openById(id).getSheetByName(name);
var lastRow = ssm.getRange('A2').getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1, 0).getRow();
ssm.getRange(lr, 1, 1, 8).setValues([data]);
我的方法采用列值的平面數組,將其反轉,搜索第一個定義的單元格的索引。
從值數組的總長度中減去該索引以獲得非反向索引,然后添加可能位於列值之前的任何行以返回電子表格中該列的實際最后一行。
const colValues = WORKSHEET.getRange(STARTROW, STARTCOLUMN, WORKSHEET.getLastRow(), 1)
.getValues()
.flat();
const getLastRowOfCol = (colValues, startRow = 1) => {
return (colValues.length - colValues.reverse().findIndex(cell => !!cell)) + startRow - 1;
};
對於我的用例,我需要在第 13 行之后獲取第 5 列的最后一行。
const ss = SpreadsheetApp.openById(mySpreadsheetId);
const ws = ss.getSheetByName('myWorkSheetName');
const colValues = ws.getRange(14, 5, ws.getLastRow(), 1)
.getValues()
.flat()
const colLastRow = getLastRowOfCol(colValues, 13)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.