Google Apps Script(GAS) - en

Make an associative array a value of a line specified by GAS

2018年4月26日

0
0

Make values of two rows specified by GAS into keys and values in associative array

The thing you want to do is to get the values of row A one by one from the top. Every time it acquires one, it creates an associative array using it as a key. At the same time, the values of the C line are also acquired one by one from the top, and these are the values of the associative array. That is, create an associative array. The associative array key is A row and the value is C row. The first image below is an Excel image of a spreadsheet, an associative array logger made by the second one, I want to make it like this.

Implementation

function myFunction() 
{   

    var sheet = SpreadsheetApp.getActiveSheet(); 
    var lastRow = sheet.getLastRow();
    // Array initialization
    var hashColor2 = {};
     
      for(var i = 2; i <= lastRow; i++) 
      {
            if(true)
            {  
              // Set a value for array key
              hashColor2[sheet.getRange(i, 1).getValue()] = sheet.getRange(i, 3).getValue();
            }
      }
  
        // Display element of array (key and value)
        for (var key in hashColor2) 
        {
            Logger.log(key + "の値:" + hashColor2[key]);
        }
}

 

Pocket
LinkedIn にシェア

  • この記事を書いた人
  • 最新記事

藤沢瞭介(Ryosuke Hujisawa)

りょすけと申します。18歳からプログラミングをはじめ、今はフロントエンドでReactを書いたり、AIの勉強を頑張っています。off.tokyoでは、ハイテクやガジェット、それからプログラミングに関する情報まで、エンジニアに役立つ情報を日々発信しています!

-Google Apps Script(GAS) - en

Copyright© off.tokyo , 2021 All Rights Reserved Powered by AFFINGER5.