LoginSignup
2
3

More than 5 years have passed since last update.

【GAS@SpreadSheet】行ごとにコンテンツを含む Last Column の番号を取得

Last updated at Posted at 2017-08-21

sheet.getLastColumn() 関数の返り値は、シート全体の一番ラストのコンテンツを含む Column 番号の値となります。尚、あるデータデーブルのフィールド項目は固定ではなく、追加可能の場合は、行ごとに Last Column 番号は異なる可能性があります。つまり、行ごとに Last Column 番号を取得する必要があります。
以下のコードは1行目の Last Column 番号を取得します:

code.gs
function get_currentLastColumn() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("sheet");

  var lastColumn = sheet.getLastColumn(); // Integer — last column of the sheet that contains content

  var iRow = 1 // current row number
  var currentRow = iRow;
  var temp, currentLastColumn;

  for (var i = lastColumn; i > 1; i--) {
    temp = sheet.getRange(currentRow, i).getValue();
    if (temp != "") {
      currentLastColumn = temp;
      break;
    }
  }
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3