LoginSignup
3
2

More than 5 years have passed since last update.

Google Apps ScriptのgetValues()の返り値

Posted at

.getValues()の返り値

公式リファレンスによると

Return
Object[][] — A two-dimensional array of values.

となっており、連想配列の形になっている。

.indexOf()では取得できない

.indexOf()では連想配列を取得できないため以下のようにして取得する。
(A列にデータがあり、keyNameの列番号を取得する)

function main(){
    var keyName = "Qiita";

    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    var list = sheet.getActiveSheet().getRange("A:A").getValues();

    var index = getIndex(keyName,list);

    if(index = -1){
      Logger.log("存在しない");
    }else{
      Logger.log("存在する");
    }
}

function getIndex(keyName, list) {
    for(var i = 0; i < list.length; i++) {
        if(list[i] == keyName) {
            return i;
        }
    }
    return -1; 
}

参考にさせていただいた記事

ありがとうございました。
.indexOf()で配列のindexが取得できないとき

3
2
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
3
2