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

スポンサードリンク

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]);
        }
}