2016-10-14 17 views
0

我編寫了一個函數來獲取Google表格中單個列中的最後一個值。在特定列中獲取最後一個值的有效方法

// Returns the row number of the first empty cell 
// Offset should be equal to the first row that has data 
function getLastRowInColumn(sheet, column, rowOffset) { 
    var lastRow = sheet.getMaxRows(); 
    for (var row = rowOffset; row < lastRow; row++) { 
    var range = sheet.getRange(row, column); 
    if (range.isBlank()) { 
     return row; 
    }; 
    } 
} 

我可以假定所有值都填寫,因此該函數實際上返回找到的第一個空單元格(在此情況下,即相當於第一空行)。

此功能需要很長時間才能運行約1000行的工作表。我怎樣才能讓它更有效率?

在此先感謝。

回答

0

閱讀所有的數據到本地陣列並通過讀,只記得該行將是一個零索引(從0開始),而不是從1開始的行號:

// Returns the row number of the first empty cell 
// Offset should be equal to the first row that has data 
function getLastRowInColumn(sheet, column, rowOffset) { 
    var lastRow = sheet.getMaxRows(); 
    var data = sheet.getDataRange().getValues() 
    for (var row = rowOffset; row < lastRow; row++) { 
    if (data[row][0] === '') { 
     return row; 
    }; 
    } 
} 
相關問題